sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
| 4 | /*--- This file (test_main.c) is ---*/ |
| 5 | /*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/ |
| 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 11 | #include <assert.h> |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 12 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 13 | #include "libvex_basictypes.h" |
| 14 | #include "libvex.h" |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 15 | |
| 16 | /*---------------------------------------------------------------*/ |
| 17 | /*--- Test ---*/ |
| 18 | /*---------------------------------------------------------------*/ |
| 19 | |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 20 | |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 21 | __attribute__ ((noreturn)) |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 22 | static |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 23 | void failure_exit ( void ) |
| 24 | { |
| 25 | fprintf(stdout, "VEX did failure_exit. Bye.\n"); |
| 26 | exit(1); |
| 27 | } |
| 28 | |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 29 | static |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 30 | void log_bytes ( Char* bytes, Int nbytes ) |
| 31 | { |
| 32 | fwrite ( bytes, 1, nbytes, stdout ); |
| 33 | } |
| 34 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 35 | #define N_LINEBUF 10000 |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 36 | static Char linebuf[N_LINEBUF]; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 37 | |
| 38 | #define N_ORIGBUF 200 |
| 39 | #define N_TRANSBUF 1000 |
| 40 | |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 41 | static Char origbuf[N_ORIGBUF]; |
| 42 | static Char transbuf[N_TRANSBUF]; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 43 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 44 | static Bool verbose = True; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 45 | |
| 46 | int main ( int argc, char** argv ) |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 47 | { |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 48 | FILE* f; |
| 49 | Int i; |
| 50 | UInt u; |
| 51 | Addr32 orig_addr; |
| 52 | Int bb_number; |
| 53 | Int orig_nbytes, trans_used, orig_used; |
| 54 | TranslateResult tres; |
| 55 | |
| 56 | if (argc != 2) { |
| 57 | fprintf(stderr, "usage: vex file.org\n"); |
| 58 | exit(1); |
| 59 | } |
| 60 | f = fopen(argv[1], "r"); |
| 61 | if (!f) { |
| 62 | fprintf(stderr, "can't open `%s'\n", argv[1]); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | LibVEX_Init ( &failure_exit, &log_bytes, |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 67 | 1, /* debug_paranoia */ |
sewardj | 750f407 | 2004-07-26 22:39:11 +0000 | [diff] [blame] | 68 | 0, /* verbosity */ |
sewardj | bad34a9 | 2004-07-22 01:14:11 +0000 | [diff] [blame] | 69 | //False, |
| 70 | True, |
sewardj | e8e9d73 | 2004-07-16 21:03:45 +0000 | [diff] [blame] | 71 | 100 ); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 72 | |
sewardj | 86898e8 | 2004-07-22 17:26:12 +0000 | [diff] [blame] | 73 | #if 0 |
sewardj | ea64e14 | 2004-07-22 16:47:21 +0000 | [diff] [blame] | 74 | {extern void test_asm86(void); |
| 75 | test_asm86(); |
| 76 | return 0; |
| 77 | } |
sewardj | 86898e8 | 2004-07-22 17:26:12 +0000 | [diff] [blame] | 78 | #endif |
sewardj | ea64e14 | 2004-07-22 16:47:21 +0000 | [diff] [blame] | 79 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 80 | while (!feof(f)) { |
| 81 | fgets(linebuf, N_LINEBUF,f); |
| 82 | //printf("%s", linebuf); |
| 83 | assert(linebuf[0] != 0); |
| 84 | if (linebuf[0] != '.') continue; |
| 85 | /* first line is: . bb-number bb-addr n-bytes */ |
| 86 | assert(3 == sscanf(&linebuf[1], " %d %x %d\n", |
| 87 | & bb_number, |
| 88 | & orig_addr, & orig_nbytes )); |
| 89 | assert(orig_nbytes >= 1); |
| 90 | assert(!feof(f)); |
| 91 | fgets(linebuf, N_LINEBUF,f); |
| 92 | assert(linebuf[0] == '.'); |
| 93 | /* second line is: . byte byte byte etc */ |
| 94 | //printf("%s", linebuf); |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 95 | if (verbose) |
| 96 | printf("\n\n============ Basic Block %d, " |
| 97 | "Start %x, nbytes %d ============\n\n", |
| 98 | bb_number, orig_addr, orig_nbytes); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 99 | assert(orig_nbytes >= 1 && orig_nbytes <= N_ORIGBUF); |
| 100 | for (i = 0; i < orig_nbytes; i++) { |
| 101 | assert(1 == sscanf(&linebuf[2 + 3*i], "%x", &u)); |
| 102 | origbuf[i] = (UChar)u; |
| 103 | } |
| 104 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 105 | // if (bb_number == 50) exit(1); |
sewardj | 81bd550 | 2004-07-21 18:49:27 +0000 | [diff] [blame] | 106 | { |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 107 | for (i = 0; i < 1; i++) |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 108 | tres = |
| 109 | LibVEX_Translate ( InsnSetX86, InsnSetX86, |
| 110 | origbuf, (Addr64)orig_addr, &orig_used, |
| 111 | transbuf, N_TRANSBUF, &trans_used, |
| 112 | NULL, NULL ); |
| 113 | assert(tres == TransOK); |
| 114 | assert(orig_used == orig_nbytes); |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 115 | } |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | fclose(f); |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 119 | LibVEX_ClearTemporary(True); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 120 | |
| 121 | #if 0 |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 122 | Int* p; |
| 123 | Int i, j, n = 0; |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 124 | LibVEX_Init ( &failure_exit, &log_bytes, |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 125 | 1, 1, False, 10 ); |
| 126 | for (j = 0; j < 5000; j++) { |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 127 | LibVEX_Clear(False); |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 128 | for (i = 0; i < 2000; i++) { |
| 129 | n++; |
| 130 | p = LibVEX_Alloc(16); |
| 131 | p[0] = p[1] = p[2] = p[3] = 44; |
| 132 | } |
| 133 | } |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 134 | LibVEX_Clear(True); |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 135 | printf("Did %d allocs\n", n); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 136 | #endif |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /*---------------------------------------------------------------*/ |
| 141 | /*--- Test (old) ---*/ |
| 142 | /*---------------------------------------------------------------*/ |
| 143 | |
| 144 | #if 0 |
| 145 | |
sewardj | 887a11a | 2004-07-05 17:26:47 +0000 | [diff] [blame] | 146 | #include "libvex_basictypes.h" |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 147 | #include "ir_defs.h" |
| 148 | #include "host_regs.h" |
| 149 | #include "x86h_defs.h" |
| 150 | |
| 151 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 152 | /* HACK */ |
| 153 | extern |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 154 | HInstrArray* /* not really, but for the time being ... */ |
sewardj | 194d54a | 2004-07-03 19:08:18 +0000 | [diff] [blame] | 155 | iselBB_X86Instr ( IRBB* bb ); |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 156 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 157 | |
| 158 | int main ( void ) |
| 159 | { |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 160 | HInstrArray* vcode; |
| 161 | IRBB* bb; |
| 162 | IRTypeEnv* env = newIRTypeEnv(); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 163 | |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 164 | IRTemp t0 = 0; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 165 | IRTemp t1 = 1; |
| 166 | IRTemp t2 = 2; |
| 167 | |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 168 | addToIRTypeEnv ( env, t0, Ity_I32 ); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 169 | addToIRTypeEnv ( env, t1, Ity_I32 ); |
| 170 | addToIRTypeEnv ( env, t2, Ity_I32 ); |
| 171 | |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 172 | IRStmt* s10 = IRStmt_Tmp(t0, IRExpr_Const(IRConst_U32(0x2000))); |
| 173 | IRStmt* s11 = IRStmt_Tmp(t1, IRExpr_Const(IRConst_U32(0x2001))); |
| 174 | IRStmt* s12 = IRStmt_Tmp(t2, IRExpr_Const(IRConst_U32(0x2002))); |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 175 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 176 | IRStmt* s1 = IRStmt_Put(8,4, IRExpr_Const(IRConst_U32(99)) ); |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 177 | #if 0 |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 178 | IRStmt* s2 = IRStmt_Put(7,4, IRExpr_Binop(Iop_Add32, |
| 179 | IRExpr_Tmp(t1), |
| 180 | IRExpr_Const(IRConst_U32(55)))); |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 181 | #endif |
| 182 | |
| 183 | IRStmt* s2 = IRStmt_Put(9,4, |
| 184 | IRExpr_Binop(Iop_Shl32, |
| 185 | IRExpr_Tmp(t0), |
| 186 | IRExpr_Binop(Iop_Add32, |
| 187 | IRExpr_Tmp(t1), |
| 188 | IRExpr_Tmp(t2)))); |
| 189 | |
| 190 | |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 191 | s10->link = s11; |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 192 | s11->link = s12; |
| 193 | s12->link = s1; |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 194 | s1->link = s2; |
| 195 | |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 196 | bb = mk_IRBB(env, s10, IRNext_UJump(IRConst_U32(-65565))); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 197 | |
| 198 | printf("bb is ...\n"); |
| 199 | ppIRBB(stdout, bb); |
| 200 | printf("\n"); |
| 201 | |
sewardj | 3b9d65e | 2004-07-03 19:52:16 +0000 | [diff] [blame] | 202 | if (0) |
sewardj | 194d54a | 2004-07-03 19:08:18 +0000 | [diff] [blame] | 203 | vcode = iselBB_X86Instr(bb); |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 204 | else |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 205 | { |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 206 | Int i; |
| 207 | HReg vr0 = mkHReg(0, HRcInt, True); |
| 208 | HReg vr1 = mkHReg(1, HRcInt, True); |
| 209 | HReg vr2 = mkHReg(2, HRcInt, True); |
| 210 | HReg vr3 = mkHReg(3, HRcInt, True); |
| 211 | HReg eax = hregX86_EAX(); |
| 212 | HReg ebx = hregX86_EBX(); |
| 213 | HReg ecx = hregX86_ECX(); |
| 214 | HReg edx = hregX86_EDX(); |
| 215 | HReg ebp = hregX86_EBP(); |
| 216 | vcode = newHInstrArray(); |
sewardj | 3b9d65e | 2004-07-03 19:52:16 +0000 | [diff] [blame] | 217 | vcode->n_vregs = 4; |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 218 | |
| 219 | addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV, |
| 220 | X86RMI_Imm(0x10001), vr0)); |
| 221 | addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV, |
| 222 | X86RMI_Imm(0x10101), vr1)); |
| 223 | addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV, |
| 224 | X86RMI_Imm(0x10201), vr2)); |
| 225 | addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV, |
| 226 | X86RMI_Imm(0x10301), vr3)); |
| 227 | |
| 228 | addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV, |
| 229 | X86RMI_Imm(0x99999), eax)); |
| 230 | addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV, |
| 231 | X86RMI_Imm(0x99999), edx)); |
| 232 | |
| 233 | addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV, |
| 234 | X86RI_Reg(vr0), |
| 235 | X86AMode_IR(0x100, ebp))); |
| 236 | addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV, |
| 237 | X86RI_Reg(vr1), |
| 238 | X86AMode_IR(0x101, ebp))); |
| 239 | addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV, |
| 240 | X86RI_Reg(vr2), |
| 241 | X86AMode_IR(0x101, ebp))); |
| 242 | addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV, |
| 243 | X86RI_Reg(vr3), |
| 244 | X86AMode_IR(0x101, ebp))); |
| 245 | printf("\nBefore\n"); |
| 246 | for (i = 0; i < vcode->arr_used; i++) { |
| 247 | ppX86Instr(stdout, vcode->arr[i]); |
| 248 | printf("\n"); |
| 249 | } |
| 250 | printf("\n"); |
| 251 | } |
| 252 | |
| 253 | { |
| 254 | Int i; |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 255 | HInstrArray* rcode; |
| 256 | HReg rregs_to_use[4]; |
| 257 | rregs_to_use[0] = hregX86_EAX(); |
| 258 | rregs_to_use[1] = hregX86_EBX(); |
| 259 | rregs_to_use[2] = hregX86_ECX(); |
| 260 | rregs_to_use[3] = hregX86_EDX(); |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 261 | |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 262 | rcode = |
sewardj | 194d54a | 2004-07-03 19:08:18 +0000 | [diff] [blame] | 263 | doRegisterAllocation(vcode, |
sewardj | 3b9d65e | 2004-07-03 19:52:16 +0000 | [diff] [blame] | 264 | rregs_to_use, 3, /* rregs */ |
sewardj | 12952ef | 2004-07-03 14:52:19 +0000 | [diff] [blame] | 265 | isMove_X86Instr, |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 266 | getRegUsage_X86Instr, |
| 267 | mapRegs_X86Instr, |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 268 | genSpill_X86, |
| 269 | genReload_X86 |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 270 | ); |
| 271 | |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 272 | printf("\nAfter\n"); |
| 273 | for (i = 0; i < rcode->arr_used; i++) { |
sewardj | fbcaf33 | 2004-07-08 01:46:01 +0000 | [diff] [blame] | 274 | ppX86Instr(stdout, rcode->arr[i]); |
| 275 | printf("\n"); |
sewardj | 2cd80dc | 2004-07-02 15:20:40 +0000 | [diff] [blame] | 276 | } |
sewardj | 0ec3325 | 2004-07-03 13:30:00 +0000 | [diff] [blame] | 277 | printf("\n"); |
| 278 | } |
| 279 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 282 | #endif |