-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisa.h
210 lines (151 loc) · 5.62 KB
/
isa.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
/* Instruction Set definition for Y86-64 Architecture */
/* Revisions:
2013-10-25:
Extended all data widths and addresses to 64 bits
Changed all 'l' instructions to 'q'
Changed registers name from e-form to r-form
Added registers %r8 to %r14
2009-03-11:
Changed RNONE to be 0xF
Changed J_XX and jump_t to C_XX and cond_t; take_branch to cond_holds
Expanded RRMOVL to include conditional moves
*/
/**************** Registers *************************/
/* REG_NONE is a special one to indicate no register */
typedef enum { REG_RAX, REG_RCX, REG_RDX, REG_RBX,
REG_RSP, REG_RBP, REG_RSI, REG_RDI,
REG_R8, REG_R9, REG_R10, REG_R11,
REG_R12, REG_R13, REG_R14, REG_NONE=0xF, REG_ERR } reg_id_t;
/* Find register ID given its name */
reg_id_t find_register(char *name);
/* Return name of register given its ID */
char *reg_name(reg_id_t id);
/**************** Instruction Encoding **************/
/* Different argument types */
typedef enum { R_ARG, M_ARG, I_ARG, NO_ARG } arg_t;
/* Different instruction types */
typedef enum { I_HALT, I_NOP, I_RRMOVQ, I_IRMOVQ, I_RMMOVQ, I_MRMOVQ,
I_ALU, I_JMP, I_CALL, I_RET, I_PUSHQ, I_POPQ,
I_IADDQ, I_POP2, I_JM } itype_t;
/* Different ALU operations */
typedef enum { A_ADD, A_SUB, A_AND, A_XOR, A_NONE } alu_t;
/* Default function code */
typedef enum { F_NONE } fun_t;
/* Return name of operation given its ID */
char op_name(alu_t op);
/* Different Jump conditions */
typedef enum { C_YES, C_LE, C_L, C_E, C_NE, C_GE, C_G } cond_t;
/* Pack itype and function into single byte */
#define HPACK(hi,lo) ((((hi)&0xF)<<4)|((lo)&0xF))
/* Unpack byte */
#define HI4(byte) (((byte)>>4)&0xF)
#define LO4(byte) ((byte)&0xF)
/* Get the opcode out of one byte instruction field */
#define GET_ICODE(instr) HI4(instr)
/* Get the ALU/JMP function out of one byte instruction field */
#define GET_FUN(instr) LO4(instr)
/* Return name of instruction given it's byte encoding */
char *iname(int instr);
/**************** Truth Values **************/
typedef enum { FALSE, TRUE } bool_t;
/* Table used to encode information about instructions */
typedef struct {
char *name;
unsigned char code; /* Byte code for instruction+op */
int bytes;
arg_t arg1;
int arg1pos;
int arg1hi; /* 0/1 for register argument, # bytes for allocation */
arg_t arg2;
int arg2pos;
int arg2hi; /* 0/1 */
} instr_t, *instr_ptr;
instr_ptr find_instr(char *name);
/* Return invalid instruction for error handling purposes */
instr_ptr bad_instr();
/*********** Implementation of Memory *****************/
typedef unsigned char byte_t;
typedef long long int word_t;
typedef long long unsigned uword_t;
/* Represent a memory as an array of bytes */
typedef struct {
int len;
word_t maxaddr;
byte_t *contents;
} mem_rec, *mem_t;
/* Create a memory with len bytes */
mem_t init_mem(int len);
void free_mem(mem_t m);
/* Set contents of memory to 0 */
void clear_mem(mem_t m);
/* Make a copy of a memory */
mem_t copy_mem(mem_t oldm);
/* Print the differences between two memories */
bool_t diff_mem(mem_t oldm, mem_t newm, FILE *outfile);
/* How big should the memory be? */
#ifdef BIG_MEM
#define MEM_SIZE (1<<16)
#else
#define MEM_SIZE (1<<13)
#endif
/*** In the following functions, a return value of 1 means success ***/
/* Load memory from .yo file. Return number of bytes read */
int load_mem(mem_t m, FILE *infile, int report_error);
/* Get byte from memory */
bool_t get_byte_val(mem_t m, word_t pos, byte_t *dest);
/* Get 8 bytes from memory */
bool_t get_word_val(mem_t m, word_t pos, word_t *dest);
/* Set byte in memory */
bool_t set_byte_val(mem_t m, word_t pos, byte_t val);
/* Set 8 bytes in memory */
bool_t set_word_val(mem_t m, word_t pos, word_t val);
/* Print contents of memory */
void dump_memory(FILE *outfile, mem_t m, word_t pos, int cnt);
/********** Implementation of Register File *************/
mem_t init_reg();
void free_reg();
/* Make a copy of a register file */
mem_t copy_reg(mem_t oldr);
/* Print the differences between two register files */
bool_t diff_reg(mem_t oldr, mem_t newr, FILE *outfile);
word_t get_reg_val(mem_t r, reg_id_t id);
void set_reg_val(mem_t r, reg_id_t id, word_t val);
void dump_reg(FILE *outfile, mem_t r);
/* **************** ALU Function **********************/
/* Compute ALU operation */
word_t compute_alu(alu_t op, word_t arg1, word_t arg2);
typedef unsigned char cc_t;
#define GET_ZF(cc) (((cc) >> 2)&0x1)
#define GET_SF(cc) (((cc) >> 1)&0x1)
#define GET_OF(cc) (((cc) >> 0)&0x1)
#define PACK_CC(z,s,o) (((z)<<2)|((s)<<1)|((o)<<0))
#define DEFAULT_CC PACK_CC(1,0,0)
/* Compute condition code. */
cc_t compute_cc(alu_t op, word_t arg1, word_t arg2);
/* Generated printed form of condition code */
char *cc_name(cc_t c);
/* **************** Status types *******************/
typedef enum
{STAT_BUB, STAT_AOK, STAT_HLT, STAT_ADR, STAT_INS, STAT_PIP } stat_t;
/* Describe Status */
char *stat_name(stat_t e);
/* **************** ISA level implementation *********/
typedef struct {
word_t pc;
mem_t r;
mem_t m;
cc_t cc;
} state_rec, *state_ptr;
state_ptr new_state(int memlen);
void free_state(state_ptr s);
state_ptr copy_state(state_ptr s);
bool_t diff_state(state_ptr olds, state_ptr news, FILE *outfile);
/* Determine if condition satisified */
bool_t cond_holds(cc_t cc, cond_t bcond);
/* Execute single instruction. Return status. */
stat_t step_state(state_ptr s, FILE *error_file);
/************************ Interface Functions *************/
#ifdef HAS_GUI
void report_line(word_t line_no, word_t addr, char *hexcode, char *line);
void signal_register_update(reg_id_t r, word_t val);
#endif