transform.py 4.2 KB

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