2
0

transform.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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-2014, 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. }
  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. None: _host_2_tcg,
  78. }
  79. ##################################################
  80. # tcg -> tcg helper definition
  81. def _tcg_2_helper_def(type_):
  82. if type_ == "TCGv":
  83. return "target_ulong"
  84. else:
  85. return type_
  86. TCG_2_TCG_HELPER_DEF = {
  87. "TCGv_i32": "uint32_t",
  88. "TCGv_i64": "uint64_t",
  89. "TCGv_ptr": "void *",
  90. None: _tcg_2_helper_def,
  91. }
  92. ##################################################
  93. # tcg -> tcg helper declaration
  94. def _tcg_2_tcg_helper_decl_error(type_):
  95. raise ValueError("Don't know how to translate type '%s' into a TCG helper declaration type\n" % type_)
  96. TCG_2_TCG_HELPER_DECL = {
  97. "TCGv" : "tl",
  98. "TCGv_ptr": "ptr",
  99. "TCGv_i32": "i32",
  100. "TCGv_i64": "i64",
  101. None: _tcg_2_tcg_helper_decl_error,
  102. }
  103. ##################################################
  104. # host/tcg -> tcg temporal constant allocation
  105. def _host_2_tcg_tmp_new(type_):
  106. if type_.startswith("TCGv"):
  107. return "tcg_temp_new_nop"
  108. raise ValueError("Don't know how to translate type '%s' into a TCG temporal allocation" % type_)
  109. HOST_2_TCG_TMP_NEW = {
  110. "uint32_t": "tcg_const_i32",
  111. "uint64_t": "tcg_const_i64",
  112. "void *" : "tcg_const_ptr",
  113. None: _host_2_tcg_tmp_new,
  114. }
  115. ##################################################
  116. # host/tcg -> tcg temporal constant deallocation
  117. def _host_2_tcg_tmp_free(type_):
  118. if type_.startswith("TCGv"):
  119. return "tcg_temp_free_nop"
  120. raise ValueError("Don't know how to translate type '%s' into a TCG temporal deallocation" % type_)
  121. HOST_2_TCG_TMP_FREE = {
  122. "uint32_t": "tcg_temp_free_i32",
  123. "uint64_t": "tcg_temp_free_i64",
  124. "void *" : "tcg_temp_free_ptr",
  125. None: _host_2_tcg_tmp_free,
  126. }