1 /******************************************************************************
2 *
3 * Copyright (C) 2006, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * ID: compinit 18.10.06 0.0.A.
11 *
12 * Author: Alexey Krivonogov ( gentee )
13 *
14 * Conributors: santy
15 * Summary:
16 *
17 ******************************************************************************/
18
19 #include "compinit.h"
20 #include "../vm/vm.h"
21 #include "../vm/vmload.h"
22 #include "../vm/vmmanage.h"
23
24 uint _compinit = 0;
25 uint _lexlist[ OPERCOUNT ];
26
27 typedef struct
28 {
29 ubyte left; // Type of the left operand
30 ubyte right; // Type of the right operand
31 ubyte ret; // Type of the result
32 } oppar, * poppar;
33
34 // Структура описания встроенных операций
35 typedef struct
36 {
37 ubyte oper; // Oparation
38 ubyte bc; // stack byte code
39 } oper, * poper;
40
41 enum
42 { // left_right_result
43 B_I_I = 0,
44 B_0_I,
45 UB_UI_UI,
46 UB_0_UI,
47 S_I_I,
48 S_0_I,
49 US_UI_UI,
50 US_0_UI,
51 UI_UI_UI,
52 UI_0_UI,
53 UI_0_I,
54 I_I_I,
55 I_0_I,
56 I_I_UI,
57 UL_UL_UL,
58 UL_0_UL,
59 UL_UL_UI,
60 UL_0_UI,
61 UL_0_L,
62 L_L_L,
63 L_0_L,
64 L_L_UI,
65 F_F_F,
66 F_0_F,
67 F_F_UI,
68 F_0_UI,
69 D_D_D,
70 D_0_D,
71 D_D_UI,
72 D_0_UI,
73 ST_ST_ST,
74 ST_0_ST,
75 };
76
77 const oppar oppars[]={
78 { TByte, TInt, TInt }, // B_I_I
79 { TByte, 0, TInt }, // B_0_I
80 { TUbyte, TUint, TUint }, // UB_UI_UI
81 { TUbyte, 0, TUint }, // UB_0_UI
82 { TShort, TInt, TInt }, // T_I_I
83 { TShort, 0, TInt }, // T_0_I
84 { TUshort, TUint, TUint }, // UT_UI_UI
85 { TUshort, 0, TUint }, // UT_0_UI
86 { TUint, TUint, TUint }, // UI_UI_UI
87 { TUint, 0, TUint }, // UI_0_UI
88 { TUint, 0, TInt }, // UI_0_I
89 { TInt, TInt, TInt }, // I_I_I
90 { TInt, 0, TInt }, // I_0_I
91 { TInt, TInt, TUint }, // I_I_UI
92 { TUlong, TUlong, TUlong }, // UL_UL_UL
93 { TUlong, 0, TUlong }, // UL_0_UL
94 { TUlong, TUlong, TUint }, // UL_UL_UI
95 { TUlong, 0, TUint }, // UL_0_UI
96 { TUlong, 0, TLong }, // UL_0_L
97 { TLong, TLong, TLong }, // L_L_L
98 { TLong, 0, TLong }, // L_0_L
99 { TLong, TLong, TUint }, // L_L_UI
100 { TFloat, TFloat, TFloat }, // F_F_F
101 { TFloat, 0, TFloat }, // F_0_F
102 { TFloat, TFloat, TUint }, // F_F_UI
103 { TFloat, 0, TUint }, // F_0_UI
104 { TDouble, TDouble, TDouble }, // D_D_D
105 { TDouble, 0, TDouble }, // D_0_D
106 { TDouble, TDouble, TUint }, // D_D_UI
107 { TDouble, 0, TUint }, // D_0_UI
108 { TStr, TStr, TStr }, // TT_TT_TT
109 { TStr, 0, TStr }, // TT_0_TT
110 };
111
112 const oper operpars[]={
113 { 0xFF, B_I_I },
114 { OpSet, CSetUB }, { OpAddset, CAddUB }, { OpSubset, CSubUB },
115 { OpMulset, CMulB }, { OpDivset, CDivB }, { OpModset, CModB },
116 { OpLeftset, CLeftB },{ OpRightset, CRightB },
117 { 0xFF, B_0_I },
118 { OpIncleft, CIncLeftUB }, { OpIncright, CIncRightUB }, { OpDecleft, CDecLeftUB },
119 { OpDecright, CDecRightUB },
120
121 { 0xFF, UB_UI_UI },
122 { OpSet, CSetUB }, { OpAddset, CAddUB }, { OpSubset, CSubUB },
123 { OpMulset, CMulUB }, { OpDivset, CDivUB }, { OpModset, CModUB },
124 { OpAndset, CAndUB }, { OpOrset, COrUB }, { OpXorset, CXorUB },
125 { OpLeftset, CLeftB },{ OpRightset, CRightB },
126 { 0xFF, UB_0_UI },
127 { OpIncleft, CIncLeftUB }, { OpIncright, CIncRightUB }, { OpDecleft, CDecLeftUB },
128 { OpDecright, CDecRightUB }, { OpLognot, CLognot },
129
130 { 0xFF, S_I_I },
131 { OpSet, CSetUS },{ OpAddset, CAddUS },{ OpSubset, CSubUS },
132 { OpMulset, CMulS }, { OpDivset, CDivS }, { OpModset, CModS },
133 { OpLeftset, CLeftS },{ OpRightset, CRightS },
134 { 0xFF, S_0_I },
135 { OpIncleft, CIncLeftUS }, { OpIncright, CIncRightUS }, { OpDecleft, CDecLeftUS },
136 { OpDecright, CDecRightUS },
137
138 { 0xFF, US_UI_UI },
139 { OpSet, CSetUS }, { OpAddset, CAddUS }, { OpSubset, CSubUS },
140 { OpMulset, CMulUS }, { OpDivset, CDivUS }, { OpModset, CModUS },
141 { OpAndset, CAndUS }, { OpOrset, COrUS }, { OpXorset, CXorUS },
142 { OpLeftset, CLeftUS }, { OpRightset, CRightUS },
143 { 0xFF, US_0_UI },
144 { OpIncleft, CIncLeftUS }, { OpIncright, CIncRightUS },
145 { OpDecleft, CDecLeftUS }, { OpDecright, CDecRightUS },
146
147 { 0xFF, UI_UI_UI },
148 { OpSet, CSetI }, { OpAdd, CAddUIUI }, { OpSub, CSubUIUI },
149 { OpMul, CMulUIUI }, { OpDiv, CDivUIUI }, { OpMod, CModUIUI },
150 { OpBinand, CAndUIUI }, { OpBinor, COrUIUI }, { OpBinxor, CXorUIUI },
151 { OpLeft, CLeftUIUI }, { OpRight, CRightUIUI }, { OpLess, CLessUIUI },
152 { OpEq, CEqUIUI }, { OpGreater, CGreaterUIUI },
153 { OpAddset, CAddUI }, { OpSubset, CSubUI }, { OpMulset, CMulUI },
154 { OpDivset, CDivUI }, { OpModset, CModUI }, { OpAndset, CAndUI },
155 { OpOrset, COrUI }, { OpXorset, CXorUI }, { OpLeftset, CLeftUI },
156 { OpRightset, CRightUI },
157 { 0xFF, UI_0_UI },
158 { OpBinnot, CNotUI }, { OpLognot, CLognot }, { OpIncleft, CIncLeftUI },
159 { OpIncright, CIncRightUI }, { OpDecleft, CDecLeftUI }, { OpDecright, CDecRightUI },
160 { 0xFF, UI_0_I },
161 { OpMinus, CSignI },
162
163 { 0xFF, I_I_I },
164 { OpSet, CSetI }, { OpAdd, CAddUIUI }, { OpSub, CSubUIUI },
165 { OpMul, CMulUIUI }, { OpDiv, CDivII }, { OpMod, CModII },
166 { OpLeft, CLeftUIUI }, { OpRight, CRightII }, { OpAddset, CAddUI },
167 { OpSubset, CSubUI }, { OpMulset, CMulUI }, { OpDivset, CDivI },
168 { OpModset, CModI }, { OpLeftset, CLeftUI },{ OpRightset, CRightI },
169 { 0xFF, I_0_I },
170 { OpMinus, CSignI }, { OpIncleft, CIncLeftUI }, { OpIncright, CIncRightUI },
171 { OpDecleft, CDecLeftUI }, { OpDecright, CDecRightUI },
172 { 0xFF, I_I_UI },
173 { OpLess, CLessII }, { OpGreater, CGreaterII },
174
175 { 0xFF, UL_UL_UL },
176 { OpSet, CSetL }, { OpAdd, CAddULUL }, { OpSub, CSubULUL },
177 { OpMul, CMulULUL }, { OpDiv, CDivULUL }, { OpMod, CModULUL },
178 { OpBinand, CAndULUL }, { OpBinor, COrULUL }, { OpBinxor, CXorULUL },
179 { OpLeft, CLeftULUL }, { OpRight, CRightULUL },
180 { OpAddset, CAddUL }, { OpSubset, CSubUL }, { OpMulset, CMulUL },
181 { OpDivset, CDivUL }, { OpModset, CModUL }, { OpAndset, CAndUL },
182 { OpOrset, COrUL }, { OpXorset, CXorUL }, { OpLeftset, CLeftUL },
183 { OpRightset, CRightUL },
184 { 0xFF, UL_0_UL },
185 { OpBinnot, CNotUL }, { OpIncleft, CIncLeftUL }, { OpIncright, CIncRightUL },
186 { OpDecleft, CDecLeftUL }, { OpDecright, CDecRightUL },
187 { 0xFF, UL_UL_UI },
188 { OpLess, CLessULUL }, { OpEq, CEqULUL }, { OpGreater, CGreaterULUL },
189 { 0xFF, UL_0_UI },
190 { OpLognot, CLoglongnot },
191 { 0xFF, UL_0_L },
192 { OpMinus, CSignL },
193
194 { 0xFF, L_L_L },
195 { OpSet, CSetL }, { OpAdd, CAddULUL }, { OpSub, CSubULUL },
196 { OpMul, CMulLL }, { OpDiv, CDivLL }, { OpMod, CModLL },
197 { OpLeft, CLeftLL }, { OpRight, CRightLL },{ OpAddset, CAddUL },
198 { OpSubset, CSubUL }, { OpMulset, CMulL }, { OpDivset, CDivL },
199 { OpModset, CModL }, { OpLeftset, CLeftL },{ OpRightset, CRightL },
200 { 0xFF, L_0_L },
201 { OpMinus, CSignL }, { OpIncleft, CIncLeftUL }, { OpIncright, CIncRightUL },
202 { OpDecleft, CDecLeftUL }, { OpDecright, CDecRightUL },
203 { 0xFF, L_L_UI },
204 { OpLess, CLessLL }, { OpGreater, CGreaterLL },
205
206 { 0xFF, F_F_F },
207 { OpSet, CSetI }, { OpAdd, CAddFF }, { OpSub, CSubFF },
208 { OpMul, CMulFF }, { OpDiv, CDivFF }, { OpAddset, CAddF },
209 { OpSubset, CSubF }, { OpMulset, CMulF },{ OpDivset, CDivF },
210 { 0xFF, F_0_F },
211 { OpMinus, CSignF }, { OpIncleft, CIncLeftF }, { OpIncright, CIncRightF },
212 { OpDecleft, CDecLeftF }, { OpDecright, CDecRightF },
213 { 0xFF, F_F_UI },
214 { OpLess, CLessFF }, { OpEq, CEqFF }, { OpGreater, CGreaterFF },
215 { 0xFF, F_0_UI },
216 { OpLognot, CLognot },
217
218 { 0xFF, D_D_D },
219 { OpSet, CSetL }, { OpAdd, CAddDD }, { OpSub, CSubDD },
220 { OpMul, CMulDD }, { OpDiv, CDivDD }, { OpAddset, CAddD },
221 { OpSubset, CSubD }, { OpMulset, CMulD }, { OpDivset, CDivD },
222 { 0xFF, D_0_D },
223 { OpMinus, CSignD }, { OpIncleft, CIncLeftD }, { OpIncright, CIncRightD },
224 { OpDecleft, CDecLeftD }, { OpDecright, CDecRightD },
225 { 0xFF, D_0_UI },
226 { OpLognot, CLoglongnot },
227 { 0xFF, D_D_UI },
228 { OpLess, CLessDD }, { OpEq, CEqDD }, { OpGreater, CGreaterDD },
229
230 // Для text функций
231 { 0xFF, ST_ST_ST },
232 { OpStradd, 0 },
233 { 0xFF, ST_0_ST },
234 { OpStrout, 0 },
235
236 { 0, 0 }
237 };
238
239 const ubyte typesto[ 5 ][ 10 ]={
240 // int, uint, byte, ubyte, short, ushort, float, double, long, ulonge
241 //* byte */{ 0, 0, 0, 0, 0, 0, Cf2i, Cd2i , Cl2i, Cul2i},
242 //* ubyte */{ 0, 0, 0, 0, 0, 0, Cf2i, Cd2i , Cl2i, Cul2i},
243 //* short */{ 0, 0, 0, 0, 0, 0, Cf2i, Cd2i , Cl2i, Cul2i},
244 //*ushort */{ 0, 0, 0, 0, 0, 0, Cf2i, Cd2i , Cl2i, Cul2i},
245 //* int */{ 0, 0, 0, 0, 0, 0, Cf2i, Cd2i , Cl2i, Cul2i},
246 /* uint */ {CNop, CNop, CNop, CNop, CNop, CNop, Cf2i, Cd2i , Cl2i, Cl2i},
247 /* float */ {Ci2f, Cui2f, Ci2f, Cui2f, Ci2f, Cui2f, CNop, Cd2f , Cl2f, Cl2f},
248 /* double*/ {Ci2d, Cui2d, Ci2d, Cui2d, Ci2d, Cui2d, Cf2d, CNop , Cl2d, Cl2d},
249 /* long */ {Ci2l, Cui2l, Ci2l, Cui2l, Ci2l, Cui2l, Cf2l, Cd2l , CNop, CNop},
250 /* ulong */ {Ci2l, Cui2l, Ci2l, Cui2l, Ci2l, Cui2l, Cf2l, Cd2l , CNop, CNop},
251 };
252
253 /*-----------------------------------------------------------------------------
254 *
255 * ID: compinit 19.10.06 0.0.A.
256 *
257 * Summary: Initialize the compiler.
258 *
259 -----------------------------------------------------------------------------*/
260
261 void STDCALL initcompile( void )
262 {
263 uint i, id, ret, idto, idfrom;
264 pvmfunc pfunc;
265 ubyte out[ 128 ];
266 puint ptr;
267 uint parl, parr;
268 pubyte ptrlex = ( pubyte )&operlexlist;
269
270 #if defined ( __GNUC__) || defined (__TINYC__)
271 pubyte ptrpar; // santy
272 #endif
273
274 if ( _compinit )
275 return;
276 _compinit = 1;
277 // if ( _vm.count > KERNEL_COUNT )
278 // return;
279
280 // Заполняем от верхних к низу
281 for ( i = 0; i < OPERCOUNT; i++ )
282 {
283 _lexlist[ i ] = 0;
284
285 mem_copyuntilzero( ( pubyte )&_lexlist[ i ], ptrlex );
286 if ( i && !_lexlist[ i ] )
287 _lexlist[ i ] = _lexlist[ i - 1 ];
288 // print("%s ", &_lexlist[ i ] );
289 ptrlex += mem_len( ptrlex ) + 1;
290 }
291
292 id = KERNEL_COUNT - 1;
293 i = 0;
294
295 while ( operpars[ i ].oper )
296 {
297 if ( operpars[i].oper == 0xFF )
298 {
299 parl = oppars[ operpars[ i ].bc ].left;
300 parr = oppars[ operpars[ i ].bc ].right;
301 ret = oppars[ operpars[ i ].bc ].ret;
302 }
303 else
304 {
305 ptrlex = ( pubyte )&_lexlist[ operpars[ i ].oper ];
306 ptr = ( puint )&out;
307 *(( pubyte )ptr)++ = 4;// OVM_EXFUNC
308 *ptr++ = GHCOM_NAME;
309 *ptr++ = ( parr ? 29 : 24 ) + mem_len( ptrlex );
310 // sprintf( ( pubyte )ptr, "#%02i", operpars[ i ].oper );
311 sprintf( ( pubyte )ptr, "#%s", ptrlex );
312 ptr = ( puint )(( pubyte )ptr + mem_len( ptr ) + 1 );
313 *ptr++ = ret;
314 *(( pubyte )ptr)++ = 0;
315
316 *ptr++ = parr ? 2 : 1;
317 *ptr++ = parl;
318 *(( pubyte )ptr)++ = 0;
319 if ( parr )
320 {
321 *ptr++ = parr;
322 *(( pubyte )ptr)++ = 0;
323 }
324 ptr = ( puint )&out;
325 #if defined ( __GNUC__) || defined (__TINYC__)
326 ptrpar = ( pubyte )ptr; // santy
327 pfunc = ( pvmfunc )load_exfunc( &ptrpar, id-- );
328 #else
329 pfunc = ( pvmfunc )load_exfunc( &( pubyte )ptr, id-- );
330 #endif
331 // Link to stack command
332 pfunc->vmo.id = operpars[ i ].bc;
333 }
334 i++;
335 }
336 for ( idto = TInt; idto <= TUlong; idto++ )
337 {
338 for ( idfrom = TInt; idfrom <= TUlong; idfrom++ )
339 {
340 ptrlex = (( pvmobj )PCMD( idto ))->name;
341
342 ptr = ( puint )&out;
343
344 *(( pubyte )ptr)++ = 4;// OVM_EXFUNC
345 *ptr++ = GHCOM_NAME;
346 *ptr++ = 24 + mem_len( ptrlex );
347 sprintf( ( pubyte )ptr, "@%s", ptrlex );
348 ptr = ( puint )(( pubyte )ptr + mem_len( ptr ) + 1 );
349 *ptr++ = idto;
350 *(( pubyte )ptr)++ = 0;
351
352 *ptr++ = 1;
353 *ptr++ = idfrom;
354 *(( pubyte )ptr)++ = 0;
355 ptr = ( puint )&out;
356 #if defined ( __GNUC__) || defined (__TINYC__)
357 ptrpar = ( pubyte )ptr; // santy
358 pfunc = ( pvmfunc )load_exfunc( &ptrpar, id-- );
359 #else
360 pfunc = ( pvmfunc )load_exfunc( &( pubyte )ptr, id-- );
361 #endif
362 // Link to stack command
363 pfunc->vmo.id = typesto[ idto < TFloat ? 0 : idto - TFloat + 1 ][ idfrom - TInt ];
364 }
365 }
366
367 }
368
369
370