-
Notifications
You must be signed in to change notification settings - Fork 15
/
emitStuff.c
555 lines (482 loc) · 15.6 KB
/
emitStuff.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* Copyright (c) 1987 Fujitsu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
emitStuff.c -- Routines to actually generate binary stuff for the
Macross assembler.
Chip Morningstar -- Lucasfilm Ltd.
14-November-1984
*/
#include "macrossTypes.h"
#include "macrossGlobals.h"
#include "actions.h"
#include "debugPrint.h"
#include "errorStuff.h"
#include "semanticMisc.h"
/*
Emitted code is stored in places that are allocated dynamically as they
are needed. This saves us from having to keep umpteenK of code buffer
around ALL the time and also lets us keep track of where code has actually
been put for purposes of generating the final object module.
The structure of the code storage is: there are two code region structs
(type 'codeRegionType'), one each for absolute and relocatable code. These
are stored in the global array 'codeRegions' which is indexed by
'currentCodeMode' which in turn indicates whether we are emitting aboslute
or relocatable code. Each of these region structs contains a vector of
[currently 64] pointers to code segment structs (type 'codeSegmentType').
Each code segment represents a [currently 1Kbyte] piece of the target
processor address space; it contains the high and low addresses within
that space that have been occupied up until now and a pointer to a
[1Kbyte] buffer that actually contains the code.
Initially, the code regions are filled with vectors of null pointers and
there are no code segments allocated. As code is emitted, the space for
the code segments and their associated buffers is allocated using 'malloc'.
Only the actual buffers needed are allocated, resulting in substantial
storage savings.
A complication that can arise from Macross 'struct's. Structs are
assembled in a small scratch buffer and then transferred to the main code
buffers as needed.
*/
/* incarnateCodeBuffer causes code buffer space to actually be allocated */
void
incarnateCodeBuffer(int bufferNum, codeBufferKindType bufferKind)
{
codeSegmentType *newCodeSegment;
codeBufferType *newCodeBuffer;
int i;
newCodeSegment = typeAlloc(codeSegmentType);
newCodeBuffer = typeAlloc(codeBufferType);
for (i=0; i<CODE_BUFFER_SIZE; i++)
(*newCodeBuffer)[i] = 0;
newCodeSegment->codeStartAddress = 0xFFFF + 1;
newCodeSegment->codeEndAddress = -1;
newCodeSegment->codeBuffer = newCodeBuffer;
codeRegions[(int)bufferKind]->codeSegments[bufferNum] =
newCodeSegment;
}
/* putByte actually puts a byte in code storage somewhere, given the address
and the byte itself. It tracks down the appropriate code buffer, taking
care to make sure said buffer actually exists before using it. */
void
putByte(addressType address, byte byteValue)
{
int bufferNum;
int bufferPos;
codeBufferType *theBuffer;
codeSegmentType *theSegment;
bufferNum = bufferNumber(address);
bufferPos = bufferPosition(address);
if (bufferNum >= CODE_BUFFERS_IN_ADDRESS_SPACE) {
fatalError(ADDRESS_OUTSIDE_ADDRESS_SPACE_ERROR, address);
return;
}
theSegment = codeRegions[(int)currentCodeMode]->codeSegments[
bufferNum];
if (theSegment == NULL) {
incarnateCodeBuffer(bufferNum, currentCodeMode);
theSegment = codeRegions[(int)currentCodeMode]->codeSegments[
bufferNum];
}
theBuffer = theSegment->codeBuffer;
if (currentCodeMode == RELOCATABLE_BUFFER && address >
relocatableHighWaterMark)
relocatableHighWaterMark = address;
if (address > theSegment->codeEndAddress)
theSegment->codeEndAddress = address;
if (address < theSegment->codeStartAddress)
theSegment->codeStartAddress = address;
if (address > codeRegions[(int)currentCodeMode]->regionEndAddress)
codeRegions[(int)currentCodeMode]->regionEndAddress = address;
if (address < codeRegions[(int)currentCodeMode]->regionStartAddress)
codeRegions[(int)currentCodeMode]->regionStartAddress =
address;
(*theBuffer)[bufferPos] = byteValue;
}
/* mapByte is like 'putByte', but places its values in the struct assembly
buffer */
void
mapByte(int address, byte byteValue)
{
if (address < MAXIMUM_ALLOWED_STRUCT_SIZE)
structScratchBuffer[address] = byteValue;
}
/* emitByte outputs one byte at the current location in either the current
code buffer or the current struct assembly buffer */
void
emitByte(byte byteValue)
{
if (debug || emitPrint)
if (structNestingDepth == 0)
printf("emitByte(%x: %x)\n", currentLocationCounter.
value, byteValue);
else
printf("emitByte in struct (%x: %x)\n",
currentFieldOffset, byteValue);
if (structNestingDepth == 0) {
putByte(currentLocationCounter.value++, byteValue);
} else {
mapByte(currentFieldOffset++, byteValue);
}
}
/* emitWord similarly emits a word */
void
emitWord(wordType wordValue)
{
byteToWordType convert;
int loByte, hiByte;
/* We hack around with this, even though it's less portable, so that we can
avoid doing a division and a modulo on every word we emit (since code
emission is in the inner loop). */
#ifdef BYTESWAPPED
loByte = 1;
hiByte = 0;
#else
loByte = 0;
hiByte = 1;
#endif
convert.wordPart = wordValue;
if (debug || emitPrint)
if (structNestingDepth == 0)
printf("emitWord(%x: %x)\n", currentLocationCounter.
value, wordValue);
else
printf("emitWord in struct (%x: %x)\n",
currentFieldOffset, wordValue);
if (structNestingDepth == 0) {
#if TARGET_CPU == CPU_6502
putByte(currentLocationCounter.value++,
convert.bytePart[loByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[hiByte]);
#elif TARGET_CPU == CPU_68000
putByte(currentLocationCounter.value++,
convert.bytePart[hiByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[loByte]);
#endif
} else {
#if TARGET_CPU == CPU_6502
mapByte(currentFieldOffset++, convert.bytePart[loByte]);
mapByte(currentFieldOffset++, convert.bytePart[hiByte]);
#elif TARGET_CPU == CPU_68000
mapByte(currentFieldOffset++, convert.bytePart[hiByte]);
mapByte(currentFieldOffset++, convert.bytePart[loByte]);
#endif
}
}
/* emitLong similarly emits a long */
void
emitLong(longType longValue)
{
byteToLongType convert;
int loByte, secondByte, thirdByte, hiByte;
/* We hack around with this, even though it's less portable, so that we can
avoid doing a division and a modulo on every long we emit (since code
emission is in the inner loop). */
#ifdef BYTESWAPPED
/* Sun workstation... */
loByte = 3;
secondByte = 2;
thirdByte = 1;
hiByte = 0;
#else
/* Vax... */
loByte = 0;
secondByte = 1;
thirdByte = 2;
hiByte = 3;
#endif
convert.longPart = longValue;
if (debug || emitPrint)
if (structNestingDepth == 0)
printf("emitLong(%x: %x)\n", currentLocationCounter.
value, longValue);
else
printf("emitLong in struct (%x: %x)\n",
currentFieldOffset, longValue);
if (structNestingDepth == 0) {
#if TARGET_CPU == CPU_6502
putByte(currentLocationCounter.value++,
convert.bytePart[loByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[secondByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[thirdByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[hiByte]);
#elif TARGET_CPU == CPU_68000
putByte(currentLocationCounter.value++,
convert.bytePart[hiByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[thirdByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[secondByte]);
putByte(currentLocationCounter.value++,
convert.bytePart[loByte]);
#endif
} else {
#if TARGET_CPU == CPU_6502
mapByte(currentFieldOffset++, convert.bytePart[loByte]);
mapByte(currentFieldOffset++, convert.bytePart[secondByte]);
mapByte(currentFieldOffset++, convert.bytePart[thirdByte]);
mapByte(currentFieldOffset++, convert.bytePart[hiByte]);
#elif TARGET_CPU == CPU_68000
mapByte(currentFieldOffset++, convert.bytePart[hiByte]);
mapByte(currentFieldOffset++, convert.bytePart[thirdByte]);
mapByte(currentFieldOffset++, convert.bytePart[secondByte]);
mapByte(currentFieldOffset++, convert.bytePart[loByte]);
#endif
}
}
/* emitByteValue takes the byte to be emitted out of a 'valueType' */
void
emitByteValue(valueType *byteValue)
{
if (byteValue->kindOfValue == ABSOLUTE_VALUE || byteValue->kindOfValue ==
RELOCATABLE_VALUE || byteValue->kindOfValue == UNDEFINED_VALUE) {
if (byteCheck(byteValue->value) && byteValue->kindOfValue!=FAIL) {
if (debug || emitPrint) {
if (structNestingDepth == 0)
printf("emitByteValue(%x: ",currentLocationCounter.value);
else
printf("emitByteValue in struct (%x:",currentFieldOffset);
printValue(byteValue);
printf(")\n");
}
emitByte(byteValue->value);
}
} else {
if (byteValue->kindOfValue != FAIL)
error(NON_ADDRESS_BYTE_VALUE_ERROR, valueKindString(byteValue->
kindOfValue));
}
}
/* emitString similarly spits out a string of bytes */
void
emitString(stringType *string)
{
if (debug || emitPrint)
if (structNestingDepth == 0)
printf("emitString(%x: \"%s\")\n",
currentLocationCounter.value, string);
else
printf("emitString in struct(%x: \"%s\")\n",
currentFieldOffset, string);
/* Horrible terrible no good very bad cretinous ugly hack, but no graceful
way to avoid it: a 0xFF byte in the string means output a 0x00 byte in
the object. This is so we can embed nuls in strings without embedding
nuls in strings, so to speak. We assume that the character 0xFF is not
likely to be needed since ASCII (and ATASCII) is a seven bit character
code. */
while (*string != 0)
if ((*string & 0xFF) == 0xFF) {
emitByte('\0');
string++;
} else {
emitByte(*string++);
}
}
/* emitWordValue emits a word out of a 'valueType' */
void
emitWordValue(valueType *wordValue)
{
if (wordValue->kindOfValue == ABSOLUTE_VALUE || wordValue->kindOfValue ==
RELOCATABLE_VALUE || wordValue->kindOfValue == UNDEFINED_VALUE) {
if (wordCheck(wordValue->value) && wordValue->kindOfValue!=FAIL) {
if (debug || emitPrint) {
if (structNestingDepth == 0)
printf("emitWordValue(%x: ",currentLocationCounter.value);
else
printf("emitWordValue in struct (%x:",currentFieldOffset);
printValue(wordValue);
printf(")\n");
}
emitWord(wordValue->value);
}
} else {
if (wordValue->kindOfValue != FAIL)
error(NON_ADDRESS_WORD_VALUE_ERROR, valueKindString(wordValue->
kindOfValue));
}
}
/* emitLongValue emits a long out of a 'valueType' */
void
emitLongValue(valueType *longValue)
{
if (longValue->kindOfValue == ABSOLUTE_VALUE || longValue->kindOfValue ==
RELOCATABLE_VALUE || longValue->kindOfValue == UNDEFINED_VALUE) {
if (longValue->kindOfValue != FAIL) {
if (debug || emitPrint) {
if (structNestingDepth == 0)
printf("emitLongValue(%x: ",currentLocationCounter.value);
else
printf("emitLongValue in struct (%x:",currentFieldOffset);
printValue(longValue);
printf(")\n");
}
emitLong(longValue->value);
}
} else {
if (longValue->kindOfValue != FAIL)
error(NON_ADDRESS_LONG_VALUE_ERROR, valueKindString(longValue->
kindOfValue));
}
}
/* pokeByteValue is like 'emitByte' but it's random access */
void
pokeByteValue(addressType location, valueType *value)
{
currentLocationCounter.value = location;
emitByteValue(value);
}
/* ditto pokeWordValue */
void
pokeWordValue(addressType location, valueType *value)
{
currentLocationCounter.value = location;
emitWordValue(value);
}
/* ditto pokeLongValue */
void
pokeLongValue(addressType location, valueType *value)
{
currentLocationCounter.value = location;
emitLongValue(value);
}
/* ditto pokeRelativeByteValue. This is a special case used in fixing up
relative branches */
void
pokeRelativeByteValue(addressType location, valueType *value)
{
int offset;
currentLocationCounter.value = location;
offset = value->value - (location - targetOffset) - 1;
if (offset < 0)
offset--;
/* if (currentCodeMode == RELOCATABLE_BUFFER)
offset = 0;*/
if (isByteOffset(offset)) {
emitByte(offset);
} else {
error(RELATIVE_OFFSET_TOO_LARGE_ERROR);
}
}
/* ditto pokeRelativeWordValue. This is a special case used in fixing up
relative branches */
void
pokeRelativeWordValue(addressType location, valueType *value)
{
int offset;
currentLocationCounter.value = location;
offset = value->value - (location - targetOffset);
if (isWordOffset(offset)) {
emitWord(offset);
} else {
error(RELATIVE_OFFSET_TOO_LARGE_ERROR);
}
}
/* getByte fetches a byte back out of the labyrinth of code buffers */
byte
getByte(addressType address)
{
int bufferNum;
int bufferPos;
codeBufferType *theBuffer;
codeSegmentType *theSegment;
bufferNum = bufferNumber(address);
bufferPos = bufferPosition(address);
theSegment = codeRegions[(int)currentCodeMode]->codeSegments[
bufferNum];
if (theSegment == NULL)
return(0);
else
return((*(theSegment->codeBuffer))[bufferPos]);
}
void
emitRelativeByteOffset(valueType *target)
{
int saveTargetOffset;
if (target == NULL) {
emitByte(0);
} else {
(target->value)++;
saveTargetOffset = targetOffset;
targetOffset = 0;
pokeRelativeByteValue(currentLocationCounter.value, target);
targetOffset = saveTargetOffset;
(target->value)--;
}
}
void
emitRelativeWordOffset(valueType *target)
{
int saveTargetOffset;
if (target == NULL) {
emitWord(0);
} else {
saveTargetOffset = targetOffset;
targetOffset = 0;
pokeRelativeWordValue(currentLocationCounter.value, target);
targetOffset = saveTargetOffset;
}
}
/* fixupBranch repairs a previously undefined branch once the branch address
has become known. */
void
fixupBranch(valueType *location, valueType target)
{
valueType saveCurrentLocation;
int saveTargetOffset;
int i;
saveCurrentLocation = currentLocationCounter;
saveTargetOffset = targetOffset;
targetOffset = 0;
for (i=0; i<COMPOUND_BRANCH_MAX; i++) {
if (location[i].value >= 0)
pokeRelativeByteValue(location[i].value, &target);
}
targetOffset = saveTargetOffset;
currentLocationCounter = saveCurrentLocation;
}
/* fixupJump similarly repairs a jump */
void
fixupJump(simpleFixupListType *locations, valueType target)
{
valueType saveCurrentLocation;
simpleFixupListType *oldLocation;
saveCurrentLocation = currentLocationCounter;
while (locations != NULL) {
currentLocationCounter = locations->locationToFixup;
noteAnonymousReference();
target.value -= targetOffset;
if (positionIndependentCodeMode)
pokeRelativeByteValue(locations->locationToFixup.
value, &target);
else
pokeWordValue(locations->locationToFixup.value,
&target);
target.value += targetOffset;
oldLocation = locations;
locations = locations->nextFixup;
free(oldLocation);
}
currentLocationCounter = saveCurrentLocation;
}