Otawa  0.10
inst.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * sem module interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2009, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 
24 #ifndef OTAWA_SEM_INST_H_
25 #define OTAWA_SEM_INST_H_
26 
27 #include <elm/io.h>
28 #include <elm/genstruct/Vector.h>
29 
30 namespace otawa {
31 
32 using namespace elm;
33 
34 namespace hard { class Platform; }
35 
36 namespace sem {
37 
38 // type of instruction
39 // NOTE: a, b, d, cond, sr, jump, type, addr, reg are field of "inst" class
40 typedef enum opcode {
41  NOP = 0,
42  BRANCH, // perform a branch on content of register a
43  TRAP, // perform a trap
44  CONT, // stop the execution of the block
45  IF, // continue if condition cond is met in register sr, else skip "jump" instructions
46  LOAD, // rd <- MEM_rb(ra)
47  STORE, // MEM_rb(ra) <- rd
48  SCRATCH, // d <- T
49  SET, // d <- a
50  SETI, // d <- cst
51  SETP, // page(d) <- cst
52  CMP, // d <- a ~ b
53  CMPU, // d <- a ~u b
54  ADD, // d <- a + b
55  SUB, // d <- a - b
56  SHL, // d <- unsigned(a) << b
57  SHR, // d <- unsigned(a) >> b
58  ASR, // d <- a >> b
59  NEG, // d <- -a
60  NOT, // d <- ~a
61  AND, // d <- a & b
62  OR, // d <- a | b
63  XOR, // d <- a ^ b
64  MUL, // d <- a * b
65  MULU, // d <- unsigned(a) * unsigned(b)
66  DIV, // d <- a / b
67  DIVU, // d <- unsigned(a) / unsigned(b)
68  MOD, // d <- a % b
69  MODU, // d <- unsigned(a) % unsigned(b)
70  SPEC, // special instruction (d: code, cst: sub-code)
71  MULH // d <- (a * b) >> bitlength(d)
72 } opcode;
73 
74 
75 // type of conditions
76 typedef enum cond_t {
77  NO_COND = 0,
78  EQ,
79  LT,
80  LE,
81  GE,
82  GT,
83  ANY_COND = 8,
84  NE,
85  ULT,
86  ULE,
87  UGE,
88  UGT,
90 } cond_t;
91 
92 
93 // types for load and store
94 typedef enum type_t {
95  NO_TYPE = 0,
96  INT8 = 1,
97  INT16 = 2,
98  INT32 = 3,
99  INT64 = 4,
100  UINT8 = 5,
101  UINT16 = 6,
102  UINT32 = 7,
103  UINT64 = 8,
104  FLOAT32 = 9,
105  FLOAT64 = 10,
106  MAX_TYPE = 11
107 } type_t;
108 
109 // inst type
110 typedef struct inst {
113  union {
114  t::uint32 cst; // set, seti, setp
115  struct { t::int16 a, b; } regs; // others
116  } args;
117 
118  inst(void): op(NOP), _d(0) { }
119  inst(opcode _op): op(_op), _d(0) { }
120  inst(opcode _op, int d): op(_op)
121  { _d = d; }
122  inst(opcode _op, int d, int a): op(_op)
123  { _d = d; args.regs.a = a; }
124  inst(opcode _op, int d, int a, int b): op(_op)
125  { _d = d; args.regs.a = a; args.regs.b = b; }
126 
127  inline t::int16 d(void) const { return _d; }
128  inline t::int16 a(void) const { return args.regs.a; }
129  inline t::int16 b(void) const { return args.regs.b; }
130 
131  // seti/setp instruction
132  inline t::uint32 cst(void) const { return args.cst; }
133 
134  // load/store instruction
135  inline t::int16 reg(void) const { return d(); }
136  inline t::int16 addr(void) const { return a(); }
137  inline type_t type(void) const { return type_t(b()); }
138 
139  // "if" instruction
140  inline cond_t cond(void) const { return cond_t(d()); }
141  inline t::int16 sr(void) const { return a(); }
142  inline t::uint16 jump(void) const { return b(); }
143 
144  void print(elm::io::Output& out) const;
145 } inst;
146 inline elm::io::Output& operator<<(elm::io::Output& out, inst i) { i.print(out); return out; }
147 
148 inline inst nop(void) { return inst(NOP); }
149 inline inst branch(int to) { return inst(BRANCH, to); }
150 inline inst trap(void) { return inst(TRAP); }
151 inline inst cont(void) { return inst(CONT); }
152 inline inst _if(int cond, int sr, int jump) { ASSERT(cond >= 0 && cond < MAX_COND); return inst(IF, cond, sr, jump); }
153 inline inst load(int d, int a, int t) { return inst(LOAD, d, a, t); }
154 inline inst load(int d, int a, type_t t) { return inst(LOAD, d, a, t); }
155 inline inst store(int d, int a, int t) { return inst(STORE, d, a, t); }
156 inline inst store(int d, int a, type_t t) { return inst(STORE, d, a, t); }
157 inline inst scratch(int d) { return inst(SCRATCH, d); }
158 inline inst set(int d, int a) { return inst(SET, d, a); }
159 inline inst seti(int d, unsigned long cst) { inst i(SETI, d); i.args.cst = cst; return i; }
160 inline inst setp(int d, unsigned long cst) { inst i(SETP, d); i.args.cst = cst; return i; }
161 inline inst cmp(int d, int a, int b) { return inst(CMP, d, a, b); }
162 inline inst cmpu(int d, int a, int b) { return inst(CMPU, d, a, b); }
163 inline inst add(int d, int a, int b) { return inst(ADD, d, a, b); }
164 inline inst sub(int d, int a, int b) { return inst(SUB, d, a, b); }
165 inline inst shl(int d, int a, int b) { return inst(SHL, d, a, b); }
166 inline inst shr(int d, int a, int b) { return inst(SHR, d, a, b); }
167 inline inst asr(int d, int a, int b) { return inst(ASR, d, a, b); }
168 inline inst neg(int d, int a) { return inst(NEG, d, a); }
169 inline inst _not(int d, int a) { return inst(NOT, d, a); }
170 inline inst _and(int d, int a, int b) { return inst(AND, d, a, b); }
171 inline inst _or(int d, int a, int b) { return inst(OR, d, a, b); }
172 inline inst mul(int d, int a, int b) { return inst(MUL, d, a, b); }
173 inline inst mulu(int d, int a, int b) { return inst(MULU, d, a, b); }
174 inline inst div(int d, int a, int b) { return inst(DIV, d, a, b); }
175 inline inst divu(int d, int a, int b) { return inst(DIVU, d, a, b); }
176 inline inst mod(int d, int a, int b) { return inst(MOD, d, a, b); }
177 inline inst modu(int d, int a, int b) { return inst(MODU, d, a, b); }
178 inline inst _xor(int d, int a, int b) { return inst(XOR, d, a, b); }
179 inline inst spec(int d, unsigned long cst) { inst i(SPEC, d); i.args.cst = cst; return i; }
180 inline inst mulh(int d, int a, int b) { return inst(MULH, d, a, b); }
181 
182 // Block class
183 class Block: public elm::genstruct::Vector<inst> {
185 public:
186  class InstIter: public S::Iterator {
187  public:
188  inline InstIter(const Block& block): S::Iterator(block) { }
189  inline InstIter(const InstIter& iter): S::Iterator(iter) { }
190  inline opcode op(void) const { return opcode(item().op); }
191  inline t::int16 d(void) const { return item().d(); }
192  inline t::int16 a(void) const { return item().a(); }
193  inline t::int16 b(void) const { return item().b(); }
194  inline t::uint32 cst(void) const { return item().cst(); }
195  };
196  void print(elm::io::Output& out) const;
197 };
198 inline elm::io::Output& operator<<(elm::io::Output& out, const Block& b) { b.print(out); return out; }
199 
200 // Printer class
201 class Printer {
202 public:
203  inline Printer(const hard::Platform *platform = 0): pf(platform) { }
204 
205  void print(elm::io::Output& out, const Block& block) const;
206  void print(elm::io::Output& out, const inst& inst) const;
207 private:
209 };
210 
211 // useful functions
212 cond_t invert(cond_t cond);
213 int size(type_t type);
216 
217 } } // otawa::sem
218 
219 #endif /* OTAWA_SEM_INST_H_ */
struct otawa::sem::inst inst
t::int16 b
Definition: inst.h:115
Definition: inst.h:51
inst shr(int d, int a, int b)
Definition: inst.h:166
Definition: inst.h:77
Definition: inst.h:55
Definition: inst.h:62
inst cmp(int d, int a, int b)
Definition: inst.h:161
inst(opcode _op, int d, int a, int b)
Definition: inst.h:124
Definition: inst.h:63
Definition: inst.h:67
cond_t invert(cond_t cond)
Invert the given condition.
Definition: sem.cpp:499
Definition: inst.h:99
Definition: inst.h:81
Definition: inst.h:95
inst setp(int d, unsigned long cst)
Definition: inst.h:160
Definition: inst.h:47
InstIter(const InstIter &iter)
Definition: inst.h:189
t::int16 _d
Definition: inst.h:112
inst spec(int d, unsigned long cst)
Definition: inst.h:179
Definition: inst.h:60
Definition: inst.h:69
inst mulh(int d, int a, int b)
Definition: inst.h:180
inst _and(int d, int a, int b)
Definition: inst.h:170
inst mul(int d, int a, int b)
Definition: inst.h:172
Definition: inst.h:49
Definition: inst.h:54
inst asr(int d, int a, int b)
Definition: inst.h:167
inst divu(int d, int a, int b)
Definition: inst.h:175
t::int16 addr(void) const
Definition: inst.h:136
Definition: inst.h:46
t::uint32 cst(void) const
Definition: inst.h:194
Definition: inst.h:86
Definition: inst.h:41
Definition: inst.h:71
inst modu(int d, int a, int b)
Definition: inst.h:177
Definition: inst.h:83
Definition: inst.h:70
Definition: inst.h:98
union otawa::sem::inst::@2 args
A block represents a sequence of semantic instructions inst.
Definition: inst.h:183
const hard::Platform * pf
Definition: inst.h:208
t::int16 reg(void) const
Definition: inst.h:135
inst cmpu(int d, int a, int b)
Definition: inst.h:162
elm::io::Output & operator<<(elm::io::Output &out, Address addr)
Definition: base.cpp:188
Definition: inst.h:82
Definition: inst.h:84
enum otawa::sem::type_t type_t
Definition: inst.h:100
inst(opcode _op, int d, int a)
Definition: inst.h:122
Definition: inst.h:45
uint32 size
Definition: inst.h:88
opcode op(void) const
Definition: inst.h:190
Definition: inst.h:85
int16_t int16
t::int16 sr(void) const
Definition: inst.h:141
Definition: inst.h:58
Definition: inst.h:48
t::int16 a(void) const
Definition: inst.h:192
Definition: inst.h:43
t::int16 b(void) const
Definition: inst.h:129
enum otawa::sem::opcode opcode
Definition: inst.h:59
inst cont(void)
Definition: inst.h:151
enum otawa::sem::cond_t cond_t
t::uint32 cst(void) const
Definition: inst.h:132
inst(opcode _op, int d)
Definition: inst.h:120
Definition: inst.h:79
inst trap(void)
Definition: inst.h:150
type_t
Definition: inst.h:94
type_t type(void) const
Definition: inst.h:137
opcode
Definition: inst.h:40
Definition: inst.h:89
inst mulu(int d, int a, int b)
Definition: inst.h:173
Definition: inst.h:52
Definition: inst.h:53
void print(elm::io::Output &out) const
Print the current block.
Definition: sem.cpp:390
Definition: inst.h:106
t::int16 d(void) const
Definition: inst.h:127
Definition: inst.h:65
Definition: inst.h:87
Definition: inst.h:102
inst(void)
Definition: inst.h:118
Definition: inst.h:44
inst scratch(int d)
Definition: inst.h:157
sys::SystemOutStream & out
void print(elm::io::Output &out) const
Output the current instruction to the given output.
Definition: sem.cpp:375
Definition: inst.h:42
cond_t
Definition: inst.h:76
inst load(int d, int a, type_t t)
Definition: inst.h:154
inst _if(int cond, int sr, int jump)
Definition: inst.h:152
t::int16 a(void) const
Definition: inst.h:128
t::int16 b(void) const
Definition: inst.h:193
Definition: inst.h:97
inst add(int d, int a, int b)
Definition: inst.h:163
inst sub(int d, int a, int b)
Definition: inst.h:164
elm::genstruct::Vector< inst > S
Definition: inst.h:184
This class records information about the architecture where the processed program will run...
Definition: Platform.h:48
Definition: inst.h:96
cond_t cond(void) const
Definition: inst.h:140
Definition: inst.h:80
Definition: inst.h:68
t::uint32 cst
Definition: inst.h:114
Definition: inst.h:103
Printer class for semantic instructions (resolve the generic register value to the their real platfor...
Definition: inst.h:201
inst _xor(int d, int a, int b)
Definition: inst.h:178
t::uint16 op
Definition: inst.h:111
Printer(const hard::Platform *platform=0)
Definition: inst.h:203
Definition: inst.h:105
Definition: inst.h:50
Definition: inst.h:104
t::uint16 jump(void) const
Definition: inst.h:142
Definition: inst.h:56
This structure class represents an instruction in the semantics representation of machine instruction...
Definition: inst.h:110
t::int16 d(void) const
Definition: inst.h:191
Definition: inst.h:61
inst store(int d, int a, type_t t)
Definition: inst.h:156
Definition: inst.h:186
Definition: Microprocessor.h:52
const Type & type(void)
Definition: type.h:163
Definition: inst.h:101
inst mod(int d, int a, int b)
Definition: inst.h:176
inst neg(int d, int a)
Definition: inst.h:168
inst branch(int to)
Definition: inst.h:149
inst shl(int d, int a, int b)
Definition: inst.h:165
Definition: Microprocessor.h:51
inst(opcode _op)
Definition: inst.h:119
InstIter(const Block &block)
Definition: inst.h:188
uint16_t uint16
inst _or(int d, int a, int b)
Definition: inst.h:171
uint32_t uint32
inst seti(int d, unsigned long cst)
Definition: inst.h:159
Definition: inst.h:57
inst div(int d, int a, int b)
Definition: inst.h:174
inst nop(void)
Definition: inst.h:148
inst _not(int d, int a)
Definition: inst.h:169
Definition: inst.h:78