blob: 61e03f5ef9cb6c69f53a55b91a71609584c28f39 [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 */
sewardj750f4072004-07-26 22:39:11 +000068 0, /* 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
sewardj86898e82004-07-22 17:26:12 +000073#if 0
sewardjea64e142004-07-22 16:47:21 +000074 {extern void test_asm86(void);
75 test_asm86();
76 return 0;
77 }
sewardj86898e82004-07-22 17:26:12 +000078#endif
sewardjea64e142004-07-22 16:47:21 +000079
sewardjced9fe52004-07-07 11:55:36 +000080 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);
sewardj1f40a0a2004-07-21 12:28:07 +000095 if (verbose)
96 printf("\n\n============ Basic Block %d, "
97 "Start %x, nbytes %d ============\n\n",
98 bb_number, orig_addr, orig_nbytes);
sewardjced9fe52004-07-07 11:55:36 +000099 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
sewardj1f40a0a2004-07-21 12:28:07 +0000105 // if (bb_number == 50) exit(1);
sewardj81bd5502004-07-21 18:49:27 +0000106 {
sewardj1f40a0a2004-07-21 12:28:07 +0000107 for (i = 0; i < 1; i++)
sewardjced9fe52004-07-07 11:55:36 +0000108 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);
sewardj1f40a0a2004-07-21 12:28:07 +0000115 }
sewardjced9fe52004-07-07 11:55:36 +0000116 }
117
118 fclose(f);
sewardj1f40a0a2004-07-21 12:28:07 +0000119 LibVEX_ClearTemporary(True);
sewardjced9fe52004-07-07 11:55:36 +0000120
121#if 0
sewardjce605f92004-07-05 14:39:15 +0000122 Int* p;
123 Int i, j, n = 0;
sewardj887a11a2004-07-05 17:26:47 +0000124 LibVEX_Init ( &failure_exit, &log_bytes,
sewardjce605f92004-07-05 14:39:15 +0000125 1, 1, False, 10 );
126 for (j = 0; j < 5000; j++) {
sewardj887a11a2004-07-05 17:26:47 +0000127 LibVEX_Clear(False);
sewardjce605f92004-07-05 14:39:15 +0000128 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 }
sewardj887a11a2004-07-05 17:26:47 +0000134 LibVEX_Clear(True);
sewardjce605f92004-07-05 14:39:15 +0000135 printf("Did %d allocs\n", n);
sewardjced9fe52004-07-07 11:55:36 +0000136#endif
sewardjce605f92004-07-05 14:39:15 +0000137 return 0;
138}
139
140/*---------------------------------------------------------------*/
141/*--- Test (old) ---*/
142/*---------------------------------------------------------------*/
143
144#if 0
145
sewardj887a11a2004-07-05 17:26:47 +0000146#include "libvex_basictypes.h"
sewardjce605f92004-07-05 14:39:15 +0000147#include "ir_defs.h"
148#include "host_regs.h"
149#include "x86h_defs.h"
150
151
sewardjc97096c2004-06-30 09:28:04 +0000152/* HACK */
153extern
sewardj2cd80dc2004-07-02 15:20:40 +0000154HInstrArray* /* not really, but for the time being ... */
sewardj194d54a2004-07-03 19:08:18 +0000155 iselBB_X86Instr ( IRBB* bb );
sewardj0ec33252004-07-03 13:30:00 +0000156
sewardjc97096c2004-06-30 09:28:04 +0000157
158int main ( void )
159{
sewardj2cd80dc2004-07-02 15:20:40 +0000160 HInstrArray* vcode;
161 IRBB* bb;
162 IRTypeEnv* env = newIRTypeEnv();
sewardjc97096c2004-06-30 09:28:04 +0000163
sewardj12952ef2004-07-03 14:52:19 +0000164 IRTemp t0 = 0;
sewardjc97096c2004-06-30 09:28:04 +0000165 IRTemp t1 = 1;
166 IRTemp t2 = 2;
167
sewardj12952ef2004-07-03 14:52:19 +0000168 addToIRTypeEnv ( env, t0, Ity_I32 );
sewardjc97096c2004-06-30 09:28:04 +0000169 addToIRTypeEnv ( env, t1, Ity_I32 );
170 addToIRTypeEnv ( env, t2, Ity_I32 );
171
sewardj12952ef2004-07-03 14:52:19 +0000172 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)));
sewardj0ec33252004-07-03 13:30:00 +0000175
sewardjc97096c2004-06-30 09:28:04 +0000176 IRStmt* s1 = IRStmt_Put(8,4, IRExpr_Const(IRConst_U32(99)) );
sewardj12952ef2004-07-03 14:52:19 +0000177#if 0
sewardjc97096c2004-06-30 09:28:04 +0000178 IRStmt* s2 = IRStmt_Put(7,4, IRExpr_Binop(Iop_Add32,
179 IRExpr_Tmp(t1),
180 IRExpr_Const(IRConst_U32(55))));
sewardj12952ef2004-07-03 14:52:19 +0000181#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
sewardj0ec33252004-07-03 13:30:00 +0000191 s10->link = s11;
sewardj12952ef2004-07-03 14:52:19 +0000192 s11->link = s12;
193 s12->link = s1;
sewardjc97096c2004-06-30 09:28:04 +0000194 s1->link = s2;
195
sewardj0ec33252004-07-03 13:30:00 +0000196 bb = mk_IRBB(env, s10, IRNext_UJump(IRConst_U32(-65565)));
sewardjc97096c2004-06-30 09:28:04 +0000197
198 printf("bb is ...\n");
199 ppIRBB(stdout, bb);
200 printf("\n");
201
sewardj3b9d65e2004-07-03 19:52:16 +0000202 if (0)
sewardj194d54a2004-07-03 19:08:18 +0000203 vcode = iselBB_X86Instr(bb);
sewardj12952ef2004-07-03 14:52:19 +0000204 else
sewardj2cd80dc2004-07-02 15:20:40 +0000205 {
sewardj0ec33252004-07-03 13:30:00 +0000206 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();
sewardj3b9d65e2004-07-03 19:52:16 +0000217 vcode->n_vregs = 4;
sewardj0ec33252004-07-03 13:30:00 +0000218
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;
sewardj2cd80dc2004-07-02 15:20:40 +0000255 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();
sewardjc97096c2004-06-30 09:28:04 +0000261
sewardj2cd80dc2004-07-02 15:20:40 +0000262 rcode =
sewardj194d54a2004-07-03 19:08:18 +0000263 doRegisterAllocation(vcode,
sewardj3b9d65e2004-07-03 19:52:16 +0000264 rregs_to_use, 3, /* rregs */
sewardj12952ef2004-07-03 14:52:19 +0000265 isMove_X86Instr,
sewardj2cd80dc2004-07-02 15:20:40 +0000266 getRegUsage_X86Instr,
267 mapRegs_X86Instr,
sewardj0ec33252004-07-03 13:30:00 +0000268 genSpill_X86,
269 genReload_X86
sewardj2cd80dc2004-07-02 15:20:40 +0000270 );
271
sewardj0ec33252004-07-03 13:30:00 +0000272 printf("\nAfter\n");
273 for (i = 0; i < rcode->arr_used; i++) {
sewardjfbcaf332004-07-08 01:46:01 +0000274 ppX86Instr(stdout, rcode->arr[i]);
275 printf("\n");
sewardj2cd80dc2004-07-02 15:20:40 +0000276 }
sewardj0ec33252004-07-03 13:30:00 +0000277 printf("\n");
278 }
279
sewardjc97096c2004-06-30 09:28:04 +0000280 return 0;
281}
sewardjce605f92004-07-05 14:39:15 +0000282#endif