Repository navigation
Expand file tree
/
Copy pathCommands.h
More file actions
224 lines (190 loc) · 6.45 KB
/
Copy pathCommands.h
File metadata and controls
224 lines (190 loc) · 6.45 KB
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
/*------------------------------------------------------------------------------
* File: Commands.h *
* Description: Names of commands, registers and their codes. *
* Created: 7 feb 2021 *
* Author: Artem Puzankov *
* Email: puzankov.ao@phystech.edu *
* GitHub: https://github.com/hellopuza *
* Copyright © 2021 Artem Puzankov. All rights reserved. *
*///------------------------------------------------------------------------
#ifndef COMMANDS_H_INCLUDED
#define COMMANDS_H_INCLUDED
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
typedef unsigned int ptr_t;
#define POINTER_PRINT_FORMAT "%u"
#define PTR_MAX UINT_MAX
#define INT_TYPE int
#define FLT_TYPE double
#define INT_PRINT_FORMAT "%d"
#define FLT_PRINT_FORMAT "%lf"
#define PTR_TYPE unsigned int
const size_t POINTER_SIZE = sizeof(PTR_TYPE);
const size_t NUMBER_INT_SIZE = sizeof(INT_TYPE);
const size_t NUMBER_FLT_SIZE = sizeof(FLT_TYPE);
const int NUM_FLAG = 0x80;
const int REG_FLAG = 0x40;
const int PTR_FLAG = 0x20;
const int PROCESS_HALT = -666;
/*------------------------------------------------------------------------------
Commands codes *
*///----------------------------------------------------------------------------
enum CommandsCodes
{
CMD_HLT = 0x00,
CMD_PUSH = 0x01,
CMD_POP = 0x02,
CMD_IN = 0x03,
CMD_OUT = 0x04,
CMD_ADD = 0x05,
CMD_SUB = 0x06,
CMD_MUL = 0x07,
CMD_DIV = 0x08,
CMD_NEG = 0x09,
CMD_AND = 0x0A,
CMD_OR = 0x0B,
CMD_XOR = 0x0C,
CMD_PUSHQ = 0x0D,
CMD_POPQ = 0x0E,
CMD_INQ = 0x0F,
CMD_OUTQ = 0x10,
CMD_ADDQ = 0x11,
CMD_SUBQ = 0x12,
CMD_MULQ = 0x13,
CMD_DIVQ = 0x14,
CMD_NEGQ = 0x15,
CMD_SIN = 0x16,
CMD_COS = 0x17,
CMD_SQRT = 0x18,
CMD_JMP = 0x19,
CMD_JE = 0x1A,
CMD_JNE = 0x1B,
CMD_JA = 0x1C,
CMD_JAE = 0x1D,
CMD_JB = 0x1E,
CMD_JBE = 0x1F,
CMD_CALL = 0x20,
CMD_RET = 0x21,
CMD_FLT2INT = 0x22,
CMD_INT2FLT = 0x23,
CMD_SCREEN = 0x24,
};
struct command
{
char code;
const char* word;
};
static command cmd_names[] =
{
{ CMD_ADD , "add" },
{ CMD_ADDQ , "addq" },
{ CMD_AND , "and" },
{ CMD_CALL , "call" },
{ CMD_COS , "cos" },
{ CMD_DIV , "div" },
{ CMD_DIVQ , "divq" },
{ CMD_FLT2INT , "flt2int" },
{ CMD_HLT , "hlt" },
{ CMD_IN , "in" },
{ CMD_INQ , "inq" },
{ CMD_INT2FLT , "int2flt" },
{ CMD_JA , "ja" },
{ CMD_JAE , "jae" },
{ CMD_JB , "jb" },
{ CMD_JBE , "jbe" },
{ CMD_JE , "je" },
{ CMD_JMP , "jmp" },
{ CMD_JNE , "jne" },
{ CMD_MUL , "mul" },
{ CMD_MULQ , "mulq" },
{ CMD_NEG , "neg" },
{ CMD_NEGQ , "negq" },
{ CMD_OR , "or" },
{ CMD_OUT , "out" },
{ CMD_OUTQ , "outq" },
{ CMD_POP , "pop" },
{ CMD_POPQ , "popq" },
{ CMD_PUSH , "push" },
{ CMD_PUSHQ , "pushq" },
{ CMD_RET , "ret" },
{ CMD_SCREEN , "screen" },
{ CMD_SIN , "sin" },
{ CMD_SQRT , "sqrt" },
{ CMD_SUB , "sub" },
{ CMD_SUBQ , "subq" },
{ CMD_XOR , "xor" },
};
const int CMD_NUM = sizeof(cmd_names)/sizeof(cmd_names[0]);
/*------------------------------------------------------------------------------
Register codes *
*///----------------------------------------------------------------------------
enum RegistersCodes
{
REG_EAX = 0x01,
REG_EBX = 0x02,
REG_ECX = 0x03,
REG_EDX = 0x04,
REG_RAX = 0x05,
REG_RBX = 0x06,
REG_RCX = 0x07,
REG_RDX = 0x08,
REG_RBP = 0x09,
REG_RSP = 0x0A,
REG_SCRX = 0x0B,
REG_SCRY = 0x0C,
};
struct reg
{
char code;
const char* word;
};
static command reg_names[] =
{
{ REG_EAX , "eax" },
{ REG_EBX , "ebx" },
{ REG_ECX , "ecx" },
{ REG_EDX , "edx" },
{ REG_RAX , "rax" },
{ REG_RBP , "rbp" },
{ REG_RBX , "rbx" },
{ REG_RCX , "rcx" },
{ REG_RDX , "rdx" },
{ REG_RSP , "rsp" },
{ REG_SCRX , "scrx" },
{ REG_SCRY , "scry" },
};
const int REG_NUM = sizeof(reg_names) / sizeof(reg_names[0]);
//------------------------------------------------------------------------------
inline int isJUMP(char code)
{
return ( (code == CMD_JMP ) ||
(code == CMD_JE ) ||
(code == CMD_JNE ) ||
(code == CMD_JA ) ||
(code == CMD_JAE ) ||
(code == CMD_JB ) ||
(code == CMD_JBE ) ||
(code == CMD_CALL) );
}
//------------------------------------------------------------------------------
inline int CompareCMD_Names (const void* p1, const void* p2)
{
assert(p1 != nullptr);
assert(p2 != nullptr);
assert(p1 != p2);
// printf("CompareCMD_Names: s1(%p): %s, s2(%p): %s\n", p1, ((struct command*)p1)->word, p2, ((struct command*)p2)->word);
return strcmp(((struct command*)p1)->word, ((struct command*)p2)->word);
}
//------------------------------------------------------------------------------
inline int CompareREG_Names (const void* p1, const void* p2)
{
assert(p1 != nullptr);
assert(p2 != nullptr);
assert(p1 != p2);
// printf("CompareREG_Names: s1(%p): %s, s2(%p): %s\n", p1, ((struct reg*)p1)->word, p2, ((struct reg*)p2)->word);
return strcmp(((struct reg*)p1)->word, ((struct reg*)p2)->word);
}
//------------------------------------------------------------------------------
#endif // COMMAND_H_INCLUDED