-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemitter.c
1370 lines (1178 loc) · 31.8 KB
/
emitter.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 <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include "lowl.h"
#include "emitter.h"
/*
* Support functions
*/
#define w(...) printf(__VA_ARGS__)
/* Scream when you see a bug! */
#define EMIT_PANIC(_s) \
fprintf(stderr, "%s:%d : %s\n", \
__FILE__, __LINE__, (_s)); \
exit(-1);
/* Out of memory. */
void oom(void)
{
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
/*
* LLVM Functions and separation between code and data:
*
* As code in LLVM assembler must be included into functions,
* we need to realize when we start generating code so that
* we can create one big function that contains all the code.
*
* Based on the following assumptions,
*
* - LOWL programs will define data and code sections in two
* separate stages,
* - Data are defined before code,
* - The first opcode will be tagged by a label (the LOWL
* manual specifies that this label will be begin, but
* this is irrelevant to us),
*
* this emitter creates a function at the first call of
* emit_label(), which gets called only when a label is
* associated with an instruction.
*/
int function_created = 0;
/*
* LOWL Program Counter.
* We simulate a program counter to let jump to specific or
* relative addresses possible.
* To be sure that in the code we have the value of current
* PC, we start at 0 and we increment it when we finish
* emitting an instruction. */
long emitter_pc = 0;
#ifdef LOWL_ML1
/*
* ML/I Hash Table support.
* The MD specification for ML/I requires to build an hash
* chain in the LOWL table. We do that in two phases:
* - First, we memorize in the tbl structure chain
* number it must be stored into and the table offset.
* - Later, when dumping the table, we use hash_getlink()
* function to retrieve the address of the last element
* stored in the chain. We store this value in our link
* and update register our offset as the last entry in
* the chain.
*/
lowlint_t hash_links[ML1_HASHSZ];
lowlint_t
hash_getlink(unsigned chain)
{
assert ( chain < ML1_HASHSZ );
return hash_links[chain];
}
void
hash_savelink(unsigned chain, unsigned long offset)
{
assert ( chain < ML1_HASHSZ );
hash_links[chain] = offset;
}
void
hash_emitlink(unsigned chain)
{
assert ( chain < ML1_HASHSZ );
lowlint_t link = hash_getlink(chain);
if ( link == 0 ) {
/* NULL pointer, don't add @LOWLTABLE offset. */
w("%%LLNUM 0");
return;
}
/* Get table offset of next entry from hash_getlink()
* and calculate pointer. */
w("%%LLNUM ptrtoint(i8* getelementptr(i8, %s, %%LLNUM %"PRIdLWI") to %%LLNUM)\n",
"i8* bitcast (%lowltabty* @LOWLTAB to i8*)",
hash_getlink(chain));
}
void
hash_emitthash(void)
{
int i;
/* Emit array of links. Since all the hash table have
* been scanned, this will generate the head of the
* hash chains. */
w("[ %d x %%LLNUM ] [ ", ML1_HASHSZ);
for ( i = 0; i < ML1_HASHSZ; i++ ) {
if ( i != 0 ) w(", ");
hash_emitlink(i);
}
w(" ]");
}
#endif
/* Table support:
* LLVM assembler does not allow to specify a data segment
* the way that machine-specific untyped assembler do.
*
* In order to create the LOWL tables, this mapper's
* approach is to scan for the whole file, collecting
* information about the LOWL program table, and
* dump it later in form of a packed structure.
*
* Labels are stored as constants retaining the value of
* the offset in this table. LAA X, C handles this special
* case.
*/
struct tble {
int type;
union {
char ch;
char *str;
uintptr_t num;
#ifdef LOWL_ML1
struct {
int chain;
unsigned long off;
} h;
#endif
} u;
struct tble *next;
};
enum {
TBL_CH,
TBL_STR,
TBL_NUM,
#ifdef LOWL_ML1
TBL_HASH,
TBL_THASH,
#endif
};
struct tble *last, *tbl = NULL;
unsigned long tbl_size = 0;
void
tbl_append(struct tble *ptr)
{
if ( tbl == NULL )
tbl = last = ptr;
else {
last->next = ptr;
last = ptr;
}
/* Collect Table Size. */
switch(ptr->type) {
case TBL_CH:
tbl_size++;
break;
case TBL_STR:
tbl_size += strlen(ptr->u.str);
break;
case TBL_NUM:
tbl_size += LLVM_PTRSIZE / 8;
break;
#ifdef LOWL_ML1
case TBL_HASH:
tbl_size += LLVM_PTRSIZE / 8;
break;
case TBL_THASH:
tbl_size += ML1_HASHSZ * (LLVM_PTRSIZE/8);
break;
#endif
default:
EMIT_PANIC("Wrong TBL Type!");
}
}
void
tbl_dump(void)
{
struct tble *ptr;
/* The LOWL Table is represented in LLVM as a packed structure.
* We scan the table two times, first time to get the types,
* second one to the values. */
w("\n\n;\n; LOWL Table\n;\n");
/* First pass. Give the structure a type name. */
w("%%lowltabty = type < { ");
ptr = tbl;
while ( ptr != NULL ) {
if ( ptr != tbl ) w(", ");
switch (ptr->type)
{
case TBL_CH:
w("i8");
break;
case TBL_NUM:
w("%%LLNUM");
break;
case TBL_STR:
w("[ %d x i8 ]", (int)strlen(ptr->u.str));
break;
#ifdef LOWL_ML1
case TBL_HASH:
w("%%LLNUM");
break;
case TBL_THASH:
w("[ %d x %%LLNUM]", ML1_HASHSZ);
break;
#endif
default:
EMIT_PANIC("Wrong TBL Type!");
}
ptr = ptr->next;
}
w(" } >\n");
/* Second pass. Write declaration. */
w("@LOWLTAB = internal global %%lowltabty < { ");
ptr = tbl;
while ( ptr != NULL ) {
if ( ptr != tbl ) w(", ");
switch (ptr->type)
{
case TBL_CH:
w("i8 %d", ptr->u.ch);
break;
case TBL_NUM:
w("%%LLNUM %"PRIdPTR, ptr->u.num);
break;
case TBL_STR:
w("[ %d x i8 ] c\"%s\"",
(int)strlen(ptr->u.str), ptr->u.str);
break;
#ifdef LOWL_ML1
case TBL_HASH:
hash_emitlink(ptr->u.h.chain);
/* Save current offset to be used by next entry in
* the chain. */
hash_savelink(ptr->u.h.chain, ptr->u.h.off);
break;
case TBL_THASH:
hash_emitthash();
break;
#endif
default:
EMIT_PANIC("Wrong TBL Type!");
}
ptr = ptr->next;
}
w(" } >; \n ");
}
/*
* Implicit data declaration.
* Strings used in MESS are implicit in LOWL but in LLVM must
* be declared outside of the function. Save them in a list and
* declare later.
*/
struct strdecl {
int id;
char *str;
struct strdecl *next;
} *strdecls = NULL;
int strings = 0;
int
str_declare(char *str)
{
struct strdecl *ptr = malloc(sizeof(struct strdecl));
if ( ptr == NULL ) oom();
ptr->str = str;
ptr->id = strings;
ptr->next = strdecls;
strdecls = ptr;
return strings++;
}
void
str_dump()
{
struct strdecl *ptr = strdecls;
while ( ptr != NULL )
{
w("@STR%d = internal constant [ %d x i8 ] c\"%s\\00\";\n",
ptr->id, (int)strlen(ptr->str)+1, ptr->str);
ptr = ptr->next;
}
}
/*
* Call Graph Analysis.
*
* In order to implement indirect branches in LLVM, apart
* from using a dispatch basic block which would pollute
* terribly the CFG, we need to know, in 'indirectbr'
* or 'switch', all the possible outcomes.
* This is implemented in two phases:
* - First, we collect, when emitting gosub, the source and
* destination labels.
* EXIT n in subroutine will emit code that will jump to a
* basic block whose emission is postponed.
* - Then, after we scanned the whole code, we can create the
* EXIT n basic block, with proper 'switch' instruction and
* the full list of possible destinations.
* The EXIT n basic block will do the following:
* - Get from lowl_stack the pc of the caller.
* - Do an indirect branch based on the exit code (known
* at emitting time) to the destination.
*/
struct callgraphe {
char *symbol;
int exitnr;
int parnm;
int linkr;
struct cg_pclist {
long pc;
struct cg_pclist *next;
} *pclist;
struct callgraphe *next;
};
struct callgraphe *callgraph = NULL;
void
callgraph_add(char *dst, long src_pc)
{
struct cg_pclist *pcl;
struct callgraphe *cge;
/* Create cg_pclist structure. */
pcl = malloc(sizeof(struct cg_pclist));
if ( pcl == NULL ) oom();
pcl->pc = src_pc;
/* Search for dst's pc_list */
cge = callgraph;
while ( cge != NULL ) {
/* Exit if found. */
if ( !strcmp(cge->symbol, dst) )
break;
cge = cge->next;
}
/* If not found, create. */
if ( cge == NULL ) {
cge = malloc(sizeof(struct callgraphe));
if ( cge == NULL ) oom();
cge->symbol = dst;
cge->pclist = NULL;
cge->next = callgraph;
callgraph = cge;
}
/* Add pc to cge's pclist */
pcl->next = cge->pclist;
cge->pclist = pcl;
}
void
callgraph_addsubr(char *subr, char parnm, char exitnr, int linkr)
{
struct callgraphe *cge;
/* Search for Subr */
cge = callgraph;
while ( cge != NULL ) {
/* Exit if found. */
if ( !strcmp(cge->symbol, subr) )
break;
cge = cge->next;
}
/* If not found, create. */
if ( cge == NULL ) {
cge = malloc(sizeof(struct callgraphe));
if ( cge == NULL ) oom();
cge->symbol = subr;
cge->pclist = NULL;
cge->next = callgraph;
callgraph = cge;
}
cge->exitnr = exitnr;
cge->parnm = parnm;
cge->linkr = linkr;
}
void
callgraph_emit_exitbb(struct callgraphe *ptr)
{
int i;
struct cg_pclist *pcl;
static int cnt = 0;
for ( i = 1; i <= ptr->exitnr; i++ ) {
w("lowl_exit_%s_%d:\n", ptr->symbol, i);
if ( ptr->linkr ) {
w("%%exitaddr.%d = load %%LLNUM, %%LLNUM* @LINKPT\n", cnt);
} else {
w("%%exitaddr.%d = call %%LLNUM @lowl_poplink();\n",
cnt);
}
w("switch %%LLNUM %%exitaddr.%d, label %%exit_jmperr [ ", cnt);
pcl = ptr->pclist;
while ( pcl != NULL ) {
w(" %%LLNUM %lu, label %%LOWL_LINE_%ld ",
pcl->pc, pcl->pc + i);
pcl = pcl->next;
}
w("] \n");
cnt++;
}
}
void
callgraph_dump()
{
struct callgraphe *ptr;
struct cg_pclist *pcl;
ptr = callgraph;
while ( ptr != NULL ) {
w("\n");
callgraph_emit_exitbb(ptr);
w("; %s is called from: ", ptr->symbol);
pcl = ptr->pclist;
while ( pcl != NULL ) {
w("%ld,", pcl->pc);
pcl = pcl->next;
}
w("\n");
if ( ptr->linkr )
w("; %s is a linkroutine.\n", ptr->symbol);
else
w("; %s has %d exits and %s PARNM.\n", ptr->symbol,
ptr->exitnr, ptr->parnm ? "has" : "does not have");
ptr = ptr->next;
}
w("\n");
}
/*
* Emitter setup
*/
/* Initialization. */
void
emitter_init(char* target)
{
/* Print some basic banner. */
w(";\n; This file has been autogenerated by LOWL-LLVM mapper.\n");
w(";\n\n\n");
/* Target */
w("target triple = \"%s\"\n", target);
/* Define basic types. */
w("; Basic types definitions.\n");
w("%%LLNUM = type i%d; Numerical is %d bits\n",
LLVM_PTRSIZE, LLVM_PTRSIZE);
w("\n");
w("; External declarations.\n");
w("declare void @lowl_puts(i8*);\n");
w("declare void @lowl_pushlink(%%LLNUM);\n");
w("declare %%LLNUM @lowl_poplink();\n");
w("declare void @lowl_clearlink();\n");
w("declare void @lowl_goadd_jmperror();\n");
w("declare void @lowl_exit_jmperror();\n");
w("declare i8 @lowl_punctuation(i8);\n");
w("declare i8 @lowl_digit(i8);\n");
w("declare void @lowl_bmove(%%LLNUM)\n");
w("declare void @lowl_fmove(%%LLNUM)\n");
emitter_md_init();
}
/* Finalization. */
void
emitter_fini(void)
{
/* Terminate the function, just in case. */
w("ret void\n\n");
/* Emit exit basic blocks. */
callgraph_dump();
/* Terminate and close the LLVM function. */
w("\n; End of LOWL code\nret void;\n}\n\n");
/* Declare MESS strings. */
str_dump();
w("\n\n");
}
/*
* EMITTER FUNCTIONS START HERE.
*/
void emit_table_label(char *lbl)
{
/* Table labels are simple constants whose value is
* the offset from the table start. Even if we haven't dumped
* the table still, the current offset can be found in current
* tbl_size value. */
w("@%s = constant %%LLNUM %ld; [%s]\n", lbl, tbl_size, lbl);
}
void emit_label(char *lbl)
{
/* See comment before function_created. */
if ( !function_created ) {
/* Dump LOWL Table now. */
tbl_dump();
w("\n");
w("\n;\n; LOWL LLVM function\n");
w("define void @lowl_main(%%LLNUM %%ffpt, %%LLNUM %%lfpt)\n");
w("{\n");
w("; Allocate LOWL registers on stack.\n");
w("%%A_REG = alloca %%LLNUM\n");
w("%%B_REG = alloca %%LLNUM\n");
w("%%C_REG = alloca i8\n");
w("; Allocate compare result register on stack.\n");
w("%%CMP = alloca %%LLNUM\n");
w("; Initialize LOWL stack.\n");
w("store %%LLNUM %%ffpt, %%LLNUM* @FFPT\n");
w("store %%LLNUM %%lfpt, %%LLNUM* @LFPT\n");
w("br label %%BEGIN; Jump to BEGIN\n");
w("\n");
w(";\n; Support Basic Blocks\n;\n");
w("goadd_jmperr:\n");
w("call void @lowl_goadd_jmperror();\n");
w("unreachable\n");
w("exit_jmperr:\n");
w("call void @lowl_exit_jmperror();\n");
w("unreachable\n");
w("\n");
function_created = 1;
} else {
/* LLVM assembler can't fall through labels,
* as internally it thinks in basic blocks.
* When adding a label in a middle of some
* code, add an inconditional branch to it
* so that the two basic blocks are
* continguous. */
w("br label %%%s\n\n", lbl);
}
w("%s:\n", lbl);
}
void emit_newpc(int stp)
{
/* To keep track of jumps relative to current
* location of the text files, for example
* in GOADD or EXIT, we create a basic block
* for each code statement.
* This is unfortunate, but it would require
* at least a second pass to implement this
* in a more saner way.
* As everywhere else, though, we can trust
* LLVM for eliminating most of this useless
* code. */
emitter_pc++;
if ( !stp )
w("br label %%LOWL_LINE_%ld", emitter_pc);
w("\nLOWL_LINE_%ld:\n", emitter_pc);
}
void emit_eol()
{
w("\n");
}
void emit_dcl(char *var)
{
w("@%s = global %%LLNUM zeroinitializer; DCL %s\n", var, var);
}
void emit_equ(char *arg1, char *arg2)
{
w("@%s = alias %%LLNUM, %%LLNUM* @%s; EQU %s %s\n",
arg1, arg2, arg1, arg2);
}
void emit_ident(char *v, intptr_t num)
{
/* IDENT handled in the parser. */
}
void emit_con(uintptr_t num)
{
struct tble *tble;
tble = malloc(sizeof(struct tble));
if ( tble == NULL )
oom();
tble->type = TBL_NUM;
tble->u.num = num;
tbl_append(tble);
}
void emit_nch(char c)
{
struct tble *tble;
tble = malloc(sizeof(struct tble));
if ( tble == NULL )
oom();
tble->type = TBL_CH;
tble->u.ch = c;
tbl_append(tble);
}
void emit_str(char *str)
{
struct tble *tble;
tble = malloc(sizeof(struct tble));
if ( tble == NULL )
oom();
tble->type = TBL_STR;
tble->u.str = str;
tbl_append(tble);
}
/* ML/I LOWL Table Items extensions. */
void emit_hash(char *str)
{
#ifdef LOWL_ML1
struct tble *tble;
tble = malloc(sizeof(struct tble));
if ( tble == NULL )
oom();
tble->type = TBL_HASH;
tble->u.h.chain = ml1_hash(str, strlen(str));
tble->u.h.off = tbl_size;
tbl_append(tble);
fprintf(stderr, "Hash of %s: %d\n", str, tble->u.h.chain);
#else
EMIT_PANIC("LOWL mapper compiled without ML/I exentions.");
#endif
}
void emit_thash()
{
#ifdef LOWL_ML1
struct tble *tble;
tble = malloc(sizeof(struct tble));
if ( tble == NULL )
oom();
tble->type = TBL_THASH;
tbl_append(tble);
#else
EMIT_PANIC("LOWL mapper compiled without ML/I exentions.");
#endif
}
void emit_rl(char *str, intptr_t nof)
{
#ifdef LOWL_ML1
/* Do not calculate the pointer, use the subsidiary expr instead. */
struct tble *tble;
tble = malloc(sizeof(struct tble));
if ( tble == NULL )
oom();
tble->type = TBL_NUM;
tble->u.num = nof;
tbl_append(tble);
#else
EMIT_PANIC("LOWL mapper compiled without ML/I exentions.");
#endif
}
void emit_lav(char *v, char rx)
{
static int lav_cnt = 0;
w("%%lav.%d = load %%LLNUM, %%LLNUM* @%s; LAV %s, %c\n", lav_cnt, v, v, rx);
w("store %%LLNUM %%lav.%d, %%LLNUM* %%A_REG;\n", lav_cnt);
lav_cnt++;
}
void emit_lbv(char *v)
{
static int lbv_cnt = 0;
w("%%lbv.%d = load %%LLNUM, %%LLNUM* @%s; LBV %s\n", lbv_cnt, v, v);
w("store %%LLNUM %%lbv.%d, %%LLNUM* %%B_REG;\n", lbv_cnt);
lbv_cnt++;
}
void emit_lal(intptr_t nof)
{
w("store %%LLNUM %"PRIdPTR", %%LLNUM* %%A_REG; LAL %"PRIdPTR"\n",
nof, nof);
}
void emit_lcn(char cn)
{
w("store i8 %d, i8* %%C_REG; LCN '%d'\n", cn, cn);
}
void emit_lam(intptr_t nof)
{
static int lam_cnt = 0;
w("%%lam.%d = load %%LLNUM, %%LLNUM* %%B_REG; LAM %"PRIdPTR"\n", lam_cnt, nof);
w("%%lam.2.%d = add %%LLNUM %%lam.%d, %"PRIdPTR"\n", lam_cnt, lam_cnt, nof);
w("%%lam.3.%d = inttoptr %%LLNUM %%lam.2.%d to %%LLNUM*\n",
lam_cnt, lam_cnt);
w("%%lam.4.%d = load %%LLNUM, %%LLNUM* %%lam.3.%d\n", lam_cnt, lam_cnt);
w("store %%LLNUM %%lam.4.%d, %%LLNUM* %%A_REG\n", lam_cnt);
w("store %%LLNUM %%lam.2.%d, %%LLNUM* %%B_REG\n", lam_cnt);
lam_cnt++;
}
void emit_lcm(intptr_t nof)
{
static int cnt = 0;
w("%%lcm.c.%d = load %%LLNUM, %%LLNUM* %%B_REG\n", cnt);
w("%%lcm.n.%d = add %%LLNUM %%lcm.c.%d, %"PRIdPTR"\n", cnt, cnt, nof);
w("%%lcm.p.%d = inttoptr %%LLNUM %%lcm.n.%d to i8*\n", cnt, cnt);
w("%%lcm.r.%d = load i8, i8* %%lcm.p.%d\n", cnt, cnt);
w("store i8 %%lcm.r.%d\n, i8* %%C_REG\n", cnt);
w("store %%LLNUM %%lcm.n.%d\n, %%LLNUM* %%B_REG\n", cnt);
cnt++;
}
void emit_lai(char *v, char rx)
{
static int cnt = 0;
w("%%lai.v.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%lai.p.%d = inttoptr %%LLNUM %%lai.v.%d to %%LLNUM*\n", cnt, cnt);
w("%%lai.r.%d = load %%LLNUM, %%LLNUM* %%lai.p.%d\n", cnt, cnt);
w("store %%LLNUM %%lai.r.%d, %%LLNUM* %%A_REG\n", cnt);
cnt++;
}
void emit_lci(char *v, char rx)
{
static int cnt = 0;
w("%%lci.v.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%lci.p.%d = inttoptr %%LLNUM %%lci.v.%d to i8*\n", cnt, cnt);
w("%%lci.r.%d = load i8, i8* %%lci.p.%d\n", cnt, cnt);
w("store i8 %%lci.r.%d, i8* %%C_REG\n", cnt);
cnt++;
}
void emit_laa(char *v, char dc)
{
static int cnt = 0;
if ( dc == 'D' ) {
w("%%laa.p.%d = getelementptr %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%laa.v.%d = ptrtoint %%LLNUM* %%laa.p.%d to %%LLNUM\n",
cnt, cnt);
} else {
w("%%laa.o.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%laa.t.%d = ptrtoint %%lowltabty* @LOWLTAB to %%LLNUM\n",
cnt);
w("%%laa.v.%d = add %%LLNUM %%laa.t.%d, %%laa.o.%d\n",
cnt, cnt, cnt);
}
w("store %%LLNUM %%laa.v.%d, %%LLNUM* %%A_REG\n", cnt);
cnt++;
}
void emit_stv(char *v, char px)
{
static int stv_cnt = 0;
w("%%stv.%d = load %%LLNUM, %%LLNUM* %%A_REG; STV %s, %c\n", stv_cnt, v, px);
w("store %%LLNUM %%stv.%d, %%LLNUM* @%s\n", stv_cnt, v);
stv_cnt++;
}
void emit_sti(char *v, char px)
{
static int cnt = 0;
w("%%sti.v.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%sti.p.%d = inttoptr %%LLNUM %%sti.v.%d to %%LLNUM*\n", cnt, cnt);
w("%%sti.a.%d = load %%LLNUM, %%LLNUM* %%A_REG\n", cnt);
w("store %%LLNUM %%sti.a.%d, %%LLNUM* %%sti.p.%d\n", cnt, cnt);
cnt++;
}
void emit_clear(char *v)
{
w("store %%LLNUM 0, %%LLNUM* @%s; CLEAR %s\n", v, v);
}
void emit_aav(char *v)
{
static int aav_cnt = 0;
w("%%aav.%d = load %%LLNUM, %%LLNUM* %%A_REG; ABV %s\n", aav_cnt, v);
w("%%aav.2.%d = load %%LLNUM, %%LLNUM* @%s\n", aav_cnt, v);
w("%%aav.3.%d = add %%LLNUM %%aav.%d, %%aav.2.%d\n",
aav_cnt, aav_cnt, aav_cnt);
w("store %%LLNUM %%aav.3.%d, %%LLNUM* %%A_REG\n", aav_cnt);
aav_cnt++;
}
void emit_abv(char *v)
{
static int cnt = 0;
w("%%abv.%d = load %%LLNUM, %%LLNUM* %%B_REG; AAV %s\n", cnt, v);
w("%%abv.2.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%abv.3.%d = add %%LLNUM %%abv.%d, %%abv.2.%d\n",
cnt, cnt, cnt);
w("store %%LLNUM %%abv.3.%d, %%LLNUM* %%B_REG\n", cnt);
cnt++;
}
void emit_aal(intptr_t nof)
{
static int cnt = 0;
w("%%aal.%d = load %%LLNUM, %%LLNUM* %%A_REG; AAL %"PRIdPTR"\n", cnt, nof);
w("%%aal.2.%d = add %%LLNUM %%aal.%d, %"PRIdPTR"\n", cnt, cnt, nof);
w("store %%LLNUM %%aal.2.%d, %%LLNUM* %%A_REG\n", cnt);
cnt++;
}
void emit_sav(char *v)
{
static int sav_cnt = 0;
w("%%sav.%d = load %%LLNUM, %%LLNUM* %%A_REG; SAV %s\n", sav_cnt, v);
w("%%sav.2.%d = load %%LLNUM, %%LLNUM* @%s\n", sav_cnt, v);
w("%%sav.3.%d = sub %%LLNUM %%sav.%d, %%sav.2.%d\n",
sav_cnt, sav_cnt, sav_cnt);
w("store %%LLNUM %%sav.3.%d, %%LLNUM* %%A_REG\n", sav_cnt);
sav_cnt++;
}
void emit_sbv(char *v)
{
static int cnt = 0;
w("%%sbv.%d = load %%LLNUM, %%LLNUM* %%B_REG; SBV %s\n", cnt, v);
w("%%sbv.2.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%sbv.3.%d = sub %%LLNUM %%sbv.%d, %%sbv.2.%d\n",
cnt, cnt, cnt);
w("store %%LLNUM %%sbv.3.%d, %%LLNUM* %%B_REG\n", cnt);
cnt++;
}
void emit_sal(intptr_t nof)
{
static int sal_cnt = 0;
w("%%sal.%d = load %%LLNUM, %%LLNUM* %%A_REG; SAL %"PRIdPTR"\n", sal_cnt, nof);
w("%%sal.3.%d = sub %%LLNUM %%sal.%d, %"PRIdPTR"\n",
sal_cnt, sal_cnt, nof);
w("store %%LLNUM %%sal.3.%d, %%LLNUM* %%A_REG\n", sal_cnt);
sal_cnt++;
}
void emit_sbl(intptr_t nof)
{
static int cnt = 0;
w("%%sbl.b.%d = load %%LLNUM, %%LLNUM* %%B_REG\n", cnt);
w("%%sbl.r.%d = sub %%LLNUM %%sbl.b.%d, %"PRIdPTR"\n", cnt, cnt, nof);
w("store %%LLNUM %%sbl.r.%d, %%LLNUM* %%B_REG\n", cnt);
cnt++;
}
void emit_multl(intptr_t nof)
{
static int cnt = 0;
w("%%mul.a.%d = load %%LLNUM, %%LLNUM* %%A_REG\n", cnt);
w("%%mul.r.%d = mul %%LLNUM %%mul.a.%d, %"PRIdPTR"\n", cnt, cnt, nof);
w("store %%LLNUM %%mul.r.%d, %%LLNUM* %%A_REG\n", cnt);
cnt++;
}
void emit_bump(char *v, uintptr_t nof)
{
static int cnt = 0;
w("%%bump.v.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%bump.r.%d = add %%LLNUM %%bump.v.%d, %"PRIdPTR"\n", cnt, cnt, nof);
w("store %%LLNUM %%bump.r.%d, %%LLNUM* @%s\n", cnt, v);
cnt++;
}
void emit_andv(char *v)
{
static int cnt = 0;
w("%%andv.v.%d = load %%LLNUM, %%LLNUM* @%s\n", cnt, v);
w("%%andv.a.%d = load %%LLNUM, %%LLNUM* %%A_REG\n", cnt);
w("%%andv.r.%d = and %%LLNUM %%andv.v.%d, %%andv.a.%d\n",
cnt, cnt, cnt);
w("store %%LLNUM %%andv.r.%d, %%LLNUM* %%A_REG\n", cnt);
cnt++;
}
void emit_andl(uintptr_t n)
{
static int cnt = 0;
w("%%andl.a.%d = load %%LLNUM, %%LLNUM* %%A_REG\n", cnt);
w("%%andl.r.%d = and %%LLNUM %"PRIuPTR", %%andl.a.%d\n", cnt, n, cnt);
w("store %%LLNUM %%andl.r.%d, %%LLNUM* %%A_REG\n", cnt);
cnt++;
}
void emit_orl(uintptr_t n)
{
#ifdef LOWL_ML1
static int cnt = 0;
w("%%orl.a.%d = load %%LLNUM, %%LLNUM* %%A_REG\n", cnt);
w("%%orl.r.%d = or %%LLNUM %"PRIuPTR", %%orl.a.%d\n", cnt, n, cnt);
w("store %%LLNUM %%orl.r.%d, %%LLNUM* %%A_REG\n", cnt);
cnt++;
#else
EMIT_PANIC("LOWL mapper compiled without ML/I exentions.");
#endif
}
void emit_cav(char *v)
{
static int cnt = 0;
w("%%cav.a.%d = load %%LLNUM, %%LLNUM* %%A_REG;\n", cnt);
w("%%cav.v.%d = load %%LLNUM, %%LLNUM* @%s;\n", cnt, v);
w("%%cav.cmp.%d = sub %%LLNUM %%cav.a.%d, %%cav.v.%d;\n", cnt,cnt,cnt);
w("store %%LLNUM %%cav.cmp.%d, %%LLNUM* %%CMP\n", cnt);
cnt++;
}
void emit_cal(intptr_t nof)
{
static int cal_cnt = 0;
w("%%cal.%d = load %%LLNUM, %%LLNUM* %%A_REG; CAL %"PRIdPTR"\n", cal_cnt, nof);
w("%%cal_cmp.%d = sub %%LLNUM %%cal.%d, %"PRIdPTR"\n", cal_cnt, cal_cnt, nof);
w("store %%LLNUM %%cal_cmp.%d, %%LLNUM* %%CMP\n", cal_cnt);
cal_cnt++;
}
void emit_ccn(char c)
{
static int cnt = 0;
w("%%ccl.c.%d = load i8, i8* %%C_REG\n", cnt);
w("%%ccl.cmp.%d = sub i8 %%ccl.c.%d , %d\n", cnt, cnt, c);
w("%%ccl.r.%d = sext i8 %%ccl.cmp.%d to %%LLNUM\n", cnt, cnt);
w("store %%LLNUM %%ccl.r.%d, %%LLNUM* %%CMP\n", cnt);
cnt++;
}
void emit_ccl(char *s)
{
emit_ccn(*s);
}