annotate s-code-ppc.c @ 17:e5c8532ab38d

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