Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- libtcc: make library! %libtcc.dll
- tcc_new: make routine! compose/deep [[
- return: [pointer]
- abi: default
- ] (libtcc) "tcc_new"]
- tcc_compile_string: make routine! compose/deep [[
- s [pointer]
- buf [pointer]
- return: [int32]
- abi: default
- ] (libtcc) "tcc_compile_string"]
- tcc_get_symbol: make routine! compose/deep [[
- s [pointer]
- name [pointer]
- return: [pointer]
- abi: default
- ] (libtcc) "tcc_get_symbol"]
- tcc_set_output_type: make routine! compose/deep [[
- s [pointer]
- output_type [int32]
- return: [int32]
- abi: default
- ] (libtcc) "tcc_set_output_type"]
- tcc_relocate: make routine! compose/deep [[
- s1 [pointer]
- ptr [pointer]
- return: [int32]
- abi: default
- ] (libtcc) "tcc_relocate"]
- tcc_delete: make routine! compose/deep [[
- s [pointer]
- return: [void]
- abi: default
- ] (libtcc) "tcc_delete"]
- ;----------------------------
- ; reflect :f 'words , reflect:f 'body
- PROT_NONE: 0
- PROT_READ: 1
- PROT_WRITE: 2
- PROT_EXEC: 4
- PAGE_EXECUTE_READWRITE: to-integer #{40}
- MAP_SHARED: 1
- MAP_PRIVATE: 2
- MAP_ANONYMOUS: 32
- ;---------------------------
- Kernel32: make library! %Kernel32.dll
- msvcrt: make library! %msvcrt.dll
- ;------------------------------------
- STD_INPUT_HANDLE: -10
- STD_OUTPUT_HANDLE: -11
- GetStdHandle: make routine! compose [ [
- nStdHandle [ int64]
- return: [int64]
- ] (Kernel32) "GetStdHandle" ]
- WriteConsole: make routine! compose [ [
- hConsoleOutput [ int64]
- lpBuffer [ int64]
- nNumberOfCharsToWrite [ int64]
- lpNumberOfCharsWritten [ int64]
- lpReserved [ int64]
- return: [int64]
- ] (Kernel32) "WriteConsoleA" ]
- ;-----------------------------
- VirtualProtect: make routine! compose [ [
- lpAddress [ pointer]
- dwSize [ int64]
- flNewProtec [ int64]
- lpflOldProtect [pointer]
- return: [int64]
- ] (Kernel32) "VirtualProtect" ]
- ;----------------------
- memcpy: make routine! compose [ [
- destination [ pointer]
- source [pointer]
- size [ int64]
- return: [int64]
- ] (msvcrt) "memcpy" ]
- ;------------------------------
- printf: make routine! compose [ [
- ; format [ pointer]
- argument [ pointer]
- return: [int64]
- ] (msvcrt) "printf" ]
- ;-------------------------
- ;---------------------------------
- &: func [data][return memcpy data data length? data ]
- PAGE_SIZE: 4096
- memory: to-binary make bitset! 16 * PAGE_SIZE
- ;&memory: (reflect :memory 'addr)
- &memory: & memory
- offset: &memory + o: PAGE_SIZE - mod &memory PAGE_SIZE
- memory: skip memory o
- adress: 0
- oldprotect: #{0000000000000000}
- &oldprotect: & oldprotect
- probe VirtualProtect offset PAGE_SIZE PAGE_EXECUTE_READWRITE &oldprotect ; read , write , exec
- ;-----------------------------------------------
- print "tcl"
- routines: to-map []
- state: tcc_new
- source state
- compile: func [ name data /local tmp ptr][
- print join "compile: " dt [
- probe tcc_compile_string state data
- TCC_OUTPUT_MEMORY: 1
- probe tcc_set_output_type state TCC_OUTPUT_MEMORY
- nullptr: & #{0000000000000000}
- size: tcc_relocate state nullptr
- source size
- ptr: tcc_get_symbol state name
- source ptr
- buffer: to-binary make bitset! 1024 * 8
- memcpy & buffer ptr 1024
- tmp: copy/part buffer find/tail buffer #{c9c3}
- source tmp
- ]
- insert routines reduce [name ptr ]
- return ptr
- ]
- add: make routine! compose [ [ a [int64] b [int64] return: [int64] ] compile "add" {
- int add(long long int a, long long int b) {
- return a + b ;
- }
- }
- ]
- ;----------------------------------------------------------
- sub: make routine! compose [ [ a [int64] b [int64] return: [int64] ] compile "sub" {
- int sub(long long int a, long long int b) {
- return a - b ;
- }
- }
- ]
- halt
- ;code: {int foo() { return 123; }}
- ;-------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement