Advertisement
Guest User

orig-llvm

a guest
Apr 15th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. ; ModuleID = '<string>'
  2. target triple = "unknown-unknown-unknown"
  3.  
  4. %thunk = type { i1, i8* }
  5. %closure.1 = type { %thunk*, i8* (%thunk*, %thunk*)* }
  6. %thunk_inner = type { i8*, i8* (i8*)* }
  7.  
  8. @add_null = private constant %closure.1 { %thunk* null, i8* (%thunk*, %thunk*)* @add_closure }
  9. @add = linkonce_odr constant %thunk { i1 true, i8* bitcast (%closure.1* @add_null to i8*) }
  10.  
  11. declare i8* @malloc(i32)
  12.  
  13. define private i8* @add_ptr(%thunk* %.1, %thunk* %.2) {
  14. .3:
  15. %.4 = call i8* @eval_thunk(%thunk* %.1)
  16. %.5 = bitcast i8* %.4 to i32*
  17. %.6 = load i32* %.5
  18. %.7 = call i8* @eval_thunk(%thunk* %.2)
  19. %.8 = bitcast i8* %.7 to i32*
  20. %.9 = load i32* %.8
  21. %res = call i32 @add_intern(i32 %.6, i32 %.9)
  22. %res_ptr = call i8* @malloc(i32 4)
  23. %res_cast = bitcast i8* %res_ptr to i32*
  24. store i32 %res, i32* %res_cast
  25. ret i8* %res_ptr
  26. }
  27.  
  28. define linkonce_odr i8* @add_closure(%thunk* %env_null, %thunk* %arg) {
  29. .3:
  30. %res = call i8* @make_closure(%thunk* %arg, i8* (%thunk*, %thunk*)* @add_ptr)
  31. ret i8* %res
  32. }
  33.  
  34. define i32 @main() {
  35. entry:
  36. %env_pass = insertvalue [1 x %thunk*] zeroinitializer, %thunk* @add, 0
  37. %env_pass_ptr = call i8* @malloc(i32 8)
  38. %env_pass_cast = bitcast i8* %env_pass_ptr to [1 x %thunk*]*
  39. store [1 x %thunk*] %env_pass, [1 x %thunk*]* %env_pass_cast
  40. %thunk.1 = call %thunk* @make_thunk(i8* %env_pass_ptr, i8* (i8*)* @.1)
  41. %res_ptr = call i8* @eval_thunk(%thunk* %thunk.1)
  42. %res_cast = bitcast i8* %res_ptr to i32*
  43. %res = load i32* %res_cast
  44. ret i32 %res
  45. }
  46.  
  47. define private i8* @.1(i8* %env) {
  48. .3:
  49. %env_ptr = bitcast i8* %env to [1 x %thunk*]*
  50. %env_val = load [1 x %thunk*]* %env_ptr
  51. %"+" = extractvalue [1 x %thunk*] %env_val, 0
  52. %env_pass = insertvalue [1 x %thunk*] zeroinitializer, %thunk* %"+", 0
  53. %env_pass_ptr = call i8* @malloc(i32 8)
  54. %env_pass_cast = bitcast i8* %env_pass_ptr to [1 x %thunk*]*
  55. store [1 x %thunk*] %env_pass, [1 x %thunk*]* %env_pass_cast
  56. %thunk.1 = call %thunk* @make_thunk(i8* %env_pass_ptr, i8* (i8*)* @.2)
  57. %num = call i8* @malloc(i32 4)
  58. %num32 = bitcast i8* %num to i32*
  59. store i32 20, i32* %num32
  60. %thunk.2 = call %thunk* @wrap_thunk(i8* %num)
  61. %operator = call i8* @eval_thunk(%thunk* %thunk.1)
  62. %res = tail call i8* @apply_closure(i8* %operator, %thunk* %thunk.2)
  63. ret i8* %res
  64. }
  65.  
  66. define private i8* @.2(i8* %env) {
  67. .3:
  68. %env_ptr = bitcast i8* %env to [1 x %thunk*]*
  69. %env_val = load [1 x %thunk*]* %env_ptr
  70. %"+" = extractvalue [1 x %thunk*] %env_val, 0
  71. %num = call i8* @malloc(i32 4)
  72. %num32 = bitcast i8* %num to i32*
  73. store i32 10, i32* %num32
  74. %thunk.1 = call %thunk* @wrap_thunk(i8* %num)
  75. %operator = call i8* @eval_thunk(%thunk* %"+")
  76. %res = tail call i8* @apply_closure(i8* %operator, %thunk* %thunk.1)
  77. ret i8* %res
  78. }
  79.  
  80. define linkonce_odr i8* @make_closure(%thunk* %env, i8* (%thunk*, %thunk*)* %fun) {
  81. %c1 = insertvalue %closure.1 zeroinitializer, %thunk* %env, 0
  82. %c2 = insertvalue %closure.1 %c1, i8* (%thunk*, %thunk*)* %fun, 1
  83. %c_ptr = call i8* @malloc(i32 16)
  84. %c_cast = bitcast i8* %c_ptr to %closure.1*
  85. store %closure.1 %c2, %closure.1* %c_cast
  86. ret i8* %c_ptr
  87. }
  88.  
  89. define linkonce_odr i8* @apply_closure(i8* %c_ptr, %thunk* %arg) {
  90. %c_cast = bitcast i8* %c_ptr to %closure.1*
  91. %c = load %closure.1* %c_cast
  92. %env = extractvalue %closure.1 %c, 0
  93. %fun = extractvalue %closure.1 %c, 1
  94. %res = call i8* %fun(%thunk* %env, %thunk* %arg)
  95. ret i8* %res
  96. }
  97.  
  98. define linkonce_odr %thunk* @make_thunk(i8* %env, i8* (i8*)* %fun) {
  99. %ti1 = insertvalue %thunk_inner zeroinitializer, i8* %env, 0
  100. %ti2 = insertvalue %thunk_inner %ti1, i8* (i8*)* %fun, 1
  101. %ti_ptr = call i8* @malloc(i32 16)
  102. %ti_cast = bitcast i8* %ti_ptr to %thunk_inner*
  103. store %thunk_inner %ti2, %thunk_inner* %ti_cast
  104. %t = insertvalue %thunk zeroinitializer, i8* %ti_ptr, 1
  105. %t_ptr = call i8* @malloc(i32 16)
  106. %t_cast = bitcast i8* %t_ptr to %thunk*
  107. store %thunk %t, %thunk* %t_cast
  108. ret %thunk* %t_cast
  109. }
  110.  
  111. define linkonce_odr %thunk* @wrap_thunk(i8* %val) {
  112. %t = insertvalue %thunk { i1 true, i8* null }, i8* %val, 1
  113. %t_ptr = call i8* @malloc(i32 16)
  114. %t_cast = bitcast i8* %t_ptr to %thunk*
  115. store %thunk %t, %thunk* %t_cast
  116. ret %thunk* %t_cast
  117. }
  118.  
  119. define linkonce_odr i8* @eval_thunk(%thunk* %t_ptr) {
  120. entry:
  121. %t = load %thunk* %t_ptr
  122. %evald = extractvalue %thunk %t, 0
  123. %val = extractvalue %thunk %t, 1
  124. br i1 %evald, label %return_val, label %evaluate
  125.  
  126. evaluate: ; preds = %entry
  127. %tin_cast = bitcast i8* %val to %thunk_inner*
  128. %tin = load %thunk_inner* %tin_cast
  129. %env = extractvalue %thunk_inner %tin, 0
  130. %fun = extractvalue %thunk_inner %tin, 1
  131. %res = call i8* %fun(i8* %env)
  132. %t_new = insertvalue %thunk { i1 true, i8* null }, i8* %res, 1
  133. store %thunk %t_new, %thunk* %t_ptr
  134. ret i8* %res
  135.  
  136. return_val: ; preds = %entry
  137. ret i8* %val
  138. }
  139.  
  140. define linkonce_odr i32 @add_intern(i32 %x, i32 %y) {
  141. %res = add i32 %x, %y
  142. ret i32 %res
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement