forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
instruction.c
219 lines (188 loc) · 5.31 KB
/
instruction.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
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ13.
*
* Codename:
* INI: Ni Interpreter
*
* Description:
* https://wis.fit.vutbr.cz/FIT/st/course-files-st.php/course/IFJ-IT/projects/ifj2013.pdf
*
* Project's GitHub repository:
* https://github.com/metthal/IFJ-Projekt
*
* Team:
* Marek Milkovič (xmilko01)
* Lukáš Vrabec (xvrabe07)
* Ján Spišiak (xspisi03)
* Ivan Ševčík (xsevci50)
* Marek Bertovič (xberto00)
*/
#include "instruction.h"
#include "symbol.h"
#include "ial.h"
#include "instruction_vector.h"
#include <stdlib.h>
#include <string.h>
extern SymbolTable *globalSymbolTable;
extern Vector *instructions;
Instruction* newInstruction()
{
Instruction *tmp = malloc(sizeof(Instruction));
memset(tmp, 0, sizeof(Instruction));
return tmp;
}
void initInstruction(Instruction *pt)
{
memset(pt, 0, sizeof(Instruction));
}
void deleteInstruction(Instruction *pt)
{
if (pt != NULL) {
}
}
void freeInstruction(Instruction **ppt)
{
if (ppt != NULL) {
if (*ppt != NULL) {
}
free(*ppt);
*ppt = NULL;
}
}
void copyInstruction(const Instruction *src, Instruction *dest)
{
dest->code = src->code;
dest->mode = src->mode;
dest->res = src->res;
dest->a = src->a;
dest->b = src->b;
}
InstructionCode builtinToInstruction(BuiltinCode builtinCode)
{
switch (builtinCode) {
case BTI_None:
return IST_Call;
case BTI_BoolVal:
return IST_BoolVal;
case BTI_DoubleVal:
return IST_DoubleVal;
case BTI_FindString:
return IST_FindString;
case BTI_GetString:
return IST_GetString;
case BTI_GetSubstring:
return IST_GetSubstring;
case BTI_IntVal:
return IST_IntVal;
case BTI_SortString:
return IST_SortString;
case BTI_StrLen:
return IST_StrLen;
case BTI_StrVal:
return IST_StrVal;
case BTI_PutString:
return IST_PutString;
}
return IST_Noop;
}
void processDefaultArg(Context *context, uint32_t *paramCount)
{
uint32_t missingCount = context->argumentCount - *paramCount;
if (missingCount > 0 && missingCount <= context->defaultCount) {
// First argument is first on stack, just append defaults
// which are in constant table in opposite order (last is first)
int64_t index = context->defaultStart + missingCount - 1;
for (; index >= context->defaultStart; index--)
generateInstruction(IST_PushC, ISM_NoConst, 0, index, 0);
*paramCount += missingCount;
}
}
uint32_t generateCall(const Symbol *symbol, BuiltinCode builtinCode, uint32_t paramCount)
{
Instruction inst;
initInstruction(&inst);
uint8_t undef = 1, badParamCount = 0;
switch(builtinCode) {
case BTI_None:
// Too many parameters are clipped
// Default parameters
processDefaultArg(&(symbol->data->func.context), ¶mCount);
// Test for too few arguments
if (symbol->data->func.context.argumentCount == paramCount)
inst.a = symbol->data->func.functionAddressIndex;
else
badParamCount = 1;
undef = 0;
break;
case BTI_PutString:
// Set number of parameters as operand.
inst.a = paramCount;
undef = 0;
break;
default:
if (paramCount != getBuiltinParamCount(builtinCode))
badParamCount = 1;
undef = 0;
break;
}
if (undef)
setError(ERR_UndefFunction);
else if (badParamCount)
setError(ERR_BadParamCount);
else {
inst.code = builtinToInstruction(builtinCode);
vectorPushInstruction(instructions, &inst);
}
return paramCount;
}
void generateInstruction(InstructionCode code, InstructionMode mode, int32_t res, int32_t a, int32_t b)
{
if (code == IST_Call) {
setError(ERR_ISTGenerator);
return;
}
Instruction inst;
inst.code = code;
inst.mode = mode;
inst.res = res;
inst.a = a;
inst.b = b;
vectorPushInstruction(instructions, &inst);
}
uint32_t generateEmptyInstruction()
{
uint32_t ret = vectorSize(instructions);
vectorPushDefaultInstruction(instructions);
return ret;
}
void fillInstruction(uint32_t index, InstructionCode code, int32_t res, int32_t a, int32_t b)
{
if (code == IST_Call) {
setError(ERR_ISTGenerator);
return;
}
Instruction *pt = vectorAt(instructions, index);
if (pt->code == IST_Noop || pt->code == IST_Break || pt->code == IST_Continue) {
pt->code = code;
pt->res = res;
pt->a = a;
pt->b = b;
}
}
Symbol* fillInstFuncInfo(const Token *funcToken, BuiltinCode *builtinCode, int64_t *paramCount)
{
Symbol *symbol = NULL;
*builtinCode = getBuiltinCode(&(funcToken->str));
if (*builtinCode == BTI_None) {
symbol = symbolTableFind(globalSymbolTable, &funcToken->str);
if (symbol == NULL) {
setError(ERR_UndefFunction);
return NULL;
}
*paramCount = symbol->data->func.context.argumentCount;
}
else
*paramCount = getBuiltinParamCount(*builtinCode);
return symbol;
}