Advertisement
qwerty1000000

jit tcc

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