-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfirmforth.c
1169 lines (992 loc) · 24.4 KB
/
firmforth.c
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
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <assert.h>
#include <dlfcn.h>
#include <libfirm/firm.h>
#include <libfirm/ident.h>
#include "firmforth.h"
#include "gitrev.h"
#include "mangle.h"
/* Format string to generate a shared object file (first parameter)
for dlopen() from assembly file (second parameter). */
#define LINK_COMMAND "gcc -shared -o %s %s"
/* Forth data stack */
union cell data_stack[1<<20];
/* Forth (pseudo) return stack */
union cell return_stack[1<<20];
union cell *rp = return_stack;
ir_node *control_stack[1<<10];
ir_node **cs = control_stack;
/* Firm type for elements of the data stack */
ir_type *type_cell;
/* Firm type for stack pointer */
ir_type *type_cell_ptr;
/* The interpreter splits the input into tokens and searches the
dictionary for a forth word of that name. Depending on the
following mode variable, it either executes the word right-away or
adds the execution of the word to the intermediate language of a
new word being defined. */
int compiling = 0;
/* Check stack for over/underflow */
#define ASSERT_STACK() \
do { \
assert(sp >= data_stack); \
assert(sp < (data_stack + sizeof(data_stack))); \
assert(cs >= control_stack); \
assert(cs < (control_stack + sizeof(control_stack))); \
} while (0)
#define foreach_irp_irg(idx, irg) \
for (bool irg##__b = true; irg##__b; irg##__b = false) \
for (size_t idx = 0, irg##__n = get_irp_n_irgs(); irg##__b && idx != irg##__n; ++idx) \
for (ir_graph *const irg = (irg##__b = false, get_irp_irg(idx)); !irg##__b; irg##__b = true)
#define foreach_compound_member(idx, mem, type) \
for (bool mem##__b = true; mem##__b; mem##__b = false) \
for (size_t idx = 0, mem##__n = get_compound_n_members(type); mem##__b && idx != mem##__n; ++idx) \
for (ir_entity *const mem = (mem##__b = false, get_compound_member(type, idx)); !mem##__b; mem##__b = true)
/* Firm type of methods implementing forth words */
ir_type *word_method_type = 0;
/* not in public Firm API */
void remove_irp_irg(ir_graph *irg);
void add_irp_irg(ir_graph *irg);
ir_graph *create_irg_copy(ir_graph *irg);
void set_entity_irg(ir_entity *ent, ir_graph *irg);
/* The first forth word method */
WORD(hi)
{
puts("firmforth " GITREV);
return sp;
}
/* Forth dictionary entry for "hi" */
struct dict hi_entry = {
.name = "hi",
.code = hi,
};
WORD(bye)
{
(void) sp;
exit(0);
}
struct dict bye_entry = {
.name = "bye",
.code = bye,
.next = &hi_entry
};
/* Definitions of standard forth words have no comments on them. See
ANSI X3.215-1994 for their definition. */
WORD(dot)
{
sp--;
printf("%ld ", sp->i);
return sp;
}
struct dict dot_entry = {
.name = ".",
.code = dot,
.next = &bye_entry,
.ldname = "dot"
};
WORD(add)
{
sp--;
sp[-1].i = sp[-1].i + sp[0].i;
return sp;
}
struct dict add_entry = {
.name = "+",
.code = add,
.next = &dot_entry,
.ldname = "add"
};
/* Floored division */
WORD(divide)
{
sp--;
sp[-1].i = (((sp[-1].i < 0) != (sp[0].i < 0))?
(((sp[-1].i + 1) / sp[0].i) - 1):
((sp[-1].i) / (sp[0].i)));
return sp;
}
struct dict divide_entry = {
.name = "/",
.code = divide,
.next = &add_entry,
.ldname = "divide"
};
WORD(greater_than)
{
sp -= 1;
sp[-1].i = sp[-1].i > sp[0].i ? 1 : 0;
return sp;
}
struct dict greater_than_entry =
{
.name = ">",
.code = greater_than,
.next = ÷_entry,
.ldname = "greater_than"
};
/* Fetch a whitespace-delimited string from stdin, skipping # and
\-comments */
__attribute__((noinline))
const char *next() {
static char *line;
static size_t n;
const char *token = 0;
do {
if (!line) {
retry:
if (0 > getline(&line, &n, stdin))
exit(0);
if (*line == '#') {
free(line);
goto retry;
}
token = strtok(line, "\n\t\v ");
} else {
token = strtok(0, "\n\t\v ");
}
if (!token || *token == '\\') {
free(line);
token = line = 0;
}
} while (!token);
return token;
}
/* Start compilation of a new word */
WORD(colon)
{
compiling = 1; /* switch interpreter mode */
/* create dictionary entry */
struct dict *entry = malloc(sizeof(struct dict));
entry->name = strdup(next());
entry->code = 0;
entry->immediate = 0;
entry->next = dictionary;
dictionary = entry;
/* Create valid linker symbol */
ident *id;
{
char *mangled = mangle("word_", entry->name, "");
id = id_unique(mangled);
free(mangled);
}
/* Create Firm entity for word */
ir_type *global_type = get_glob_type();
entry->entity = new_entity(global_type, id, word_method_type);
set_entity_ld_ident(entry->entity, id);
/* Create IR graph */
/* We use one local variable for the stack pointer */
ir_graph *irg = new_ir_graph(entry->entity, 1);
set_current_ir_graph(irg);
/* Put stack pointer argument into local variable 0 */
ir_node *proj_arg_t = new_Proj(get_irg_start(irg), mode_T, pn_Start_T_args);
set_value(0, new_Proj(proj_arg_t, mode_P, 0));
entry->ldname = get_id_str(id);
return sp;
}
struct dict colon_entry =
{
.name = ":",
.code = colon,
.next = &greater_than_entry,
.ldname = "colon"
};
/* Create Return node. Uses local variable 0 and get_store() to
retrieve the sp/memory to be returned */
void create_return(void)
{
ir_node *mem = get_store();
ir_node *ir_sp = get_value(0, mode_P);
ir_node *return_node = new_Return(mem, 1, &ir_sp);
ir_node *end_block = get_irg_end_block(current_ir_graph);
add_immBlock_pred(end_block, return_node);
mature_immBlock(get_cur_block());
set_cur_block(NULL);
}
void after_inline_opt(ir_graph *irg)
{
scalar_replacement_opt(irg);
optimize_graph_df(irg);
optimize_cf(irg);
optimize_load_store(irg);
combo(irg);
}
void codegen(struct dict *entry) {
ir_entity *method = entry->entity;
ir_graph *irg = get_entity_irg(method);
/* Record the pristine IRG for future inlining. */
ir_graph *pristine = create_irg_copy(irg);
/* Generate assembly */
char filename_s[64];
snprintf(filename_s, sizeof(filename_s), "jit-%s.s", entry->ldname);
FILE *out = fopen(filename_s, "w");
if(out == NULL) {
perror("couldn't open assembly file for writing");
exit(-1);
}
be_main(out, "cup");
fclose(out);
/* Assemble shared object */
char filename_so[64];
snprintf(filename_so, sizeof(filename_so), "./jit-%s.so", entry->ldname);
{
char command[128];
snprintf(command, sizeof(command), LINK_COMMAND,
filename_so, filename_s);
int rc = system(command);
if (!WIFEXITED(rc) || WEXITSTATUS(rc)) {
fprintf(stderr, "assembler/linker command failed: %s\n", command);
exit(-1);
}
}
/* dlopen() the shared object */
void *dlhandle = dlopen(filename_so, RTLD_NOW|RTLD_GLOBAL);
const char *err = dlerror();
if(err)
puts(err), exit(-1);
entry->code = dlsym(dlhandle, entry->ldname);
err = dlerror();
if(err)
puts(err), exit(-1);
/* restore pristine IRG for future inlining */
remove_irp_irg(irg);
set_irg_entity(pristine, entry->entity);
set_entity_irg(entry->entity, pristine);
add_irp_irg(pristine);
/* Avoid repeated code generation for the entity */
foreach_irp_irg(i, old_irg) {
ir_entity *ent = get_irg_entity(old_irg);
set_entity_linkage(ent, IR_LINKAGE_NO_CODEGEN);
}
/* TODO: This includes static data, need to find a better way.
Repeatedly emit global entites for every word for now. */
/* foreach_compound_member(i, mem, get_glob_type()) { */
/* set_entity_linkage(mem, IR_LINKAGE_NO_CODEGEN); */
/* set_entity_visibility(mem, ir_visibility_external); */
/* } */
}
/* End compilation of a word */
WORD(semicolon)
{
ASSERT_STACK();
compiling = 0;
ir_graph *irg = get_current_ir_graph();
create_return();
irg_finalize_cons(irg);
irg_assert_verify(irg);
dump_ir_graph(irg, "constructed");
/* perform a bunch of optimisations */
do_loop_inversion(irg);
optimize_reassociation(irg);
optimize_load_store(irg);
optimize_cf(irg);
dump_ir_graph(irg, "pre-inline");
inline_functions(250 /* maxsize */,
0 /* threshold */,
after_inline_opt);
dump_ir_graph(irg, "inline");
optimize_graph_df(irg);
combo(irg);
scalar_replacement_opt(irg);
place_code(irg);
optimize_reassociation(irg);
optimize_graph_df(irg);
opt_jumpthreading(irg);
optimize_graph_df(irg);
construct_confirms(irg);
optimize_graph_df(irg);
remove_confirms(irg);
optimize_cf(irg);
optimize_load_store(irg);
optimize_graph_df(irg);
combo(irg);
place_code(irg);
optimize_cf(irg);
lower_highlevel_graph(irg);
dump_ir_graph(irg, "optimized");
codegen(dictionary);
/* Make sure there's no control flow still unwired. */
assert (cs == control_stack);
return sp;
}
struct dict semicolon_entry =
{
.name = ";",
.code = semicolon,
.next = &colon_entry,
.immediate = 1,
.ldname = "semicolon"
};
WORD(key)
{
sp->i = getchar();
sp++;
return sp;
}
struct dict key_entry =
{
.name = "key",
.code = key,
.next = &semicolon_entry,
};
WORD(emit)
{
sp--;
putchar(sp->i);
return sp;
}
struct dict emit_entry =
{
.name = "emit",
.code = emit,
.next = &key_entry,
};
WORD(sp_load)
{
sp->a = sp;
sp++;
return sp;
}
struct dict sp_load_entry =
{
.name = "sp@",
.code = sp_load,
.next = &emit_entry,
.ldname = "sp_load"
};
WORD(sp_store)
{
sp = sp[-1].a;
return --sp;
}
struct dict sp_store_entry =
{
.name = "sp!",
.code = sp_store,
.next = &sp_load_entry,
.ldname = "sp_store"
};
WORD(pick)
{
sp[-1] = sp[-sp[-1].i - 2];
return sp;
}
struct dict sp_pick_entry =
{
.name = "pick",
.code = pick,
.next = &sp_store_entry,
};
WORD(cells)
{
sp[-1].i = sizeof(union cell) * sp[-1].i;
return sp;
}
struct dict cells_entry =
{
.name = "cells",
.code = cells,
.next = &sp_pick_entry,
};
WORD(swap)
{
cell tmp = sp[-1];
sp[-1] = sp[-2];
sp[-2] = tmp;
return sp;
}
struct dict swap_entry =
{
.name = "swap",
.code = swap,
.next = &cells_entry,
};
WORD(drop)
{
return --sp;
}
struct dict drop_entry =
{
.name = "drop",
.code = drop,
.next = &swap_entry,
};
WORD(equal)
{
sp[-2].i = sp[-2].i == sp[-1].i;
return --sp;
}
struct dict equal_entry =
{
.name = "=",
.code = equal,
.next = &drop_entry,
.ldname = "equal",
};
WORD(negate)
{
sp[-1].i = -sp[-1].i;
return sp;
}
struct dict negate_entry =
{
.name = "negate",
.code = negate,
.next = &equal_entry,
};
WORD(load)
{
sp[-1].a = *(sp[-1].aa);
return sp;
}
struct dict load_entry =
{
.name = "@",
.code = load,
.next = &negate_entry,
.ldname = "load"
};
/* ( x a-addr -- ) */
WORD(store)
{
*(sp[-1].aa) = sp[-2].a;
sp -= 2;
return sp;
}
struct dict store_entry =
{
.name = "!",
.code = store,
.next = &load_entry,
.ldname = "store"
};
/* Construct IR to fetch a value from the stack and fork control flow.
Two new basic blocks are created. The current basic block is
matured. The basic block projected from the true condition is made
the current one. The basic block for the false condition is pushed
on the stack. */
WORD(w_if) /* C: -- bb_false */
{
/* IR to load value at top of stack sp[-1] */
ir_node *offset = new_Const_long(mode_Ls, -sizeof(union cell));
ir_node *ir_sp = get_value(0, mode_P);
ir_sp = new_Add(ir_sp, offset);
set_value(0, ir_sp);
ir_node *load_data = new_Load(get_store(), ir_sp, mode_Lu, type_cell, 0);
ir_node *load_data_res = new_Proj(load_data, mode_Lu, pn_Load_res);
ir_node *load_data_mem = new_Proj(load_data, mode_M, pn_Load_M);
set_store(load_data_mem);
/* IR to compare with zero */
ir_node *cmp = new_Cmp(load_data_res,
new_Const_long(mode_Lu, 0),
ir_relation_less_greater);
ir_node *cond = new_Cond(cmp);
/* Create block for condition true */
ir_node *proj_true = new_Proj(cond, mode_X, pn_Cond_true);
ir_node *block_true = new_immBlock();
add_immBlock_pred(block_true, proj_true);
/* Same for false */
ir_node *proj_false = new_Proj(cond, mode_X, pn_Cond_false);
ir_node *block_false = new_immBlock();
add_immBlock_pred(block_false, proj_false);
mature_immBlock(get_cur_block());
set_cur_block(block_true);
/* Push false block on stack */
*cs++ = block_false;
return sp;
}
struct dict if_entry =
{
.name = "if",
.immediate = 1,
.code = w_if,
.ldname = "w_if",
.next = &store_entry,
};
/* Mature the basic block under construction and switch construction
to the basic block on the stack. Push the basic block following
the true/false blocks onto the stack. */
WORD(w_else) /* bb_false -- bb_then */
{
ir_node *bb_false = *--cs;
ir_node *jump = new_Jmp();
ir_node *bb_then = new_immBlock();
add_immBlock_pred(bb_then, jump);
mature_immBlock(get_cur_block());
set_cur_block(bb_false);
*cs++ = bb_then;
return sp;
}
struct dict else_entry =
{
.name = "else",
.immediate = 1,
.code = w_else,
.ldname = "w_else",
.next = &if_entry,
};
/* Finalize current basic block as well as the condition block and
make the one on the stack the current one. */
/* ( bb_then -- ) */
WORD(w_then)
{
ir_node *bb_then = *--cs;
ir_node *jump = new_Jmp();
mature_immBlock(get_cur_block());
add_immBlock_pred(bb_then, jump);
set_cur_block(bb_then);
return sp;
}
struct dict then_entry =
{
.name = "then",
.immediate = 1,
.code = w_then,
.ldname = "w_then",
.next = &else_entry,
};
/* BEGIN loop construction */
/* -- bb_head */
WORD(begin)
{
ir_node *jump = new_Jmp();
mature_immBlock(get_cur_block());
ir_node *bb_head = new_immBlock();
*cs++ = bb_head;
set_cur_block(bb_head);
add_immBlock_pred(bb_head, jump);
ir_node *body = new_immBlock();
add_immBlock_pred(body, new_Jmp());
set_cur_block(body);
return sp;
}
struct dict begin_entry =
{
.name = "begin",
.immediate = 1,
.code = begin,
.next = &then_entry
};
/* Finalize unconditional loop construction */
/* ( bb_head -- ) */
WORD(again)
{
ir_node *bb_head = *--cs;
ir_node *jump = new_Jmp();
add_immBlock_pred(bb_head, jump);
/* force a PhiM to be created */
get_store();
/* connect loop with End node */
keep_alive(bb_head);
mature_immBlock(bb_head);
set_cur_block(new_immBlock());
return sp;
}
struct dict again_entry =
{
.name = "again",
.immediate = 1,
.code = again,
.next = &begin_entry
};
/* Finalize conditional loop construction */
/* ( bb_head bb_then -- ) */
WORD(repeat)
{
ir_node *bb_then = *--cs;
ir_node *jump = new_Jmp();
mature_immBlock(get_cur_block());
ir_node *bb_head = *--cs;
add_immBlock_pred(bb_head, jump);
mature_immBlock(bb_head);
set_cur_block(bb_then);
return sp;
}
struct dict repeat_entry =
{
.name = "repeat",
.immediate = 1,
.code = repeat,
.next = &again_entry
};
/* Finalize conditional loop construction */
/* ( bb_head -- ) */
WORD(until)
{
sp = w_if(sp);
/* (bb_head bb_false) */
ir_node *bb_true = get_cur_block();
ir_node *bb_false = *--cs;
set_cur_block(bb_false);
*cs++ = bb_true;
/* (bb_head bb_true) */
return sp = repeat(sp);
}
struct dict until_entry =
{
.name = "until",
.immediate = 1,
.code = until,
.next = &repeat_entry
};
WORD(compile_comma)
{
struct dict *entry = sp[-1].a;
assert(entry);
ir_node *mem = get_store();
ir_node *ptr;
if (entry->entity) {
ptr = new_Address(entry->entity);
} else {
assert(entry->code);
ptr = new_Const_long(mode_P, (long)(entry->code));
}
ir_node *ir_sp = get_value(0, mode_P);
ir_node *call = new_Call(mem, ptr, 1, &ir_sp, word_method_type);
mem = new_Proj(call, mode_M, pn_Call_M);
set_store(mem);
ir_sp = new_Proj(call, mode_T, pn_Call_T_result);
ir_sp = new_Proj(ir_sp, mode_P, 0);
set_value(0, ir_sp);
return --sp;
}
struct dict compile_comma_entry = {
.name = "compile,",
.code = compile_comma,
.immediate = 1,
.next = &until_entry,
.ldname = "compile_comma"
};
WORD(w_word)
{
sp->a = strdup(next()); /* TODO: fix memory leak */
return ++sp;
}
struct dict word_entry = {
.name = "word",
.ldname = "w_word",
.code = w_word,
.next = &compile_comma_entry
};
WORD(find)
{
struct dict *entry = dictionary;
const char *token = sp[-1].a;
while (entry && strcasecmp(entry->name, token)) {
entry = entry->next;
}
if (!entry) {
sp->i = 0;
} else {
sp[-1].a = entry; /* TODO: fix memory leak */
if (entry->immediate)
sp->i = 1;
else
sp->i = -1;
}
return ++sp;
}
struct dict find_entry =
{
.name = "find",
.code = find,
.next = &word_entry
};
WORD(immediate)
{
dictionary->immediate = 1;
return sp;
}
struct dict immediate_entry =
{
.name = "immediate",
.code = immediate,
.next = &find_entry
};
WORD(tor)
{
*rp++ = *--sp;
return sp;
}
struct dict tor_entry =
{
.name = ">r",
.ldname = "tor",
.code = tor,
.next = &immediate_entry
};
WORD(rfrom)
{
*sp++ = *--rp;
return sp;
}
struct dict rfrom_entry =
{
.name = "r>",
.ldname = "rfrom",
.code = rfrom,
.next = &tor_entry
};
WORD(rload)
{
*sp++ = rp[-1];
return sp;
}
struct dict rload_entry =
{
.name = "r@",
.ldname = "rload",
.code = rload,
.next = &rfrom_entry
};
/* Compile code to put a Const on the stack */
WORD(literal)
{
ir_node *ir_sp = get_value(0, mode_P);
ir_node *constnode = new_Const_long(mode_Lu, sp[-1].u);
sp--;
ir_node *store = new_Store(get_store(), ir_sp, constnode, type_cell, 0);
ir_node *mem = new_Proj(store, mode_M, pn_Store_M);
set_store(mem);
ir_node *add = new_Add(ir_sp, new_Const_long(mode_Ls, sizeof(union cell)));
set_value(0, add);
return sp;
}
struct dict literal_entry = {
.name = "literal",
.code = literal,
.immediate = 1,
.next = &rload_entry
};
WORD(w_brkleft)
{
compiling = 0;
return sp;
}
struct dict w_brkleft_entry = {
.name = "[",
.ldname = "w_brkleft",
.code = w_brkleft,
.immediate = 1,
.next = &literal_entry,
};
WORD(w_brkright)
{
compiling = 1;
return sp;
}
struct dict w_brkright_entry = {
.name = "]",
.ldname = "w_brkright",
.code = w_brkright,
.next = &w_brkleft_entry,
};
/* ( u -- a-addr ior ) */
WORD(allocate)
{
size_t sz = sp[-1].u;
sp[-1].a = malloc(sz);
assert(sp[-1].a);
sp[0].a = 0;
return ++sp;
}
struct dict allocate_entry = {
.name = "allocate",
.code = allocate,
.next = &w_brkright_entry
};
WORD(rot)
{
cell tmp = sp[-1];
sp[-1] = sp[-3];
sp[-3] = sp[-2];
sp[-2] = tmp;
return sp;
}
struct dict rot_entry = {
.name = "rot",
.code = rot,
.next = &allocate_entry
};
WORD(mod)
{
sp[-2].i = sp[-2].i % sp[-1].i;
return --sp;
}
struct dict mod_entry = {
.name = "mod",
.code = rot,
.next = &rot_entry
};
WORD(depth)
{
sp->u = (sp - data_stack);
return ++sp;
}
struct dict depth_entry = {
.name = "depth",
.code = depth,
.next = &mod_entry
};
struct dict *dictionary = &depth_entry;
static ir_entity *find_global_entity(const char *name)
{
size_t n_members = get_compound_n_members(get_glob_type());
for (size_t i = 0; i < n_members; ++i) {
ir_entity *member = get_compound_member(get_glob_type(), i);
if (!strcmp(get_entity_ld_name(member), name))
return member;
}
return NULL;
}
/* initialize libfirm and set globally visible entities/types */