transform.py 4.2 KB

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