/rpython/rtyper/test/test_llop.py

https://github.com/mesalock-linux/mesapy · Python · 84 lines · 64 code · 18 blank · 2 comment · 0 complexity · a4d8669b61d2dbf0310db3eab2996075 MD5 · raw file

  1. import struct
  2. from rpython.rtyper.test.tool import BaseRtypingTest
  3. from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
  4. from rpython.rtyper.lltypesystem.lloperation import llop
  5. from rpython.rtyper.lltypesystem.rstr import STR
  6. from rpython.rtyper.annlowlevel import llstr
  7. from rpython.rlib.rarithmetic import r_singlefloat
  8. from rpython.rlib.rgc import (resizable_list_supporting_raw_ptr,
  9. ll_for_resizable_list)
  10. def str_gc_load(TYPE, buf, offset):
  11. base_ofs = (llmemory.offsetof(STR, 'chars') +
  12. llmemory.itemoffsetof(STR.chars, 0))
  13. scale_factor = llmemory.sizeof(lltype.Char)
  14. lls = llstr(buf)
  15. return llop.gc_load_indexed(TYPE, lls, offset,
  16. scale_factor, base_ofs)
  17. def newlist_and_gc_store(TYPE, value):
  18. size = rffi.sizeof(TYPE)
  19. lst = resizable_list_supporting_raw_ptr(['\x00']*size)
  20. ll_data = ll_for_resizable_list(lst)
  21. ll_items = ll_data.items
  22. LIST = lltype.typeOf(ll_data).TO # rlist.LIST_OF(lltype.Char)
  23. base_ofs = llmemory.itemoffsetof(LIST.items.TO, 0)
  24. scale_factor = llmemory.sizeof(lltype.Char)
  25. value = lltype.cast_primitive(TYPE, value)
  26. llop.gc_store_indexed(lltype.Void, ll_items, 0,
  27. value, scale_factor, base_ofs)
  28. return lst
  29. class BaseLLOpTest(object):
  30. def test_gc_load_indexed(self):
  31. buf = struct.pack('dfi', 123.456, 123.456, 0x12345678)
  32. val = self.gc_load_from_string(rffi.DOUBLE, buf, 0)
  33. assert val == 123.456
  34. #
  35. val = self.gc_load_from_string(rffi.FLOAT, buf, 8)
  36. assert val == r_singlefloat(123.456)
  37. #
  38. val = self.gc_load_from_string(rffi.INT, buf, 12)
  39. assert val == 0x12345678
  40. def test_gc_store_indexed_int(self):
  41. expected = struct.pack('i', 0x12345678)
  42. self.newlist_and_gc_store(rffi.INT, 0x12345678, expected)
  43. def test_gc_store_indexed_double(self):
  44. expected = struct.pack('d', 123.456)
  45. self.newlist_and_gc_store(rffi.DOUBLE, 123.456, expected)
  46. def test_gc_store_indexed_float(self):
  47. expected = struct.pack('f', 123.456)
  48. self.newlist_and_gc_store(rffi.FLOAT, 123.456, expected)
  49. class TestDirect(BaseLLOpTest):
  50. def gc_load_from_string(self, TYPE, buf, offset):
  51. return str_gc_load(TYPE, buf, offset)
  52. def newlist_and_gc_store(self, TYPE, value, expected):
  53. got = newlist_and_gc_store(TYPE, value)
  54. got = ''.join(got)
  55. assert got == expected
  56. class TestRTyping(BaseLLOpTest, BaseRtypingTest):
  57. def gc_load_from_string(self, TYPE, buf, offset):
  58. def fn(offset):
  59. return str_gc_load(TYPE, buf, offset)
  60. return self.interpret(fn, [offset])
  61. def newlist_and_gc_store(self, TYPE, value, expected):
  62. def fn(value):
  63. return newlist_and_gc_store(TYPE, value)
  64. ll_res = self.interpret(fn, [value])
  65. got = ''.join(ll_res.items)
  66. assert got == expected