removed p! and p@ and added 'mem' that just pushes the addr os the persistent memory on to the stack and than you can write/read using standard ! and @ nd write at an offset uaing pointer arithmetic
This commit is contained in:
36
alloc.sl
Normal file
36
alloc.sl
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import stdlib/stdlib.sl
|
||||||
|
import stdlib/io.sl
|
||||||
|
|
||||||
|
: alloc
|
||||||
|
0 # addr hint (NULL)
|
||||||
|
swap # size
|
||||||
|
3 # prot (PROT_READ | PROT_WRITE)
|
||||||
|
34 # flags (MAP_PRIVATE | MAP_ANON)
|
||||||
|
-1 # fd
|
||||||
|
0 # offset
|
||||||
|
mmap
|
||||||
|
;
|
||||||
|
|
||||||
|
: free
|
||||||
|
munmap drop
|
||||||
|
;
|
||||||
|
|
||||||
|
: test-mem-alloc
|
||||||
|
4096 alloc dup 1337 swap ! # allocate 4096 bytes, store 1337 at start
|
||||||
|
dup @ puti cr # print value at start
|
||||||
|
4096 free # free the memory
|
||||||
|
;
|
||||||
|
|
||||||
|
struct: Point
|
||||||
|
field x 8
|
||||||
|
field y 8
|
||||||
|
;struct
|
||||||
|
|
||||||
|
: main2
|
||||||
|
32 alloc # allocate 32 bytes (enough for a Point struct)
|
||||||
|
dup 111 swap Point.x!
|
||||||
|
dup 222 swap Point.y!
|
||||||
|
dup Point.x@ puti cr
|
||||||
|
Point.y@ puti cr
|
||||||
|
32 free # free the memory
|
||||||
|
;
|
||||||
3
main.py
3
main.py
@@ -324,7 +324,6 @@ IntrinsicEmitter = Callable[["FunctionEmitter"], None]
|
|||||||
class Word:
|
class Word:
|
||||||
name: str
|
name: str
|
||||||
immediate: bool = False
|
immediate: bool = False
|
||||||
stack_effect: str = "( -- )"
|
|
||||||
definition: Optional[Union[Definition, AsmDefinition]] = None
|
definition: Optional[Union[Definition, AsmDefinition]] = None
|
||||||
macro: Optional[MacroHandler] = None
|
macro: Optional[MacroHandler] = None
|
||||||
intrinsic: Optional[IntrinsicEmitter] = None
|
intrinsic: Optional[IntrinsicEmitter] = None
|
||||||
@@ -1511,8 +1510,6 @@ class _CTHandleTable:
|
|||||||
class Assembler:
|
class Assembler:
|
||||||
def __init__(self, dictionary: Dictionary) -> None:
|
def __init__(self, dictionary: Dictionary) -> None:
|
||||||
self.dictionary = dictionary
|
self.dictionary = dictionary
|
||||||
self.stack_bytes = 65536
|
|
||||||
self.io_buffer_bytes = 128
|
|
||||||
self._string_literals: Dict[str, Tuple[str, int]] = {}
|
self._string_literals: Dict[str, Tuple[str, int]] = {}
|
||||||
self._float_literals: Dict[float, str] = {}
|
self._float_literals: Dict[float, str] = {}
|
||||||
self._data_section: Optional[List[str]] = None
|
self._data_section: Optional[List[str]] = None
|
||||||
|
|||||||
36
mem.sl
36
mem.sl
@@ -1,36 +1,8 @@
|
|||||||
import stdlib/stdlib.sl
|
import stdlib/stdlib.sl
|
||||||
import stdlib/io.sl
|
import stdlib/io.sl
|
||||||
|
import stdlib/debug.sl
|
||||||
|
|
||||||
: alloc
|
: main
|
||||||
0 # addr hint (NULL)
|
mem dup 5 swap !
|
||||||
swap # size
|
@ puti cr
|
||||||
3 # prot (PROT_READ | PROT_WRITE)
|
|
||||||
34 # flags (MAP_PRIVATE | MAP_ANON)
|
|
||||||
-1 # fd
|
|
||||||
0 # offset
|
|
||||||
mmap
|
|
||||||
;
|
;
|
||||||
|
|
||||||
: free
|
|
||||||
munmap drop
|
|
||||||
;
|
|
||||||
|
|
||||||
: test-mem-alloc
|
|
||||||
4096 alloc dup 1337 swap ! # allocate 4096 bytes, store 1337 at start
|
|
||||||
dup @ puti cr # print value at start
|
|
||||||
4096 free # free the memory
|
|
||||||
;
|
|
||||||
|
|
||||||
struct: Point
|
|
||||||
field x 8
|
|
||||||
field y 8
|
|
||||||
;struct
|
|
||||||
|
|
||||||
: main2
|
|
||||||
32 alloc # allocate 32 bytes (enough for a Point struct)
|
|
||||||
dup 111 swap Point.x!
|
|
||||||
dup 222 swap Point.y!
|
|
||||||
dup Point.x@ puti cr
|
|
||||||
Point.y@ puti cr
|
|
||||||
32 free # free the memory
|
|
||||||
;
|
|
||||||
@@ -1,27 +1,11 @@
|
|||||||
# Reserve 64 bytes in .bss
|
# Reserve 64 bytes in .bss
|
||||||
# persistent: resb 64
|
# persistent: resb 64
|
||||||
|
# push the addr of it
|
||||||
|
|
||||||
# : p@ ( offset -- n )
|
:asm mem {
|
||||||
:asm p@ {
|
lea rax, [rel persistent]
|
||||||
mov rax, [r12] ; offset
|
sub r12, 8
|
||||||
lea rbx, [rel persistent]
|
mov [r12], rax
|
||||||
add rax, rbx ; address = persistent + offset
|
|
||||||
mov rax, [rax] ; load value
|
|
||||||
mov [r12], rax ; store on stack
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
# : p! ( value offset -- )
|
|
||||||
:asm p! {
|
|
||||||
mov rbx, [r12] ; offset
|
|
||||||
add r12, 8
|
|
||||||
mov rax, [r12] ; value
|
|
||||||
lea rcx, [rel persistent]
|
|
||||||
add rcx, rbx ; address = persistent + offset
|
|
||||||
mov [rcx], rax ; store value
|
|
||||||
add r12, 8 ; pop value
|
|
||||||
ret
|
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user