forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstruction.h
218 lines (200 loc) · 5.52 KB
/
instruction.h
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
/*
* 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)
*/
/**
* @file instruction.h
* @brief Declares all instruction related stuff.
*/
#ifndef INSTRUCTION_H
#define INSTRUCTION_H
#include "builtin.h"
#include "token.h"
#include "symbol.h"
/** Instruction operand conventions:
* Noop:
* nothing
*
* Mov:
* res - destination
* a - source
*
* Jmp:
* a - distance
*
* Jmpz:
* res - exprStart
* a - distance
* b - condition
*
* Push:
* a - item
*
* PushC:
* a - constants table index
*
* Reserve:
* a - count
*
* Pop:
* a - count
*
* ClearExpr:
* a - exprStart
*
* Call:
* a - address index
*
* Return:
* a - parameter count
*
* Nullify:
* a - item
*
*/
typedef enum
{
IST_Noop = 0, //!< IST_Noop
IST_Mov, //!< IST_Mov
IST_MovC, //!< IST_MovC
IST_Jmp, //!< IST_Jmp
IST_Jmpz, //!< IST_Jmpz
IST_Jmpnz, //!< IST_Jmpnz
IST_Push, //!< IST_Push
IST_PushC, //!< IST_PushC
IST_PushRef, //!< IST_PushRef
IST_Reserve, //!< IST_Reserve
IST_Pop, //!< IST_Pop
IST_ClearExpr, //!< IST_ClearExpr
IST_Call, //!< IST_Call
IST_Return, //!< IST_Return
IST_Nullify, //!< IST_Nullify
// Built-in functions instructions
IST_BoolVal, //!< IST_BoolVal
IST_DoubleVal, //!< IST_DoubleVal
IST_FindString, //!< IST_FindString
IST_GetString, //!< IST_GetString
IST_GetSubstring,//!< IST_GetSubstring
IST_IntVal, //!< IST_IntVal
IST_PutString, //!< IST_PutString
IST_SortString, //!< IST_SortString
IST_StrLen, //!< IST_StrLen
IST_StrVal, //!< IST_StrVal
// Helper instructions that aren't interpreted
IST_Break, //!< IST_Break
IST_Continue, //!< IST_Continue
// Expression related instructions
IST_Add, //!< IST_Add
IST_Subtract, //!< IST_Subtract
IST_Multiply, //!< IST_Multiply
IST_Divide, //!< IST_Divide
IST_Concat, //!< IST_Concat
IST_Equal, //!< IST_Equal
IST_NotEqual, //!< IST_NotEqual
IST_Less, //!< IST_Less
IST_LessEq, //!< IST_LessEq
IST_Greater, //!< IST_Greater
IST_GreaterEq, //!< IST_GreaterEq
IST_And, //!< IST_And
IST_Or, //!< IST_Or
IST_Not //!< IST_Not
} InstructionCode;
/** Instruction modes. */
typedef enum
{
ISM_NoConst = 0,
ISM_FirstConst = 1,
ISM_SecondConst = 2,
ISM_AllConst = 3
} InstructionMode;
/** Structure that holds data of Instruction. */
typedef struct
{
InstructionCode code;
InstructionMode mode;
int32_t a;
int32_t b;
int32_t res;
} Instruction;
/**
* @brief Creates new instruction on heap.
* @return Pointer to created instruction.
*/
Instruction* newInstruction();
/**
* @brief Initializes existing instruction.
* @param pt Instruction to initialize.
*/
void initInstruction(Instruction *pt);
/**
* @brief Deletes existing instruction.
* @param pt Instruction to delete.
*/
void deleteInstruction(Instruction *pt);
/**
* @brief Frees an instruction created on heap.
* @param ppt Instruction to free.
*/
void freeInstruction(Instruction **ppt);
/**
* @brief Makes a copy of an instruction.
* @param src Source instruction used as image.
* @param dest Destination instruction to be filled.
*/
void copyInstruction(const Instruction *src, Instruction *dest);
/**
* @brief Makes checks and generates a function call instruction.
* @param symbol Symbol that holds informations about function.
* @param builtinCode Code of built-in function to be called.
* @param paramCount Number of parameters with which to call.
* @return
*/
uint32_t generateCall(const Symbol *symbol, BuiltinCode builtinCode, uint32_t paramCount);
/**
* @brief Generates most of instructions.
* @param code Code of instruction to generate. Can't be IST_Call.
* @param mode Instruction mode.
* @param res Result index for instruction.
* @param a A operand index for instruction.
* @param b B operand index for instruction.
*/
void generateInstruction(InstructionCode code, InstructionMode mode, int32_t res, int32_t a, int32_t b);
/**
* @brief Generates an empty no-op instruction that can be filled later.
* @return Index of generated instruction in instruction vector.
*/
uint32_t generateEmptyInstruction();
/**
* @brief Fills pre-generated empty instruction.
* @param index Index of instruction to fill.
* @param code Code of instruction.
* @param res Result index for instruction.
* @param a A operand index for instruction.
* @param b B operand index for instruction.
*/
void fillInstruction(uint32_t index, InstructionCode code, int32_t res, int32_t a, int32_t b);
/**
* @brief Returns informations about function using token from scanner.
* @param funcToken Token holding function's informations.
* @param builtinCode If token represents built-in function, it's code is set here.
* @param paramCount Number of function's parameters.
* @return Symbol that golds informations about function.
*/
Symbol* fillInstFuncInfo(const Token *funcToken, BuiltinCode *builtinCode, int64_t *paramCount);
#endif