Lluís Vilanova | e6d6c4b | 2014-05-30 14:11:32 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ |
| 5 | Type-transformation rules. |
| 6 | """ |
| 7 | |
| 8 | __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>" |
| 9 | __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>" |
| 10 | __license__ = "GPL version 2 or (at your option) any later version" |
| 11 | |
| 12 | __maintainer__ = "Stefan Hajnoczi" |
| 13 | __email__ = "stefanha@linux.vnet.ibm.com" |
| 14 | |
| 15 | |
| 16 | def _transform_type(type_, trans): |
| 17 | if isinstance(trans, str): |
| 18 | return trans |
| 19 | elif isinstance(trans, dict): |
| 20 | if type_ in trans: |
| 21 | return _transform_type(type_, trans[type_]) |
| 22 | elif None in trans: |
| 23 | return _transform_type(type_, trans[None]) |
| 24 | else: |
| 25 | return type_ |
| 26 | elif callable(trans): |
| 27 | return trans(type_) |
| 28 | else: |
| 29 | raise ValueError("Invalid type transformation rule: %s" % trans) |
| 30 | |
| 31 | |
| 32 | def transform_type(type_, *trans): |
| 33 | """Return a new type transformed according to the given rules. |
| 34 | |
| 35 | Applies each of the transformation rules in trans in order. |
| 36 | |
| 37 | If an element of trans is a string, return it. |
| 38 | |
| 39 | If an element of trans is a function, call it with type_ as its only |
| 40 | argument. |
| 41 | |
| 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_. |
| 45 | |
| 46 | Otherwise, return type_. |
| 47 | |
| 48 | Parameters |
| 49 | ---------- |
| 50 | type_ : str |
| 51 | Type to transform. |
| 52 | trans : list of function or dict |
| 53 | Type transformation rules. |
| 54 | """ |
| 55 | if len(trans) == 0: |
| 56 | raise ValueError |
| 57 | res = type_ |
| 58 | for t in trans: |
| 59 | res = _transform_type(res, t) |
| 60 | return res |
| 61 | |
| 62 | |
| 63 | ################################################## |
| 64 | # tcg -> host |
| 65 | |
| 66 | def _tcg_2_host(type_): |
| 67 | if type_ == "TCGv": |
| 68 | # force a fixed-size type (target-independent) |
| 69 | return "uint64_t" |
| 70 | else: |
| 71 | return type_ |
| 72 | |
| 73 | TCG_2_HOST = { |
| 74 | "TCGv_i32": "uint32_t", |
| 75 | "TCGv_i64": "uint64_t", |
| 76 | "TCGv_ptr": "void *", |
| 77 | None: _tcg_2_host, |
| 78 | } |
| 79 | |
| 80 | |
| 81 | ################################################## |
| 82 | # host -> host compatible with tcg sizes |
| 83 | |
| 84 | HOST_2_TCG_COMPAT = { |
| 85 | "uint8_t": "uint32_t", |
| 86 | } |
| 87 | |
| 88 | |
| 89 | ################################################## |
| 90 | # host/tcg -> tcg |
| 91 | |
| 92 | def _host_2_tcg(type_): |
| 93 | if type_.startswith("TCGv"): |
| 94 | return type_ |
| 95 | raise ValueError("Don't know how to translate '%s' into a TCG type\n" % type_) |
| 96 | |
| 97 | HOST_2_TCG = { |
| 98 | "uint32_t": "TCGv_i32", |
| 99 | "uint64_t": "TCGv_i64", |
| 100 | "void *" : "TCGv_ptr", |
| 101 | None: _host_2_tcg, |
| 102 | } |
| 103 | |
| 104 | |
| 105 | ################################################## |
| 106 | # tcg -> tcg helper definition |
| 107 | |
| 108 | def _tcg_2_helper_def(type_): |
| 109 | if type_ == "TCGv": |
| 110 | return "target_ulong" |
| 111 | else: |
| 112 | return type_ |
| 113 | |
| 114 | TCG_2_TCG_HELPER_DEF = { |
| 115 | "TCGv_i32": "uint32_t", |
| 116 | "TCGv_i64": "uint64_t", |
| 117 | "TCGv_ptr": "void *", |
| 118 | None: _tcg_2_helper_def, |
| 119 | } |
| 120 | |
| 121 | |
| 122 | ################################################## |
| 123 | # tcg -> tcg helper declaration |
| 124 | |
| 125 | def _tcg_2_tcg_helper_decl_error(type_): |
| 126 | raise ValueError("Don't know how to translate type '%s' into a TCG helper declaration type\n" % type_) |
| 127 | |
| 128 | TCG_2_TCG_HELPER_DECL = { |
| 129 | "TCGv" : "tl", |
| 130 | "TCGv_ptr": "ptr", |
| 131 | "TCGv_i32": "i32", |
| 132 | "TCGv_i64": "i64", |
| 133 | None: _tcg_2_tcg_helper_decl_error, |
| 134 | } |
| 135 | |
| 136 | |
| 137 | ################################################## |
| 138 | # host/tcg -> tcg temporal constant allocation |
| 139 | |
| 140 | def _host_2_tcg_tmp_new(type_): |
| 141 | if type_.startswith("TCGv"): |
| 142 | return "tcg_temp_new_nop" |
| 143 | raise ValueError("Don't know how to translate type '%s' into a TCG temporal allocation" % type_) |
| 144 | |
| 145 | HOST_2_TCG_TMP_NEW = { |
| 146 | "uint32_t": "tcg_const_i32", |
| 147 | "uint64_t": "tcg_const_i64", |
| 148 | "void *" : "tcg_const_ptr", |
| 149 | None: _host_2_tcg_tmp_new, |
| 150 | } |
| 151 | |
| 152 | |
| 153 | ################################################## |
| 154 | # host/tcg -> tcg temporal constant deallocation |
| 155 | |
| 156 | def _host_2_tcg_tmp_free(type_): |
| 157 | if type_.startswith("TCGv"): |
| 158 | return "tcg_temp_free_nop" |
| 159 | raise ValueError("Don't know how to translate type '%s' into a TCG temporal deallocation" % type_) |
| 160 | |
| 161 | HOST_2_TCG_TMP_FREE = { |
| 162 | "uint32_t": "tcg_temp_free_i32", |
| 163 | "uint64_t": "tcg_temp_free_i64", |
| 164 | "void *" : "tcg_temp_free_ptr", |
| 165 | None: _host_2_tcg_tmp_free, |
| 166 | } |