Debian Codegraph @

terps/git/opcodes.c

raw symbols SCIP
1 // $Id: opcodes.c,v 1.20 2004/12/22 12:40:07 iain Exp $
2
3 #include "git.h"
4 #include "opcodes.h"
5
6 static void parseModeNibbles (git_uint32* pc, int numModes, int * modeBuffer)
7 {
8 int * mode = modeBuffer;
9
10 while (numModes > 0)
11 {
12 // Load byte.
13 git_uint32 byte = memRead8((*pc)++);
14
15 // Load low nibble.
16 *mode++ = byte & 0x0f;
17 --numModes;
18
19 // Check whether we need to load any more.
20 if (numModes == 0)
21 break;
22
23 // Load high nibble.
24 *mode++ = (byte >> 4) & 0x0f;
25 --numModes;
26 }
27 }
28
29 static void parseLS (git_uint32* pc, Label op)
30 {
31 int modes [2];
32 parseModeNibbles (pc, 2, modes);
33
34 parseLoad (pc, reg_L1, modes [0], size32, NULL);
35 emitCode (op);
36 parseStore (pc, reg_S1, modes [1], size32);
37 }
38 static void parseLLS (git_uint32* pc, Label op)
39 {
40 int modes [3];
41 parseModeNibbles (pc, 3, modes);
42
43 parseLoad (pc, reg_L1, modes [0], size32, NULL);
44 parseLoad (pc, reg_L2, modes [1], size32, NULL);
45 emitCode (op);
46 parseStore (pc, reg_S1, modes [2], size32);
47 }
48 static void parseLSS (git_uint32* pc, Label op)
49 {
50 int modes [3];
51 parseModeNibbles (pc, 3, modes);
52
53 parseLoad (pc, reg_L1, modes [0], size32, NULL);
54 emitCode (op);
55 parseStore (pc, reg_S1, modes [1], size32);
56 parseStore (pc, reg_S2, modes [2], size32);
57 }
58 static void parseLLSS (git_uint32* pc, Label op)
59 {
60 int modes [4];
61 parseModeNibbles (pc, 4, modes);
62
63 parseLoad (pc, reg_L1, modes [0], size32, NULL);
64 parseLoad (pc, reg_L2, modes [1], size32, NULL);
65 emitCode (op);
66 parseStore (pc, reg_S1, modes [2], size32);
67 parseStore (pc, reg_S2, modes [3], size32);
68 }
69 static void parseLLLLSS (git_uint32* pc, Label op)
70 {
71 int modes [6];
72 parseModeNibbles (pc, 6, modes);
73
74 parseLoad (pc, reg_L1, modes [0], size32, NULL);
75 parseLoad (pc, reg_L2, modes [1], size32, NULL);
76 parseLoad (pc, reg_L3, modes [2], size32, NULL);
77 parseLoad (pc, reg_L4, modes [3], size32, NULL);
78 emitCode (op);
79 parseStore (pc, reg_S1, modes [4], size32);
80 parseStore (pc, reg_S2, modes [5], size32);
81 }
82 static void parseL (git_uint32* pc, Label op)
83 {
84 int modes [1];
85 parseModeNibbles (pc, 1, modes);
86
87 parseLoad (pc, reg_L1, modes [0], size32, NULL);
88 emitCode (op);
89 }
90 static void parseLL (git_uint32* pc, Label op)
91 {
92 int modes [2];
93 parseModeNibbles (pc, 2, modes);
94
95 parseLoad (pc, reg_L1, modes [0], size32, NULL);
96 parseLoad (pc, reg_L2, modes [1], size32, NULL);
97 emitCode (op);
98 }
99 static void parse_finish_branch (git_uint32* pc, Label op, LoadReg reg, int mode)
100 {
101 git_sint32 val;
102 if (parseLoad (pc, reg, mode, size32, &val))
103 {
104 // The branch offset is a constant, so we can
105 // check for the special values 0 and 1 right here.
106
107 if (val == 0)
108 {
109 emitCode (op - label_jeq_var + label_jeq_return0);
110 }
111 else if (val == 1)
112 {
113 emitCode (op - label_jeq_var + label_jeq_return1);
114 }
115 else
116 {
117 // Calculate the destination address and
118 // emit a constant branch opcode.
119 emitConstBranch (op - label_jeq_var + label_jeq_const, *pc + val - 2);
120 }
121 }
122 else
123 {
124 // The branch offset isn't a constant, so just
125 // emit the normal opcode plus the current PC.
126
127 emitCode (op);
128 emitData(*pc);
129 }
130 }
131 static void parseLLLLLLL_branch (git_uint32* pc, Label op)
132 {
133 int modes [7];
134 parseModeNibbles (pc, 7, modes);
135
136 parseLoad (pc, reg_L1, modes [0], size32, NULL);
137 parseLoad (pc, reg_L2, modes [1], size32, NULL);
138 parseLoad (pc, reg_L3, modes [2], size32, NULL);
139 parseLoad (pc, reg_L4, modes [3], size32, NULL);
140 parseLoad (pc, reg_L5, modes [4], size32, NULL);
141 parseLoad (pc, reg_L6, modes [5], size32, NULL);
142 parse_finish_branch (pc, op, reg_L7, modes [6]);
143 }
144 static void parseLLLLL_branch (git_uint32* pc, Label op)
145 {
146 int modes [5];
147 parseModeNibbles (pc, 5, modes);
148
149 parseLoad (pc, reg_L1, modes [0], size32, NULL);
150 parseLoad (pc, reg_L2, modes [1], size32, NULL);
151 parseLoad (pc, reg_L3, modes [2], size32, NULL);
152 parseLoad (pc, reg_L4, modes [3], size32, NULL);
153 parse_finish_branch (pc, op, reg_L5, modes [4]);
154 }
155 static void parseLLLL_branch (git_uint32* pc, Label op)
156 {
157 int modes [4];
158 parseModeNibbles (pc, 4, modes);
159
160 parseLoad (pc, reg_L1, modes [0], size32, NULL);
161 parseLoad (pc, reg_L2, modes [1], size32, NULL);
162 parseLoad (pc, reg_L3, modes [2], size32, NULL);
163 parse_finish_branch (pc, op, reg_L4, modes [3]);
164 }
165 static void parseLLL_branch (git_uint32* pc, Label op)
166 {
167 int modes [3];
168 parseModeNibbles (pc, 3, modes);
169
170 parseLoad (pc, reg_L1, modes [0], size32, NULL);
171 parseLoad (pc, reg_L2, modes [1], size32, NULL);
172 parse_finish_branch (pc, op, reg_L3, modes [2]);
173 }
174 static void parseLL_branch (git_uint32* pc, Label op)
175 {
176 int modes [2];
177 parseModeNibbles (pc, 2, modes);
178
179 parseLoad (pc, reg_L1, modes [0], size32, NULL);
180 parse_finish_branch (pc, op, reg_L2, modes [1]);
181 }
182 static void parseL_branch (git_uint32* pc, Label op)
183 {
184 int modes [1];
185 parseModeNibbles (pc, 1, modes);
186
187 parse_finish_branch (pc, op, reg_L1, modes [0]);
188 }
189 static void parseLLL (git_uint32* pc, Label op)
190 {
191 int modes [3];
192 parseModeNibbles (pc, 3, modes);
193
194 parseLoad (pc, reg_L1, modes [0], size32, NULL);
195 parseLoad (pc, reg_L2, modes [1], size32, NULL);
196 parseLoad (pc, reg_L3, modes [2], size32, NULL);
197 emitCode (op);
198 }
199 static void parseS (git_uint32* pc, Label op)
200 {
201 int modes [1];
202 parseModeNibbles (pc, 1, modes);
203
204 emitCode (op);
205 parseStore (pc, reg_S1, modes [0], size32);
206 }
207 static void parseSS (git_uint32* pc, Label op)
208 {
209 int modes [2];
210 parseModeNibbles (pc, 2, modes);
211
212 emitCode (op);
213 parseStore (pc, reg_S1, modes [0], size32);
214 parseStore (pc, reg_S2, modes [1], size32);
215 }
216 static void parseCatch (git_uint32 * pc)
217 {
218 int modes [2];
219 parseModeNibbles (pc, 2, modes);
220
221 parseCatchStub (pc, modes);
222 }
223 void parseInstruction (git_uint32* pc, int * done)
224 {
225 git_uint32 pcStart = *pc;
226 int modes [8];
227 git_uint32 opcode;
228
229 static int ops = 0;
230 ++ops;
231
232 // Fetch the opcode.
233 opcode = memRead8((*pc)++);
234
235 // Check for multi-byte opcode.
236 if (opcode & 0x80)
237 {
238 if (opcode & 0x40)
239 {
240 // Four-byte opcode.
241 opcode &= 0x3F;
242 opcode = (opcode << 8) | memRead8((*pc)++);
243 opcode = (opcode << 8) | memRead8((*pc)++);
244 opcode = (opcode << 8) | memRead8((*pc)++);
245 }
246 else
247 {
248 // Two-byte opcode.
249 opcode &= 0x7F;
250 opcode = (opcode << 8) | memRead8((*pc)++);
251 }
252 }
253
254 if (gDebug)
255 {
256 emitCode (label_debug_step);
257 emitData (pcStart);
258 emitData (opcode);
259 }
260
261 // printf (" opcode=0x%x", opcode);
262
263 // Now we have an opcode number,
264 // parse the operands and emit code.
265
266 switch (opcode)
267 {
268 case op_nop: emitCode (label_nop); break;
269
270 // Arithmetic and logic
271
272 case op_add: parseLLS (pc, label_add_discard); break;
273 case op_sub: parseLLS (pc, label_sub_discard); break;
274 case op_mul: parseLLS (pc, label_mul_discard); break;
275 case op_div: parseLLS (pc, label_div_discard); break;
276 case op_mod: parseLLS (pc, label_mod_discard); break;
277
278 case op_bitand: parseLLS (pc, label_bitand_discard); break;
279 case op_bitor: parseLLS (pc, label_bitor_discard); break;
280 case op_bitxor: parseLLS (pc, label_bitxor_discard); break;
281
282 case op_neg: parseLS (pc, label_neg_discard); break;
283 case op_bitnot: parseLS (pc, label_bitnot_discard); break;
284
285 case op_shiftl: parseLLS (pc, label_shiftl_discard); break;
286 case op_ushiftr: parseLLS (pc, label_ushiftr_discard); break;
287 case op_sshiftr: parseLLS (pc, label_sshiftr_discard); break;
288
289 // Branches
290
291 case op_jump: parseL_branch (pc, label_jump_var); *done = 1; break;
292 case op_jz: parseLL_branch (pc, label_jz_var); break;
293 case op_jnz: parseLL_branch (pc, label_jnz_var); break;
294 case op_jeq: parseLLL_branch (pc, label_jeq_var); break;
295 case op_jne: parseLLL_branch (pc, label_jne_var); break;
296 case op_jlt: parseLLL_branch (pc, label_jlt_var); break;
297 case op_jgt: parseLLL_branch (pc, label_jgt_var); break;
298 case op_jle: parseLLL_branch (pc, label_jle_var); break;
299 case op_jge: parseLLL_branch (pc, label_jge_var); break;
300 case op_jltu: parseLLL_branch (pc, label_jltu_var); break;
301 case op_jgtu: parseLLL_branch (pc, label_jgtu_var); break;
302 case op_jleu: parseLLL_branch (pc, label_jleu_var); break;
303 case op_jgeu: parseLLL_branch (pc, label_jgeu_var); break;
304
305 case op_jumpabs: parseL (pc, label_jumpabs); *done = 1; break;
306
307 // Moving data
308
309 case op_copy:
310 parseModeNibbles (pc, 2, modes);
311 parseLoad (pc, reg_L1, modes [0], size32, NULL);
312 parseStore (pc, reg_S1, modes [1], size32);
313 break;
314
315 case op_copys:
316 parseModeNibbles (pc, 2, modes);
317 parseLoad (pc, reg_L1, modes [0], size16, NULL);
318 emitCode (label_copys_discard);
319 parseStore (pc, reg_S1, modes [1], size16);
320 break;
321
322 case op_copyb:
323 parseModeNibbles (pc, 2, modes);
324 parseLoad (pc, reg_L1, modes [0], size8, NULL);
325 emitCode (label_copyb_discard);
326 parseStore (pc, reg_S1, modes [1], size8);
327 break;
328
329 case op_sexs: parseLS (pc, label_sexs_discard); break;
330 case op_sexb: parseLS (pc, label_sexb_discard); break;
331
332 // Array data
333
334 case op_aload: parseLLS (pc, label_aload_discard); break;
335 case op_aloads: parseLLS (pc, label_aloads_discard); break;
336 case op_aloadb: parseLLS (pc, label_aloadb_discard); break;
337 case op_aloadbit: parseLLS (pc, label_aloadbit_discard); break;
338
339 case op_astore: parseLLL (pc, label_astore); break;
340 case op_astores: parseLLL (pc, label_astores); break;
341 case op_astoreb: parseLLL (pc, label_astoreb); break;
342 case op_astorebit: parseLLL (pc, label_astorebit); break;
343
344 // The stack
345
346 case op_stkcount: parseS (pc, label_stkcount); break;
347 case op_stkpeek: parseLS (pc, label_stkpeek); break;
348 case op_stkswap: emitCode (label_stkswap); break;
349 case op_stkcopy: parseL (pc, label_stkcopy); break;
350 case op_stkroll: parseLL (pc, label_stkroll); break;
351
352 // Functions
353
354 case op_call:
355 parseModeNibbles (pc, 3, modes);
356 parseLoad (pc, reg_L1, modes [0], size32, NULL);
357 parseLoad (pc, reg_L2, modes [1], size32, NULL);
358 emitCode (label_args_stack);
359 parseCallStub (pc, modes [2]);
360 break;
361
362 case op_callf:
363 parseModeNibbles (pc, 2, modes);
364 parseLoad (pc, reg_L1, modes [0], size32, NULL);
365 emitCode (label_args_0);
366 parseCallStub (pc, modes [1]);
367 break;
368
369 case op_callfi:
370 parseModeNibbles (pc, 3, modes);
371 parseLoad (pc, reg_L1, modes [0], size32, NULL);
372 parseLoad (pc, reg_L2, modes [1], size32, NULL);
373 emitCode (label_args_1);
374 parseCallStub (pc, modes [2]);
375 break;
376
377 case op_callfii:
378 parseModeNibbles (pc, 4, modes);
379 parseLoad (pc, reg_L1, modes [0], size32, NULL);
380 parseLoad (pc, reg_L2, modes [1], size32, NULL);
381 parseLoad (pc, reg_L3, modes [2], size32, NULL);
382 emitCode (label_args_2);
383 parseCallStub (pc, modes [3]);
384 break;
385
386 case op_callfiii:
387 parseModeNibbles (pc, 5, modes);
388 parseLoad (pc, reg_L1, modes [0], size32, NULL);
389 parseLoad (pc, reg_L2, modes [1], size32, NULL);
390 parseLoad (pc, reg_L3, modes [2], size32, NULL);
391 parseLoad (pc, reg_L4, modes [3], size32, NULL);
392 emitCode (label_args_3);
393 parseCallStub (pc, modes [4]);
394 break;
395
396 case op_return:
397 parseL (pc, label_return);
398 *done = 1;
399 break;
400
401 case op_tailcall:
402 parseModeNibbles (pc, 2, modes);
403 parseLoad (pc, reg_L1, modes [0], size32, NULL);
404 parseLoad (pc, reg_L2, modes [1], size32, NULL);
405 emitCode (label_args_stack);
406 emitCode (label_tailcall);
407 *done = 1;
408 break;
409
410 // Continuations
411
412 case op_catch: parseCatch (pc); break;
413 case op_throw:
414 parseLL (pc, label_throw);
415 *done = 1;
416 break;
417
418 case op_random: parseLS (pc, label_random); break;
419 case op_setrandom: parseL (pc, label_setrandom); break;
420
421 case op_getmemsize: parseS (pc, label_getmemsize); break;
422 case op_setmemsize: parseLS (pc, label_setmemsize); break;
423
424 case op_quit:
425 emitCode (label_quit);
426 *done = 1;
427 break;
428
429 case op_restart:
430 emitCode (label_restart);
431 *done = 1;
432 break;
433
434 case op_restore: parseLS (pc, label_restore); break;
435 case op_restoreundo: parseS (pc, label_restoreundo); break;
436 case op_protect: parseLL (pc, label_protect); break;
437 case op_verify: parseS (pc, label_verify); break;
438 case op_hasundo: parseS (pc, label_hasundo); break;
439 case op_discardundo: emitCode (label_discardundo); break;
440
441 case op_save:
442 parseModeNibbles (pc, 2, modes);
443 parseLoad (pc, reg_L1, modes [0], size32, NULL);
444 parseSaveStub (pc, modes [1]);
445 break;
446
447 case op_saveundo:
448 parseModeNibbles (pc, 1, modes);
449 parseUndoStub (pc, modes [0]);
450 break;
451
452 case op_getiosys: parseSS (pc, label_getiosys); break;
453 case op_setiosys: parseLL (pc, label_setiosys); break;
454
455 case op_getstringtbl: parseS (pc, label_getstringtbl); break;
456 case op_setstringtbl: parseL (pc, label_setstringtbl); break;
457
458 case op_streamchar: parseL (pc, label_streamchar); emitData(*pc); break;
459 case op_streamnum: parseL (pc, label_streamnum); emitData(*pc); break;
460 case op_streamstr: parseL (pc, label_streamstr); emitData(*pc); break;
461 case op_streamunichar: parseL (pc, label_streamunichar); emitData(*pc); break;
462
463 case op_glk: parseLLS (pc, label_glk); break;
464 case op_gestalt: parseLLS (pc, label_gestalt); break;
465
466 case op_binarysearch:
467 case op_linearsearch:
468 parseModeNibbles (pc, 8, modes);
469 parseLoad (pc, reg_L1, modes [0], size32, NULL);
470 parseLoad (pc, reg_L2, modes [1], size32, NULL);
471 parseLoad (pc, reg_L3, modes [2], size32, NULL);
472 parseLoad (pc, reg_L4, modes [3], size32, NULL);
473 parseLoad (pc, reg_L5, modes [4], size32, NULL);
474 parseLoad (pc, reg_L6, modes [5], size32, NULL);
475 parseLoad (pc, reg_L7, modes [6], size32, NULL);
476 emitCode (opcode == op_linearsearch ? label_linearsearch : label_binarysearch);
477 parseStore (pc, reg_S1, modes [7], size32);
478 break;
479
480 case op_linkedsearch:
481 parseModeNibbles (pc, 7, modes);
482 parseLoad (pc, reg_L1, modes [0], size32, NULL);
483 parseLoad (pc, reg_L2, modes [1], size32, NULL);
484 parseLoad (pc, reg_L3, modes [2], size32, NULL);
485 parseLoad (pc, reg_L4, modes [3], size32, NULL);
486 parseLoad (pc, reg_L5, modes [4], size32, NULL);
487 parseLoad (pc, reg_L6, modes [5], size32, NULL);
488 emitCode (label_linkedsearch);
489 parseStore (pc, reg_S1, modes [6], size32);
490 break;
491
492 case op_debugtrap:
493 parseL (pc, label_debugtrap);
494 break;
495
496 // Memory management
497
498 case op_mzero: parseLL (pc, label_mzero); break;
499 case op_mcopy: parseLLL (pc, label_mcopy); break;
500
501 case op_malloc: parseLS (pc, label_malloc); break;
502 case op_mfree: parseL (pc, label_mfree); break;
503
504 // Function acceleration
505
506 case op_accelfunc: parseLL (pc, label_accelfunc); break;
507 case op_accelparam: parseLL (pc, label_accelparam); break;
508
509 // Floating point
510
511 case op_numtof: parseLS (pc, label_numtof); break;
512 case op_ftonumz: parseLS (pc, label_ftonumz); break;
513 case op_ftonumn: parseLS (pc, label_ftonumn); break;
514 case op_ceil: parseLS (pc, label_ceil); break;
515 case op_floor: parseLS (pc, label_floor); break;
516 case op_sqrt: parseLS (pc, label_sqrt); break;
517 case op_exp: parseLS (pc, label_exp); break;
518 case op_log: parseLS (pc, label_log); break;
519
520 case op_fadd: parseLLS (pc, label_fadd_discard); break;
521 case op_fsub: parseLLS (pc, label_fsub_discard); break;
522 case op_fmul: parseLLS (pc, label_fmul_discard); break;
523 case op_fdiv: parseLLS (pc, label_fdiv_discard); break;
524 case op_pow: parseLLS (pc, label_pow); break;
525 case op_atan2: parseLLS (pc, label_atan2); break;
526
527 case op_fmod: parseLLSS (pc, label_fmod); break;
528
529 case op_sin: parseLS (pc, label_sin); break;
530 case op_cos: parseLS (pc, label_cos); break;
531 case op_tan: parseLS (pc, label_tan); break;
532 case op_asin: parseLS (pc, label_asin); break;
533 case op_acos: parseLS (pc, label_acos); break;
534 case op_atan: parseLS (pc, label_atan); break;
535
536 case op_jfeq: parseLLLL_branch (pc, label_jfeq_var); break;
537 case op_jfne: parseLLLL_branch (pc, label_jfne_var); break;
538
539 case op_jflt: parseLLL_branch (pc, label_jflt_var); break;
540 case op_jfle: parseLLL_branch (pc, label_jfle_var); break;
541 case op_jfgt: parseLLL_branch (pc, label_jfgt_var); break;
542 case op_jfge: parseLLL_branch (pc, label_jfge_var); break;
543
544 case op_jisnan: parseLL_branch (pc, label_jisnan_var); break;
545 case op_jisinf: parseLL_branch (pc, label_jisinf_var); break;
546
547 // Double-precision
548
549 case op_numtod: parseLSS (pc, label_numtod); break;
550 case op_dtonumz: parseLLS (pc, label_dtonumz); break;
551 case op_dtonumn: parseLLS (pc, label_dtonumn); break;
552 case op_ftod: parseLSS (pc, label_ftod); break;
553 case op_dtof: parseLLS (pc, label_dtof); break;
554
555 case op_dceil: parseLLSS (pc, label_dceil); break;
556 case op_dfloor: parseLLSS (pc, label_dfloor); break;
557 case op_dsqrt: parseLLSS (pc, label_dsqrt); break;
558 case op_dexp: parseLLSS (pc, label_dexp); break;
559 case op_dlog: parseLLSS (pc, label_dlog); break;
560
561 case op_dadd: parseLLLLSS (pc, label_dadd); break;
562 case op_dsub: parseLLLLSS (pc, label_dsub); break;
563 case op_dmul: parseLLLLSS (pc, label_dmul); break;
564 case op_ddiv: parseLLLLSS (pc, label_ddiv); break;
565 case op_dpow: parseLLLLSS (pc, label_dpow); break;
566 case op_datan2: parseLLLLSS (pc, label_datan2); break;
567
568 case op_dmodq: parseLLLLSS (pc, label_dmodq); break;
569 case op_dmodr: parseLLLLSS (pc, label_dmodr); break;
570
571 case op_dsin: parseLLSS (pc, label_dsin); break;
572 case op_dcos: parseLLSS (pc, label_dcos); break;
573 case op_dtan: parseLLSS (pc, label_dtan); break;
574 case op_dasin: parseLLSS (pc, label_dasin); break;
575 case op_dacos: parseLLSS (pc, label_dacos); break;
576 case op_datan: parseLLSS (pc, label_datan); break;
577
578 case op_jdeq: parseLLLLLLL_branch (pc, label_jdeq_var); break;
579 case op_jdne: parseLLLLLLL_branch (pc, label_jdne_var); break;
580 case op_jdlt: parseLLLLL_branch (pc, label_jdlt_var); break;
581 case op_jdle: parseLLLLL_branch (pc, label_jdle_var); break;
582 case op_jdgt: parseLLLLL_branch (pc, label_jdgt_var); break;
583 case op_jdge: parseLLLLL_branch (pc, label_jdge_var); break;
584
585 case op_jdisnan: parseLLL_branch (pc, label_jdisnan_var); break;
586 case op_jdisinf: parseLLL_branch (pc, label_jdisinf_var); break;
587
588 // Special Git opcodes
589
590 case op_git_setcacheram: parseL (pc, label_git_setcacheram); break;
591 case op_git_prunecache: parseLL (pc, label_git_prunecache); break;
592
593 default:
594 // Unknown opcode.
595 abortCompilation();
596 break;
597 }
598 }