blob: ad1c7175fab956fed5859dd8608549004be8d31a [file] [log] [blame]
sewardjc97096c2004-06-30 09:28:04 +00001
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>
sewardjced9fe52004-07-07 11:55:36 +000011#include <assert.h>
sewardjc97096c2004-06-30 09:28:04 +000012
sewardjced9fe52004-07-07 11:55:36 +000013#include "libvex_basictypes.h"
14#include "libvex.h"
sewardjc97096c2004-06-30 09:28:04 +000015
16/*---------------------------------------------------------------*/
17/*--- Test ---*/
18/*---------------------------------------------------------------*/
19
sewardjce605f92004-07-05 14:39:15 +000020
sewardj2b515872004-07-05 20:50:45 +000021__attribute__ ((noreturn))
sewardj41f43bc2004-07-08 14:23:22 +000022static
sewardjce605f92004-07-05 14:39:15 +000023void failure_exit ( void )
24{
25 fprintf(stdout, "VEX did failure_exit. Bye.\n");
26 exit(1);
27}
28
sewardj41f43bc2004-07-08 14:23:22 +000029static
sewardjce605f92004-07-05 14:39:15 +000030void log_bytes ( Char* bytes, Int nbytes )
31{
32 fwrite ( bytes, 1, nbytes, stdout );
33}
34
sewardjced9fe52004-07-07 11:55:36 +000035#define N_LINEBUF 10000
sewardj41f43bc2004-07-08 14:23:22 +000036static Char linebuf[N_LINEBUF];
sewardjced9fe52004-07-07 11:55:36 +000037
38#define N_ORIGBUF 200
39#define N_TRANSBUF 1000
40
sewardj41f43bc2004-07-08 14:23:22 +000041static Char origbuf[N_ORIGBUF];
42static Char transbuf[N_TRANSBUF];
sewardjced9fe52004-07-07 11:55:36 +000043
sewardj1f40a0a2004-07-21 12:28:07 +000044static Bool verbose = True;
sewardjced9fe52004-07-07 11:55:36 +000045
46int main ( int argc, char** argv )
sewardjce605f92004-07-05 14:39:15 +000047{
sewardjced9fe52004-07-07 11:55:36 +000048 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,
sewardj1f40a0a2004-07-21 12:28:07 +000067 1, /* debug_paranoia */
68 1, /* verbosity */
sewardjbad34a92004-07-22 01:14:11 +000069 //False,
70 True,
sewardje8e9d732004-07-16 21:03:45 +000071 100 );
sewardjced9fe52004-07-07 11:55:36 +000072
sewardjea64e142004-07-22 16:47:21 +000073 {extern void test_asm86(void);
74 test_asm86();
75 return 0;
76 }
77
sewardjced9fe52004-07-07 11:55:36 +000078 while (!feof(f)) {
79 fgets(linebuf, N_LINEBUF,f);
80 //printf("%s", linebuf);
81 assert(linebuf[0] != 0);
82 if (linebuf[0] != '.') continue;
83 /* first line is: . bb-number bb-addr n-bytes */
84 assert(3 == sscanf(&linebuf[1], " %d %x %d\n",
85 & bb_number,
86 & orig_addr, & orig_nbytes ));
87 assert(orig_nbytes >= 1);
88 assert(!feof(f));
89 fgets(linebuf, N_LINEBUF,f);
90 assert(linebuf[0] == '.');
91 /* second line is: . byte byte byte etc */
92 //printf("%s", linebuf);
sewardj1f40a0a2004-07-21 12:28:07 +000093 if (verbose)
94 printf("\n\n============ Basic Block %d, "
95 "Start %x, nbytes %d ============\n\n",
96 bb_number, orig_addr, orig_nbytes);
sewardjced9fe52004-07-07 11:55:36 +000097 assert(orig_nbytes >= 1 && orig_nbytes <= N_ORIGBUF);
98 for (i = 0; i < orig_nbytes; i++) {
99 assert(1 == sscanf(&linebuf[2 + 3*i], "%x", &u));
100 origbuf[i] = (UChar)u;
101 }
102
sewardj1f40a0a2004-07-21 12:28:07 +0000103 // if (bb_number == 50) exit(1);
sewardj81bd5502004-07-21 18:49:27 +0000104 {
sewardj1f40a0a2004-07-21 12:28:07 +0000105 for (i = 0; i < 1; i++)
sewardjced9fe52004-07-07 11:55:36 +0000106 tres =
107 LibVEX_Translate ( InsnSetX86, InsnSetX86,
108 origbuf, (Addr64)orig_addr, &orig_used,
109 transbuf, N_TRANSBUF, &trans_used,
110 NULL, NULL );
111 assert(tres == TransOK);
112 assert(orig_used == orig_nbytes);
sewardj1f40a0a2004-07-21 12:28:07 +0000113 }
sewardjced9fe52004-07-07 11:55:36 +0000114 }
115
116 fclose(f);
sewardj1f40a0a2004-07-21 12:28:07 +0000117 LibVEX_ClearTemporary(True);
sewardjced9fe52004-07-07 11:55:36 +0000118
119#if 0
sewardjce605f92004-07-05 14:39:15 +0000120 Int* p;
121 Int i, j, n = 0;
sewardj887a11a2004-07-05 17:26:47 +0000122 LibVEX_Init ( &failure_exit, &log_bytes,
sewardjce605f92004-07-05 14:39:15 +0000123 1, 1, False, 10 );
124 for (j = 0; j < 5000; j++) {
sewardj887a11a2004-07-05 17:26:47 +0000125 LibVEX_Clear(False);
sewardjce605f92004-07-05 14:39:15 +0000126 for (i = 0; i < 2000; i++) {
127 n++;
128 p = LibVEX_Alloc(16);
129 p[0] = p[1] = p[2] = p[3] = 44;
130 }
131 }
sewardj887a11a2004-07-05 17:26:47 +0000132 LibVEX_Clear(True);
sewardjce605f92004-07-05 14:39:15 +0000133 printf("Did %d allocs\n", n);
sewardjced9fe52004-07-07 11:55:36 +0000134#endif
sewardjce605f92004-07-05 14:39:15 +0000135 return 0;
136}
137
138/*---------------------------------------------------------------*/
139/*--- Test (old) ---*/
140/*---------------------------------------------------------------*/
141
142#if 0
143
sewardj887a11a2004-07-05 17:26:47 +0000144#include "libvex_basictypes.h"
sewardjce605f92004-07-05 14:39:15 +0000145#include "ir_defs.h"
146#include "host_regs.h"
147#include "x86h_defs.h"
148
149
sewardjc97096c2004-06-30 09:28:04 +0000150/* HACK */
151extern
sewardj2cd80dc2004-07-02 15:20:40 +0000152HInstrArray* /* not really, but for the time being ... */
sewardj194d54a2004-07-03 19:08:18 +0000153 iselBB_X86Instr ( IRBB* bb );
sewardj0ec33252004-07-03 13:30:00 +0000154
sewardjc97096c2004-06-30 09:28:04 +0000155
156int main ( void )
157{
sewardj2cd80dc2004-07-02 15:20:40 +0000158 HInstrArray* vcode;
159 IRBB* bb;
160 IRTypeEnv* env = newIRTypeEnv();
sewardjc97096c2004-06-30 09:28:04 +0000161
sewardj12952ef2004-07-03 14:52:19 +0000162 IRTemp t0 = 0;
sewardjc97096c2004-06-30 09:28:04 +0000163 IRTemp t1 = 1;
164 IRTemp t2 = 2;
165
sewardj12952ef2004-07-03 14:52:19 +0000166 addToIRTypeEnv ( env, t0, Ity_I32 );
sewardjc97096c2004-06-30 09:28:04 +0000167 addToIRTypeEnv ( env, t1, Ity_I32 );
168 addToIRTypeEnv ( env, t2, Ity_I32 );
169
sewardj12952ef2004-07-03 14:52:19 +0000170 IRStmt* s10 = IRStmt_Tmp(t0, IRExpr_Const(IRConst_U32(0x2000)));
171 IRStmt* s11 = IRStmt_Tmp(t1, IRExpr_Const(IRConst_U32(0x2001)));
172 IRStmt* s12 = IRStmt_Tmp(t2, IRExpr_Const(IRConst_U32(0x2002)));
sewardj0ec33252004-07-03 13:30:00 +0000173
sewardjc97096c2004-06-30 09:28:04 +0000174 IRStmt* s1 = IRStmt_Put(8,4, IRExpr_Const(IRConst_U32(99)) );
sewardj12952ef2004-07-03 14:52:19 +0000175#if 0
sewardjc97096c2004-06-30 09:28:04 +0000176 IRStmt* s2 = IRStmt_Put(7,4, IRExpr_Binop(Iop_Add32,
177 IRExpr_Tmp(t1),
178 IRExpr_Const(IRConst_U32(55))));
sewardj12952ef2004-07-03 14:52:19 +0000179#endif
180
181 IRStmt* s2 = IRStmt_Put(9,4,
182 IRExpr_Binop(Iop_Shl32,
183 IRExpr_Tmp(t0),
184 IRExpr_Binop(Iop_Add32,
185 IRExpr_Tmp(t1),
186 IRExpr_Tmp(t2))));
187
188
sewardj0ec33252004-07-03 13:30:00 +0000189 s10->link = s11;
sewardj12952ef2004-07-03 14:52:19 +0000190 s11->link = s12;
191 s12->link = s1;
sewardjc97096c2004-06-30 09:28:04 +0000192 s1->link = s2;
193
sewardj0ec33252004-07-03 13:30:00 +0000194 bb = mk_IRBB(env, s10, IRNext_UJump(IRConst_U32(-65565)));
sewardjc97096c2004-06-30 09:28:04 +0000195
196 printf("bb is ...\n");
197 ppIRBB(stdout, bb);
198 printf("\n");
199
sewardj3b9d65e2004-07-03 19:52:16 +0000200 if (0)
sewardj194d54a2004-07-03 19:08:18 +0000201 vcode = iselBB_X86Instr(bb);
sewardj12952ef2004-07-03 14:52:19 +0000202 else
sewardj2cd80dc2004-07-02 15:20:40 +0000203 {
sewardj0ec33252004-07-03 13:30:00 +0000204 Int i;
205 HReg vr0 = mkHReg(0, HRcInt, True);
206 HReg vr1 = mkHReg(1, HRcInt, True);
207 HReg vr2 = mkHReg(2, HRcInt, True);
208 HReg vr3 = mkHReg(3, HRcInt, True);
209 HReg eax = hregX86_EAX();
210 HReg ebx = hregX86_EBX();
211 HReg ecx = hregX86_ECX();
212 HReg edx = hregX86_EDX();
213 HReg ebp = hregX86_EBP();
214 vcode = newHInstrArray();
sewardj3b9d65e2004-07-03 19:52:16 +0000215 vcode->n_vregs = 4;
sewardj0ec33252004-07-03 13:30:00 +0000216
217 addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV,
218 X86RMI_Imm(0x10001), vr0));
219 addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV,
220 X86RMI_Imm(0x10101), vr1));
221 addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV,
222 X86RMI_Imm(0x10201), vr2));
223 addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV,
224 X86RMI_Imm(0x10301), vr3));
225
226 addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV,
227 X86RMI_Imm(0x99999), eax));
228 addHInstr(vcode, X86Instr_Alu32R(Xalu_MOV,
229 X86RMI_Imm(0x99999), edx));
230
231 addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV,
232 X86RI_Reg(vr0),
233 X86AMode_IR(0x100, ebp)));
234 addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV,
235 X86RI_Reg(vr1),
236 X86AMode_IR(0x101, ebp)));
237 addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV,
238 X86RI_Reg(vr2),
239 X86AMode_IR(0x101, ebp)));
240 addHInstr(vcode, X86Instr_Alu32M(Xalu_MOV,
241 X86RI_Reg(vr3),
242 X86AMode_IR(0x101, ebp)));
243 printf("\nBefore\n");
244 for (i = 0; i < vcode->arr_used; i++) {
245 ppX86Instr(stdout, vcode->arr[i]);
246 printf("\n");
247 }
248 printf("\n");
249 }
250
251 {
252 Int i;
sewardj2cd80dc2004-07-02 15:20:40 +0000253 HInstrArray* rcode;
254 HReg rregs_to_use[4];
255 rregs_to_use[0] = hregX86_EAX();
256 rregs_to_use[1] = hregX86_EBX();
257 rregs_to_use[2] = hregX86_ECX();
258 rregs_to_use[3] = hregX86_EDX();
sewardjc97096c2004-06-30 09:28:04 +0000259
sewardj2cd80dc2004-07-02 15:20:40 +0000260 rcode =
sewardj194d54a2004-07-03 19:08:18 +0000261 doRegisterAllocation(vcode,
sewardj3b9d65e2004-07-03 19:52:16 +0000262 rregs_to_use, 3, /* rregs */
sewardj12952ef2004-07-03 14:52:19 +0000263 isMove_X86Instr,
sewardj2cd80dc2004-07-02 15:20:40 +0000264 getRegUsage_X86Instr,
265 mapRegs_X86Instr,
sewardj0ec33252004-07-03 13:30:00 +0000266 genSpill_X86,
267 genReload_X86
sewardj2cd80dc2004-07-02 15:20:40 +0000268 );
269
sewardj0ec33252004-07-03 13:30:00 +0000270 printf("\nAfter\n");
271 for (i = 0; i < rcode->arr_used; i++) {
sewardjfbcaf332004-07-08 01:46:01 +0000272 ppX86Instr(stdout, rcode->arr[i]);
273 printf("\n");
sewardj2cd80dc2004-07-02 15:20:40 +0000274 }
sewardj0ec33252004-07-03 13:30:00 +0000275 printf("\n");
276 }
277
sewardjc97096c2004-06-30 09:28:04 +0000278 return 0;
279}
sewardjce605f92004-07-05 14:39:15 +0000280#endif