-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.c
executable file
·118 lines (104 loc) · 3.81 KB
/
code.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
//*****************************************************************************
// Code.c
//-----------------------------------------------------------------------------
//
// Code generator module for generic assembler example.
//-----------------------------------------------------------------------------
#include <stdio.h>
#include "analyse.h"
#include "symboltable.h"
#include "compiler.h"
#include "code.h"
#include "Boolean.h"
// ----------------------------External Globals--------------------------------
extern SymTabType *gSymbolTable[];
extern int gNoOfSymbols;
// ----------------------------Global Variables--------------------------------
int gFreeSpace; // Marks free data area
Boolean gPopped; // last operation was a pop
int gNextLabel;
Boolean gLoaded;
FILE *gOutFp; // output file pointer
struct {
TOKEN tok;
int value;
} gLast; // Last token
//*****************************************************************************
// Declare - allocate space for variables
//-----------------------------------------------------------------------------
void Declare(char *s)
{
fprintf(gOutFp, "%s EQ %X\n", s, gFreeSpace);
gFreeSpace += 2; // assume byte addressing
}
//*****************************************************************************
// PrintOp - print operator
//-----------------------------------------------------------------------------
void PrintOp(char *operator)
{
fprintf(gOutFp, "%s", operator);
if (gPopped) {
fprintf(gOutFp,"_TMP"); // last op was a pop so load from TMP
gPopped = false;
}
else if (gLast.tok == NUM) {
fprintf(gOutFp, "%d", gLast.value); // a literal
}
else {
fprintf(gOutFp,"%s", gSymbolTable[gLast.value]->entry);
}
fprintf(gOutFp, "\n");
}
//*****************************************************************************
// PrintLabel -
//-----------------------------------------------------------------------------
void PrintLabel(int x)
{
fprintf(gOutFp,"L%d:\n", x);
}
//*****************************************************************************
// Generate - generates code for each construct in a simple pseudo-assembler.
// All operations are of the form <LABEL:> operator operand
//-----------------------------------------------------------------------------
void Generate(TOKEN tok, int value)
{
switch (tok) {
case PLUS : PrintOp(" ADD "); break;
case MINUS :
case GREATERTHAN :PrintOp(" SUB "); break;
case MULT : PrintOp(" MUL "); break;
case DIVI : PrintOp(" DIV "); break;
case BECOMES: PrintOp(" STORE "); break;
case EQUALS : PrintOp(" XOR "); break;
case LBRACKET: fprintf(gOutFp, " PUSH\n"); break;
case RBRACKET: fprintf(gOutFp, " POP _TMP\n"); gPopped = true; break;
case ID : if (!gLoaded) // start of expression
fprintf(gOutFp," LOAD %s\n",gSymbolTable[value]->entry);
break;
case NUM : if (!gLoaded) // start of expression
fprintf(gOutFp," LOAD %d\n",value);
break;
case TLABEL : PrintLabel(value); break;
case GOFALSE: fprintf(gOutFp," JNZ "); PrintLabel(value); break;
case TGOTO : fprintf(gOutFp," JMP "); PrintLabel(value); break;
default : break; // don't do anything with unrecognized token types...
}
gLast.tok = tok;
gLast.value = value;
gLoaded = true;
}
//*****************************************************************************
// InitCode - initialise globals in the code module
//-----------------------------------------------------------------------------
void InitCode(char *theFileName)
{
//if ( (gOutFp = fopen("theFileName, "w")) == NULL) {
// printf("AArrgh: couldn't open the output file %s.\n", theFileName);
// exit(1);
// }
gOutFp = stdout;
gFreeSpace = 1000; // start of data area
Declare("_TMP"); // set aside temporary storage
gNextLabel = 1; // set first label's value
gLoaded = false; // accumulator is not loaded
}