forked from cornerStone-Dev/simpleOs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fith.re
1555 lines (1424 loc) · 32 KB
/
fith.re
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
// fith.re
#include "../localTypes.h"
#include "../inc/fith_i.h"
// High level vision is of repl/shell interface is always live with
// compiled code active and alive at runtime.
// TODO taking address of words/globals
// implement call word to call words via address
// implement globals
// implement large constants (like strings, needed for globals)
// all other combined operators (eg =*)
// optimize word call's when arguments are locals (maybe globals too?)
// implement word types inside of compiler, like globals vs constants vs words
// use word types to implement inlineable words.
// implement correct behavior for recursion
// implement goto behavior?
// eval strings with compiler at runtime?
// generic cfunction interface?
//
// clean up code
// Section Types
typedef long long (*fithFunc)(u32 tosValue, u32 *expressionStackPointer);
typedef struct {
u32 savedIndex;
u32 blockType;
} Block;
typedef struct Work {
struct Work *next;
u32 workType;
u32 codeIndex;
u32 target;
} Work;
typedef struct String {
struct String *next;
u8 *string;
u32 length;
u32 codeIndex;
} String;
typedef struct FithState {
u8 bi;
u8 li;
u8 insideParams;
u8 notLeaf;
u32 tosValue;
u32 *expressionStackPointer;
u32 *expressionStack;
u16 *codeBuff;
u32 codeIndex;
u32 codeBuffSize;
avlNode *words;
avlNode *globals;
avlNode *locals;
Work *workList;
String *stringLits;
Block blocks[8];
} FithState;
static FithState f;
enum {
BLOCK_NONE,
BLOCK_WORD,
BLOCK_COND,
BLOCK_ELSE,
BLOCK_WHILE,
};
enum {
WORK_NONE,
WORK_WORDCALL,
};
#define ARM_NOP 0xBF00
#define COND_BEQ 0x0
#define COND_BNE 0x1
#define COND_BGE 0xA
#define COND_BLT 0xB
#define COND_BGT 0xC
#define COND_BLE 0xD
/*!re2c // start of re2c block
scm = "\\" [^\x00\n]*;
wsp = [ \t\r\n]; // removed \v
// integer literals
dec = [0-9]+;
hex = "0x" [0-9A-F]+; // a-f removed
integer = "-"? (dec | hex);
string_lit = ["] ([^"\x00] | ([\\] ["]))* ["];
//string_lit_chain = string_lit ([ \n\t\r]* string_lit)+;
//string_lit_chain = ([^"\n] | ([\\] ["]))* ("\n" | ["]);
string_lit_chain = ([^'\n] | ([\\] [']))* "\n";
string_lit_end = ([^'\n] | ([\\] [']))* ['];
mangled_string_lit = ['] ([^'\x00\x03] | ([\\] [']))* "\x00";
char_lit = [`] ([^`\x03] | ([\\] [`]))* [`];
wordOperators = [@#$^~;:!?];
ident = [a-zA-Z_] [a-zA-Z_0-9?!-]*;
word = [a-zA-Z_] [a-zA-Z0-9?!_]*;
word_definition = word "{";
word_def_params = word "(";
var_declaration = word ";";
var_assign = word "=";
word_address = word "@";
const_declaration = word ":=";
word_increment = word "++";
function_call_addr = "@" word ;
function_definition = word ":";
*/ // end of re2c block
enum {
WORD_OTHER,
WORD_EXIT,
WORD_NEG,
WORD_WHILE,
WORD_AND,
WORD_DUP,
WORD_DROP,
WORD_OVER,
WORD_PICK,
WORD_SWAP,
WORD_ABS,
WORD_ALLOC,
WORD_FREE,
WORD_PS,
};
static s32 builtInWord(u8 *YYCURSOR, s32 length)
{
u8 *YYMARKER;
switch (length)
{
case 1:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
*/ // end of re2c block
case 2:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
"ps" { return WORD_PS; }
*/ // end of re2c block
case 3:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
"neg" { return WORD_NEG; }
"and" { return WORD_AND; }
"dup" { return WORD_DUP; }
"abs" { return WORD_ABS; }
*/ // end of re2c block
case 4:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
"exit" { return WORD_EXIT; }
"drop" { return WORD_DROP; }
"free" { return WORD_FREE; }
"over" { return WORD_OVER; }
"pick" { return WORD_PICK; }
"swap" { return WORD_SWAP; }
*/ // end of re2c block
case 5:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
"while" { return WORD_WHILE; }
"alloc" { return WORD_ALLOC; }
*/ // end of re2c block
case 6:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
*/ // end of re2c block
case 7:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
*/ // end of re2c block
case 8:
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { return WORD_OTHER; }
*/ // end of re2c block
default: return WORD_OTHER;
}
}
/*e*/
void picoFith(u8 *sourceCode)/*p;*/
{
u8 *YYCURSOR = sourceCode;
u8 *YYMARKER;
u8 *start;
loop:
start = YYCURSOR;
/*!re2c // start of re2c block **/
re2c:define:YYCTYPE = "u8";
re2c:yyfill:enable = 0;
* { io_prints("fith: lexical error\n"); return; }
[\x00] {
if (f.codeIndex == 1) { return; }
if (f.bi) { return; }
// assume execute immediately
fithFunc exec = (fithFunc)((u32)completeWord() + 1);
long long result = exec(f.tosValue, f.expressionStackPointer);
f.tosValue = result;
f.expressionStackPointer = (u32*)(u32)(result >> 32);
u32 *base = f.expressionStack;
if (base != f.expressionStackPointer)
{
base-=2;
while (base >= f.expressionStackPointer) {
io_prints("(");
io_printi(*base--);
io_prints(")");
}
io_prints("(");
io_printi(f.tosValue);
io_prints(")\n");
}
free(f.codeBuff);
f.codeBuff = zalloc(32);
f.codeIndex = 0;
f.codeBuffSize = 16;
putMachineCode(ARM_NOP);
return;
}
wsp { goto loop; }
scm { goto loop; }
string_lit {
stackStringLit(start + 1, YYCURSOR - start - 2);
goto loop;
}
"+" { stackAdd(); goto loop; }
"-" { stackSub(); goto loop; }
">>" { stackRS(); goto loop; }
"<<" { stackLS(); goto loop; }
"*" { stackMul(); goto loop; }
"/" { stackDiv(); goto loop; }
"%" { stackMod(); goto loop; }
"&" { stackAnd(); goto loop; }
"|" { stackOr(); goto loop; }
"^" { stackXor(); goto loop; }
"~" { stackNot(); goto loop; }
"&~" { stackBitClear(); goto loop; }
// memory manipulation
"[]" { stackLdr(); goto loop; }
"[]=" { stackStr(); goto loop; }
"$" { stackLdrTo(); goto loop; }
"$=" { stackStrTo(); goto loop; }
"[b]" { stackBitClear(); goto loop; }
"[b]=" { stackBitClear(); goto loop; }
"[h]" { stackBitClear(); goto loop; }
"[h]=" { stackBitClear(); goto loop; }
"={" { stackCond(COND_BNE); goto loop; }
"!{" { stackCond(COND_BEQ); goto loop; }
"<{" { stackCond(COND_BGE); goto loop; }
"<={" { stackCond(COND_BGT); goto loop; }
">{" { stackCond(COND_BLE); goto loop; }
">={" { stackCond(COND_BLT); goto loop; }
"}else{" { stackElse(); goto loop; }
"}" { endBlock(); goto loop; }
"){" { endParams(); goto loop; }
integer { s32 val = s2i(start); pushVal(val); goto loop; }
word {
s32 wordType = builtInWord(start, YYCURSOR - start);
switch (wordType) {
case WORD_EXIT:
lineHandler = shell_inputLine; return;
case WORD_NEG:
negTos(); break;
case WORD_WHILE:
stackWhile(); break;
case WORD_AND:
stackLogicAnd(); break;
case WORD_DUP:
pushTos(); break;
case WORD_DROP:
popTos(); break;
case WORD_OVER:
pushTos(); putMachineCode(armLdrOffset(0, 1, 1)); break;
case WORD_PICK:
pushTos(); putMachineCode(armLdrOffset(0, 1, 2)); break;
case WORD_SWAP:
//~ popScratch(); pushTos(); putMachineCode(armMov(0, 2)); break;
putMachineCode(armMov(2, 0)); popTos(); pushScratch(); break;
case WORD_ABS:
stackAbs(); break;
case WORD_ALLOC:
stackAlloc(); break;
case WORD_FREE:
stackFree(); break;
case WORD_PS:
stackPs(); break;
}
speakWord(start, YYCURSOR - start);
goto loop;
}
word_definition {
s32 wordType = builtInWord(start, YYCURSOR - start - 1);
if (wordType != 0)
{
} else {
createWord(start, YYCURSOR - start - 1);
}
goto loop;
}
word_def_params {
s32 wordType = builtInWord(start, YYCURSOR - start - 1);
if (wordType != 0)
{
} else {
createWord(start, YYCURSOR - start - 1);
f.insideParams = 1;
}
goto loop;
}
var_declaration {
u32 localNum = createVar(start, YYCURSOR - start - 1);
setVar(localNum);
goto loop;
}
var_assign {
assignToVar(start, YYCURSOR - start - 1);
goto loop;
}
word "=+" {
assignAddToVar(start, YYCURSOR - start - 2);
goto loop;
}
word "=-" {
assignSubToVar(start, YYCURSOR - start - 2);
goto loop;
}
*/ // end of re2c block
}
/*e*/
void fith_init(void)/*p;*/
{
f.expressionStack = zalloc(32*4);
f.expressionStack = f.expressionStack + 32;
f.expressionStackPointer = f.expressionStack;
f.codeBuff = zalloc(32);
f.codeBuffSize = 16;
putMachineCode(ARM_NOP);
return;
}
// Section Arm Machine Code
/*e*/static u32
armPush(u32 regBits)/*i;*/
{
u32 code = 0xB400;
code += regBits;
return code;
}
/*e*/static u32
armPop(u32 regBits)/*i;*/
{
u32 code = 0xBC00;
code += regBits;
return code;
}
static u32
setLocalRegsBits(s32 numVars)
{
u32 regBits = 0;
for (s32 x = 0; x < numVars; x++)
{
regBits <<= 1;
regBits++;
}
regBits <<= 8 - numVars;
return regBits;
}
static u32
armPushFuncStart(s32 numVars)
{
u32 regBits = setLocalRegsBits(numVars);
regBits += (1<<8);
return armPush(regBits);
}
static u32
armPopFuncEnd(s32 numVars)
{
u32 regBits = setLocalRegsBits(numVars);
regBits += (1<<8);
return armPop(regBits);
}
static u32
armMovImm(u32 dest, u32 val)
{
u32 code = 0x2000;
code += val + (dest << 8);
return code;
}
//~ static u32
//~ armMov(u32 dest, u32 src)
//~ {
//~ u32 code = 0x4600;
//~ code += ((dest>>3)<<7) + ((dest<<29)>>29) + (src << 3);
//~ return code;
//~ }
/*e*/static u32
armMov(u32 dest, u32 src)/*i;*/
{
u32 code = 0x0000;
code += dest + (src << 3);
return code;
}
/*e*/static u32
armLdrOffset(u32 dest, u32 src, u32 offset)/*i;*/
{
u32 code = 0x6800;
code += dest + (src << 3) + (offset << 6);
return code;
}
static u32
armLdrIndex(u32 dest, u32 src, u32 index)
{
u32 code = 0x5800;
code += dest + (src << 3) + (index << 6);
return code;
}
static u32
armLdm(u32 src, u32 regBits)
{
u32 code = 0xC800;
code += regBits + (src << 8);
return code;
}
static u32
armStrOffset(u32 src, u32 dest, u32 offset)
{
u32 code = 0x6000;
code += src + (dest << 3) + (offset << 6);
return code;
}
static u32
armStrIndex(u32 src, u32 dest, u32 index)
{
u32 code = 0x5000;
code += src + (dest << 3) + (index << 6);
return code;
}
static u32
armAdd3(u32 dest, u32 arg1, u32 arg2)
{
u32 code = 0x1800;
code += dest + (arg1 << 3) + (arg2 << 6);
return code;
}
static u32
armAddImm(u32 dest, u32 val)
{
u32 code = 0x3000;
code += val + (dest << 8);
return code;
}
static u32
armAdr(u32 dest, u32 imm)
{
u32 code = 0xA000;
code += imm + (dest << 8);
return code;
}
static u32
armSubImm(u32 dest, u32 val)
{
u32 code = 0x3800;
code += val + (dest << 8);
return code;
}
static u32
armSub3(u32 dest, u32 arg1, u32 arg2)
{
u32 code = 0x1A00;
code += dest + (arg1 << 3) + (arg2 << 6);
return code;
}
static u32
armLslsImm(u32 dest, u32 src, u32 val)
{
u32 code = 0x0000;
code += dest + (src << 3) + (val << 6);
return code;
}
static u32
armLsls(u32 dest, u32 arg1)
{
u32 code = 0x4080;
code += dest + (arg1 << 3);
return code;
}
static u32
armLsrsImm(u32 dest, u32 src, u32 val)
{
u32 code = 0x0800;
code += dest + (src << 3) + (val << 6);
return code;
}
static u32
armLsrs(u32 dest, u32 arg1)
{
u32 code = 0x40C0;
code += dest + (arg1 << 3);
return code;
}
static u32
armAsrsImm(u32 dest, u32 src, u32 val)
{
u32 code = 0x1000;
code += dest + (src << 3) + (val << 6);
return code;
}
static u32
armMul(u32 dest, u32 arg1)
{
u32 code = 0x4340;
code += dest + (arg1 << 3);
return code;
}
static u32
armAnd(u32 dest, u32 arg1)
{
u32 code = 0x4000;
code += dest + (arg1 << 3);
return code;
}
static u32
armOr(u32 dest, u32 arg1)
{
u32 code = 0x4300;
code += dest + (arg1 << 3);
return code;
}
static u32
armXor(u32 dest, u32 arg1)
{
u32 code = 0x4040;
code += dest + (arg1 << 3);
return code;
}
static u32
armNot(u32 dest, u32 arg1)
{
u32 code = 0x43C0;
code += dest + (arg1 << 3);
return code;
}
static u32
armBic(u32 dest, u32 arg1)
{
u32 code = 0x4380;
code += dest + (arg1 << 3);
return code;
}
static u32
armSbcs(u32 dest, u32 arg1)
{
u32 code = 0x4180;
code += dest + (arg1 << 3);
return code;
}
static u32
armNeg(u32 dest, u32 arg1)
{
u32 code = 0x4240;
code += dest + (arg1 << 3);
return code;
}
static u32
armBX(u32 reg)
{
u32 code = 0x4700;
code += reg << 3;
return code;
}
static u32
armCond(u32 cond, s32 imm)
{
u32 code = 0xD000;
code += ((imm<<24)>>24) + (cond << 8);
return code;
}
static u32
armBranch(u32 imm)
{
u32 code = 0xE000;
code += (imm<<21)>>21;
return code;
}
// Section Logical Instruction
/*e*/static void
pushTos(void)/*i;*/
{
decrementESP();
putMachineCode(armStrOffset(0, 1, 0));
//~ u32 code = 0xC101;
//~ putMachineCode(code);
}
/*e*/static void
pushScratch(void)/*i;*/
{
decrementESP();
putMachineCode(armStrOffset(2, 1, 0));
//~ u32 code = 0xC101;
//~ putMachineCode(code);
}
/*e*/static void
popTos(void)/*i;*/
{
putMachineCode(armLdm(1, (1<<0)));
//~ decrementESP();
//~ putMachineCode(armLdrOffset(0, 1, 0));
}
/*e*/static void
popScratch(void)/*i;*/
{
putMachineCode(armLdm(1, (1<<2)));
//~ decrementESP();
//~ putMachineCode(armLdrOffset(2, 1, 0));
}
//~ /*e*/static void
//~ popLocal(u32 local)/*i;*/
//~ {
//~ decrementESP();
//~ putMachineCode(armLdrOffset(local+3, 1, 0));
//~ }
/*e*/static void
negTos(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{
// we just pushed a local, re-write
f.codeIndex -= 1;
u32 src = prevCode>>3;
putMachineCode(armNeg(0,src));
return;
}
putMachineCode(armNeg(0, 0));
}
/*e*/static void
pushVal(s32 val)/*i;*/
{
if (val <= 255 && val>=0)
{
// small value
pushTos();
putMachineCode(armMovImm(0, val));
} else if (val < 0 && val >= -255) {
pushTos();
putMachineCode(armMovImm(0, -val));
negTos();
}
}
/*e*/static void
putMachineCode(u32 code)/*i;*/
{
if (f.codeIndex >= f.codeBuffSize)
{
f.codeBuffSize *= 2;
f.codeBuff = realloc(f.codeBuff, f.codeBuffSize*2);
}
f.codeBuff[f.codeIndex++] = code;
}
/*e*/static void
decrementESP(void)/*i;*/
{
putMachineCode(armSubImm(1, 4));
}
/*e*/static void
stackAbs(void)/*i;*/
{
putMachineCode(armAsrsImm(2,0,31));
putMachineCode(armAdd3(0,0,2));
putMachineCode(armXor(0,2));
}
/*e*/static void
stackAlloc(void)/*i;*/
{
// save local in r3 and sp in r1
putMachineCode(armPush((1<<1)+(1<<3)));
stackCallWord((u32)zalloc);
// restore local in r3 and sp in r1
putMachineCode(armPop((1<<1)+(1<<3)));
}
/*e*/static void
stackFree(void)/*i;*/
{
// save local in r3 and sp in r1
putMachineCode(armPush((1<<1)+(1<<3)));
stackCallWord((u32)free);
// restore local in r3 and sp in r1
putMachineCode(armPop((1<<1)+(1<<3)));
popTos();
}
/*e*/static void
stackPs(void)/*i;*/
{
// save local in r3 and sp in r1
putMachineCode(armPush((1<<1)+(1<<3)));
stackCallWord((u32)io_printsn);
// restore local in r3 and sp in r1
putMachineCode(armPop((1<<1)+(1<<3)));
popTos();
}
/*e*/static void
stackAdd(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( (prevCode>>11) == 4)
{
// we just pushed a small constant, re-write
f.codeIndex -= 3;
u32 val = (prevCode<<24)>>24;
// TODO can optimize val <= 7 and local to add with load
putMachineCode(armAddImm(0, val));
return;
}
if ( ((prevCode>>6) == 0x00) )
{
// we just pushed a local, re-write
f.codeIndex -= 3;
u32 src = prevCode>>3;
putMachineCode(armAdd3(0,0,src));
return;
}
popScratch();
putMachineCode(armAdd3(0,0,2));
}
/*e*/static void
stackSub(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( (prevCode>>11) == 4)
{
// we just pushed a small constant, re-write
f.codeIndex -= 3;
u32 val = (prevCode<<24)>>24;
// TODO can optimize val <= 7 and local to add with load
putMachineCode(armSubImm(0, val));
return;
}
if ( ((prevCode>>6) == 0x00) )
{
// we just pushed a local, re-write
f.codeIndex -= 3;
u32 src = prevCode>>3;
putMachineCode(armSub3(0,0,src));
return;
}
popScratch();
putMachineCode(armSub3(0,2,0));
}
/*e*/static void
stackLS(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( (prevCode>>11) == 4)
{
// we just pushed a small constant, re-write
f.codeIndex -= 3;
u32 val = (prevCode<<24)>>24;
putMachineCode(armLslsImm(0, 0, val));
return;
}
if ( ((prevCode>>6) == 0x00) )
{
// we just pushed a local, re-write
f.codeIndex -= 3;
u32 src = prevCode>>3;
putMachineCode(armLsls(0,src));
return;
}
putMachineCode(armMov(2, 0));
popTos();
putMachineCode(armLsls(0,2));
}
/*e*/static void
stackRS(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( (prevCode>>11) == 4)
{
// we just pushed a small constant, re-write
f.codeIndex -= 3;
u32 val = (prevCode<<24)>>24;
putMachineCode(armLsrsImm(0, 0, val));
return;
}
if ( ((prevCode>>6) == 0x00) )
{
// we just pushed a local, re-write
f.codeIndex -= 3;
u32 src = prevCode>>3;
putMachineCode(armLsrs(0,src));
return;
}
putMachineCode(armMov(2, 0));
popTos();
putMachineCode(armLsrs(0,2));
}
/*e*/static void
stackStrTo(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{
// we just pushed a local, re-write
f.codeIndex -= 3;
u32 dst = prevCode>>3;
prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{
f.codeIndex -= 3;
u32 data = prevCode>>3;
putMachineCode(armStrOffset(data, dst, 0));
popTos();
return;
}
popScratch();
putMachineCode(armStrOffset(2,dst,0));
popTos();
return;
}
popScratch();
putMachineCode(armStrOffset(2,0,0));
popTos();
}
/*e*/static void
stackStr(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( (prevCode>>11) == 4)
{
// we just pushed a small constant, re-write
f.codeIndex -= 3;
u32 val = (prevCode<<24)>>24;
prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{
f.codeIndex -= 3;
u32 addr = prevCode>>3;
prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{
f.codeIndex -= 3;
u32 data = prevCode>>3;
putMachineCode(armStrOffset(data, addr, val>>2));
return;
}
putMachineCode(armStrOffset(0, addr, val>>2));
popTos();
return;
}
popScratch();
putMachineCode(armStrOffset(2, 0, val>>2));
popTos();
return;
}
if ( ((prevCode>>6) == 0x00) )
{
// we just pushed a local, re-write
f.codeIndex -= 3;
u32 src = prevCode>>3;
prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{
f.codeIndex -= 3;
u32 addr = prevCode>>3;
prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{
f.codeIndex -= 3;
u32 data = prevCode>>3;
putMachineCode(armStrIndex(data, addr, src));
return;
}
putMachineCode(armStrIndex(0, addr, src));
popTos();
return;
}
popScratch();
putMachineCode(armStrIndex(2,0,src));
popTos();
return;
}
popScratch();
putMachineCode(armAdd3(0,0,2));
popScratch();
putMachineCode(armStrOffset(2,0,0));
popTos();
}
/*e*/static void
stackLdrTo(void)/*i;*/
{
u32 prevCode = f.codeBuff[f.codeIndex-1];
if ( ((prevCode>>6) == 0x00) )
{