Advertisement
qwerty1000000

jit tcc

Oct 10th, 2022 (edited)
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
REBOL 3.91 KB | Source Code | 0 0
  1.  
  2. libtcc: make library! %libtcc.dll
  3.  
  4.  
  5.     tcc_new: make routine! compose/deep [[
  6.         return: [pointer]
  7.         abi: default
  8.     ] (libtcc) "tcc_new"]
  9.  
  10.  
  11.     tcc_compile_string: make routine! compose/deep [[
  12.         s [pointer]
  13.         buf [pointer]
  14.         return: [int32]
  15.         abi: default
  16.     ] (libtcc) "tcc_compile_string"]
  17.  
  18.  
  19.  
  20.     tcc_get_symbol: make routine! compose/deep [[
  21.         s [pointer]
  22.         name [pointer]
  23.         return: [pointer]
  24.         abi: default
  25.     ] (libtcc) "tcc_get_symbol"]
  26.  
  27. tcc_set_output_type: make routine! compose/deep [[
  28.         s [pointer]
  29.         output_type [int32]
  30.         return: [int32]
  31.         abi: default
  32.     ] (libtcc) "tcc_set_output_type"]
  33.  
  34.  
  35.     tcc_relocate: make routine! compose/deep [[
  36.         s1 [pointer]
  37.         ptr [pointer]
  38.         return: [int32]
  39.         abi: default
  40.     ] (libtcc) "tcc_relocate"]
  41.  
  42. tcc_delete: make routine! compose/deep [[
  43.         s [pointer]
  44.         return: [void]
  45.         abi: default
  46.     ] (libtcc) "tcc_delete"]
  47. ;----------------------------
  48. ; reflect :f 'words , reflect:f 'body
  49.  
  50.  
  51.    PROT_NONE:     0
  52.     PROT_READ:     1
  53.     PROT_WRITE:   2
  54.     PROT_EXEC: 4
  55.  
  56.  
  57. PAGE_EXECUTE_READWRITE: to-integer  #{40}
  58.  
  59. MAP_SHARED: 1
  60. MAP_PRIVATE: 2
  61. MAP_ANONYMOUS: 32
  62.  
  63. ;---------------------------
  64.  
  65. Kernel32: make library! %Kernel32.dll
  66.  
  67. msvcrt: make library! %msvcrt.dll
  68.  
  69. ;------------------------------------
  70.  
  71. STD_INPUT_HANDLE: -10
  72. STD_OUTPUT_HANDLE: -11
  73.  
  74. GetStdHandle: make routine!  compose [ [
  75.  
  76.   nStdHandle [ int64]
  77.  
  78.  
  79.  return:  [int64]
  80.  
  81. ]  (Kernel32) "GetStdHandle" ]
  82.  
  83.  
  84.  
  85. WriteConsole: make routine!  compose [ [
  86.  
  87.  
  88.   hConsoleOutput [ int64]
  89.  
  90.   lpBuffer  [ int64]
  91.  
  92.   nNumberOfCharsToWrite  [ int64]
  93.  
  94.   lpNumberOfCharsWritten  [ int64]
  95.  
  96.   lpReserved [ int64]
  97.  
  98.  
  99. return:  [int64]
  100.  
  101. ]  (Kernel32) "WriteConsoleA" ]
  102.  
  103.  
  104.  
  105.  
  106. ;-----------------------------
  107.  
  108. VirtualProtect: make routine!  compose [ [
  109.  
  110.   lpAddress [ pointer]
  111.  
  112.   dwSize [ int64]
  113.  
  114.   flNewProtec [ int64]
  115.  
  116.   lpflOldProtect [pointer]
  117.  
  118.  return:  [int64]
  119.  
  120. ]  (Kernel32) "VirtualProtect" ]
  121.  
  122. ;----------------------
  123. memcpy:  make routine!  compose [ [
  124.  
  125.   destination [ pointer]
  126.  
  127.   source [pointer]
  128.  
  129.   size [ int64]
  130.  
  131.  return:  [int64]
  132.  
  133. ]  (msvcrt) "memcpy" ]
  134.  
  135. ;------------------------------
  136.  
  137. printf: make routine!  compose [ [
  138.  
  139. ;  format [ pointer]
  140.  
  141.  argument [ pointer]
  142.  
  143.  return:  [int64]
  144.  
  145. ]  (msvcrt) "printf" ]
  146.  
  147. ;-------------------------
  148.  
  149.  
  150. ;---------------------------------
  151.  
  152. &: func [data][return memcpy data data length? data ]
  153.  
  154. PAGE_SIZE: 4096  
  155.  
  156. memory: to-binary make bitset! 16 * PAGE_SIZE
  157.  
  158. ;&memory: (reflect :memory 'addr)
  159.  
  160. &memory: & memory
  161.  
  162. offset: &memory + o: PAGE_SIZE - mod &memory PAGE_SIZE
  163.  
  164. memory: skip memory o
  165.  
  166. adress: 0
  167.  
  168. oldprotect: #{0000000000000000}
  169.  
  170. &oldprotect: & oldprotect
  171.  
  172. probe VirtualProtect offset  PAGE_SIZE  PAGE_EXECUTE_READWRITE  &oldprotect ; read , write , exec
  173.  
  174. ;-----------------------------------------------
  175. print "tcl"
  176.  
  177.  
  178.  
  179.  
  180. routines: to-map []
  181.  
  182.  
  183. state: tcc_new
  184.  
  185. source state
  186.  
  187. compile: func [ name data /local  tmp ptr][
  188.  
  189.  
  190.  
  191.  
  192.  
  193. print join "compile: " dt [
  194.  
  195. probe tcc_compile_string state data
  196.  
  197.  
  198. TCC_OUTPUT_MEMORY: 1
  199.  
  200. probe tcc_set_output_type state   TCC_OUTPUT_MEMORY
  201.  
  202. nullptr:  & #{0000000000000000}
  203.  
  204.  
  205. size:  tcc_relocate state  nullptr
  206. source size
  207. ptr: tcc_get_symbol state name
  208. source ptr
  209. buffer: to-binary make bitset! 1024 * 8
  210.  
  211. memcpy & buffer ptr 1024
  212.  
  213. tmp: copy/part buffer find/tail buffer #{c9c3}
  214.  
  215. source tmp
  216.  
  217.  
  218.  
  219. ]
  220.  
  221.  
  222. insert  routines  reduce [name ptr ]
  223.  
  224. return ptr
  225.  
  226. ]
  227.  
  228.  
  229. add: make routine! compose [ [ a [int64] b [int64] return:  [int64] ]  compile "add" {
  230.  
  231.  int add(long long int a, long long int b) {
  232.     return a + b ;
  233.  }
  234.  
  235.  }
  236.  
  237. ]
  238.  
  239.  
  240. ;----------------------------------------------------------
  241.  
  242. sub: make routine! compose [ [ a [int64] b [int64] return:  [int64] ]  compile "sub" {
  243.  
  244.  int sub(long long int a, long long int b) {
  245.     return a - b ;
  246.  }
  247.  
  248.  }
  249.  
  250. ]
  251.  
  252.  
  253. halt
  254. ;code: {int foo() { return 123; }}
  255.  
  256. ;-------------------------
  257.  
Tags: jit tcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement