imrpove safety of alloc

This commit is contained in:
nora 2023-08-05 14:41:54 +02:00
parent 64d81b5608
commit dc9e33f339
3 changed files with 57 additions and 51 deletions

View file

@ -574,6 +574,14 @@ export class InferContext {
if (rhs.kind === "struct" && lhs.itemId === rhs.itemId) { if (rhs.kind === "struct" && lhs.itemId === rhs.itemId) {
return; return;
} }
break;
}
case "rawptr": {
if (rhs.kind === "rawptr") {
this.assign(lhs.inner, rhs.inner, span);
return;
}
break;
} }
} }

View file

@ -72,24 +72,20 @@ global HEAP_START: HeapPtr = 2048_I32;
// 4+ 4+ 4* 9 = 8+36=42 (round to 64) // 4+ 4+ 4* 9 = 8+36=42 (round to 64)
global HEAP_REGION_START: I32 = 2112_I32; global HEAP_REGION_START: I32 = 2112_I32;
// typedef struct node_t { type NodeS = struct {
// uint hole; hole: I32,
// uint size; size: I32,
// struct node_t* next; next: *NodeS,
// struct node_t* prev; prev: *NodeS,
// } node_t; };
global NODE_HOLE: I32 = 0_I32;
global NODE_SIZE: I32 = 4_I32;
global NODE_NEXT: I32 = 8_I32;
global NODE_PREV: I32 = 12_I32;
// typedef struct { type FootS = struct {
// node_t *header; header: *NodeS,
// } footer_t; };
// typedef struct { type BinS = struct {
// node_t* head; head: *NodeS,
// } bin_t; };
global SIZEOF_NODE: I32 = 16_I32; global SIZEOF_NODE: I32 = 16_I32;
global SIZEOF_FOOTER: I32 = 4_I32; global SIZEOF_FOOTER: I32 = 4_I32;
@ -102,62 +98,63 @@ type BinPtr = I32;
function initHeap() = ( function initHeap() = (
let heap_init_size = 65536_I32 - HEAP_REGION_START; let heap_init_size = 65536_I32 - HEAP_REGION_START;
__i32_store(HEAP_REGION_START + NODE_HOLE, 1_I32); // START.hole = let init_region: *NodeS = ___transmute(HEAP_REGION_START);
__i32_store(HEAP_REGION_START + NODE_SIZE, heap_init_size - SIZEOF_NODE - SIZEOF_FOOTER); // START.size = init_region.hole = 1_I32;
init_region.size = heap_init_size - SIZEOF_NODE - SIZEOF_FOOTER;
createFoot(HEAP_REGION_START); createFoot(init_region);
); );
function createFoot(head_node: NodePtr) = ( function createFoot(head: *NodeS) = (
let foot = getFoot(head_node); let foot = getFoot(head);
__i32_store(foot, head_node); // foot.header = head_node foot.header = head;
); );
function getFoot(node: NodePtr): FootPtr = ( function getFoot(node: *NodeS): *FootS = (
let node_size = __i32_load(node + NODE_SIZE); let node_addr: I32 = ___transmute(node);
node + SIZEOF_NODE + node_size ___transmute(node_addr + SIZEOF_NODE + node.size)
); );
function getWilderness() =; function getWilderness() =;
// llist.c // llist.c
function addNode(bin: BinPtr, node: NodePtr) = ( function addNode(bin: *BinS, node: *NodeS) = (
__i32_store(node + NODE_NEXT, 0_I32); // node.next = node.next = ___transmute(0_I32);
__i32_store(node + NODE_PREV, 0_I32); // node.prev = node.prev = ___transmute(0_I32);
let bin_head: NodePtr = __i32_load(bin); // bin.head let bin_head_addr: I32 = ___transmute(bin.head); // bin.head
if (bin_head == 0_I32) then if (bin_head_addr == 0_I32) then (
__i32_store(bin, node) // bin.head = bin.head = node;
else ( ) else (
let current: NodePtr = bin_head; let current: *NodeS = bin.head;
let previous: NodePtr = 0_I32; let previous: *NodeS = ___transmute(0_I32);
loop ( loop (
if (current != 0_I32) let currentAddr: I32 = ___transmute(current);
& (__i32_load(current + NODE_SIZE) // current.size if (currentAddr != 0_I32) & (current.size <= node.size) then break;
<= __i32_load(node + NODE_SIZE)) // node.size
then break;
previous = current; previous = current;
current = __i32_load(current + NODE_NEXT); // current.next current = current.next;
); );
if (current == 0_I32) then ( let currentAddr: I32 = ___transmute(current);
__i32_store(previous + NODE_NEXT, node); // previous.next if (currentAddr == 0_I32) then (
__i32_store(node + NODE_PREV, previous); // node.prev previous.next = node;
node.prev = previous;
) else ( ) else (
if (previous != 0_I32) then ( let previous_addr: I32 = ___transmute(previous);
__i32_store(node + NODE_NEXT, current); // node.next if (previous_addr != 0_I32) then (
__i32_store(previous + NODE_NEXT, node); // previous.next node.next = current;
previous.next = node;
__i32_store(node + NODE_PREV, previous); // node.prev node.prev = previous;
__i32_store(current + NODE_PREV, node); // current.prev current.prev = node;
) else ( ) else (
__i32_store(node + NODE_NEXT, __i32_load(bin)); // node.next = bin.head node.next = bin.head;
__i32_store(__i32_load(bin) + NODE_PREV, node); // bin.head.prev = node; bin.head.prev = node;
__i32_store(bin, node); // bin.head bin.head = node;
); );
); );
) )

View file

@ -9,5 +9,6 @@ function main() = (
); );
function rawr(a: *A) = ( function rawr(a: *A) = (
let a: *A = a;
a.a = 1; a.a = 1;
); );