libvex_ir.h revision 35421a3cfd43bc829d27ee15bd34bbc7cb690805
1
2/*---------------------------------------------------------------*/
3/*---                                                         ---*/
4/*--- This file (libjit_ir.h) is                              ---*/
5/*--- Copyright (c) 2004 OpenWorks LLP.  All rights reserved. ---*/
6/*---                                                         ---*/
7/*---------------------------------------------------------------*/
8
9#ifndef __LIBJIT_IR_H
10#define __LIBJIT_IR_H
11
12#include "libjit_basictypes.h"
13
14
15/*---------------------------------------------------------------*/
16/*--- Type definitions for the IR                             ---*/
17/*---------------------------------------------------------------*/
18
19/* ------------------ Types ------------------ */
20
21typedef
22   enum { Ity_Bit, Ity_I8, Ity_I16, Ity_I32, Ity_I64 }
23   IRType;
24
25extern void ppIRType ( IRType );
26
27
28/* ------------------ Constants ------------------ */
29
30typedef
31   enum { Ico_U8, Ico_U16, Ico_U32, Ico_U64 }
32   IRConstTag;
33
34typedef
35   struct _IRConst {
36      IRConstTag tag;
37      union {
38         UChar  U8;
39         UShort U16;
40         UInt   U32;
41         ULong  U64;
42      } Ico;
43   }
44   IRConst;
45
46extern IRConst* IRConst_U8  ( UChar );
47extern IRConst* IRConst_U16 ( UShort );
48extern IRConst* IRConst_U32 ( UInt );
49extern IRConst* IRConst_U64 ( ULong );
50
51extern void ppIRConst ( IRConst* );
52
53
54/* ------------------ Temporaries ------------------ */
55
56typedef int IRTemp;
57
58extern void ppIRTemp ( IRTemp );
59
60
61/* ------------------ Binary and unary ops ------------------ */
62
63typedef
64   enum { Iop_Add32,
65          Iop_Sub32,
66          Iop_Mul32,
67          Iop_Or32,
68          Iop_And32,
69          Iop_Xor32,
70          Iop_Shl32,
71          Iop_Shr32,
72          Iop_Sar32,
73          /* Tags for unary ops */
74          Iop_Not32,
75          Iop_Neg32
76   }
77   IROp;
78
79extern void ppIROp ( IROp );
80
81
82/* ------------------ Expressions ------------------ */
83/*
84data Expr
85   = GET   Int Int         -- offset, size
86   | TMP   Temp            -- value of temporary
87   | BINOP Op Expr Expr    -- binary op
88   | UNOP  Op Expr         -- unary op
89   | LDle  Type Expr       -- load of the given type, Expr:: 32 or 64
90   | CONST Const           -- 8/16/32/64-bit int constant
91*/
92typedef
93   enum { Iex_Get, Iex_Tmp, Iex_Binop, Iex_Unop, Iex_LDle, Iex_Const }
94   IRExprTag;
95
96typedef
97   struct _IRExpr {
98      IRExprTag tag;
99      union {
100         struct {
101            Int offset;
102            Int size;
103         } Get;
104         struct {
105            IRTemp tmp;
106         } Tmp;
107         struct {
108            IROp op;
109            struct _IRExpr* arg1;
110            struct _IRExpr* arg2;
111         } Binop;
112         struct {
113            IROp op;
114            struct _IRExpr* arg;
115         } Unop;
116         struct {
117            IRType ty;
118            struct _IRExpr* addr;
119         } LDle;
120         struct {
121            IRConst* con;
122         } Const;
123      } Iex;
124   }
125   IRExpr;
126
127extern IRExpr* IRExpr_Get   ( Int off, Int sz );
128extern IRExpr* IRExpr_Tmp   ( IRTemp tmp );
129extern IRExpr* IRExpr_Binop ( IROp op, IRExpr* arg1, IRExpr* arg2 );
130extern IRExpr* IRExpr_Unop  ( IROp op, IRExpr* arg );
131extern IRExpr* IRExpr_LDle  ( IRType ty, IRExpr* addr );
132extern IRExpr* IRExpr_Const ( IRConst* con );
133
134extern void ppIRExpr ( IRExpr* );
135
136
137/* ------------------ Statements ------------------ */
138/*
139data Stmt
140   = PUT    Int Int Expr      -- offset, size, value
141   | TMP    Temp Expr         -- store value in Temp
142   | STle   Expr Expr         -- address (32 or 64 bit), value
143*/
144typedef
145   enum { Ist_Put, Ist_Tmp, Ist_STle }
146   IRStmtTag;
147
148typedef
149   struct _IRStmt {
150      IRStmtTag tag;
151      union {
152         struct {
153            Int     offset;
154            Int     size;
155            IRExpr* expr;
156         } Put;
157         struct {
158            IRTemp  tmp;
159            IRExpr* expr;
160         } Tmp;
161         struct {
162            IRExpr* addr;
163            IRExpr* data;
164         } STle;
165      } Ist;
166      struct _IRStmt* link;
167   }
168   IRStmt;
169
170extern IRStmt* IRStmt_Put  ( Int off, Int sz, IRExpr* value );
171extern IRStmt* IRStmt_Tmp  ( IRTemp tmp, IRExpr* expr );
172extern IRStmt* IRStmt_STle ( IRExpr* addr, IRExpr* value );
173
174extern void ppIRStmt ( IRStmt* );
175
176
177/* ------------------ Basic block enders. ------------------ */
178/*
179   IRConst represents a guest address, which is either a
180   32 or 64 bit integer, depending on the architecture we're simulating.
181
182data Next
183   = UJump    Const              -- unconditional jump
184   | CJump01  Expr  Const Const  -- conditional jump, Expr::TY_Bit
185   | IJump    Expr               -- jump to unknown address
186*/
187typedef
188   enum { Inx_UJump, Inx_CJump01, Inx_IJump }
189   IRNextTag;
190
191typedef
192   struct {
193      IRNextTag tag;
194      union {
195         struct {
196            IRConst* dst;
197         } UJump;
198         struct {
199            IRExpr*  cond;
200            IRConst* dst0;
201            IRConst* dst1;
202         } CJump01;
203         struct {
204            IRExpr* dst;
205         } IJump;
206      } Inx;
207   }
208   IRNext;
209
210extern IRNext* IRNext_UJump ( IRConst* dst );
211
212extern void ppIRNext ( IRNext* );
213
214
215/* ------------------ Basic Blocks ------------------ */
216
217/* A bunch of statements, expressions, etc, are incomplete without an
218   environment indicating the type of each IRTemp.  So this provides
219   one.
220*/
221typedef
222   struct {
223      IRTemp name;
224      IRType type;
225   }
226   IRTypeEnvMaplet;
227
228typedef
229   struct {
230      IRTypeEnvMaplet* map;
231      Int map_size;
232      Int map_used;
233   }
234   IRTypeEnv;
235
236extern void ppIRTypeEnv ( IRTypeEnv* );
237
238
239/* Basic blocks contain 3 fields:
240   - A table giving a type for each temp
241   - A list of statements
242   - A Next
243*/
244typedef
245   struct _IRBB {
246      IRTypeEnv* tyenv;
247      IRStmt*    stmts;
248      IRNext*    next;
249   }
250   IRBB;
251
252extern IRBB* mk_IRBB ( IRTypeEnv*, IRStmt*, IRNext* );
253
254extern void ppIRBB ( IRBB* );
255
256
257/*---------------------------------------------------------------*/
258/*--- Helper functions for the IR                             ---*/
259/*---------------------------------------------------------------*/
260
261/* For messing with IR type environments */
262extern IRTypeEnv* newIRTypeEnv    ( void );
263extern void       addToIRTypeEnv  ( IRTypeEnv*, IRTemp, IRType );
264extern IRType     lookupIRTypeEnv ( IRTypeEnv*, IRTemp );
265
266/* What is the type of this expression? */
267extern IRType typeOfIRExpr ( IRTypeEnv*, IRExpr* );
268
269
270#endif /* ndef __LIBJIT_IR_H */
271
272
273/*---------------------------------------------------------------*/
274/*---                                             libjit_ir.h ---*/
275/*---------------------------------------------------------------*/
276