Guest User

Untitled

a guest
Nov 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. [<Fact>]
  2. let ``nested push32`` () =
  3. let var = ref None
  4. let var2 = ref None
  5. let var3 = ref None
  6. let arg1 = ref <| Some "arg1"
  7. let arg2 = ref <| Some "arg2"
  8.  
  9. let type_here = Push32 <| I 32
  10. // now we intro the push32 type who's variable-sized!
  11. let type_here = Push32 <| type_here
  12.  
  13. // let's design a vivid case:
  14. // head: i32 = 2
  15. // - subhead1: i32 = 3
  16. // 1 : i32
  17. // 2 : i32
  18. // 3 : i32
  19. // - subhead2: i32 = 2
  20. // 1 : i32
  21. // 2 : i32
  22. // the size of them (in bytes) is
  23. // 3 * 4(3 i32 heads) + 5 * 4(3 + 2 elems) = 32
  24. // so we should allocate 32/4 = 8
  25. let num = 8
  26.  
  27. let body = Suite [
  28.  
  29. // allocate 2 to gain a 12-bytes memory piece(4 + 8).
  30. Tmp(var, Alloca(type_here, Some num))
  31.  
  32. //prepare data
  33. Let(var2,
  34. Bitcast(Get var, Ptr <| I 32),
  35. let prepare_data a b =
  36. Store(GEP(Get var2, integer 32 a, []),
  37. integer 32 b)
  38. Suite [
  39. prepare_data 0 2 // head = 2
  40. prepare_data 1 3 // subhead1 = 3
  41. prepare_data 2 10 // subhead[1] = 10
  42. prepare_data 3 20 // subhead[2] = 20
  43. prepare_data 4 30 // subhead[3] = 30
  44. prepare_data 5 2 // subhead2 = 2
  45. prepare_data 6 40 // subhead2[1] = 40
  46. prepare_data 7 50 // subhead2[2] = 50
  47. ])
  48.  
  49. (let index_fst_level =
  50. GEPEx(Get var, integer 32 0, Some <| Get arg1)
  51. let index_snd_level =
  52. GEPEx(index_fst_level, integer 32 0, Some <| Get arg2)
  53. Load index_snd_level)
  54. ]
  55. let defun = Defun("my_func2", ["arg1", I 32; "arg2", I 32], I 32, body)
  56. let defun = Suite [
  57. Decl("printf", [Ptr <| I 8; I 32], I 32)
  58. defun
  59.  
  60. ]
  61. let my_module =
  62. try
  63. h.compile defun
  64. |> h.Parse
  65. with LLException e ->
  66. failwithf "%A" e
  67. //h.Optimize my_module
  68. //printf1 "%A" my_module
  69. h.Load my_module
  70. let fp = h.FindSymbol "my_func2"
  71. let f = Marshal.GetDelegateForFunctionPointer(fp.Address, typeof<intint2int>) :?> intint2int
  72.  
  73. for i = 0 to 1 do
  74. for j = 0 to 2 do
  75. printf1 "%d\n" <| f.Invoke(i, j)
  76.  
  77. let res = f.Invoke(0, 0) //subhead1[1]
  78. Assert.Equal(res, 10)
  79.  
  80. let res = f.Invoke(0, 1) //subhead1[2]
  81. Assert.Equal(res, 20)
  82.  
  83. let res = f.Invoke(0, 2) //subhead1[3]
  84. Assert.Equal(res, 30)
  85.  
  86. let res = f.Invoke(1, 0) //subhead2[1]
  87. Assert.Equal(res, 40)
  88.  
  89. let res = f.Invoke(1, 1) //subhead2[2]
  90. Assert.Equal(res, 50)
  91.  
  92. h.Unload my_module
Add Comment
Please, Sign In to add comment