-
Notifications
You must be signed in to change notification settings - Fork 7
/
CODEGEN.C
3037 lines (2425 loc) · 90.7 KB
/
CODEGEN.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
/****************************************************************************/
/* COPYRIGHT NOTICE */
/****************************************************************************/
/* A PL/I Compiler for the Win32 platform. */
/* Copyright (C) 1997 - 2006 Hugh Gleaves. */
/* */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation; either version 2 */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 */
/* USA. */
/****************************************************************************/
/****************************************************************************/
/* Modification History */
/****************************************************************************/
/* Who When What */
/* ------------------------------------------------------------------------ */
/* HWG 12-04-91 Initial Prototype. */
/* HWG 20-05-91 Initial code for simple iterative loops. */
/* HWG 21-05-91 Block entry and exit code now generated. */
/* HWG 21-05-91 Static and automatic items now accessed. */
/* HWG 23-05-91 Procedure call/return implemented. */
/* HWG 07-10-91 Conversion started for interface to code emitter. */
/* HWG 13-10-91 Stack walking code created when accessing vars in */
/* any outer blocks */
/* HWG 17-10-91 Stack walker wasnt using segment overrides for _EBX */
/* HWG 03-11-91 Stack accessing code is now created when a block */
/* references a parameter. */
/* HWG 17-11-91 The fixup for the unconditional forward jump in a */
/* iterative loop, was 1 byte too far, this caused a */
/* vital POP instruction to be missed when a loop was */
/* ended, this caused a gradual stack destruction. */
/* HWG 28-11-91 Call statements used to access the static and code */
/* of the target block using _EBX, this has been changed*/
/* to _DI because get_static_base uses _EBX itself. */
/* Get_static_base has been inserted to allow calls */
/* to external entries declared in outer blocks. */
/* HWG 17-12-91 When passing a parameter that was a static item */
/* in an outer block, the data segment was passed as */
/* DS this is the calling blocks static however, NOT */
/* the declaring block. ES is now pushed in this case.*/
/* HWG 10-01-92 A major alteration was made to make the compiler */
/* pass parameters in the same way as Turbo C. */
/* This involves reversing the order of Seg/Ofx when */
/* pushing an arguments address, and also making the */
/* caller pop any args rather than the callee. */
/* It is now possible to write language-support code */
/* in C, since stack-frame management is compatible. */
/* HWG 15-01-92 Multiplication of bin(15) vals & constants has */
/* been implemented. */
/* HWG 16-01-92 Array references are now translated, the function */
/* load_ofx() is used to acheive this. */
/* HWG 22-01-92 The use of fixups for setting the offset of forward*/
/* jumps when generating loops, is discontinued. */
/* by its nature, it limited the code in the loop to */
/* about 900/1000 bytes. Backpatching is now used. */
/* HWG 30-03-92 An initial form of the if-then-else statement has */
/* been incorporated. */
/* */
/* HWG 24-06-93 tran_assignment Now checks to see if the result of */
/* an expression is in the NDP (CoProcessor) and uses */
/* it accordingly. */
/* */
/* HWG 02-07-93 The ops: >= and <= added for IF stmts. */
/* */
/* HWG01 15-01-96 'tran_call' did not check to see if there were any */
/* args before checking them, it refd NULL in this */
/* case and NT slapped it ! */
/* */
/* HWG02 18-10-02 Added WORD_PTR to the instruction that addresses a */
/* numeric constant when addressing a reference, */
/****************************************************************************/
/****************************************************************************/
/* Functional Description */
/****************************************************************************/
/* This source file contains all those functions that are concerned with */
/* code generation. The code phase, walks down the parse-tree (like pass2) */
/* and generates code for each type of node encountered. */
/* The code generated in here is for the iAP-80286 CPU. */
/* Code is generated and written in a fairly 'high level' manner, the code */
/* emitter is responsible for the physical creation of code records. */
/****************************************************************************/
typedef unsigned char chur;
typedef unsigned short unt;
# include <setjmp.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <dos.h>
/* # include <dir.h> */
# include "c_types.h"
# include "tokens.h"
# include "nodes.h"
# include "symtab.h"
# include "pentium.h"
# include "fixup.h"
# include "options.h"
# include "coff.h"
# include "globals.h"
# include "pli_machine.h"
# define _LINE_ ((short)__LINE__)
extern FILE *OB;
extern Instruction Inst;
extern fixup Fixup;
extern char line_no[10];
/*extern*/ Block_ptr block_root; /* root of symtab */
extern Procedure_ptr parse_root;
Any_ptr next_node (void);
static short tran_nodes (Any_ptr);
short nodetype (Any_ptr);
void report (short,char *,short);
static void tran_procedure (void);
static void tran_if (void);
static void tran_stop (void);
static void tran_call (void);
static void tran_put (void);
static void tran_entry (void);
static void tran_return (void);
static void tran_loop (void);
static void tran_do (void);
static void tran_begin (void);
static void tran_allocate (void);
static void tran_free (void);
static void tran_goto (void);
static void tran_end (void);
static void tran_label (void);
static void tran_assignment (void);
static void tran_add (Any_ptr);
static void tran_relation (Any_ptr);
static void tran_sub (Any_ptr);
static void tran_mul (Any_ptr);
static void tran_div (Any_ptr);
static void tran_expression (Any_ptr);
static void tran_reference (Ref_ptr);
void set_error_line (Any_ptr);
short next_node_type (Any_ptr);
void open_object (char *);
void enter_code (void);
void retreat (void);
//void write_modend (void);
void CpuSetSectPtr (Section_ptr);
void CpuGenerate (void);
void write_code (void);
void start_code (Block_ptr);
void patch_code (long,Any_ptr,short);
short get_code_offset (void);
short get_ledata_offset (void);
short get_segment_offset (void);
long get_file_offset (void);
void write_fixup (void);
static void gen_fixup (void);
void begin_fixup (void);
chur get_auto_reg (Symbol_ptr);
static chur get_static_base (Symbol_ptr);
static long frame_offset (Symbol_ptr);
static long runtime_offset (Symbol_ptr);
static void address_reference (Ref_ptr,short,chur);
long unique (void);
chur load_ofx (Ref_ptr,chur);
short identical (Any_ptr,Any_ptr);
jmp_buf exit_code;
short defbas_depth = 0;
short block_ctr;
char recursive_name[33];
long unique_ctr = 0;
long patch_posn;
long return_posn;
Any_ptr curr_node_ptr = NULL;
Block_ptr curr_parent_block = NULL;
Block_ptr curr_ES_block = NULL; /* optimize access to outer statics */
Block_ptr curr_EBX_block = NULL; /* ------------------------ automatics */
Ref_ptr curr_DI_ref = NULL; /* optimize array index calcs */
char result_in_NDP = 0; /* If an expr's result is in NDP */
/****************************************************************************/
/* This is the main function entry for pass2 processing. */
/****************************************************************************/
void enter_code (void)
{
Any_ptr ptr;
block_ctr = 0;
begin_fixup();
if (setjmp(exit_code) == 0) /* Exit code, if we get a null node-ptr */
{ /* This will only ocurr as a result of */
ptr = next_node(); /* a compiler error. */
tran_nodes (ptr);
CoffWriteObjFile (obj_root);
// write_code(); /* flush any buffered code record */
// write_fixup(); /* TEST FUNCTION ONLY !!! */
// write_modend();
}
else
return;
}
/****************************************************************************/
/* This function will take a node-ptr and call a code function to tran */
/* that node ! */
/****************************************************************************/
static short
tran_nodes (Any_ptr node_ptr)
{
Procedure_ptr p_ptr;
Block_ptr here_ptr;
Block_ptr cb_ptr;
Block_ptr prev_parent_block;
short ntype;
if (node_ptr == NULL)
return (0);
ntype = nodetype(node_ptr);
switch (ntype) {
case (PROCEDURE):
p_ptr = node_ptr;
prev_parent_block = curr_parent_block;
curr_parent_block = p_ptr->proc;
here_ptr = curr_node_ptr;
curr_node_ptr = p_ptr->proc->first_stmt;
tran_procedure ();
curr_node_ptr = here_ptr;
curr_parent_block = prev_parent_block;
break;
case (IF):
tran_if();
break;
case (STOP):
tran_stop();
break;
case (CALL):
tran_call();
break;
case (RETURN):
tran_return();
break;
case (LOOP):
tran_loop();
break;
case (DO):
tran_do();
break;
case (BEGIN):
tran_begin();
break;
case (ALLOCATE):
tran_allocate();
break;
case (FREE):
tran_free();
break;
case (GOTO):
tran_goto();
break;
case (END):
set_error_line(node_ptr);
return(END);
case (LABEL):
tran_label();
break;
case (ASSIGNMENT):
tran_assignment();
break;
case (PUT):
tran_put();
break;
case (ENTRY):
tran_entry();
break;
default:
{
report (134,"",_LINE_);
return(0);
}
}
if (ntype == PROCEDURE)
{
/******************************************************************/
/* OK We have processed a PL/1 block, but we must now process all */
/* child blocks of this block. */
/******************************************************************/
cb_ptr = p_ptr->proc->child;
while (cb_ptr != NULL)
{
tran_nodes(cb_ptr->first_stmt);
cb_ptr = cb_ptr->sister;
}
}
return(0);
}
/*************************************************************************/
/* Translate a PL/1 procedure node. */
/*************************************************************************/
static void tran_procedure (void)
{
Any_ptr next;
Procedure_ptr ptr;
short code_size;
//long I,J,K;
Reg regs[8];
Section_ptr sec_ptr;
CoffReloc reloc;
regs[0] = _EAX;
regs[1] = _ECX;
regs[2] = _EDX;
regs[3] = _EBX;
regs[4] = _ESP;
regs[5] = _EBP;
regs[6] = _ESI;
regs[7] = _EDI;
/******************************************************************/
/* If this block isnt called, then issue a warning and return. */
/******************************************************************/
ptr = curr_node_ptr;
/* if (ptr->proc->function == 0)
if (ptr->proc->called == 0)
{
report(-123,ptr->proc->block_name,_LINE_);
return;
} */
/******************************************************************/
/* Start a new CODE segment and a new LEDATA header etc for this */
/* new PL/1 block. */
/******************************************************************/
block_ctr++;
CpuSetSectPtr (CoffGetSectionPtr(obj_root,ptr->proc->code_idx));
set_error_line (curr_node_ptr);
next = next_node();
/* Test code for generating arbitrary instructions */
/* This is for testing the machine generfator */
/*
Inst.opcode = ADD;
Inst.target.reg = _EAX;
Inst.source.reg = _EBX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.reg = _EAX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EBX;
Inst.source.reg = _EAX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.reg = _EBX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EAX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EBX;
Inst.source.bas = _EBX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EBX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EBX;
Inst.source.bas = _EAX;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EAX;
Inst.source.dis = 255;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EAX;
Inst.source.dis = 15;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EBX;
Inst.source.dis = 255;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EBX;
Inst.source.idx = _EAX;
Inst.source.dis = 255;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EBX;
Inst.source.idx = _ECX;
Inst.source.dis = 255;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _EAX;
Inst.source.idx = _EAX;
Inst.source.dis = 255;
CpuGenerate(); */
/*
Inst.opcode = MOV;
Inst.target.reg = _EAX;
Inst.source.bas = _ECX;
Inst.source.idx = _ESP;
Inst.source.dis = 255;
CpuGenerate();
Inst.opcode = MOV;
Inst.source.reg = _EAX;
Inst.target.bas = _ECX;
Inst.target.idx = _ESP;
Inst.target.dis = 255;
CpuGenerate(); */
/*
Inst.opcode = ADD;
Inst.target.reg = _EBP;
Inst.source.idx = _ESI; // must be validated in some way, all this sort of stuff
Inst.source.scale = 8;
Inst.source.dis = 0x1122;
CpuGenerate();
Inst.opcode = ADD;
Inst.target.reg = _EBP;
Inst.source.bas = _EBP;
Inst.source.idx = _ESI; // must be validated in some way, all this sort of stuff
Inst.source.scale = 8;
Inst.source.dis = 0x1122;
CpuGenerate();
Inst.opcode = SUB;
Inst.target.reg = _EBP;
Inst.source.idx = _ESI; // must be validated in some way, all this sort of stuff
Inst.source.scale = 8;
Inst.source.dis = 0x1122;
CpuGenerate();
Inst.opcode = SUB;
Inst.target.reg = _EBP;
Inst.source.bas = _EBP;
Inst.source.idx = _ESI; // must be validated in some way, all this sort of stuff
Inst.source.scale = 8;
Inst.source.dis = 0x1122;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EBP;
Inst.source.idx = _ESI; // must be validated in some way, all this sort of stuff
Inst.source.scale = 8;
Inst.source.dis = 0x1122;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = _EBP;
Inst.source.bas = _EBP;
Inst.source.idx = _ESI; // must be validated in some way, all this sort of stuff
Inst.source.scale = 8;
Inst.source.dis = 0x1122;
CpuGenerate();
*/
/*
for (I=0; I<8; I++)
for (J=0; J<8; J++)
for (K=0; K<8; K++)
{
Inst.opcode = JA;
Inst.target.imm = 23;
Inst.target.len = BYTE_PTR;
CpuGenerate();
Inst.opcode = JA;
Inst.target.imm = 23;
Inst.target.len = DWORD_PTR;
CpuGenerate();
Inst.opcode = FILD;
Inst.target.bas = regs[J];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
Inst.target.scale = 4;
Inst.target.dis = I*J*K*J;
Inst.target.len = WORD_PTR;
CpuGenerate();
Inst.opcode = FILD;
Inst.target.bas = regs[J];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
Inst.target.scale = 4;
Inst.target.dis = I*J*K*J;
Inst.target.len = DWORD_PTR;
CpuGenerate();
Inst.opcode = FILD;
Inst.target.bas = regs[J];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
Inst.target.scale = 4;
Inst.target.dis = I*J*K*J;
Inst.target.len = QWORD_PTR;
CpuGenerate();
Inst.opcode = MUL;
Inst.source.reg = regs[I];
CpuGenerate();
Inst.opcode = MUL;
Inst.target.bas = regs[J];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
Inst.target.scale = 4;
Inst.target.dis = I*J*K*J;
CpuGenerate();
Inst.opcode = MOV;
Inst.source.reg = regs[I];
Inst.target.bas = regs[J];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
Inst.target.scale = 4;
Inst.target.dis = I*J*K*J;
CpuGenerate();
Inst.opcode = CMP;
Inst.source.reg = regs[I];
Inst.target.bas = regs[J];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
Inst.target.scale = 4;
Inst.target.dis = I*J*K*J;
CpuGenerate();
Inst.opcode = CAL;
Inst.source.bas = regs[J];
if (K != 4)
Inst.source.idx = regs[K];
else
Inst.source.idx = 0;
Inst.source.scale = 4;
Inst.source.dis = I*J*K*J;
CpuGenerate();
Inst.opcode = CAL;
Inst.target.imm = I*J*K;
Inst.target.len = DWORD_PTR;
CpuGenerate();
Inst.opcode = CAL;
Inst.target.imm = I*J*K;
Inst.target.len = DWORD_PTR;
CpuGenerate();
Inst.opcode = MOV;
Inst.target.reg = regs[I];
Inst.source.bas = regs[J];
if (K != 4)
Inst.source.idx = regs[K];
else
Inst.source.idx = 0;
Inst.source.scale = 4;
Inst.source.dis = I*J*K*J;
CpuGenerate();
Inst.opcode = CMP;
Inst.target.reg = regs[I];
Inst.source.bas = regs[J];
if (K != 4)
Inst.source.idx = regs[K];
else
Inst.source.idx = 0;
Inst.source.scale = 4;
Inst.source.dis = I*J*K*J;
CpuGenerate();
Inst.opcode = ADD;
Inst.target.reg = regs[I];
Inst.source.bas = regs[J];
if (K != 4)
Inst.source.idx = regs[K];
else
Inst.source.idx = 0;
Inst.source.dis = I*J*K*I * (-1 * K%2);
CpuGenerate();
Inst.opcode = SUB;
Inst.target.reg = regs[I];
Inst.source.bas = regs[J];
if (K != 4)
Inst.source.idx = regs[K];
else
Inst.source.idx = 0;
Inst.source.dis = I*J*K*K;
CpuGenerate();
Inst.opcode = ADC;
Inst.target.reg = regs[I];
Inst.source.bas = regs[J];
if (K != 4)
Inst.source.idx = regs[K];
else
Inst.source.idx = 0;
Inst.source.dis = I*J*K*I * (-1 * K%2);
CpuGenerate();
Inst.opcode = SBB;
Inst.target.reg = regs[I];
Inst.source.bas = regs[J];
if (K != 4)
Inst.source.idx = regs[K];
else
Inst.source.idx = 0;
Inst.source.dis = I*J*K*K;
CpuGenerate();
Inst.opcode = INC;
Inst.target.reg = regs[I];
CpuGenerate();
Inst.opcode = INC;
Inst.target.bas = regs[I];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
CpuGenerate();
Inst.opcode = DEC;
Inst.target.reg = regs[I];
CpuGenerate();
Inst.opcode = DEC;
Inst.target.bas = regs[I];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
CpuGenerate();
Inst.opcode = PUSH;
Inst.target.reg = regs[I];
CpuGenerate();
Inst.opcode = POP;
Inst.target.reg = regs[I];
CpuGenerate();
Inst.opcode = PUSH;
Inst.target.bas = regs[I];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
CpuGenerate();
Inst.opcode = POP;
Inst.target.bas = regs[J];
if (K != 4)
Inst.target.idx = regs[K];
else
Inst.target.idx = 0;
CpuGenerate();
}
*/
/********************************************************************/
/* Generate the standard PL/1 entry sequence. */
/********************************************************************/
Inst.opcode = ENTER;
Inst.target.imm = (short)ptr->proc->stack;
Inst.target.len = WORD_PTR;
Inst.source.imm = 0;
Inst.source.len = BYTE_PTR;
CpuGenerate();
Inst.opcode = MOV;
Inst.source.reg = _ECX;
Inst.target.bas = _EBP;
Inst.target.len = DWORD_PTR;
Inst.target.dis = -((short)(ptr->proc->stack)); /* save CX at offset 0 in frame */
CpuGenerate();
//Inst.target.reg = EDS; /* Load the procs DS seg reg */
//Inst.source.reg = _ECX;
//Inst.opcode = MOV;
//CpuGenerate();
/*********************************************************************/
/* Loop and process all statement nodes in this block. The loop will */
/* terminate when the END for this block has been reached. */
/*********************************************************************/
while (tran_nodes (next) == 0)
{
next = next_node();
}
/********************************************************************/
/* Return to invoking procedure or PLIMAIN (if main proc). */
/********************************************************************/
Inst.opcode = EXIT; /* leave on MC68000 */
CpuGenerate();
/* caller now pops arg ptrs */
Inst.opcode = RET;
Inst.source.imm = 0; /* ptr->proc->num_args; */
CpuGenerate();
reloc.RelUn.VirtualAddress = 8;
reloc.SymbolTableIndex = 7;
reloc.Type = 20; //IMAGE_REL_I386_DIR32;
sec_ptr = CoffGetSectionPtr(obj_root,ptr->proc->code_idx);
//CoffInsertReloc(sec_ptr,&reloc);
/*******************************************************************/
/* Flush any code for this block from the code emitters buffers. */
/*******************************************************************/
write_code();
code_size = get_segment_offset();
patch_code(ptr->proc->seg_pos,&code_size,2); /* patch seglen etc */
write_fixup();
return; /* An END was read so were done with this procedure/block */
}
/***************************************************************************/
/* This function is called to tran a PL/1 expression tree. */
/***************************************************************************/
static void tran_expression (Any_ptr node_ptr)
{
switch (nodetype(node_ptr)) {
case (PLUS):
tran_add (node_ptr);
break;
case (MINUS):
tran_sub (node_ptr);
break;
case (TIMES):
tran_mul (node_ptr);
break;
case (DIVIDE):
tran_div (node_ptr);
break;
case (EQUALS):
case (NOTEQUAL):
case (LT):
case (GT):
case (GE):
case (LE):
case (NOTGT):
case (NOTLT):
case (NOTGE):
case (NOTLE):
tran_relation (node_ptr);
break;
case (REFERENCE):
tran_reference (node_ptr);
break;
default:
{
report(134,"",_LINE_);
}
}
}
/*************************************************************************/
/* Analyse and process a PL/1 + operator node, and any subnodes. */
/*************************************************************************/
static void tran_add (Any_ptr g_ptr)
{
Oper_ptr ptr;
Ref_ptr rl_ptr;
Ref_ptr rr_ptr;
short lt,rt;
ptr = g_ptr;
lt = nodetype(ptr->left_ptr);
rt = nodetype(ptr->rite_ptr);
/***************************************************************/
/* If both operands of this plus operator are refs then ...... */
/***************************************************************/
if ((lt == REFERENCE) &&
(rt == REFERENCE))
{
rl_ptr = (Ref_ptr)ptr->left_ptr;
rr_ptr = (Ref_ptr)ptr->rite_ptr;
address_reference(rl_ptr,SOURCE,DWORD_PTR);
Inst.opcode = MOV;
Inst.target.reg = _EAX;
CpuGenerate();
/***************************************************************/
/* If we are adding a constant of '0' then emit no instruction */
/***************************************************************/
if (rr_ptr->attribs->class == CONSTANT)
if (rr_ptr->attribs->type == NUMERIC)
if (strcmp(rr_ptr->spelling,"0") == 0)
return;
/***************************************************************/
/* If we are adding a constant of '1' then use inc instruction */
/***************************************************************/
if (rr_ptr->attribs->class == CONSTANT)
if (rr_ptr->attribs->type == NUMERIC)
if (strcmp(rr_ptr->spelling,"1") == 0)
{
Inst.opcode = INC;
Inst.target.reg = _EAX;
CpuGenerate();
return;
}
address_reference(rr_ptr,SOURCE,DWORD_PTR);
Inst.opcode = ADD;
Inst.target.reg = _EAX;
CpuGenerate();
return;
}
/*****************************************************************/
/* If the left operand is a ref and the right is an expression...*/
/*****************************************************************/
if ((lt == REFERENCE) &&
(rt != REFERENCE))
{
rl_ptr = (Ref_ptr)ptr->left_ptr;
tran_expression (ptr->rite_ptr);
Inst.opcode = PUSH;
Inst.source.reg = _EAX;
CpuGenerate();
address_reference(rl_ptr,SOURCE,DWORD_PTR);
Inst.opcode = MOV;
Inst.target.reg = _EAX;
CpuGenerate();
Inst.opcode = POP;
Inst.target.reg = _EBX;
CpuGenerate();
Inst.opcode = ADD;
Inst.target.reg = _EAX;
Inst.source.reg = _EBX;
CpuGenerate();
return;
}
/*****************************************************************/
/* If the left operand is an expression and the rite is a ref... */
/*****************************************************************/
if ((lt != REFERENCE) &&
(rt == REFERENCE))
{
rr_ptr = (Ref_ptr)ptr->rite_ptr;
tran_expression (ptr->left_ptr);
/***************************************************************/
/* If we are adding a constant of '0' then emit no instruction */
/***************************************************************/
if (rr_ptr->attribs->class == CONSTANT)
if (rr_ptr->attribs->type == NUMERIC)
if (strcmp(rr_ptr->spelling,"0") == 0)
return;
/***************************************************************/
/* If we are adding a constant of '1' then use inc instruction */
/***************************************************************/
if (rr_ptr->attribs->class == CONSTANT)
if (rr_ptr->attribs->type == NUMERIC)
if (strcmp(rr_ptr->spelling,"1") == 0)
{
Inst.opcode = INC;