blob: ae3082cd418393e15a6b2e2d8514b15807b1c960 [file] [log] [blame]
sewardjec6ad592004-06-20 12:26:53 +00001
2/*---------------------------------------------------------------*/
3/*--- ---*/
4/*--- This file (ir_defs.h) is ---*/
5/*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/
6/*--- ---*/
7/*---------------------------------------------------------------*/
8
9#ifndef __IR_DEFS_H
10#define __IR_DEFS_H
11
sewardjec6ad592004-06-20 12:26:53 +000012
13/*---------------------------------------------------------------*/
sewardjac6b7122004-06-27 01:03:57 +000014/*--- Type definitions for the IR ---*/
sewardjec6ad592004-06-20 12:26:53 +000015/*---------------------------------------------------------------*/
16
sewardjc97096c2004-06-30 09:28:04 +000017/* ------------------ Types ------------------ */
sewardje3d0d2e2004-06-27 10:42:44 +000018
19typedef
20 enum { Ity_Bit, Ity_I8, Ity_I16, Ity_I32, Ity_I64 }
21 IRType;
22
sewardjc97096c2004-06-30 09:28:04 +000023extern void ppIRType ( FILE* f, IRType );
sewardje3d0d2e2004-06-27 10:42:44 +000024
sewardjc97096c2004-06-30 09:28:04 +000025
26/* ------------------ Constants ------------------ */
sewardjec6ad592004-06-20 12:26:53 +000027
sewardjac6b7122004-06-27 01:03:57 +000028typedef
sewardjc97096c2004-06-30 09:28:04 +000029 enum { Ico_U8, Ico_U16, Ico_U32, Ico_U64 }
sewardjac6b7122004-06-27 01:03:57 +000030 IRConstTag;
31
32typedef
sewardje3d0d2e2004-06-27 10:42:44 +000033 struct _IRConst {
sewardjac6b7122004-06-27 01:03:57 +000034 IRConstTag tag;
35 union {
sewardjc97096c2004-06-30 09:28:04 +000036 UChar U8;
37 UShort U16;
38 UInt U32;
39 ULong U64;
sewardjac6b7122004-06-27 01:03:57 +000040 } Ico;
41 }
42 IRConst;
sewardjec6ad592004-06-20 12:26:53 +000043
sewardjc97096c2004-06-30 09:28:04 +000044extern IRConst* IRConst_U8 ( UChar );
45extern IRConst* IRConst_U16 ( UShort );
46extern IRConst* IRConst_U32 ( UInt );
47extern IRConst* IRConst_U64 ( ULong );
sewardjec6ad592004-06-20 12:26:53 +000048
sewardjc97096c2004-06-30 09:28:04 +000049extern void ppIRConst ( FILE* f, IRConst* );
50
51
52/* ------------------ Temporaries ------------------ */
sewardjec6ad592004-06-20 12:26:53 +000053
sewardjac6b7122004-06-27 01:03:57 +000054typedef int IRTemp;
sewardjec6ad592004-06-20 12:26:53 +000055
sewardjc97096c2004-06-30 09:28:04 +000056extern void ppIRTemp ( FILE* f, IRTemp );
sewardjec6ad592004-06-20 12:26:53 +000057
sewardjc97096c2004-06-30 09:28:04 +000058
59/* ------------------ Binary and unary ops ------------------ */
sewardjec6ad592004-06-20 12:26:53 +000060
sewardjac6b7122004-06-27 01:03:57 +000061typedef
62 enum { Iop_Add32,
63 Iop_Sub32,
64 Iop_Mul32,
65 Iop_Or32,
66 Iop_And32,
67 Iop_Xor32,
68 Iop_Shl32,
69 Iop_Shr32,
70 Iop_Sar32,
sewardje3d0d2e2004-06-27 10:42:44 +000071 /* Tags for unary ops */
sewardjac6b7122004-06-27 01:03:57 +000072 Iop_Not32,
73 Iop_Neg32
74 }
75 IROp;
sewardjec6ad592004-06-20 12:26:53 +000076
sewardjc97096c2004-06-30 09:28:04 +000077extern void ppIROp ( FILE* f, IROp );
sewardjec6ad592004-06-20 12:26:53 +000078
sewardje3d0d2e2004-06-27 10:42:44 +000079
sewardjc97096c2004-06-30 09:28:04 +000080/* ------------------ Expressions ------------------ */
81/*
sewardje3d0d2e2004-06-27 10:42:44 +000082data Expr
83 = GET Int Int -- offset, size
84 | TMP Temp -- value of temporary
85 | BINOP Op Expr Expr -- binary op
86 | UNOP Op Expr -- unary op
87 | LDle Type Expr -- load of the given type, Expr:: 32 or 64
88 | CONST Const -- 8/16/32/64-bit int constant
89*/
90typedef
91 enum { Iex_Get, Iex_Tmp, Iex_Binop, Iex_Unop, Iex_LDle, Iex_Const }
92 IRExprTag;
93
94typedef
95 struct _IRExpr {
96 IRExprTag tag;
97 union {
98 struct {
99 Int offset;
100 Int size;
101 } Get;
102 struct {
103 IRTemp tmp;
104 } Tmp;
105 struct {
106 IROp op;
107 struct _IRExpr* arg1;
108 struct _IRExpr* arg2;
109 } Binop;
110 struct {
111 IROp op;
112 struct _IRExpr* arg;
113 } Unop;
114 struct {
115 IRType ty;
116 struct _IRExpr* addr;
117 } LDle;
118 struct {
sewardj66f2f792004-06-30 16:37:16 +0000119 IRConst* con;
sewardje3d0d2e2004-06-27 10:42:44 +0000120 } Const;
121 } Iex;
122 }
123 IRExpr;
124
sewardjc97096c2004-06-30 09:28:04 +0000125extern IRExpr* IRExpr_Get ( Int off, Int sz );
126extern IRExpr* IRExpr_Tmp ( IRTemp tmp );
127extern IRExpr* IRExpr_Binop ( IROp op, IRExpr* arg1, IRExpr* arg2 );
128extern IRExpr* IRExpr_Unop ( IROp op, IRExpr* arg );
129extern IRExpr* IRExpr_LDle ( IRType ty, IRExpr* addr );
130extern IRExpr* IRExpr_Const ( IRConst* con );
sewardje3d0d2e2004-06-27 10:42:44 +0000131
sewardjc97096c2004-06-30 09:28:04 +0000132extern void ppIRExpr ( FILE* f, IRExpr* );
sewardjec6ad592004-06-20 12:26:53 +0000133
sewardjc97096c2004-06-30 09:28:04 +0000134
135/* ------------------ Statements ------------------ */
136/*
sewardjec6ad592004-06-20 12:26:53 +0000137data Stmt
138 = PUT Int Int Expr -- offset, size, value
139 | TMP Temp Expr -- store value in Temp
140 | STle Expr Expr -- address (32 or 64 bit), value
141*/
sewardjac6b7122004-06-27 01:03:57 +0000142typedef
143 enum { Ist_Put, Ist_Tmp, Ist_STle }
144 IRStmtTag;
145
146typedef
147 struct _IRStmt {
148 IRStmtTag tag;
149 union {
150 struct {
151 Int offset;
152 Int size;
sewardjc97096c2004-06-30 09:28:04 +0000153 IRExpr* expr;
sewardjac6b7122004-06-27 01:03:57 +0000154 } Put;
155 struct {
156 IRTemp tmp;
sewardjc97096c2004-06-30 09:28:04 +0000157 IRExpr* expr;
sewardjac6b7122004-06-27 01:03:57 +0000158 } Tmp;
159 struct {
sewardjc97096c2004-06-30 09:28:04 +0000160 IRExpr* addr;
161 IRExpr* data;
sewardjac6b7122004-06-27 01:03:57 +0000162 } STle;
163 } Ist;
164 struct _IRStmt* link;
165 }
166 IRStmt;
sewardjec6ad592004-06-20 12:26:53 +0000167
sewardjc97096c2004-06-30 09:28:04 +0000168extern IRStmt* IRStmt_Put ( Int off, Int sz, IRExpr* value );
169extern IRStmt* IRStmt_Tmp ( IRTemp tmp, IRExpr* expr );
170extern IRStmt* IRStmt_STle ( IRExpr* addr, IRExpr* value );
sewardjec6ad592004-06-20 12:26:53 +0000171
sewardjc97096c2004-06-30 09:28:04 +0000172extern void ppIRStmt ( FILE* f, IRStmt* );
173
174
175/* ------------------ Basic block enders. ------------------ */
176/*
sewardjac6b7122004-06-27 01:03:57 +0000177 IRConst represents a guest address, which is either a
sewardjec6ad592004-06-20 12:26:53 +0000178 32 or 64 bit integer, depending on the architecture we're simulating.
179
180data Next
sewardjac6b7122004-06-27 01:03:57 +0000181 = UJump Const -- unconditional jump
182 | CJump01 Expr Const Const -- conditional jump, Expr::TY_Bit
sewardjec6ad592004-06-20 12:26:53 +0000183 | IJump Expr -- jump to unknown address
184*/
sewardjac6b7122004-06-27 01:03:57 +0000185typedef
186 enum { Inx_UJump, Inx_CJump01, Inx_IJump }
187 IRNextTag;
188
189typedef
sewardj66f2f792004-06-30 16:37:16 +0000190 struct {
sewardjac6b7122004-06-27 01:03:57 +0000191 IRNextTag tag;
192 union {
193 struct {
sewardj66f2f792004-06-30 16:37:16 +0000194 IRConst* dst;
sewardjac6b7122004-06-27 01:03:57 +0000195 } UJump;
196 struct {
sewardj66f2f792004-06-30 16:37:16 +0000197 IRExpr* cond;
198 IRConst* dst0;
199 IRConst* dst1;
sewardjac6b7122004-06-27 01:03:57 +0000200 } CJump01;
201 struct {
sewardj66f2f792004-06-30 16:37:16 +0000202 IRExpr* dst;
sewardjac6b7122004-06-27 01:03:57 +0000203 } IJump;
204 } Inx;
205 }
206 IRNext;
sewardjec6ad592004-06-20 12:26:53 +0000207
sewardjc97096c2004-06-30 09:28:04 +0000208extern IRNext* IRNext_UJump ( IRConst* dst );
209
210extern void ppIRNext ( FILE* f, IRNext* );
211
212
213/* ------------------ Basic Blocks ------------------ */
214
215/* A bunch of statements, expressions, etc, are incomplete without an
216 environment indicating the type of each IRTemp. So this provides
217 one.
218*/
219typedef
220 struct {
221 IRTemp name;
222 IRType type;
223 }
224 IRTypeEnvMaplet;
225
226typedef
227 struct {
228 IRTypeEnvMaplet* map;
229 Int map_size;
230 Int map_used;
231 }
232 IRTypeEnv;
233
234extern void ppIRTypeEnv ( FILE* f, IRTypeEnv* );
235
sewardjec6ad592004-06-20 12:26:53 +0000236
sewardjec6ad592004-06-20 12:26:53 +0000237/* Basic blocks contain 3 fields:
sewardjc97096c2004-06-30 09:28:04 +0000238 - A table giving a type for each temp
sewardjec6ad592004-06-20 12:26:53 +0000239 - A list of statements
240 - A Next
241*/
sewardjac6b7122004-06-27 01:03:57 +0000242typedef
243 struct _IRBB {
sewardjc97096c2004-06-30 09:28:04 +0000244 IRTypeEnv* tyenv;
245 IRStmt* stmts;
246 IRNext* next;
sewardjac6b7122004-06-27 01:03:57 +0000247 }
248 IRBB;
sewardjec6ad592004-06-20 12:26:53 +0000249
sewardjc97096c2004-06-30 09:28:04 +0000250extern IRBB* mk_IRBB ( IRTypeEnv*, IRStmt*, IRNext* );
251
252extern void ppIRBB ( FILE* f, IRBB* );
253
sewardjec6ad592004-06-20 12:26:53 +0000254
255/*---------------------------------------------------------------*/
sewardjc97096c2004-06-30 09:28:04 +0000256/*--- Helper functions for the IR ---*/
sewardjec6ad592004-06-20 12:26:53 +0000257/*---------------------------------------------------------------*/
258
sewardjc97096c2004-06-30 09:28:04 +0000259/* For messing with IR type environments */
260extern IRTypeEnv* newIRTypeEnv ( void );
261extern void addToIRTypeEnv ( IRTypeEnv*, IRTemp, IRType );
262extern IRType lookupIRTypeEnv ( IRTypeEnv*, IRTemp );
sewardjec6ad592004-06-20 12:26:53 +0000263
sewardjc97096c2004-06-30 09:28:04 +0000264/* What is the type of this expression? */
265extern IRType typeOfIRExpr ( IRTypeEnv*, IRExpr* );
sewardjec6ad592004-06-20 12:26:53 +0000266
267
268#endif /* ndef __IR_DEFS_H */