File: fncode.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,389; cs: 879; awk: 590; perl: 245; xml: 95
file content (2300 lines) | stat: -rw-r--r-- 59,032 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
/*******************************************************************************
 * fncode.cpp
 *
 * This module implements the the function type used by iso surfaces and
 * the function pattern.
 *
 * This module is inspired by code by D. Skarda, T. Bily and R. Suzuki.
 *
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/source/backend/vm/fncode.cpp $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <algorithm>

// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "backend/parser/parse.h"
#include "backend/vm/fncode.h"
#include "backend/vm/fnpovfpu.h"
#include "backend/vm/fnintern.h"
#include "backend/colour/colour.h"

// this must be the last header file included
#include "base/povdebug.h"

namespace pov
{

/*****************************************************************************
*
* FUNCTION
*
*   FNCode::FNCode
*
* INPUT
*   
* OUTPUT
*
*   f - function to init
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Prepares a new function.
*
* CHANGES
*
*   -
*
******************************************************************************/

FNCode::FNCode(Parser *pa, FunctionCode *f, bool is_local, const char *n)
{
	unsigned int i = 0;

#if (DEBUG_FLOATFUNCTION == 1)
	asm_input = NULL;
	asm_output = NULL;
	asm_error = NULL;
#endif

	max_program_size = 0;
	level = 0;
	max_stack_size = 0;
	stack_pointer = 0;
	parameter_stack_pointer = 0;

	parser = pa;
	functionVM = parser->sceneData->functionVM;

	function = f;

	function->program = NULL;
	function->program_size = 0;
	function->return_size = 0; // zero implies return in register r0
	function->parameter_cnt = 0;
	function->localvar_cnt = 0;
	for(i = 0; i < MAX_PARAMETER_LIST; i++)
	{
		function->localvar_pos[i] = 0;
		function->localvar[i] = NULL;
		function->parameter[i] = NULL;
	}

	if(n != NULL)
		function->name = POV_STRDUP(n);
	else
		function->name = POV_STRDUP("");
	function->filename = pa->UCS2_strdup(parser->Token.FileHandle->name());
	if(parser->Token.FileHandle != NULL)
		function->filepos = parser->Token.FileHandle->tellg();
	else
	{
		function->filepos.lineno = 0;
		function->filepos.offset = 0;
	}
	function->flags = 0;
	function->private_copy_method = NULL;
	function->private_destroy_method = NULL;
	function->private_data = NULL;

	if(is_local == true)
		function->flags |= FN_LOCAL_FLAG;
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::Parameter
*
* INPUT
*   
* OUTPUT
*
*   f - function containing the parameter list
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Parse the parameter list of a function declaration.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::Parameter()
{
	parser->Get_Token();
	if(parser->Token.Token_Id == LEFT_PAREN_TOKEN)
	{
		for(function->parameter_cnt = 0;
		    ((parser->Token.Token_Id != RIGHT_PAREN_TOKEN) || (function->parameter_cnt == 0)) && (function->parameter_cnt < MAX_PARAMETER_LIST);
		    function->parameter_cnt++)
		{
			parser->Get_Token();

			if((parser->Token.Function_Id != IDENTIFIER_TOKEN) && (parser->Token.Function_Id != X_TOKEN) &&
			   (parser->Token.Function_Id != Y_TOKEN) && (parser->Token.Function_Id != Z_TOKEN) &&
			   (parser->Token.Function_Id != U_TOKEN) && (parser->Token.Function_Id != V_TOKEN))
				parser->Expectation_Error("parameter identifier");

			function->parameter[function->parameter_cnt] = POV_STRDUP(parser->Token.Token_String);

			parser->Parse_Comma();
		}

		parser->Get_Token();

		if(function->parameter_cnt == 0)
			parser->Error("At least one function parameter is required!");
	}
	else
		parser->Unget_Token();
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::Compile
*
* INPUT
*
*   expression - expression tree that will be compiled
*   
* OUTPUT
*
*   f - function containing the compiled code
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Take an expression tree and compiles it into code understood by the
*   virtual machine.
*   
*     R0        R1        R2        R3        R4        R5        R6        R7
*   right 1   right 2     x,        y,        z,      left 1    left 2    left 3
*                       param 1   param 2   param 3
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::Compile(Parser::ExprNode *expression)
{
	unsigned int gpos = 0;

	// allocate some program memory in advance
	max_program_size = 256;
	function->program_size = 0;
	function->program = (Instruction *)POV_MALLOC(sizeof(Instruction) * max_program_size, "fn: program");

#if (DEBUG_FLOATFUNCTION == 1)
	if(asm_input != NULL)
	{
		asm_error = NULL;
		if(assemble(asm_input) < 0)
		{
			POV_FREE(asm_input);
			asm_input = NULL;
			Error("Assembler Error: %s", asm_error);
		}
		else if(asm_error != NULL)
		{
			POV_FREE(asm_input);
			asm_input = NULL;
			Expectation_Error("valid function expression");
		}
		POV_FREE(asm_input);
		asm_input = NULL;
	}
	else // this is intentional [trf]
#endif

	if(expression->op == OP_TRAP)
	{
		// use either trap or traps, depending on the expected return size
		if(function->return_size > 0)
		{
			// make sure the internal function exists
			if(expression->trap >= POVFPU_TrapSTableSize)
				parser->Error("Function 'internal(%d)' does not exist.", (int)(expression->trap));

			// call internal function
			compile_instruction(OPCODE_TRAPS, 0, 0, expression->trap);

			// set the basic information
			function->parameter_cnt = POVFPU_TrapSTable[expression->trap].parameter_cnt;
		}
		else
		{
			// make sure the internal function exists
			if(expression->trap >= POVFPU_TrapTableSize)
				parser->Error("Function 'internal(%d)' does not exist.", (int)(expression->trap));

			// call internal function
			compile_instruction(OPCODE_TRAP, 0, 0, expression->trap);

			// set the basic information
			function->parameter_cnt = POVFPU_TrapTable[expression->trap].parameter_cnt;
		}
	}
	else
	{
		// fill in "grow max_stack_size" later (see below)
		gpos = compile_instruction(OPCODE_NOP, 0, 0, 0);

		// load the parameters x, y, z into registers and
		// assign stack locations to all other parameters
		compile_parameters();

		// prepare to compile the recursive expression
		// note that compile_parameters may have changed
		// function->parameter_cnt [trf]
		level = 0;
		parameter_stack_pointer = 0;
		max_stack_size = function->parameter_cnt;
		stack_pointer = function->parameter_cnt;

		// compile the expression
		compile_recursive(expression);

		// fill in "grow max_stack_size" now
		compile_instruction(gpos, OPCODE_GROW, 0, 0, max_stack_size);
	}

	// return from function
	compile_instruction(OPCODE_RTS, 0, 0, 0);

	// set optimal size of program memory
	function->program = (Instruction *)POV_REALLOC(function->program, sizeof(Instruction) * function->program_size, "fn: program");

#if (DEBUG_FLOATFUNCTION == 1)
	if(asm_output != NULL)
	{
		asm_error = NULL;
		if(disassemble(asm_output) < 0)
		{
			POV_FREE(asm_output);
			asm_output = NULL;
			Error("Disassembler Error: %s", asm_error);
		}
		else if(asm_error != NULL)
		{
			POV_FREE(asm_output);
			asm_output = NULL;
			Expectation_Error("valid function expression");
		}
		POV_FREE(asm_output);
		asm_output = NULL;
	}
#endif
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode_Copy
*
* INPUT
*
*   f - source function
*   
* OUTPUT
*
*   fnew - destination function
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Copy a compiled function.
*
* CHANGES
*
*   -
*
******************************************************************************/
/*
void FNCode_Copy(FunctionCode *f, FunctionCode *fnew)
{
	int i;

	if(f->program != NULL)
	{
		fnew->program = (Instruction *)POV_MALLOC(sizeof(Instruction) * f->program_size, "fn: program");
		POV_MEMCPY(fnew->program, f->program, sizeof(Instruction) * f->program_size);
	}
	if(f->name != NULL)
	{
		fnew->name = (char *)POV_MALLOC((UCS2_strlen(f->name) + 1) * sizeof(UCS2), "fn: name");
		UCS2_strcpy(fnew->name, f->name);
	}
	if(f->filename != NULL)
	{
		fnew->filename = (char *)POV_MALLOC(strlen(f->filename) + 1, "fn: scene file name");
		strcpy(fnew->filename, f->filename);
	}

	fnew->program_size = f->program_size;
	fnew->parameter_cnt = f->parameter_cnt;
	for(i = 0; i < fnew->parameter_cnt; i++)
		fnew->parameter[i] = POV_STRDUP(f->parameter[i]);
	fnew->localvar_cnt = f->localvar_cnt;
	for(i = 0; i < fnew->localvar_cnt; i++)
		fnew->localvar[i] = POV_STRDUP(f->localvar[i]);
	fnew->filepos.lineno = f->filepos.lineno;
	fnew->filepos.offset = f->filepos.offset;
	fnew->flags = f->flags;

	fnew->private_copy_method = f->private_copy_method;
	fnew->private_destroy_method = f->private_destroy_method;
	if(f->private_data != NULL)
	{
		if(f->private_copy_method != NULL)
			fnew->private_data = f->private_copy_method(f->private_data);
		else
			fnew->private_data = NULL;
	}
}
*/

/*****************************************************************************
*
* FUNCTION
*
*   FNCode_Delete
*
* INPUT
*
*   f - function to delete
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Delete a compiled function.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode_Delete(FunctionCode *f)
{
	int i;

	if(f->program != NULL)
	{
		POV_FREE(f->program);
		f->program = NULL;
	}
	if(f->name != NULL)
	{
		POV_FREE(f->name);
		f->name = NULL;
	}
	if(f->filename != NULL)
	{
		POV_FREE(f->filename);
		f->filename = NULL;
	}
	for(i = 0; i < f->parameter_cnt; i++)
	{
		if(f->parameter[i] != NULL)
		{
			POV_FREE(f->parameter[i]);
			f->parameter[i] = NULL;
		}
	}
	for(i = 0; i < f->localvar_cnt; i++)
	{
		if(f->localvar[i] != NULL)
		{
			POV_FREE(f->localvar[i]);
			f->localvar[i] = NULL;
		}
	}
	if(f->private_data != NULL)
	{
		if(f->private_destroy_method != NULL)
			f->private_destroy_method(f->private_data);
		else
			POV_FREE(f->private_data);
		f->private_data = NULL;
	}
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::SetFlag
*
* INPUT
*
*   flag - compiler flag field
*   str - flag field data
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Set compiler flag.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::SetFlag(unsigned int flag, char *str)
{
#if (DEBUG_FLOATFUNCTION == 1)
	if(flag == 1)
	{
		if(asm_input != NULL)
			POV_FREE(asm_input);
		asm_input = POV_STRDUP(str);
	}
	else if(flag == 2)
	{
		if(asm_output != NULL)
			POV_FREE(asm_output);
		asm_output = POV_STRDUP(str);
	}
#else
	// silence compiler warnings
	flag = 0;
	str = NULL;
#endif
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_recursive
*
* INPUT
*
*   expr - expression (sub-) tree to compile
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles an expression (sub-) tree recursivly.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_recursive(Parser::ExprNode *expr)
{
	unsigned int local_k = 0;

	if(expr->op <= OP_LEFTMOST)
		local_k = compile_push_result();

	for(Parser::ExprNode *i = expr; i != NULL; i = i->next)
	{
		if(i->child != NULL)
		{
			if(i->child->op == OP_CONSTANT)
			{
				switch(i->op)
				{
					case OP_ADD:
						if(i->child->number != 0.0)
							compile_instruction(OPCODE_ADDI, 0, 5, functionVM->AddConstant(i->child->number));
						continue;
					case OP_SUB:
						if(i->child->number != 0.0)
							compile_instruction(OPCODE_SUBI, 0, 5, functionVM->AddConstant(i->child->number));
						continue;
					case OP_MUL:
						if(i->child->number != 1.0)
							compile_instruction(OPCODE_MULI, 0, 5, functionVM->AddConstant(i->child->number));
						continue;
					case OP_DIV:
						if(i->child->number == 0.0)
							parser->Error("Division by zero.");
						else if(i->child->number != 1.0)
							compile_instruction(OPCODE_MULI, 0, 5, functionVM->AddConstant((DBL)1.0 / (i->child->number)));
						continue;
					case OP_POW:
						if(i->child->number == 0.0)
						{
							compile_instruction(OPCODE_LOADI, 0, 5, functionVM->AddConstant(1.0));
							parser->Warning(0, "Zero power optimised to constant 1.0!");
							continue;
						}
						else if(i->child->number == 2.0)
						{
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							continue;
						}
						else if(i->child->number == 3.0)
						{
							compile_instruction(OPCODE_MOVE, 5, 0, 0);
							compile_instruction(OPCODE_MUL, 0, 5, 0);
							compile_instruction(OPCODE_MUL, 0, 5, 0);
							continue;
						}
						else if(i->child->number == 4.0)
						{
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							continue;
						}
						else if(i->child->number == 5.0)
						{
							compile_instruction(OPCODE_MOVE, 5, 0, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 0, 5, 0);
							continue;
						}
						else if(i->child->number == 6.0)
						{
							compile_instruction(OPCODE_MOVE, 5, 0, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 0, 0, 0);
							compile_instruction(OPCODE_MUL, 0, 5, 0);
							continue;
						}
						else if(i->child->number == 7.0)
						{
							compile_instruction(OPCODE_MOVE, 5, 0, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 0, 5, 0);
							compile_instruction(OPCODE_MUL, 0, 0, 0);
							compile_instruction(OPCODE_MUL, 0, 5, 0);
							continue;
						}
						else if(i->child->number == 8.0)
						{
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							compile_instruction(OPCODE_MUL, 5, 5, 0);
							continue;
						}
						break;
				}
			}

			if(i->op != OP_CALL)
				compile_recursive(i->child);
		}

		switch(i->op)
		{
			case OP_CONSTANT:
				compile_instruction(OPCODE_LOADI, 0, 0, functionVM->AddConstant(expr->number));
				break;
			case OP_VARIABLE:
				compile_variable(expr->variable);
				break;
			case OP_DOT:
				// do nothing
				break;
			case OP_MEMBER:
				compile_member(expr->variable);
				break;
			case OP_CALL:
				compile_call(i->child, i->call.fn, i->call.token, i->call.name);
				break;
			case OP_CMP_EQ:
				compile_instruction(OPCODE_CMP, 0, 5, 0);
				compile_instruction(OPCODE_SEQ, 0, 5, 0);
				break;
			case OP_CMP_NE:
				compile_instruction(OPCODE_CMP, 0, 5, 0);
				compile_instruction(OPCODE_SNE, 0, 5, 0);
				break;
			case OP_CMP_LT:
				compile_instruction(OPCODE_CMP, 0, 5, 0);
				compile_instruction(OPCODE_SLT, 0, 5, 0);
				break;
			case OP_CMP_LE:
				compile_instruction(OPCODE_CMP, 0, 5, 0);
				compile_instruction(OPCODE_SLE, 0, 5, 0);
				break;
			case OP_CMP_GT:
				compile_instruction(OPCODE_CMP, 0, 5, 0);
				compile_instruction(OPCODE_SGT, 0, 5, 0);
				break;
			case OP_CMP_GE:
				compile_instruction(OPCODE_CMP, 0, 5, 0);
				compile_instruction(OPCODE_SGE, 0, 5, 0);
				break;
			case OP_ADD:
				compile_instruction(OPCODE_ADD, 0, 5, 0);
				break;
			case OP_SUB:
				compile_instruction(OPCODE_SUB, 0, 5, 0);
				break;
			case OP_OR:
				compile_instruction(OPCODE_TNE, 0, 5, 0);
				compile_instruction(OPCODE_TNE, 0, 0, 0);
				compile_instruction(OPCODE_ADD, 0, 5, 0);
				compile_instruction(OPCODE_TNE, 0, 5, 0);
				break;
			case OP_MUL:
				compile_instruction(OPCODE_MUL, 0, 5, 0);
				break;
			case OP_DIV:
				compile_instruction(OPCODE_XEQ, 0, 0, 0);
				compile_instruction(OPCODE_DIV, 0, 5, 0);
				break;
			case OP_AND:
				compile_instruction(OPCODE_TNE, 0, 5, 0);
				compile_instruction(OPCODE_TNE, 0, 0, 0);
				compile_instruction(OPCODE_MUL, 0, 5, 0);
				break;
			case OP_POW:
				compile_instruction(OPCODE_XDZ, 0, 5, 0);
				compile_instruction(OPCODE_MOVE, 0, 1, 0);
				compile_instruction(OPCODE_MOVE, 5, 0, 0);
				compile_instruction(OPCODE_SYS2, 0, 0, TRAP_SYS2_POW);
				compile_instruction(OPCODE_MOVE, 0, 5, 0);
				break;
			case OP_NEG:
				compile_instruction(OPCODE_NEG, 0, 5, 0);
				break;
			case OP_NOT:
				// compile_instruction(OPCODE_NOT, 0, 5, 0);
				break;
			case OP_LEFTMOST:
				compile_instruction(OPCODE_MOVE, 0, 5, 0);
				break;
			default:
				break;
		}
	}

	if(expr->op <= OP_LEFTMOST)
	{
		compile_instruction(OPCODE_MOVE, 5, 0, 0);
		compile_pop_result(local_k);
	}
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_member
*
* INPUT
*
*   expr - member expression
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a member access. Works for functions only!!!
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_member(char *name)
{
	unsigned int return_parameter_sp = 0;

	// determine position of return values on stack
	// (see compile_vector_function_call for stack layout)
	return_parameter_sp = (unsigned int)(((int)stack_pointer) + max(((int)(level)) - 2, 0) + min(((int)(level)) + 1, 3));

	// compile member access
	if(name[1] == 0)
	{
		if((name[0] == 'x') || (name[0] == 'u'))
		{
			compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp);
			return;
		}
		else if((name[0] == 'y') || (name[0] == 'v'))
		{
			compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp + 1);
			return;
		}
		else if(name[0] == 'z')
		{
			compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp + 2);
			return;
		}
		else if(name[0] == 't')
		{
			compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp + 3);
			return;
		}
	}

	if(strcmp(name, "red") == 0)
		compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp);
	else if(strcmp(name, "green") == 0)
		compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp + 1);
	else if(strcmp(name, "blue") == 0)
		compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp + 2);
	else if(strcmp(name, "filter") == 0)
		compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp + 3);
	else if(strcmp(name, "transmit") == 0)
		compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp + 4);
	else if((strcmp(name, "gray") == 0) || (strcmp(name, "grey") == 0))
	{
		// values taken from GREY_SCALE!!!
		compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp);
		compile_instruction(OPCODE_MULI, 0, 5, functionVM->AddConstant(RED2GRAY));
		compile_instruction(OPCODE_LOAD, 1, 0, return_parameter_sp + 1);
		compile_instruction(OPCODE_MULI, 0, 0, functionVM->AddConstant(GREEN2GRAY));
		compile_instruction(OPCODE_ADD, 0, 5, 0);
		compile_instruction(OPCODE_LOAD, 1, 0, return_parameter_sp + 2);
		compile_instruction(OPCODE_MULI, 0, 0, functionVM->AddConstant(BLUE2GRAY));
		compile_instruction(OPCODE_ADD, 0, 5, 0);
	}
	else if(strcmp(name, "hf") == 0)
	{
		// for MegaPOV compatibility (not supported officially)
		compile_instruction(OPCODE_LOAD, 1, 5, return_parameter_sp);
		compile_instruction(OPCODE_LOAD, 1, 0, return_parameter_sp + 1);
		compile_instruction(OPCODE_MULI, 0, 0, functionVM->AddConstant(1.0 / 255.0));
		compile_instruction(OPCODE_ADD, 0, 5, 0);
		compile_instruction(OPCODE_MULI, 0, 5, functionVM->AddConstant(0.996093));
// TODO FIXME		Experimental_Flag |= EF_ISOFN;
	}
	else
		parser->Error("Invalid member access: Valid member names are x, y, z, t, u, v,\nred, green, blue, grey, filter and transmit.");
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_call
*
* INPUT
*
*   expr - list of function parameters (required)
*   fn - user defined function reference (optional)
*   token - token of the function (required)
*   name - name of the function (required)
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a function call.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_call(Parser::ExprNode *expr, FUNCTION fn, int token, char *name)
{
	unsigned int domain_check = 0;
	unsigned int domain_check_2nd = 0;
	unsigned int local_k = 0;
	unsigned int k = 0;
	int op_state = 1;

	if(expr == NULL)
		parser->Error("Invalid number of parameters: At least one parameter expected!");

	switch(token)
	{
		case SIN_TOKEN:
			k = TRAP_SYS1_SIN;
			break;
		case COS_TOKEN:
			k = TRAP_SYS1_COS;
			break;
		case TAN_TOKEN:
			k = TRAP_SYS1_TAN;
			break;
		case ASIN_TOKEN:
			k = TRAP_SYS1_ASIN;
			break;
		case ACOS_TOKEN:
			k = TRAP_SYS1_ACOS;
			break;
		case ATAN_TOKEN:
			k = TRAP_SYS1_ATAN;
			break;
		case SINH_TOKEN:
			k = TRAP_SYS1_SINH;
			break;
		case COSH_TOKEN:
			k = TRAP_SYS1_COSH;
			break;
		case TANH_TOKEN:
			k = TRAP_SYS1_TANH;
			break;
		case ASINH_TOKEN:
			k = TRAP_SYS1_ASINH;
			break;
		case ACOSH_TOKEN:
			k = TRAP_SYS1_ACOSH;
			break;
		case ATANH_TOKEN:
			k = TRAP_SYS1_ATANH;
			break;
		case ABS_TOKEN:
			op_state = 7;
			break;
		case RADIANS_TOKEN:
			op_state = 3;
			break;
		case DEGREES_TOKEN:
			op_state = 4;
			break;
		case DIV_TOKEN:
			k = TRAP_SYS2_DIV;
			domain_check_2nd = OPCODE_XEQ;
			op_state = 2;
			break;
		case INT_TOKEN:
			k = TRAP_SYS1_INT;
			break;
		case FLOOR_TOKEN:
			k = TRAP_SYS1_FLOOR;
			break;
		case CEIL_TOKEN:
			k = TRAP_SYS1_CEIL;
			break;
		case SQRT_TOKEN:
			k = TRAP_SYS1_SQRT;
			break;
		case EXP_TOKEN:
			k = TRAP_SYS1_EXP;
			break;
		case LN_TOKEN:
			k = TRAP_SYS1_LN;
			domain_check = OPCODE_XLE;
			break;
		case LOG_TOKEN:
			k = TRAP_SYS1_LOG;
			domain_check = OPCODE_XLE;
			break;
		case MIN_TOKEN:
			op_state = 5;
			break;
		case MAX_TOKEN:
			op_state = 6;
			break;
		case ATAN2_TOKEN:
			k = TRAP_SYS2_ATAN2;
			op_state = 2;
			break;
		case POW_TOKEN:
			k = TRAP_SYS2_POW;
			op_state = 11;
			break;
		case MOD_TOKEN:
			k = TRAP_SYS2_MOD;
			domain_check_2nd = OPCODE_XEQ;
			op_state = 2;
			break;
		case SELECT_TOKEN:
			op_state = 8;
			break;
		case SUM_TOKEN:
			op_state = 13;
			break;
		case PROD_TOKEN:
			op_state = 14;
			break;
		case SQR_TOKEN:
			op_state = 12;
			break;
		case FUNCT_ID_TOKEN:
			op_state = 9;
			break;
		case VECTFUNCT_ID_TOKEN:
			op_state = 10;
			break;
		default:
			parser->Expectation_Error("function identifier");
			break;
	}

	switch(op_state)
	{
		case 1: // one parameter
			compile_recursive(expr->child);
			if(domain_check != 0)
				compile_instruction(domain_check, 0, 0, 0);
			compile_instruction(OPCODE_SYS1, 0, 0, k);
			if(expr->next != NULL)
				parser->Error("Invalid number of parameters for '%s': Only one parameter expected!", name);
			break;
		case 2: // two parameters
			// first evaluate right parameter
			if(expr->next == NULL)
				parser->Error("Invalid number of parameters for '%s': Two parameters expected!", name);
			compile_recursive(expr->next->child);
			if(domain_check_2nd != 0)
				compile_instruction(domain_check_2nd, 0, 0, 0);
			// temporary storage of right parameter in r5
			local_k = compile_push_result();
			compile_instruction(OPCODE_MOVE, 0, 5, 0);
			// evaluate left parameter
			compile_recursive(expr->child);
			if(domain_check != 0)
				compile_instruction(domain_check, 0, 0, 0);
			// move right parameter from r5 to r1
			compile_instruction(OPCODE_MOVE, 5, 1, 0);
			compile_pop_result(local_k);
			// make sure there are not more than 2 parameters
			if(expr->next->next != NULL)
				parser->Error("Invalid number of parameters for '%s': Only two parameters expected!", name);
			compile_instruction(OPCODE_SYS2, 0, 0, k);
			break;
		case 3: // radians
			compile_recursive(expr->child);
			compile_instruction(OPCODE_MULI, 0, 0, functionVM->AddConstant(M_PI / 180.0));
			if(expr->next != NULL)
				parser->Error("Invalid number of parameters for '%s': Only one parameter expected!", name);
			break;
		case 4: // degrees
			compile_recursive(expr->child);
			compile_instruction(OPCODE_MULI, 0, 0, functionVM->AddConstant(180.0 / M_PI));
			if(expr->next != NULL)
				parser->Error("Invalid number of parameters for '%s': Only one parameter expected!", name);
			break;
		case 5: // min
			compile_recursive(expr->child);
			if(expr->next == NULL)
				parser->Error("Invalid number of parameters for '%s': At least two parameters expected!", name);
			// compare all parameters, searching for minimum
			for(expr = expr->next; expr != NULL; expr = expr->next)
			{
				// temporary storage of last minimum in r5
				local_k = compile_push_result();
				compile_instruction(OPCODE_MOVE, 0, 5, 0);
				compile_recursive(expr->child);
				// move last minimum from r5 to r1
				compile_instruction(OPCODE_MOVE, 5, 1, 0);
				compile_pop_result(local_k);
				// compare last minimum and current parameter
				compile_instruction(OPCODE_CMP, 0, 1, 0);
				compile_instruction(OPCODE_BGT, 0, 0, function->program_size + 2);
				compile_instruction(OPCODE_MOVE, 1, 0, 0);
			}
			break;
		case 6: // max
			compile_recursive(expr->child);
			if(expr->next == NULL)
				parser->Error("Invalid number of parameters for '%s': At least two parameters expected!", name);
			// compare all parameters, searching for maximum
			for(expr = expr->next; expr != NULL; expr = expr->next)
			{
				// temporary storage of last maximum in r5
				local_k = compile_push_result();
				compile_instruction(OPCODE_MOVE, 0, 5, 0);
				compile_recursive(expr->child);
				// move last maximum from r5 to r1
				compile_instruction(OPCODE_MOVE, 5, 1, 0);
				compile_pop_result(local_k);
				// compare last maximum and current parameter
				compile_instruction(OPCODE_CMP, 0, 1, 0);
				compile_instruction(OPCODE_BLT, 0, 0, function->program_size + 2);
				compile_instruction(OPCODE_MOVE, 1, 0, 0);
			}
			break;
		case 7: // abs
			compile_recursive(expr->child);
			compile_instruction(OPCODE_ABS, 0, 0, 0);
			if(expr->next != NULL)
				parser->Error("Invalid number of parameters for '%s': Only one parameter expected!", name);
			break;
		case 8: // select
			compile_select(expr);
			break;
		case 9: // user defined floating-point function
			compile_float_function_call(expr, fn, name);
			break;
		case 10: // user defined vector function
			compile_vector_function_call(expr, fn, name);
			break;
		case 11: // pow
			// first evaluate right parameter
			if(expr->next == NULL)
				parser->Error("Invalid number of parameters for '%s': Two parameters expected!", name);
			compile_recursive(expr->next->child);
			// temporary storage of right parameter in r5
			local_k = compile_push_result();
			compile_instruction(OPCODE_MOVE, 0, 5, 0);
			// evaluate left parameter
			compile_recursive(expr->child);
			// move right parameter from r5 to r1
			compile_instruction(OPCODE_MOVE, 5, 1, 0);
			compile_pop_result(local_k);
			// check domain error (if r0 and r1 are zero)
			compile_instruction(OPCODE_XDZ, 0, 1, 0);
			// make sure there are not more than 2 parameters
			if(expr->next->next != NULL)
				parser->Error("Invalid number of parameters for '%s': Only two parameters expected!", name);
			compile_instruction(OPCODE_SYS2, 0, 0, k);
			break;
		case 12: // sqr
			compile_recursive(expr->child);
			compile_instruction(OPCODE_MUL, 0, 0, 0);
			if(expr->next != NULL)
				parser->Error("Invalid number of parameters for '%s': Only one parameter expected!", name);
			break;
		case 13: // sum
			compile_seq_op(expr, OPCODE_ADD, 0.0);
			break;
		case 14: // prod
			compile_seq_op(expr, OPCODE_MUL, 1.0);
			break;
	}
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_select
*
* INPUT
*
*   expr - list of function parameters
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a select function call.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_select(Parser::ExprNode *expr)
{
	unsigned int greater_pos;
	unsigned int less_pos;
	unsigned int equal_end;
	unsigned int greater_end;
	unsigned int all_end;
	bool have_fourth = false;

	if(expr->next == NULL) // second
		parser->Error("Invalid number of parameters: Three or four parameters expected!");
	if(expr->next->next == NULL) // third
		parser->Error("Invalid number of parameters: Three or four parameters expected!");
	if(expr->next->next->next != NULL) // fourth
	{
		if(expr->next->next->next->next != NULL) // fifth
			parser->Error("Invalid number of parameters: Only three or four parameters expected!");
		have_fourth = true;
	}

	// compile condition
	compile_recursive(expr->child);
	compile_instruction(OPCODE_CMPI, 0, 0, functionVM->AddConstant(0.0));
	greater_pos = compile_instruction(OPCODE_NOP, 0, 0, 0); // bgt
	if(have_fourth == true)
		less_pos = compile_instruction(OPCODE_NOP, 0, 0, 0); // blt
	// compile equal (four parameters) or equal or less than (three parameters) part
	compile_recursive(expr->next->next->child);
	equal_end = compile_instruction(OPCODE_NOP, 0, 0, 0); // jmp
	// compile greater than part
	compile_recursive(expr->next->child);
	// if there is a fourth argument, compile less than part
	if(have_fourth == true)
	{
		greater_end = compile_instruction(OPCODE_NOP, 0, 0, 0); // jmp
		compile_recursive(expr->next->next->next->child);
	}
	// get the position after the last compiled instruction
	all_end = function->program_size;

	// fix the branches
	compile_instruction(greater_pos, OPCODE_BLT, 0, 0, equal_end + 1);
	compile_instruction(equal_end, OPCODE_JMP, 0, 0, all_end);
	if(have_fourth == true)
	{
		compile_instruction(less_pos, OPCODE_BGT, 0, 0, greater_end + 1);
		compile_instruction(greater_end, OPCODE_JMP, 0, 0, all_end);
	}
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_seq_op
*
* INPUT
*
*   expr - list of function parameters
*   op - operation to perfrom on value and result for each step
*   neutral - neutral element of the operation to apply
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a sum function call.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_seq_op(Parser::ExprNode *expr, unsigned int op, DBL neutral)
{
	unsigned int sum_k = 0;
	unsigned int local_k = 0;
	unsigned int max_k = 0;
	unsigned int begin_loop;
	unsigned int end_loop;
	unsigned int loop_condition;
	unsigned int old_level;
	unsigned int var_sp;
	unsigned int r5_content;

	if(expr->next == NULL) // second
		parser->Error("Invalid number of parameters: Four parameters expected!");
	if(expr->next->next == NULL) // third
		parser->Error("Invalid number of parameters: Four parameters expected!");
	if(expr->next->next->next != NULL) // fourth
	{
		if(expr->next->next->next->next != NULL) // fifth
			parser->Error("Invalid number of parameters: Only four parameters expected!");
	}

	// create a local variable, the sum and its limit on the stack
	if(function->localvar_cnt >= MAX_PARAMETER_LIST)
		parser->Error("Too many local variables!");

	// save r5 content
	r5_content = compile_push_result();

	// calculate variablestack pointer
	if (level >= 3)
		var_sp = stack_pointer + level - 3;
	else
		var_sp = stack_pointer;

	max_stack_size = (unsigned int)max((int)var_sp + 3, (int)max_stack_size);

	old_level = level;

	sum_k = var_sp;
	local_k = var_sp + 1;
	max_k = var_sp + 2;

	// compile_push_result (called by compile_recursive below)
	// should not overwrite the variables on the stack, hence:
	//
	// stack_pointer + level - 3 >= var_sp + 3

	level = var_sp + 3 - stack_pointer + 3;

	function->localvar_pos[function->localvar_cnt] = local_k;
	function->localvar[function->localvar_cnt] = expr->child->variable;
	if(expr->child->op != OP_VARIABLE)
		parser->Error("Local variable name expected!");
	function->localvar_cnt++;

	// clear result variable
	compile_instruction(OPCODE_LOADI, 1, 5, functionVM->AddConstant(neutral));
	compile_instruction(OPCODE_STORE, 1, 5, sum_k);
	// compile initial value expression and put it into the local variable
	compile_recursive(expr->next->child);
	compile_instruction(OPCODE_STORE, 1, 0, local_k);
	// compile final value expression and put it on the stack
	compile_recursive(expr->next->next->child);
	compile_instruction(OPCODE_STORE, 1, 0, max_k);

	// begin loop
	begin_loop = function->program_size;

		// load current value into r5
		compile_instruction(OPCODE_LOAD, 1, 5, local_k);
		// load final value into r0
		compile_instruction(OPCODE_LOAD, 1, 0, max_k);
		// check if the current value is less or equal to the final value
		compile_instruction(OPCODE_CMP, 0, 5, 0);
		loop_condition = compile_instruction(OPCODE_NOP, 0, 0, 0); // bgt

		// compile function to sum up
		compile_recursive(expr->next->next->next->child);
		// load result into r5
		compile_instruction(OPCODE_LOAD, 1, 5, sum_k);
		// add function result to sum
		compile_instruction(op, 0, 5, sum_k);
		// store sum
		compile_instruction(OPCODE_STORE, 1, 5, sum_k);

		// load current value into r5
		compile_instruction(OPCODE_LOAD, 1, 5, local_k);
		// increment current value
		compile_instruction(OPCODE_ADDI, 0, 5, functionVM->AddConstant(1.0));
		// store current value
		compile_instruction(OPCODE_STORE, 1, 5, local_k);

	// end loop
	compile_instruction(OPCODE_JMP, 0, 0, begin_loop);
	end_loop = function->program_size;

	// fix the branches
	compile_instruction(loop_condition, OPCODE_BGT, 0, 0, end_loop);

	// move sum into return register
	compile_instruction(OPCODE_LOAD, 1, 0, sum_k);

	// remove the local variable, the sum and its limit from the stack
	function->localvar_cnt--;
	function->localvar_pos[function->localvar_cnt] = 0;
	function->localvar[function->localvar_cnt] = NULL;

	level = old_level;
	// restore r5 content
	compile_pop_result(r5_content);
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_float_function_call
*
* INPUT
*
*   expr - list of function parameters
*   fn - user defined function reference
*   name - name of the function (required)
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a user-defined floating-point function call.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_float_function_call(Parser::ExprNode *expr, FUNCTION fn, char *name)
{
	FunctionCode *f = NULL;
	Parser::ExprNode *i = NULL;
	unsigned int cur_p = 0;
	unsigned int local_k = 0;
	unsigned int old_sp = 0;
	unsigned int old_parameter_sp = 0;
	unsigned int old_level = 0;
	unsigned int r567_sp = 0;
	unsigned int call_sp = 0;
	unsigned int call_parameter_sp = 0;

	// The VM stack format looks like this:
	//
	//           *
	//          ***
	//         *****
	//        *******
	//      Stack grows
	//     from lower to
	//    higher addresses
	//
	// +--------------------+
	// |      Temporary     |
	// |      Variables     |
	// +--------------------+ <= Temporary Variables Stack Pointer (call_sp)
	// |    Function Call   |
	// |     Parameters     |
	// +--------------------+ <= Call Stack Pointer (call_parameter_sp)
	// |      Registers     |    (will be new stack pointer for call)
	// |    r5, r6 and r7   |
	// +--------------------+ <= Register Buffer Stack Pointer (r567_sp)
	// |      Temporary     |
	// |      Variables     |
	// +--------------------+ <= Stack Pointer (stack_pointer)
	// |      Function      |
	// |   Local Variables  |
	// +--------------------+ <= (Currently there are no local variables!)
	// |      Function      |
	// |     Parameters     |
	// +--------------------+ <= Parameter Stack Pointer (parameter_stack_pointer)

	if(strcmp(name, function->name) == 0)
	{
//		Warning(0, "Recursive function call may have unexpected side effects or not\n"
//		           "work as expected! Make sure the recursion is not infinite!!!");
		parser->PossibleError("Recursive function calls are not allowed!");
		// recursive calls may not increase the reference count [trf]
		f = function;
	}
	else
		f = functionVM->GetFunctionAndReference(fn);

	// calculate stack pointer to hold the registers
	r567_sp = (unsigned int)(((int)stack_pointer) + max(((int)(level)) - 2, 0));
	// calculate call parameter stack pointer
	call_parameter_sp = r567_sp + min(level + 1, 3);
	// copy result register r5 to the stack
	compile_instruction(OPCODE_STORE, 1, 5, r567_sp);
	// copy result register r6 to the stack
	if(level >= 1)
		compile_instruction(OPCODE_STORE, 1, 6, r567_sp + 1);
	// copy result register r7 to the stack
	if(level >= 2)
		compile_instruction(OPCODE_STORE, 1, 7, r567_sp + 2);

	// calculate call stack pointer
	call_sp = call_parameter_sp + f->parameter_cnt;
	// keep current level, stack pointer and function
	old_level = level;
	old_sp = stack_pointer;
	old_parameter_sp = parameter_stack_pointer;
	// set new stack pointer, reset level and change function
	level = 0;
	stack_pointer = call_sp;
	parameter_stack_pointer = call_parameter_sp;
	// make sure the max_stack_size is at least the current size
	max_stack_size = (unsigned int)max((int)stack_pointer, (int)max_stack_size);

	// determine all the parameters
	for(i = expr, cur_p = 0; i != NULL; i = i->next, cur_p++)
	{
		// compile the parameter expression
		compile_recursive(i->child);
		// copy the result as parameter to the stack
		compile_instruction(OPCODE_STORE, 1, 0, call_parameter_sp + cur_p);
	}
	// make sure the supplied number of parameters matches the required number
	if(cur_p != f->parameter_cnt)
		parser->Error("Invalid number of parameters: %d supplied, %d required!",
		              (int)(cur_p), (int)(f->parameter_cnt));

	// restore the old level, stack pointer and function
	level = old_level;
	stack_pointer = old_sp;
	parameter_stack_pointer = old_parameter_sp;

	// move the stack pointer so the parameters are at the stack base
	compile_instruction(OPCODE_PUSH, 0, 0, call_parameter_sp); // call_parameter_sp - parameter_stack_pointer
	// call the function and inline non-recursive calls to functions which are less than 16 instructions size
/*	if((f->program_size < 16) && (f != function))
	{
		POVFPU_RemoveFunction(fn);
		compile_inline(f);
	}
	else
*/		compile_instruction(OPCODE_CALL, 0, 0, fn);
	// move the stack pointer back
	compile_instruction(OPCODE_POP, 0, 0, call_parameter_sp); // call_parameter_sp - parameter_stack_pointer

	// copy result register r5 back from the stack
	compile_instruction(OPCODE_LOAD, 1, 5, r567_sp);
	// copy result register r6 back from the stack
	if(level >= 1)
		compile_instruction(OPCODE_LOAD, 1, 6, r567_sp + 1);
	// copy result register r7 back from the stack
	if(level >= 2)
		compile_instruction(OPCODE_LOAD, 1, 7, r567_sp + 2);

	// restore parameters x, y and z
	compile_parameters();
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_vector_function_call
*
* INPUT
*
*   expr - list of function parameters
*   fn - user defined function reference
*   name - name of the function (required)
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a user-defined vector function call.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_vector_function_call(Parser::ExprNode *expr, FUNCTION fn, char *name)
{
	FunctionCode *f = NULL;
	Parser::ExprNode *i = NULL;
	unsigned int cur_p = 0;
	unsigned int local_k = 0;
	unsigned int old_sp = 0;
	unsigned int old_parameter_sp = 0;
	unsigned int old_level = 0;
	unsigned int r567_sp = 0;
	unsigned int call_sp = 0;
	unsigned int call_parameter_sp = 0;

	// The VM stack format looks like this:
	//
	//           *
	//          ***
	//         *****
	//        *******
	//      Stack grows
	//     from lower to
	//    higher addresses
	//
	// +--------------------+
	// |      Temporary     |
	// |      Variables     |
	// +--------------------+ <= Temporary Variables Stack Pointer (call_sp)
	// |    Function Call   |
	// |     Parameters     |
	// +--------------------+ <= Call Stack Pointer (call_parameter_sp)
	// |      Function      |
	// |    Return Values   |
	// +--------------------+ <= Return Stack Pointer
	// |      Registers     |    (will be new stack pointer for call)
	// |    r5, r6 and r7   |
	// +--------------------+ <= Register Buffer Stack Pointer (r567_sp)
	// |      Temporary     |
	// |      Variables     |
	// +--------------------+ <= Stack Pointer (stack_pointer)
	// |      Function      |
	// |   Local Variables  |
	// +--------------------+ <= (Currently there are no local variables!)
	// |      Function      |
	// |     Parameters     |
	// +--------------------+ <= Parameter Stack Pointer (parameter_stack_pointer)

	if(strcmp(name, function->name) == 0)
	{
//		Warning(0, "Recursive function call may have unexpected side effects or not\n"
//		           "work as expected! Make sure the recursion is not infinite!!!");
		parser->PossibleError("Recursive function calls are not allowed!");
		// recursive calls may not increase the reference count [trf]
		f = function;
	}
	else
		f = functionVM->GetFunctionAndReference(fn);

	// calculate stack pointer to hold the registers
	r567_sp = (unsigned int)(((int)stack_pointer) + max(((int)(level)) - 2, 0));
	// calculate call parameter stack pointer
	call_parameter_sp = r567_sp + f->return_size + min(level + 1, 3);
	// copy result register r5 to the stack
	compile_instruction(OPCODE_STORE, 1, 5, r567_sp);
	// copy result register r6 to the stack
	if(level >= 1)
		compile_instruction(OPCODE_STORE, 1, 6, r567_sp + 1);
	// copy result register r7 to the stack
	if(level >= 2)
		compile_instruction(OPCODE_STORE, 1, 7, r567_sp + 2);

	// calculate call stack pointer
	call_sp = call_parameter_sp + f->parameter_cnt;
	// keep current level, stack pointer and function
	old_level = level;
	old_sp = stack_pointer;
	old_parameter_sp = parameter_stack_pointer;
	// set new stack pointer, reset level and change function
	level = 0;
	stack_pointer = call_sp;
	parameter_stack_pointer = call_parameter_sp;
	// make sure the max_stack_size is at least the current size
	max_stack_size = (unsigned int)max((int)stack_pointer, (int)max_stack_size);

	// determine all the parameters
	for(i = expr, cur_p = 0; i != NULL; i = i->next, cur_p++)
	{
		// compile the parameter expression
		compile_recursive(i->child);
		// copy the result as parameter to the stack
		compile_instruction(OPCODE_STORE, 1, 0, call_parameter_sp + cur_p);
	}
	// make sure the supplied number of parameters matches the required number
	if(cur_p != f->parameter_cnt)
		parser->Error("Invalid number of parameters: %d supplied, %d required!",
		              (int)(cur_p), (int)(f->parameter_cnt));

	// restore the old level, stack pointer and function
	level = old_level;
	stack_pointer = old_sp;
	parameter_stack_pointer = old_parameter_sp;

	// move the stack pointer so the parameters are at the stack base
	compile_instruction(OPCODE_PUSH, 0, 0, call_parameter_sp - f->return_size); //  - parameter_stack_pointer
	// call the function
	// call the function and inline non-recursive calls to functions which are less than 16 instructions size
/*	if((f->program_size < 16) && (f != function))
	{
		POVFPU_RemoveFunction(fn);
		compile_inline(f);
	}
	else
*/		compile_instruction(OPCODE_CALL, 0, 0, fn);
	// move the stack pointer back
	compile_instruction(OPCODE_POP, 0, 0, call_parameter_sp - f->return_size); //  - parameter_stack_pointer

	// copy result register r5 back from the stack
	compile_instruction(OPCODE_LOAD, 1, 5, r567_sp);
	// copy result register r6 back from the stack
	if(level >= 1)
		compile_instruction(OPCODE_LOAD, 1, 6, r567_sp + 1);
	// copy result register r7 back from the stack
	if(level >= 2)
		compile_instruction(OPCODE_LOAD, 1, 7, r567_sp + 2);

	// restore parameters x, y and z
	compile_parameters();
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_inline
*
* INPUT
*
*   fn - user defined function reference
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a user-defined function as inline function.
*   Does not work yet! [trf]
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_inline(FunctionCode *f)
{
	unsigned int offset = function->program_size - 1;
	unsigned int op, k;
	unsigned int i = 0;

	for(i = 0; i < f->program_size - 1; i++)
	{
		op = GET_OP(f->program[i]);
		k = GET_K(f->program[i]);
		if(((op >= OPCODE_BEQ) && (op <= OPCODE_BGE)) || (op == OPCODE_JMP))
			compile_instruction(GET_OP(op), 0, 0, GET_K(k + offset));
		else
			compile_instruction(GET_OP(op), 0, 0, GET_K(k));
	}
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_variable
*
* INPUT
*   
*   name - variable name (required)
*   number - pointer to declared constant variable value (optional)
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles a variable access.  Note that parameters will hide declared
*   constants.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_variable(char *name)
{
	unsigned int i = 0, found = MAX_K;

	// first, handle register parameters x,y,z,u and v
	if(name[1] == 0)
	{
		if((name[0] == 'x') || (name[0] == 'u'))
		{
			compile_instruction(OPCODE_MOVE, 2, 0, 0);
			return;
		}
		else if((name[0] == 'y') || (name[0] == 'v'))
		{
			compile_instruction(OPCODE_MOVE, 3, 0, 0);
			return;
		}
		else if(name[0] == 'z')
		{
			compile_instruction(OPCODE_MOVE, 4, 0, 0);
			return;
		}
	}

	// second, handle stack local variables
	for(i = 0; i < function->localvar_cnt; i++)
	{
		if(strcmp(name, function->localvar[i]) == 0)
			found = i;
	}
	if(found < function->localvar_cnt)
	{
		compile_instruction(OPCODE_LOAD, 1, 0, function->localvar_pos[found]);
		return;
	}

	// third, handle stack parameters
	for(i = 0; i < function->parameter_cnt; i++)
	{
		if(strcmp(name, function->parameter[i]) == 0)
		{
			compile_instruction(OPCODE_LOAD, 1, 0, i);
			return;
		}
	}

	parser->Expectation_Error("parameter identifier or floating-point constant identifier");
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_parameters
*
* INPUT
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Compiles the function call parameters. Load the parameters x,y,z into
*   registers and assign stack locations to all other parameters.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_parameters()
{
	unsigned int i = 0;
	bool had_x = false;
	bool had_y = false;
	bool had_z = false;

	// if it is a function with default parameters, add them
	if(function->parameter_cnt == 0)
	{
		function->parameter_cnt = 3;
		function->parameter[0] = POV_STRDUP("x");
		function->parameter[1] = POV_STRDUP("y");
		function->parameter[2] = POV_STRDUP("z");
	}

	for(i = 0; i < function->parameter_cnt; i++)
	{
		if((strcmp(function->parameter[i], "x") == 0) ||
		   (strcmp(function->parameter[i], "u") == 0))
		{
			compile_instruction(OPCODE_LOAD, 1, 2, i);
			had_x = true;
		}
		else if((strcmp(function->parameter[i], "y") == 0) ||
		        (strcmp(function->parameter[i], "v") == 0))
		{
			compile_instruction(OPCODE_LOAD, 1, 3, i);
			had_y = true;
		}
		else if(strcmp(function->parameter[i], "z") == 0)
		{
			compile_instruction(OPCODE_LOAD, 1, 4, i);
			had_z = true;
		}
	}

	if(had_x == false)
		compile_instruction(OPCODE_LOADI, 0, 2, functionVM->AddConstant(0.0));
	if(had_y == false)
		compile_instruction(OPCODE_LOADI, 0, 3, functionVM->AddConstant(0.0));
	if(had_z == false)
		compile_instruction(OPCODE_LOADI, 0, 4, functionVM->AddConstant(0.0));
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_push_result
*
* INPUT
*   
* OUTPUT
*   
* RETURNS
*
*   unsigned int - local variable reference number (if any)
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Push the result register r5 on a stack or unused register.  This is
*   required to handle different operator precedences correctly.  The
*   function uses the global variable "level" to find the right storage
*   location. Also see description of partner function compile_pop_result.
*
* CHANGES
*
*   -
*
******************************************************************************/

unsigned int FNCode::compile_push_result()
{
	max_stack_size = (unsigned int)max(((int)(stack_pointer)) + ((int)(level)) - 2, ((int)(max_stack_size)));

	if(level == 1)
		compile_instruction(OPCODE_MOVE, 5, 6, 0);
	else if(level == 2)
		compile_instruction(OPCODE_MOVE, 5, 7, 0);
	else if(level >= 3)
		compile_instruction(OPCODE_STORE, 1, 5, stack_pointer + level - 3);

	level++;

	// NOTE: Always returns a valid result also there is not always one! [trf]
	return max(((int)(stack_pointer)) + ((int)(level)) - 4, 0);
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_pop_result
*
* INPUT
*
*   local_k - local variable reference number (if any)
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Pop the result register r5 from a stack or unused register.  This is
*   required to handle different operator precedences correctly.  The
*   function uses the global variable "level" to find the right storage
*   location. Also see description of partner function compile_push_result.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::compile_pop_result(unsigned int local_k)
{
	level--;

	if(level == 1)
		compile_instruction(OPCODE_MOVE, 6, 5, 0);
	else if(level == 2)
		compile_instruction(OPCODE_MOVE, 7, 5, 0);
	else if(level >= 3)
		compile_instruction(OPCODE_LOAD, 1, 5, local_k);
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_instruction
*
* INPUT
*
*   op - instruction opcode (required)
*   rs - source register (if any), set to 0 if not used
*   rd - destination register (if any), set to 0 if not used
*   k - optional parameter (if any), set to 0 if not used
*   
* OUTPUT
*   
* RETURNS
*
*   unsigned int - location of the instruction in the program memory
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Assemble an individual instruction and store it in the program memory.
*
* CHANGES
*
*   -
*
******************************************************************************/

unsigned int FNCode::compile_instruction(unsigned int op, unsigned int rs, unsigned int rd, unsigned int k)
{
	if(function->program_size >= max_program_size)
	{
		max_program_size = min(MAX_K, ((unsigned int)max_program_size) + 256);
		function->program = (Instruction *)POV_REALLOC(function->program, sizeof(Instruction) * max_program_size, "fn: program");
	}
/*	if(function->program_size > 0)
	{
		unsigned int i;

		// eliminate unnecessary move instruction
		//
		// ************************************************************
		//
		// IMPORTANT NOTE:
		// These optimisations do not take conditional branch
		// instructions in account!  This would have dangerous
		// side effects, but the current compiler does not
		// generate code for which these optimisations will
		// fail.  In the future the compiler will be extended
		// to use an intermediate code representation and more
		// advanced optimisation techniques, but this is a
		// project of its own and it would have made a release
		// of POV-Ray 3.5 next to impossible for at least
		// another year... [trf]
		//
		// ************************************************************
		//
		if(op == OPCODE_MOVE)
		{
			for(i = 0; i < function->program_size; i++)
			{
				// If there is a conditional branch instruction make sure it does not
				// cover the part that will be optimised away! [trf]
				if((function->program[i].op & OPCODE(13, 0, 0)) & 0x03C0 == OPCODE(13, 0, 0))
				{
					if(function->program[i].k >= function->program_size)
						break;
				}
			}

			// optimise only if the loop found no conditional branch
			if(i < function->program_size)
			{
				// case
				//   move rx, ry
				//   move ry, rx
				//  to
				//   move rx, ry
				// case
				//   move rx, ry
				//   move rx, ry
				//  to
				//   move rx, ry
				if((function->program[function->program_size - 1].op == (OPCODE_MOVE | (rd << 3) | rs)) ||
				   (function->program[function->program_size - 1].op == (OPCODE_MOVE | (rs << 3) | rd)))
				{
					return function->program_size - 1;
				}
				// case
				//   move rx, ry
				//   move rz, ry
				//  to
				//   move rz, ry
				else if((function->program[function->program_size - 1].op & 0x03C7) == (OPCODE_MOVE | rd ))
				{
					function->program[function->program_size - 1].op = OPCODE_MOVE | (rs << 3) | rd;
					return function->program_size - 1;
				}
			}
		}
	}*/
	function->program[function->program_size] = MAKE_INSTRUCTION(op | (rs << 3) | rd, k);
	function->program_size++;
	if(function->program_size >= MAX_K)
		parser->Error("Function too complex!");

	return function->program_size - 1;
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::compile_instruction
*
* INPUT
*
*   pos - insert location of the instruction (required)
*   op - instruction opcode (required)
*   rs - source register (if any), set to 0 if not used
*   rd - destination register (if any), set to 0 if not used
*   k - optional parameter (if any), set to 0 if not used
*   
* OUTPUT
*   
* RETURNS
*
*   unsigned int - location of the instruction in the program memory
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Assemble an individual instruction and store it in the program memory at
*   the specified position.
*
* CHANGES
*
*   -
*
******************************************************************************/

unsigned int FNCode::compile_instruction(unsigned int pos, unsigned int op, unsigned int rs, unsigned int rd, unsigned int k)
{
	if(pos >= function->program_size)
		parser->Error("Internal function compiler failed in 'compile_instruction'.");
	function->program[pos] = MAKE_INSTRUCTION(op | (rs << 3) | rd, k);

	return pos;
}


#if (DEBUG_FLOATFUNCTION == 1)

/*****************************************************************************
*
* FUNCTION
*
*   FNCode::disassemble
*
* INPUT
*
*   filename - output file
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Disassembler for debugging of the virtual machine and the compiler.
*
* CHANGES
*
*   -
*
******************************************************************************/

int FNCode::disassemble(char *filename)
{
	FILE *f;
	int i;

	if(strcmp(filename, "stdout") == 0)
		f = stdout;
	else
	{
		f = fopen(filename, "w");
		if(f == NULL)
		{
			asm_error = "Cannot open disassembler output file.";
			return 1;
		}
	}

	for(i = 0; i < function->program_size; i++)
		disassemble_instruction(f, function->program[i]);

	if(f != stdout)
		fclose(f);

	return 0;
}


/*****************************************************************************
*
* FUNCTION
*
*   FNCode::disassemble_instruction
*
* INPUT
*   
*   f - output file
*   i - instruction to disassemble
*   
* OUTPUT
*   
* RETURNS
*
* AUTHOR
*
*   Thorsten Froehlich
*   
* DESCRIPTION
*
*   Disassemble a single instruction.
*
* CHANGES
*
*   -
*
******************************************************************************/

void FNCode::disassemble_instruction(FILE *f, Instruction& i)
{
	int ii;
	unsigned int op;
	unsigned int program_op;
	unsigned int program_k;

	program_op = GET_OP(i);
	program_k = GET_K(i);

	for(ii = 0; POVFPU_Opcodes[ii].name != NULL; ii++)
	{
		op = POVFPU_Opcodes[ii].code;
		switch(POVFPU_Opcodes[ii].type)
		{
			case ITYPE_R:
				if(op == (program_op & 0x03C0))
				{
					fprintf(f, "%-8s", POVFPU_Opcodes[ii].name);
					fprintf(f, "R%d,R%d\n", (int)((program_op >> 3) & 7), (int)(program_op & 7));
				}
				break;
			case ITYPE_I:
				if(op == (program_op & 0x03F8))
				{
					fprintf(f, "%-8s", POVFPU_Opcodes[ii].name);
					fprintf(f, "%f,R%d   # const(%d)\n", (float)(POVFPU_Consts[program_k]),
					        (int)(program_op & 7), (int)program_k);
				}
				break;
			case ITYPE_S:
				if(op == (program_op & 0x03F8))
				{
					fprintf(f, "%-8s", POVFPU_Opcodes[ii].name);
					if((program_op & 0x03F8) == OPCODE_XDZ)
						fprintf(f, "R0,");
					fprintf(f, "R%d\n", (int)(program_op & 7));
				}
				break;
			case ITYPE_J:
				if(op == (program_op & 0x03FF))
				{
					fprintf(f, "%-8s", POVFPU_Opcodes[ii].name);
					fprintf(f, "%d\n", (int)program_k);
				}
				break;
			case ITYPE_X:
				if(op == (program_op & 0x03FF))
				{
					fprintf(f, "%-8s", POVFPU_Opcodes[ii].name);
					fprintf(f, "\n");
				}
				break;
			case ITYPE_M:
				if(op == (program_op & 0x03C0))
				{
					fprintf(f, "%-8s", POVFPU_Opcodes[ii].name);
					if((program_op & 0x03C0) == OPCODE_LOAD)
					{
						if((program_op & 8) == 0)
							fprintf(f, "0(%d),R%d\n", (int)program_k, (int)(program_op & 7));
						else
							fprintf(f, "SP(%d),R%d\n", (int)program_k, (int)(program_op & 7));
					}
					else if((program_op & 0x03C0) == OPCODE_STORE)
					{
						if((program_op & 8) == 0)
							fprintf(f, "R%d,0(%d)\n", (int)(program_op & 7), (int)program_k);
						else
							fprintf(f, "R%d,SP(%d)\n", (int)(program_op & 7), (int)program_k);
					}
				}
				break;
		}
	}
}

#include "fnasm.cpp"

#endif

}