-
Notifications
You must be signed in to change notification settings - Fork 15
/
encode.c
664 lines (585 loc) · 16.1 KB
/
encode.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*
* 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.
*/
/* WARNING: this file has not yet been separated into 6502 and non-6052 parts */
/*
encode.c -- Routines to encode expressions for Macross object files.
Chip Morningstar -- Lucasfilm Ltd.
8-November-1985
*/
#include "macrossTypes.h"
#include "macrossGlobals.h"
#include "y.tab.h"
#include "encode.h"
#include "debugPrint.h"
#include "errorStuff.h"
#include "parserMisc.h"
#include "semanticMisc.h"
#include "slinkyExpressions.h"
#define nullEncode(thing) if (thing==NULL) return(TRUE);
#define byteOp(op) ((op)-256)
bool encodingFunction;
bool
encodeByte(byte aByte)
{
if (expressionBufferSize < EXPRESSION_BUFFER_LIMIT) {
expressionBuffer[expressionBufferSize++] = aByte;
return(TRUE);
} else {
error(EXPRESSION_TOO_BIG_TO_FIT_IN_OBJECT_ERROR);
return(FALSE);
}
}
bool
encodeBigword(int bigword)
{
int i;
for (i=0; i<sizeof(int); ++i) {
if (!encodeByte(bigword & 0xFF))
return(FALSE);
bigword >>= 8;
}
return(TRUE);
}
bool
encodeAssignmentTerm(binopTermType *assignmentTerm)
{
nullEncode(assignmentTerm);
if ((assignmentKindType)assignmentTerm->binop != ASSIGN_ASSIGN) {
error(FUNNY_ASSIGNMENT_KIND_IN_OBJECT_EXPRESSION_ERROR);
return(FALSE);
}
return(
encodeByte(BINOP_TAG) &&
encodeByte(byteOp(ASSIGN)) &&
encodeIdentifier(assignmentTerm->leftArgument) &&
encodeExpression(assignmentTerm->rightArgument)
);
}
bool
encodeBinopTerm(binopTermType *binopTerm)
{
nullEncode(binopTerm);
return (
encodeByte(BINOP_TAG) &&
encodeByte(byteOp(binopTerm->binop)) &&
encodeExpression(binopTerm->leftArgument) &&
encodeExpression(binopTerm->rightArgument)
);
}
bool
encodeCondition(conditionType condition)
{
return(
encodeByte(CONDITION_CODE_TAG) &&
encodeByte(condition)
);
}
int
functionNumber(functionDefinitionType *function)
{
if (function->ordinal == -1) {
function->ordinal = externalFunctionCount++;
if (externalFunctionList == NULL) {
externalFunctionList = endOfExternalFunctionList =
function;
} else {
endOfExternalFunctionList->nextExternalFunction =
function;
endOfExternalFunctionList = function;
}
}
return(function->ordinal);
}
bool
encodeFunctionCall(functionCallTermType *functionCall)
{
functionDefinitionType *theFunction;
int functionOrdinal;
symbolInContextType *workingContext;
operandListType *parameterList;
nullEncode(functionCall);
workingContext = getWorkingContext(functionCall->functionName);
if (isFunction(workingContext)) {
if (!encodeByte(FUNCTION_CALL_TAG))
return(FALSE);
theFunction = (functionDefinitionType *)workingContext->
value->value;
if (!encodeBigword(functionNumber(theFunction)))
return(FALSE);
} else if (isBuiltInFunction(workingContext)) {
functionOrdinal = workingContext->value->value;
if (builtInFunctionTable[functionOrdinal].isSpecialFunction)
return(encodeValue((*builtInFunctionTable[
functionOrdinal].functionEntry)(functionCall->
parameters, NO_FIXUP)));
if (!encodeByte(BUILTIN_FUNCTION_CALL_TAG))
return(FALSE);
if (builtInFunctionTable[functionOrdinal].ordinal < 0) {
error(BUILT_IN_FUNCTION_NOT_AVAILABLE_IN_OBJECT_ERROR,
builtInFunctionTable[functionOrdinal].
functionName);
return(FALSE);
} else if (!encodeBigword(builtInFunctionTable[
functionOrdinal].ordinal)) {
return(FALSE);
}
} else {
error(NOT_A_FUNCTION_ERROR, symbName(functionCall->
functionName));
return(FALSE);
}
parameterList = functionCall->parameters;
if (!encodeByte(countParameters(parameterList)))
return(FALSE);
while (parameterList != NULL)
if (!encodeOperand(parameterList))
return(FALSE);
else
parameterList = parameterList->nextOperand;
return(TRUE);
}
bool
encodeHere(void)
{
return(encodeByte(HERE_TAG));
}
bool
encodeIdentifier(symbolTableEntryType *identifier)
{
symbolInContextType *workingContext;
environmentType *saveEnvironment;
bool result;
nullEncode(identifier);
if (symbName(identifier)[0] == '$') {
error(TEMP_SYMBOL_IN_OBJECT_ERROR, symbName(identifier));
return(FALSE);
}
if (encodingFunction) {
return(encodeByte(IDENTIFIER_TAG) &&
encodeBigword(identifier->ordinal));
}
if ((workingContext = getWorkingContext(identifier)) == NULL) {
error(UNDEFINED_SYMBOL_ERROR, symbName(identifier));
return(FALSE);
}
if (workingContext->usage == FUNCTION_SYMBOL || workingContext->usage
== BUILT_IN_FUNCTION_SYMBOL) {
error(FUNCTION_IS_NOT_A_VALUE_ERROR, symbName(identifier));
return(FALSE);
}
if (workingContext->value == NULL) {
error(UNDEFINED_SYMBOL_ERROR, symbName(identifier));
return(FALSE);
}
if (workingContext->value->kindOfValue == UNDEFINED_VALUE) {
if (workingContext->attributes & GLOBAL_ATT) {
return(encodeByte(IDENTIFIER_TAG) &&
encodeBigword(identifier->ordinal));
} else {
error(UNDEFINED_SYMBOL_ERROR, symbName(identifier));
return(FALSE);
}
}
if (workingContext->value->kindOfValue == RELOCATABLE_VALUE) {
return(encodeByte(IDENTIFIER_TAG) &&
encodeBigword(identifier->ordinal));
}
if (workingContext->value->kindOfValue == FAIL) {
error(UNASSIGNED_SYMBOL_ERROR, symbName(identifier));
return(FALSE);
}
if (workingContext->value->kindOfValue == OPERAND_VALUE) {
saveEnvironment = currentEnvironment;
if (workingContext->usage == ARGUMENT_SYMBOL) {
currentEnvironment = currentEnvironment->
previousEnvironment;
}
result = encodeOperand(workingContext->value->value);
currentEnvironment = saveEnvironment;
return(result);
}
if (workingContext->value->kindOfValue == BLOCK_VALUE) {
error(BLOCK_OPERAND_IN_OBJECT_EXPRESSION_ERROR);
return(FALSE);
}
return(encodeValue(workingContext->value));
}
bool
encodeNumber(numberTermType number)
{
return(
encodeByte(NUMBER_TAG) &&
encodeBigword(number)
);
}
bool
encodeRelocatableNumber(numberTermType number)
{
return(
encodeByte(RELOCATABLE_TAG) &&
encodeBigword(number)
);
}
bool
encodeOperand(operandType *operand)
{
switch (operand->kindOfOperand) {
case EXPRESSION_OPND:
case IMMEDIATE_OPND:
case INDIRECT_OPND:
case POST_INDEXED_Y_OPND:
case PRE_INDEXED_X_OPND:
case X_INDEXED_OPND:
case Y_INDEXED_OPND:
return(encodeExpression(operand->theOperand.expressionUnion));
case A_REGISTER_OPND:
case X_REGISTER_OPND:
case Y_REGISTER_OPND:
error(REGISTER_OPERAND_IN_OBJECT_EXPRESSION_ERROR);
return(FALSE);
case X_SELECTED_OPND:
case Y_SELECTED_OPND:
case PRE_SELECTED_X_OPND:
error(SELECTION_OPERAND_IN_OBJECT_EXPRESSION_ERROR);
return(FALSE);
case STRING_OPND:
return(encodeString(operand->theOperand.stringUnion));
case BLOCK_OPND:
error(BLOCK_OPERAND_IN_OBJECT_EXPRESSION_ERROR);
return(FALSE);
}
}
bool
encodePostopTerm(postOpTermType *postopTerm)
{
nullEncode(postopTerm);
return(
encodeByte(POSTOP_TAG) &&
encodeByte(byteOp(postopTerm->postOp)) &&
encodeExpression(postopTerm->postOpArgument)
);
}
bool
encodePreopTerm(preOpTermType *preopTerm)
{
nullEncode(preopTerm);
return(
encodeByte(PREOP_TAG) &&
encodeByte(byteOp(preopTerm->preOp)) &&
encodeExpression(preopTerm->preOpArgument)
);
}
bool
encodeString(stringType *string)
{
if (!encodeByte(STRING_TAG))
return(FALSE);
while (*string != '\0') {
if (!encodeByte(*string++))
return(FALSE);
}
return(encodeByte('\0'));
}
bool
encodeUnopTerm(unopTermType *unopTerm)
{
nullEncode(unopTerm);
return(
encodeByte(UNOP_TAG) &&
encodeByte(byteOp(unopTerm->unop)) &&
encodeExpression(unopTerm->unopArgument)
);
}
bool
encodeValue(valueType *value)
{
switch (value->kindOfValue) {
case ABSOLUTE_VALUE:
return(encodeNumber(value->value));
case RELOCATABLE_VALUE:
return(encodeRelocatableNumber(value->value));
case OPERAND_VALUE:
return(encodeOperand(value->value));
case STRING_VALUE:
return(encodeString(value->value));
case CONDITION_VALUE:
return(encodeCondition(value->value));
case DATA_VALUE:
case BSS_VALUE:
case STRUCT_VALUE:
case FIELD_VALUE:
case MACRO_VALUE:
case UNDEFINED_VALUE:
case FUNCTION_VALUE:
case BLOCK_VALUE:
case BUILT_IN_FUNCTION_VALUE:
case ARRAY_VALUE:
case FAIL:
error(WRONG_KIND_OF_VALUE_IN_OBJECT_EXPRESSION_ERROR,
valueKindString(value->kindOfValue));
return(FALSE);
}
}
bool
encodeExpression(expressionType *expression)
{
nullEncode(expression);
switch (expression->kindOfTerm) {
case ARRAY_EXPR:
error(ARRAY_TERM_IN_OBJECT_EXPRESSION_ERROR);
return(FALSE);
break;
case ASSIGN_EXPR:
return(encodeAssignmentTerm(expression->expressionTerm.binopUnion));
break;
case BINOP_EXPR:
return(encodeBinopTerm(expression->expressionTerm.binopUnion));
break;
case CONDITION_CODE_EXPR:
return(encodeCondition(expression->expressionTerm.conditionTypeUnion));
break;
case FUNCTION_CALL_EXPR:
return(encodeFunctionCall(expression->expressionTerm.functionCallUnion));
break;
case HERE_EXPR:
return(encodeHere());
break;
case IDENTIFIER_EXPR:
return(encodeIdentifier(expression->expressionTerm.identifierUnion));
break;
case NUMBER_EXPR:
return(encodeNumber(expression->expressionTerm.numberUnion));
break;
case POSTOP_EXPR:
return(encodePostopTerm(expression->expressionTerm.postOpUnion));
break;
case PREOP_EXPR:
return(encodePreopTerm(expression->expressionTerm.preOpUnion));
break;
case SUBEXPRESSION_EXPR:
encodeExpression(expression->expressionTerm.expressionUnion);
break;
case STRING_EXPR:
return(encodeString(expression->expressionTerm.stringUnion));
break;
case UNOP_EXPR:
return(encodeUnopTerm(expression->expressionTerm.unopUnion));
break;
case VALUE_EXPR:
return(encodeValue(expression->expressionTerm.valueUnion));
break;
default:
botch("encodeExpression: funny expression kind %d\n",
expression->kindOfTerm);
break;
}
}
bool
encodeAssertStatement(assertStatementBodyType *assertStatement)
{
return(
encodeByte(ASSERT_TAG) &&
encodeExpression(assertStatement->condition) &&
encodeExpression(assertStatement->message)
);
}
bool
encodeFreturnStatement(freturnStatementBodyType *freturnStatement)
{
return(
encodeByte(FRETURN_TAG) &&
encodeExpression(freturnStatement)
);
}
bool
encodeMdefineStatement(defineStatementBodyType *mdefineStatement)
{
return(
encodeByte(MDEFINE_TAG) &&
encodeIdentifier(mdefineStatement->theSymbol) &&
encodeExpression(mdefineStatement->theValue)
);
}
bool
encodeMdoUntilStatement(mdoUntilStatementBodyType *mdoUntilStatement)
{
return(
encodeByte(MDOUNTIL_TAG) &&
encodeExpression(mdoUntilStatement->mdoUntilCondition) &&
encodeBlock(mdoUntilStatement->mdoUntilLoop)
);
}
bool
encodeMdoWhileStatement(mdoWhileStatementBodyType *mdoWhileStatement)
{
return(
encodeByte(MDOWHILE_TAG) &&
encodeExpression(mdoWhileStatement->mdoWhileCondition) &&
encodeBlock(mdoWhileStatement->mdoWhileLoop)
);
}
bool
encodeMforStatement(mforStatementBodyType *mforStatement)
{
return(
encodeByte(MFOR_TAG) &&
encodeExpression(mforStatement->initExpression) &&
encodeExpression(mforStatement->testExpression) &&
encodeExpression(mforStatement->incrExpression) &&
encodeBlock(mforStatement->forLoop)
);
}
bool
encodeMifStatement(mifStatementBodyType *mifStatement)
{
return(
encodeByte(MIF_TAG) &&
encodeExpression(mifStatement->mifCondition) &&
encodeBlock(mifStatement->mifConsequence) &&
encodeBlock(mifStatement->mifContinuation.mifBlockUnion)
);
}
bool
encodeMswitchStatement(mswitchStatementBodyType *mswitchStatement)
{
caseListType *caseList;
caseType *theCase;
expressionListType *tagExpressionList;
if (!(encodeByte(MSWITCH_TAG) && encodeExpression(mswitchStatement->
switchExpression)))
return(FALSE);
for (caseList=mswitchStatement->cases; caseList!=NULL; caseList=caseList->
nextCase) {
theCase = caseList->theCase;
for (tagExpressionList=theCase->caseTags; tagExpressionList!=NULL;
tagExpressionList=tagExpressionList->nextExpression) {
if (!encodeExpression(tagExpressionList->theExpression))
return(FALSE);
}
if (!encodeBlock(theCase->caseBody))
return(FALSE);
}
return(encodeByte(END_TAG));
}
bool
encodeMvariableStatement(mvariableStatementBodyType *mvariableStatement)
{
int length;
if ((length=expressionListLength(mvariableStatement->theValue) > 1) ||
mvariableStatement->theDimension!=NULL) {
error(ARRAY_MVARIABLE_IN_OBJECT_FUNCTION_ERROR);
return(FALSE);
}
if (!(encodeByte(MVARIABLE_TAG) && encodeIdentifier(mvariableStatement->
theSymbol)))
return(FALSE);
if (length == 1)
return(encodeExpression(mvariableStatement->theValue->
theExpression));
else
return(encodeExpression(NULL));
}
bool
encodeMwhileStatement(mwhileStatementBodyType *mwhileStatement)
{
return(
encodeByte(MWHILE_TAG) &&
encodeExpression(mwhileStatement->mwhileCondition) &&
encodeBlock(mwhileStatement->mwhileLoop)
);
}
bool
encodeStatement(statementType *statement)
{
switch(statement->kindOfStatement) {
case ALIGN_STATEMENT:
case BLOCK_STATEMENT:
case BYTE_STATEMENT:
case CONSTRAIN_STATEMENT:
case DBYTE_STATEMENT:
case DEFINE_STATEMENT:
case DO_UNTIL_STATEMENT:
case DO_WHILE_STATEMENT:
case EXTERN_STATEMENT:
case FUNCTION_STATEMENT:
case IF_STATEMENT:
case INCLUDE_STATEMENT:
case INSTRUCTION_STATEMENT:
case LONG_STATEMENT:
case MACRO_STATEMENT:
case ORG_STATEMENT:
case REL_STATEMENT:
case START_STATEMENT:
case STRING_STATEMENT:
case STRUCT_STATEMENT:
case TARGET_STATEMENT:
case UNDEFINE_STATEMENT:
case VARIABLE_STATEMENT:
case WHILE_STATEMENT:
case WORD_STATEMENT:
error(ILLEGAL_STATEMENT_IN_OBJECT_FILE_FUNCTION_ERROR,
statementKindString(statement->kindOfStatement));
return(FALSE);
case ASSERT_STATEMENT:
return(encodeAssertStatement(statement->statementBody.assertUnion));
case FRETURN_STATEMENT:
return(encodeFreturnStatement(statement->statementBody.freturnUnion));
case GROUP_STATEMENT:
return(encodeBlock(statement->statementBody.groupUnion));
case MDEFINE_STATEMENT:
return(encodeMdefineStatement(statement->statementBody.defineUnion));
case MDO_UNTIL_STATEMENT:
return(encodeMdoUntilStatement(statement->statementBody.mdoUntilUnion));
case MDO_WHILE_STATEMENT:
return(encodeMdoWhileStatement(statement->statementBody.mdoWhileUnion));
case MFOR_STATEMENT:
return(encodeMforStatement(statement->statementBody.mforUnion));
case MIF_STATEMENT:
return(encodeMifStatement(statement->statementBody.mifUnion));
case MSWITCH_STATEMENT:
return(encodeMswitchStatement(statement->statementBody.mswitchUnion));
case MVARIABLE_STATEMENT:
return(encodeMvariableStatement(statement->statementBody.mvariableUnion));
case MWHILE_STATEMENT:
return(encodeMwhileStatement(statement->statementBody.mwhileUnion));
case NULL_STATEMENT:
return(TRUE);
case PERFORM_STATEMENT:
return(encodeExpression(statement->statementBody.expressionUnion));
default:
botch("encodeStatementBody doesn't know kind %d\n",
statement->kindOfStatement);
return(FALSE);
}
}
bool
encodeBlock(blockType *block)
{
if (!encodeByte(BLOCK_TAG))
return(FALSE);
while (block != NULL) {
if (!encodeStatement(block))
return(FALSE);
block = block->nextStatement;
}
return(encodeByte(END_TAG));
}