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",
|
|
19 " _main:",
|
|
20 " mflr r0",
|
|
21 " bcl 20,31,"L00000000001$pb"",
|
|
22 " "L00000000001$pb":",
|
|
23 " mflr r10",
|
|
24 " mtlr r0",
|
|
25 NULL
|
|
26 };
|
|
27
|
|
28 static
|
|
29 char *ending[] = {
|
|
30 " .cstring",
|
|
31 " .align 2",
|
|
32 " LC0:",
|
|
33 " .ascii "= %d\12\0"",
|
|
34 " .text",
|
|
35 " .align 2",
|
|
36 " .globl _print",
|
|
37 " _print:",
|
|
38 " mflr r0",
|
|
39 " stw r31,-4(r1)",
|
|
40 " stw r0,8(r1)",
|
|
41 " stwu r1,-80(r1)",
|
|
42 " bcl 20,31,"L00000000002$pb"",
|
|
43 " "L00000000002$pb":",
|
|
44 " mflr r31",
|
|
45 " mr r4,r3",
|
|
46 " addis r3,r31,ha16(LC0-"L00000000002$pb")",
|
|
47 " la r3,lo16(LC0-"L00000000002$pb")(r3)",
|
|
48 " bl L_printf$stub",
|
|
49 " addi r1,r1,80",
|
|
50 " lwz r0,8(r1)",
|
|
51 " mtlr r0",
|
|
52 " lwz r31,-4(r1)",
|
|
53 " mtlr r0",
|
|
54 " lwz r31,-4(r1)",
|
|
55 " blr",
|
|
56 " .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32",
|
|
57 " .align 5",
|
|
58 " L_printf$stub:",
|
|
59 " .indirect_symbol _printf",
|
|
60 " mflr r0",
|
|
61 " bcl 20,31,"L00000000001$spb"",
|
|
62 " "L00000000001$spb":",
|
|
63 " mflr r11",
|
|
64 " addis r11,r11,ha16(L_printf$lazy_ptr-"L00000000001$spb")",
|
|
65 " mtlr r0",
|
|
66 " lwzu r12,lo16(L_printf$lazy_ptr-"L00000000001$spb")(r11)",
|
|
67 " mtctr r12",
|
|
68 " bctr",
|
|
69 " .lazy_symbol_pointer",
|
|
70 " L_printf$lazy_ptr:",
|
|
71 " .indirect_symbol _printf",
|
|
72 " .long dyld_stub_binding_helper",
|
|
73 " .subsections_via_symbols",
|
|
74 "\t.align 2\n",
|
|
75 ".comm _variable,192\n",
|
|
76 NULL
|
|
77 };
|
|
78
|
|
79 void
|
|
80 emit_push()
|
|
81 {
|
|
82 printf("\tstwu r3,-4(r1)\n");
|
|
83 }
|
|
84
|
|
85 void
|
|
86 emit_compare()
|
|
87 {
|
|
88 printf("\tlwz r4,(r1)\n");
|
|
89 printf("\taddis r1,r1,4\n");
|
|
90 cmpw cr7,r3,r4
|
|
91 mfcr r3
|
|
92 rlwinm r3,r3,30,1
|
|
93
|
|
94 }
|
|
95
|
|
96 void
|
|
97 emit_store(assign)
|
|
98 int assign;
|
|
99 {
|
|
100 slwi r3,r3,2
|
|
101 addis r2,r10,ha16(L_variable$non_lazy_ptr-"L00000000002$pb")
|
|
102 lwz r2,lo16(L_variable$non_lazy_ptr-"L00000000002$pb")(r2)
|
|
103 stwx r4,r3,r2
|
|
104
|
|
105 }
|
|
106
|
|
107 static
|
|
108 char *opcode[] = {
|
|
109 "",
|
|
110 "sub",
|
|
111 "add",
|
|
112 "mul",
|
|
113 "div",
|
|
114 "",
|
|
115 "",
|
|
116 "",
|
|
117 "",
|
|
118 "sub",
|
|
119 "div",
|
|
120 };
|
|
121
|
|
122 void
|
|
123 emit_calc(enum opcode op)
|
|
124 {
|
|
125 printf("\tlwz r4,(r1)\n");
|
|
126 printf("\taddis r1,r1,4\n");
|
|
127 if(op==O_DIV) {
|
|
128 printf("\tdiv r4,r3,r3\n");
|
|
129 } else if(op==O_SUB) {
|
|
130 printf("\tsub r4,r3,r3\n");
|
|
131 } else {
|
|
132 printf("\t%s r4,r3,r3\n",opcode[op]);
|
|
133 }
|
|
134 }
|
|
135
|
|
136 void
|
|
137 emit_value(d)
|
|
138 int d;
|
|
139 {
|
|
140 printf("\tmovl $%d,%%eax\n",d);
|
|
141 }
|
|
142
|
|
143 void
|
|
144 emit_load(d)
|
|
145 int d;
|
|
146 {
|
|
147 printf("\tmovl _variable+%d,%%eax\n",d*4);
|
|
148 }
|
|
149
|
|
150 void
|
|
151 emit_comment()
|
|
152 {
|
|
153 if (before < ptr) {
|
|
154 putchar('#'); putchar('#'); putchar(' ');
|
|
155 while (before < ptr) {
|
|
156 if(*before && *before!='\n') {
|
|
157 putchar(*before);
|
|
158 }
|
|
159 before++;
|
|
160 }
|
|
161 putchar('\n');
|
|
162 }
|
|
163 }
|
|
164
|
|
165 void
|
|
166 emit_print()
|
|
167 {
|
|
168 printf("\tbl _print\n");
|
|
169 }
|
|
170
|
|
171
|
|
172 void
|
|
173 emit_intro()
|
|
174 {
|
|
175 char **iptr;
|
|
176 for(iptr=intro;*iptr;iptr++) {
|
|
177 printf("%s",*iptr);
|
|
178 }
|
|
179 }
|
|
180
|
|
181 void
|
|
182 emit_ending()
|
|
183 {
|
|
184 char **iptr;
|
|
185 for(iptr=ending;*iptr;iptr++) {
|
|
186 printf("%s",*iptr);
|
|
187 }
|
|
188 }
|
|
189
|
|
190 /* end */
|