include: deprecate syscalls leftovers
[cavatools.git] / caveat / insn.h
1 /*
2 Copyright (c) 2020 Peter Hsu. All Rights Reserved. See LICENCE file for details.
3 */
4
5 #pragma once
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11
12 #define RISCV_PGSHIFT 12
13 #define RISCV_PGSIZE (1 << RISCV_PGSHIFT)
14
15 #define ROUNDUP(a, b) ((((a)-1)/(b)+1)*(b))
16 #define ROUNDDOWN(a, b) ((a)/(b)*(b))
17
18
19 typedef long Addr_t;
20 #define GEN_SEGV (*((char*)0) = 0)
21
22
23 struct reg_t {
24 union {
25 int64_t l;
26 uint64_t ul;
27 int32_t i;
28 uint32_t ui;
29 double d;
30 float f;
31 void* p;
32 Addr_t a;
33 #ifdef SOFT_FP
34 float32_t f32;
35 float64_t f64;
36 #endif
37 };
38 };
39
40 struct insn_t {
41 enum Opcode_t op_code : 16;
42 uint8_t op_rd; // 0..31 = integer registers
43 uint8_t op_rs1; // 32..63 = floating point registers
44 union {
45 int32_t op_constant;
46 struct {
47 int16_t immed;
48 uint8_t rs2; // 64 = not valid
49 uint8_t rs3; // only for floating multiply-add
50 } op;
51 };
52 };
53
54 #define ZERO 0 // Register x0 always zero
55 #define RA 1 // Standard RISC-V ABI convention
56 #define SP 2
57 #define GP 3
58 #define TP 4
59 #define NOREG 64
60
61
62 struct insnAttr_t {
63 const char* name; /* asserbler opcode */
64 unsigned int flags; /* upper case, from Instructions.def */
65 enum units_t unit : 8; /* lower case, functional units */
66 unsigned char latency; /* filled in by simulator */
67 };
68
69
70 struct insnSpace_t {
71 struct insn_t* insn_array;
72 Addr_t base, bound;
73 };
74
75
76 /* Process information */
77 struct pinfo_t {
78 long phnum;
79 long phent;
80 long phdr;
81 long phdr_size;
82 Addr_t entry;
83 Addr_t stack_top;
84 Addr_t brk;
85 Addr_t brk_min;
86 Addr_t brk_max;
87 };
88
89 extern struct pinfo_t current;
90
91
92 extern struct insnSpace_t insnSpace;
93 extern struct insnAttr_t insnAttr[]; /* Attribute array indexed by Opcode_t */
94 extern const char* regName[];
95
96
97
98 void insnSpace_init();
99 void decode_instruction( const struct insn_t* p, Addr_t PC );
100
101 Addr_t load_elf_binary( const char* file_name, int include_data );
102 Addr_t initialize_stack( int argc, const char** argv, const char** envp );
103 int find_symbol( const char* name, Addr_t* begin, Addr_t* end );
104 int find_pc( long pc, const char** name, long* offset );
105
106
107 #define insn(pc) ( &insnSpace.insn_array[(pc-insnSpace.base)/2] )
108
109 static inline int valid_pc(Addr_t pc)
110 {
111 return insnSpace.base <= pc && pc < insnSpace.bound;
112 }
113
114 static inline void insert_breakpoint(Addr_t pc)
115 {
116 assert(valid_pc(pc));
117 struct insn_t* p = &insnSpace.insn_array[(pc-insnSpace.base)/2];
118 if (shortOp(p->op_code))
119 p->op_code = Op_c_ebreak;
120 else
121 p->op_code = Op_ebreak;
122 }
123
124
125 int find_symbol( const char* name, long* begin_addr, long* end_addr);
126 /*
127 returns TRUE or FALSE found in ELF symbol table
128 name - function or variable
129 begin_addr - pointer to where addresses will be written
130 end_addr - writes NULL if symbol not found
131 */
132
133
134 void print_symbol( long address, FILE* output_file );
135 /*
136 Prints address as name+hex
137 address - in text or data segment
138 output_file - writes to this file
139 */
140
141 int format_pc(char* buf, int width, Addr_t pc);
142
143
144 void print_pc( long pc, FILE* output_file );
145 /*
146 Print symbolic program counter
147 pc - program counter in text segment
148 file_descr - write to this file descriptor
149 */
150
151 int format_insn( char* buf, const struct insn_t* p, long pc, unsigned int image );
152 /*
153 Disassemble instruction into buffer
154 returns length of string in buf
155 buf - buffer to hold disassembled text
156 p - must be insn(pc)
157 pc - program counter in text segment
158 */
159
160 void print_insn( long address, FILE* output_file );
161 /*
162 Disassemble instruction and print
163 address - program counter in text segment
164 file_descr - write to this file descriptor
165 */
166
167 void print_registers( struct reg_t regs[], FILE* output_file );
168 /*
169 Print register files
170 regs - register files, IR+FR
171 file_descr - write to this file descriptor
172 */
173
174
175 #ifdef __cplusplus
176 }
177 #endif