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