annotate lwasm/os9.c @ 365:6a98cc90c14f

Added resolve passes
author lost@starbug
date Wed, 14 Apr 2010 20:49:04 -0600
parents 60568b123281
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 os9.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 This file implements the various pseudo operations related to OS9 target
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24 #include <errno.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <string.h>
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
28
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
29 #include <lw_expr.h>
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
30
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 #include "lwasm.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 #include "instab.h"
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
33
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
35 // OS9 syscall
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
36 PARSEFUNC(pseudo_parse_os9)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
38 lw_expr_t e;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
40 if (as -> output_format != OUTPUT_OS9)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
42 lwasm_register_error(as, l, "os9 directive only valid for OS9 target");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 // fetch immediate value
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
47 e = lwasm_parse_expr(as, p);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
48 if (!e)
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
49 {
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
50 lwasm_register_error(as, l, "Bad operand");
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
51 return;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
52 }
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
53 lwasm_save_expr(l, 0, e);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
54 l -> len = 3;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
57 EMITFUNC(pseudo_emit_os9)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
59 lw_expr_t e;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
61 e = lwasm_fetch_expr(l, 0);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
62
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
63 lwasm_emitop(l, 0x103f);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
64 lwasm_emitexpr(l, e, 1);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
65 }
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
66
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
67 PARSEFUNC(pseudo_parse_mod)
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
68 {
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
69 lw_expr_t e;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
70 int i;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
71
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
72 if (as -> output_format != OUTPUT_OS9)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
74 lwasm_register_error(as, l, "mod directive only valid for OS9 target");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 if (as -> inmod)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
80 lwasm_register_error(as, l, "Already in a module!");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 // parse 6 expressions...
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 for (i = 0; i < 5; i++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
87 e = lwasm_parse_expr(as, p);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
88 if (!e)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
90 lwasm_register_error(as, l, "Bad operand");
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
91 return;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 }
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
93
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
94 lwasm_save_expr(l, i, e);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
95
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 if (**p != ',')
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
98 lwasm_register_error(as, l, "Bad operand");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 (*p)++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 }
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
103
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
104 e = lwasm_parse_expr(as, p);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
105 if (!e)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
107 lwasm_register_error(as, l, "Bad operand");
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
108 return;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 }
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
110 lwasm_save_expr(l, 5, e);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 l -> inmod = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 // we have an implicit ORG 0 with "mod"
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
115 lw_expr_destroy(l -> addr);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
116 l -> addr = lw_expr_build(lw_expr_type_int, 0);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 // init crc
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
119 as -> inmod = 1;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
120 }
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
121
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
122 EMITFUNC(pseudo_emit_mod)
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
123 {
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
124 lw_expr_t e1, e2, e3, e4;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
125 int csum;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
126
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 as -> crc[0] = 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 as -> crc[1] = 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 as -> crc[2] = 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 // sync bytes
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
132 lwasm_emit(l, 0x87);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
133 lwasm_emit(l, 0xcd);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135 // mod length
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
136 lwasm_emitexpr(l, e1 = lwasm_fetch_expr(l, 0), 2);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 // name offset
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
139 lwasm_emitexpr(l, e2 = lwasm_fetch_expr(l, 1), 2);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 // type
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
142 lwasm_emitexpr(l, e3 = lwasm_fetch_expr(l, 2), 1);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 // flags/rev
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
145 lwasm_emitexpr(l, e4 = lwasm_fetch_expr(l, 3), 1);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 // header check
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
148 csum = ~(0x87 ^ 0xCD ^(lw_expr_intval(e1) >> 8) ^ (lw_expr_intval(e1) & 0xff)
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
149 ^ (lw_expr_intval(e2) >> 8) ^ (lw_expr_intval(e2) & 0xff)
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
150 ^ lw_expr_intval(e3) ^ lw_expr_intval(e4));
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
151 lwasm_emit(l, csum);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 // module type specific output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 // note that these are handled the same for all so
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 // there need not be any special casing
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 // exec offset or fmgr name offset
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
158 lwasm_emitexpr(l, lwasm_fetch_expr(l, 4), 2);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160 // data size or drvr name offset
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
161 lwasm_emitexpr(l, lwasm_fetch_expr(l, 5), 2);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
164 PARSEFUNC(pseudo_parse_emod)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
166 if (as -> output_format != OUTPUT_OS9)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
167 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
168 lwasm_register_error(as, l, "emod directive only valid for OS9 target");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
171
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172 if (!(as -> inmod))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
173 {
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
174 lwasm_register_error(as, l, "not in a module!");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
175 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
177
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
178 as -> inmod = 0;
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
179 }
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
180
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
181 EMITFUNC(pseudo_emit_emod)
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
182 {
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
183 unsigned char tcrc[3];
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
184
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
185 // don't mess with CRC!
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
186 tcrc[0] = as -> crc[0] ^ 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
187 tcrc[1] = as -> crc[1] ^ 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
188 tcrc[2] = as -> crc[2] ^ 0xff;
354
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
189 lwasm_emit(l, tcrc[0]);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
190 lwasm_emit(l, tcrc[1]);
60568b123281 Added os9 opcodes and ERROR
lost@starbug
parents: 339
diff changeset
191 lwasm_emit(l, tcrc[2]);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192 }