2
|
1 /*
|
|
2 Very Simple Code Generator for Mac OS X (PPC)
|
|
3 $Id$
|
|
4 */
|
|
5
|
|
6 #include "s-compile.h"
|
|
7
|
|
8 int label = 0;
|
|
9 char *comments = "#####";
|
|
10
|
|
11 static
|
|
12 char *intro[] = {
|
|
13 " .section __TEXT,__text,regular,pure_instructions",
|
|
14 " .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32",
|
|
15 " .machine ppc",
|
|
16 " .text",
|
|
17 " .align 2",
|
|
18 " .globl _main",
|
4
|
19 "_main:",
|
2
|
20 " mflr r0",
|
4
|
21 " stmw r29,-12(r1)",
|
|
22 " stw r0,8(r1)",
|
|
23 " stwu r1,-80(r1)",
|
3
|
24 " bcl 20,31,\"L00000000001$pb\"",
|
4
|
25 "\"L00000000001$pb\":",
|
3
|
26 " mflr r31",
|
2
|
27 NULL
|
|
28 };
|
|
29
|
4
|
30
|
2
|
31 static
|
|
32 char *ending[] = {
|
4
|
33 " addi r1,r1,80",
|
|
34 " lwz r0,8(r1)",
|
|
35 " mtlr r0",
|
|
36 " lmw r29,-12(r1)",
|
|
37 " blr",
|
|
38 "",
|
2
|
39 " .cstring",
|
|
40 " .align 2",
|
4
|
41 "LC0:",
|
3
|
42 " .ascii \"= %d\\12\\0\"",
|
2
|
43 " .text",
|
|
44 " .align 2",
|
|
45 " .globl _print",
|
4
|
46 "_print:",
|
2
|
47 " mflr r0",
|
|
48 " stw r31,-4(r1)",
|
|
49 " stw r0,8(r1)",
|
|
50 " stwu r1,-80(r1)",
|
3
|
51 " bcl 20,31,\"L00000000002$pb\"",
|
4
|
52 "\"L00000000002$pb\":",
|
2
|
53 " mflr r31",
|
|
54 " mr r4,r3",
|
3
|
55 " addis r3,r31,ha16(LC0-\"L00000000002$pb\")",
|
|
56 " la r3,lo16(LC0-\"L00000000002$pb\")(r3)",
|
2
|
57 " bl L_printf$stub",
|
|
58 " addi r1,r1,80",
|
|
59 " lwz r0,8(r1)",
|
|
60 " mtlr r0",
|
|
61 " lwz r31,-4(r1)",
|
|
62 " mtlr r0",
|
|
63 " lwz r31,-4(r1)",
|
|
64 " blr",
|
3
|
65 "\t.align 2",
|
|
66 ".comm _variable,192",
|
2
|
67 " .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32",
|
|
68 " .align 5",
|
4
|
69 "L_printf$stub:",
|
2
|
70 " .indirect_symbol _printf",
|
|
71 " mflr r0",
|
3
|
72 " bcl 20,31,\"L00000000001$spb\"",
|
4
|
73 "\"L00000000001$spb\":",
|
2
|
74 " mflr r11",
|
3
|
75 " addis r11,r11,ha16(L_printf$lazy_ptr-\"L00000000001$spb\")",
|
2
|
76 " mtlr r0",
|
3
|
77 " lwzu r12,lo16(L_printf$lazy_ptr-\"L00000000001$spb\")(r11)",
|
2
|
78 " mtctr r12",
|
|
79 " bctr",
|
|
80 " .lazy_symbol_pointer",
|
4
|
81 "L_printf$lazy_ptr:",
|
2
|
82 " .indirect_symbol _printf",
|
|
83 " .long dyld_stub_binding_helper",
|
3
|
84 " .non_lazy_symbol_pointer",
|
|
85 "L_variable$non_lazy_ptr:",
|
|
86 " .indirect_symbol _variable",
|
|
87 " .long 0",
|
|
88 " .subsections_via_symbols",
|
|
89
|
2
|
90 NULL
|
|
91 };
|
|
92
|
|
93 void
|
|
94 emit_push()
|
|
95 {
|
3
|
96 printf("\taddis r1,r1,-4\n");
|
|
97 printf("\tstw r3,0(r1)\n");
|
2
|
98 }
|
|
99
|
|
100 void
|
|
101 emit_compare()
|
|
102 {
|
3
|
103 printf("\tlwz r4,0(r1)\n");
|
2
|
104 printf("\taddis r1,r1,4\n");
|
5
|
105 printf("\tcmpw cr7,r4,r3\n");
|
3
|
106 printf("\tmfcr r3\n");
|
|
107 printf("\trlwinm r3,r3,30,1\n");
|
2
|
108 }
|
|
109
|
|
110 void
|
|
111 emit_store(assign)
|
|
112 int assign;
|
|
113 {
|
4
|
114 printf("\taddis r2,r31,ha16(L_variable$non_lazy_ptr-\"L00000000001$pb\")\n");
|
|
115 printf("\tlwz r2,lo16(L_variable$non_lazy_ptr-\"L00000000001$pb\")(r2)\n");
|
3
|
116 printf("\tstw r3,%d(r2)\n",assign*4);
|
2
|
117 }
|
|
118
|
|
119 static
|
|
120 char *opcode[] = {
|
|
121 "",
|
3
|
122 "subf",
|
2
|
123 "add",
|
3
|
124 "mullw",
|
|
125 "divw",
|
2
|
126 "",
|
|
127 "",
|
|
128 "",
|
|
129 "",
|
3
|
130 "subf",
|
|
131 "divw",
|
2
|
132 };
|
|
133
|
|
134 void
|
|
135 emit_calc(enum opcode op)
|
|
136 {
|
3
|
137 printf("\tlwz r4,0(r1)\n");
|
2
|
138 printf("\taddis r1,r1,4\n");
|
|
139 if(op==O_DIV) {
|
5
|
140 printf("\tdivw r3,r4,r3\n");
|
|
141 // } else if(op==O_SUB) {
|
|
142 // printf("\tsubf r3,r3,r4\n");
|
2
|
143 } else {
|
5
|
144 printf("\t%s r3,r3,r4\n",opcode[op]);
|
2
|
145 }
|
|
146 }
|
|
147
|
|
148 void
|
|
149 emit_value(d)
|
|
150 int d;
|
|
151 {
|
5
|
152 unsigned int d0 = (d&0xffff);
|
|
153 unsigned int d1 = ((d>>16)&0xffff);
|
|
154 printf("\tlis r3,0x%x\n",d1);
|
|
155 printf("\tori r3,r3,0x%x\n",d0);
|
3
|
156
|
2
|
157 }
|
|
158
|
|
159 void
|
|
160 emit_load(d)
|
|
161 int d;
|
|
162 {
|
4
|
163 printf("\taddis r2,r31,ha16(L_variable$non_lazy_ptr-\"L00000000001$pb\")\n");
|
|
164 printf("\tlwz r2,lo16(L_variable$non_lazy_ptr-\"L00000000001$pb\")(r2)\n");
|
3
|
165 printf("\tlwz r3,%d(r2)\n",d*4); // -1024<d <1024
|
2
|
166 }
|
|
167
|
|
168 void
|
|
169 emit_comment()
|
|
170 {
|
|
171 if (before < ptr) {
|
|
172 putchar('#'); putchar('#'); putchar(' ');
|
|
173 while (before < ptr) {
|
|
174 if(*before && *before!='\n') {
|
|
175 putchar(*before);
|
|
176 }
|
|
177 before++;
|
|
178 }
|
|
179 putchar('\n');
|
|
180 }
|
|
181 }
|
|
182
|
|
183 void
|
|
184 emit_print()
|
|
185 {
|
|
186 printf("\tbl _print\n");
|
|
187 }
|
|
188
|
|
189
|
|
190 void
|
|
191 emit_intro()
|
|
192 {
|
|
193 char **iptr;
|
|
194 for(iptr=intro;*iptr;iptr++) {
|
3
|
195 printf("%s\n",*iptr);
|
2
|
196 }
|
|
197 }
|
|
198
|
|
199 void
|
|
200 emit_ending()
|
|
201 {
|
|
202 char **iptr;
|
|
203 for(iptr=ending;*iptr;iptr++) {
|
3
|
204 printf("%s\n",*iptr);
|
2
|
205 }
|
|
206 }
|
|
207
|
|
208 /* end */
|