2 # -*- coding: utf-8 -*-
5 Type-transformation rules.
8 __author__
= "Lluís Vilanova <vilanova@ac.upc.edu>"
9 __copyright__
= "Copyright 2012-2016, Lluís Vilanova <vilanova@ac.upc.edu>"
10 __license__
= "GPL version 2 or (at your option) any later version"
12 __maintainer__
= "Stefan Hajnoczi"
13 __email__
= "stefanha@linux.vnet.ibm.com"
16 def _transform_type(type_
, trans
):
17 if isinstance(trans
, str):
19 elif isinstance(trans
, dict):
21 return _transform_type(type_
, trans
[type_
])
23 return _transform_type(type_
, trans
[None])
29 raise ValueError("Invalid type transformation rule: %s" % trans
)
32 def transform_type(type_
, *trans
):
33 """Return a new type transformed according to the given rules.
35 Applies each of the transformation rules in trans in order.
37 If an element of trans is a string, return it.
39 If an element of trans is a function, call it with type_ as its only
42 If an element of trans is a dict, search type_ in its keys. If type_ is
43 a key, use the value as a transformation rule for type_. Otherwise, if
44 None is a key use the value as a transformation rule for type_.
46 Otherwise, return type_.
52 trans : list of function or dict
53 Type transformation rules.
59 res
= _transform_type(res
, t
)
63 ##################################################
66 def _tcg_2_host(type_
):
68 # force a fixed-size type (target-independent)
74 "TCGv_i32": "uint32_t",
75 "TCGv_i64": "uint64_t",
81 ##################################################
82 # host -> host compatible with tcg sizes
85 "uint8_t": "uint32_t",
86 "uint16_t": "uint32_t",
90 ##################################################
93 def _host_2_tcg(type_
):
94 if type_
.startswith("TCGv"):
96 raise ValueError("Don't know how to translate '%s' into a TCG type\n" % type_
)
99 "uint32_t": "TCGv_i32",
100 "uint64_t": "TCGv_i64",
101 "void *" : "TCGv_ptr",
102 "CPUArchState *": "TCGv_env",
107 ##################################################
108 # tcg -> tcg helper definition
110 def _tcg_2_helper_def(type_
):
112 return "target_ulong"
116 TCG_2_TCG_HELPER_DEF
= {
117 "TCGv_i32": "uint32_t",
118 "TCGv_i64": "uint64_t",
119 "TCGv_ptr": "void *",
120 None: _tcg_2_helper_def
,
124 ##################################################
125 # tcg -> tcg helper declaration
127 def _tcg_2_tcg_helper_decl_error(type_
):
128 raise ValueError("Don't know how to translate type '%s' into a TCG helper declaration type\n" % type_
)
130 TCG_2_TCG_HELPER_DECL
= {
136 None: _tcg_2_tcg_helper_decl_error
,
140 ##################################################
141 # host/tcg -> tcg temporal constant allocation
143 def _host_2_tcg_tmp_new(type_
):
144 if type_
.startswith("TCGv"):
145 return "tcg_temp_new_nop"
146 raise ValueError("Don't know how to translate type '%s' into a TCG temporal allocation" % type_
)
148 HOST_2_TCG_TMP_NEW
= {
149 "uint32_t": "tcg_const_i32",
150 "uint64_t": "tcg_const_i64",
151 "void *" : "tcg_const_ptr",
152 None: _host_2_tcg_tmp_new
,
156 ##################################################
157 # host/tcg -> tcg temporal constant deallocation
159 def _host_2_tcg_tmp_free(type_
):
160 if type_
.startswith("TCGv"):
161 return "tcg_temp_free_nop"
162 raise ValueError("Don't know how to translate type '%s' into a TCG temporal deallocation" % type_
)
164 HOST_2_TCG_TMP_FREE
= {
165 "uint32_t": "tcg_temp_free_i32",
166 "uint64_t": "tcg_temp_free_i64",
167 "void *" : "tcg_temp_free_ptr",
168 None: _host_2_tcg_tmp_free
,