forked from oriansj/mescc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex2_word.c
365 lines (341 loc) · 8.69 KB
/
hex2_word.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
/* Copyright (C) 2017 Jeremiah Orians
* Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org>
* This file is part of mescc-tools
*
* mescc-tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mescc-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hex2_globals.h"
unsigned shiftregister;
unsigned tempword;
int updates;
void outOfRange(char* s, int value)
{
line_error();
fputs("error: value ", stderr);
fputs(int2str(value, 10, TRUE), stderr);
fputs(" out of range for field type ", stderr);
fputs(s, stderr);
fputs("\n", stderr);
exit(EXIT_FAILURE);
}
void UpdateShiftRegister(char ch, int value)
{
if ('.' == ch)
{
unsigned swap;
/* Assume the user knows what they are doing */
if(!BigEndian)
{
/* Swap from big-endian to little endian order */
swap = (((value >> 24) & 0xFF) |
((value << 8) & 0xFF0000) |
((value >> 8) & 0xFF00) |
((value << 24) & 0xFF000000));
}
else
{
/* Big endian needs no change */
swap = value;
}
/* we just take the 4 bytes after the . and shove in the shift register */
shiftregister = shiftregister ^ swap;
}
else if ('!' == ch)
{
/* Corresponds to RISC-V I format */
/* Will need architecture specific logic if more architectures go this route */
/* no range check because it needs to work with labels for lui/addi + AUIPC combos */
/* !label is used in the second instruction of AUIPC combo but we want an offset from */
/* the first instruction */
value = value + 4;
tempword = (value & 0xfff) << 20;
/* Update shift register */
shiftregister = shiftregister ^ tempword;
}
else if ('@' == ch)
{
/* Corresponds to RISC-V B format (formerly known as SB) */
/* Will need architecture specific logic if more architectures go this route */
if ((value < -0x1000 || value > 0xfff) || (value & 1)) outOfRange("B", value);
/* Prepare the immediate's word */
tempword = ((value & 0x1e) << 7)
| ((value & 0x7e0) << (31 - 11))
| ((value & 0x800) >> 4)
| ((value & 0x1000) << (31 - 12));
/* Update shift register */
shiftregister = shiftregister ^ tempword;
}
else if ('$' == ch)
{
/* Corresponds with RISC-V J format (formerly known as UJ) */
/* Will need architecture specific logic if more architectures go this route */
if ((value < -0x100000 || value > 0xfffff) || (value & 1)) outOfRange("J", value);
tempword = ((value & 0x7fe) << (30 - 10))
| ((value & 0x800) << (20 - 11))
| ((value & 0xff000))
| ((value & 0x100000) << (31 - 20));
shiftregister = shiftregister ^ tempword;
}
else if ('~' == ch)
{
/* Corresponds with RISC-V U format */
/* Will need architecture specific logic if more architectures go this route */
if ((value & 0xfff) < 0x800) tempword = value & 0xfffff000;
else tempword = (value & 0xfffff000) + 0x1000;
shiftregister = shiftregister ^ tempword;
}
else
{
line_error();
fputs("error: UpdateShiftRegister reached impossible case: ch=", stderr);
fputc(ch, stderr);
fputs("\n", stderr);
exit(EXIT_FAILURE);
}
}
void WordStorePointer(char ch, FILE* source_file)
{
/* Get string of pointer */
ip = ip + 4;
Clear_Scratch(scratch);
int base_sep_p = consume_token(source_file);
/* Lookup token */
int target = GetTarget(scratch);
int displacement;
int base = ip;
/* Change relative base address to :<base> */
if ('>' == base_sep_p)
{
Clear_Scratch(scratch);
consume_token (source_file);
base = GetTarget (scratch);
/* Force universality of behavior */
displacement = (target - base);
}
else
{
displacement = Architectural_displacement(target, base);
}
/* output calculated difference */
if('&' == ch) outputPointer(target, 4); /* Deal with & */
else if('%' == ch) outputPointer(displacement, 4); /* Deal with % */
else
{
line_error();
fputs("error: WordStorePointer reached impossible case: ch=", stderr);
fputc(ch, stderr);
fputs("\n", stderr);
exit(EXIT_FAILURE);
}
}
unsigned sr_nextb()
{
unsigned rv = shiftregister & 0xff;
shiftregister = shiftregister >> 8;
return rv;
}
void DoByte(char c, FILE* source_file, int write, int update)
{
if(HEX == ByteMode)
{
if(0 <= hex(c, source_file))
{
if(toggle)
{
if(write) fputc(((hold * 16)) + hex(c, source_file) ^ sr_nextb(), output);
ip = ip + 1;
if(update)
{
hold = (hold * 16) + hex(c, source_file);
tempword = (tempword << 8) ^ hold;
updates = updates + 1;
}
hold = 0;
}
else
{
hold = hex(c, source_file);
}
toggle = !toggle;
}
}
else if(OCTAL ==ByteMode)
{
if(0 <= octal(c, source_file))
{
if(2 == toggle)
{
if(write) fputc(((hold * 8)) + octal(c, source_file) ^ sr_nextb(), output);
ip = ip + 1;
if(update)
{
hold = ((hold * 8) + octal(c, source_file));
tempword = (tempword << 8) ^ hold;
updates = updates + 1;
}
hold = 0;
toggle = 0;
}
else if(1 == toggle)
{
hold = ((hold * 8) + octal(c, source_file));
toggle = 2;
}
else
{
hold = octal(c, source_file);
toggle = 1;
}
}
}
else if(BINARY == ByteMode)
{
if(0 <= binary(c, source_file))
{
if(7 == toggle)
{
if(write) fputc((hold * 2) + binary(c, source_file) ^ sr_nextb(), output);
ip = ip + 1;
if(update)
{
hold = ((hold * 2) + binary(c, source_file));
tempword = (tempword << 8) ^ hold;
updates = updates + 1;
}
hold = 0;
toggle = 0;
}
else
{
hold = ((hold * 2) + binary(c, source_file));
toggle = toggle + 1;
}
}
}
}
void WordFirstPass(struct input_files* input)
{
if(NULL == input) return;
WordFirstPass(input->next);
filename = input->filename;
linenumber = 1;
FILE* source_file = fopen(filename, "r");
if(NULL == source_file)
{
fputs("The file: ", stderr);
fputs(input->filename, stderr);
fputs(" can not be opened!\n", stderr);
exit(EXIT_FAILURE);
}
toggle = FALSE;
int c;
for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
{
/* Check for and deal with label */
if(':' == c)
{
c = storeLabel(source_file, ip);
}
/* check for and deal with relative/absolute pointers to labels */
if('.' == c)
{
/* Read architecture specific number of bytes for what is defined as a word */
/* 4bytes in RISC-V's case */
updates = 0;
tempword = 0;
while (updates < 4)
{
c = fgetc(source_file);
DoByte(c, source_file, FALSE, TRUE);
}
ip = ip - 4;
}
else if(in_set(c, "!@$~"))
{
/* Don't update IP */
c = Throwaway_token(source_file);
}
else if(in_set(c, "%&"))
{
ip = ip + 4;
c = Throwaway_token(source_file);
if ('>' == c)
{ /* deal with label>base */
c = Throwaway_token(source_file);
}
}
else if('<' == c)
{
pad_to_align(FALSE);
}
else if('^' == c)
{
/* Just ignore */
continue;
}
else DoByte(c, source_file, FALSE, FALSE);
}
fclose(source_file);
}
void WordSecondPass(struct input_files* input)
{
shiftregister = 0;
tempword = 0;
if(NULL == input) return;
WordSecondPass(input->next);
filename = input->filename;
linenumber = 1;
FILE* source_file = fopen(filename, "r");
/* Something that should never happen */
if(NULL == source_file)
{
fputs("The file: ", stderr);
fputs(input->filename, stderr);
fputs(" can not be opened!\nWTF-pass2\n", stderr);
exit(EXIT_FAILURE);
}
toggle = FALSE;
hold = 0;
int c;
for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
{
if(':' == c) c = Throwaway_token(source_file); /* Deal with : */
else if('.' == c)
{
/* Read architecture specific number of bytes for what is defined as a word */
/* 4bytes in RISC-V's case */
updates = 0;
tempword = 0;
while (updates < 4)
{
c = fgetc(source_file);
DoByte(c, source_file, FALSE, TRUE);
}
UpdateShiftRegister('.', tempword);
ip = ip - 4;
}
else if(in_set(c, "%&")) WordStorePointer(c, source_file); /* Deal with % and & */
else if(in_set(c, "!@$~"))
{
Clear_Scratch(scratch);
consume_token(source_file);
UpdateShiftRegister(c, Architectural_displacement(GetTarget(scratch), ip)); /* Play with shift register */
}
else if('<' == c) pad_to_align(TRUE);
else if('^' == c) ALIGNED = TRUE;
else DoByte(c, source_file, TRUE, FALSE);
}
fclose(source_file);
}