about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-03-21 21:22:20 -0700
committerKartik Agaram <vc@akkartik.com>2020-03-21 22:43:43 -0700
commit0ef7f1d2d6f76ca3111cdbc59d4ae0038f61c2aa (patch)
tree3731aa414e0c81d38159731c2f96669e3f453b2a
parentfa805850240423ed3aa079270da26839a68c763e (diff)
downloadmu-0ef7f1d2d6f76ca3111cdbc59d4ae0038f61c2aa.tar.gz
6160 - plan for safe heap access
Based on apps/handle.subx (commit 4894), but we're able to simplify it
further now that we know more about the situation we find ourselves in.
6 instructions, zero pushes or pops.

Before I can start building this, I need to reorganize my use of handles.
They're going to be fat pointers so we can't store them in registers anymore.
-rw-r--r--mu_instructions33
-rw-r--r--mu_summary24
2 files changed, 55 insertions, 2 deletions
diff --git a/mu_instructions b/mu_instructions
index 4c62d4bd..7b2d53b3 100644
--- a/mu_instructions
+++ b/mu_instructions
@@ -157,7 +157,7 @@ Address operations
 var/reg: (addr T) <- address var: T
   => "8d/copy-address *(ebp+" var.stack-offset ") " reg "/r32"
 
-Array operations
+Array operations (TODO: bounds-checking)
 
 var/reg <- length arr/reg2: (addr array T)
   => "8b/-> *" reg2 " " reg "/r32"
@@ -187,4 +187,33 @@ var/reg: (addr T_f) <- get var2/reg2: (addr F), f
 var/reg: (addr T_f) <- get var2: (addr F), f
   => "8d/copy-address *(ebp+" var2.stack-offset "+" offset(f) ") " reg "/r32"
 
-vim:ft=mu:nowrap:tw&
+Handles for safe access to the heap
+
+copy-handle-to dest: (handle T), src: (handle T)
+  => "50/push-eax"
+     "8b/-> *(ebp+" src.stack-offset ") 0/r32/eax"
+     "89/<- *(ebp+" dest.stack-offset ") 0/r32/eax"
+     "8b/-> *(ebp+" src.stack-offset+4 ") 0/r32/eax"
+     "89/<- *(ebp+" dest.stack-offset+4 ") 0/r32/eax"
+     "58/pop-to-eax"
+
+copy-handle-to *dest/reg: (addr handle T), src: (handle T)
+  => "50/push-eax"
+     "8b/-> *(ebp+" src.stack-offset ") 0/r32/eax"
+     "89/<- *" reg " 0/r32/eax"
+     "8b/-> *(ebp+" src.stack-offset+4 ") 0/r32/eax"
+     "89/<- *(" reg "+4) 0/r32/eax"
+     "58/pop-to-eax"
+
+out/reg: (addr T) <- lookup in: (handle T)
+  => # payload_allocid = in->address->allocid
+     "8b/-> *(epb+" (in.stack-offset+4) ") " reg "/r32"
+     "8b/-> *" reg " " reg "/r32"
+     # if (payload_allocid != handle->allocid) abort
+     "39/compare *(ebp+" in.stack-offset ") " reg "/r32"
+     "0f 85/jump-if-!= $lookup:abort/disp32"
+     # return payload
+     "8b/-> *(epb+" (in.stack-offset+4) ") " reg "/r32"
+     "81 0/subop/add %" reg " 4/imm32"  # skip payload->allocid
+
+vim:ft=mu:nowrap:textwidth=0
diff --git a/mu_summary b/mu_summary
index d098d28f..1ac7a086 100644
--- a/mu_summary
+++ b/mu_summary
@@ -216,3 +216,27 @@ Similarly, conditional loops:
   var/reg: (addr T_f) <- get var/reg: (addr T), f
     where record (product) type T has elements a, b, c, ... of types T_a, T_b, T_c, ...
   var/reg: (addr T_f) <- get var: (addr T), f
+
+## Handles for safe access to the heap
+
+Say we created a handle like this on the stack (it can't be in a register)
+  var x: (handle T)
+  allocate Heap, T, x
+
+You can copy handles to another variable on the stack like this:
+  var y: (handle T)
+  copy-handle-to y, x
+
+You can also save handles inside other user-defined types like this:
+  var y/reg: (addr handle T_f) <- get var: (addr T), f
+  copy-handle-to *y, x
+
+Or this:
+  var y/reg: (addr handle T) <- index arr: (addr array handle T), n
+  copy-handle-to *y, x
+
+Handles can be converted into addresses like this:
+  var y/reg: (addr T) <- lookup x
+
+It's illegal to continue to use this addr after a function that reclaims heap
+memory. You have to repeat the lookup.
Ben Morrison <ben@gbmor.dev> 2019-05-13 17:42:32 -0400 committer Ben Morrison <ben@gbmor.dev> 2019-05-13 22:13:54 -0400 template initialization added' href='/gbmor/getwtxt/commit/assets/tmpl/index.html?h=v0.4.12&id=920306cbcad3df05e01699cd19f3767e069ab139'>920306c ^
3df037d ^









920306c ^
3df037d ^





0f645e7 ^
29c35a3 ^
3df037d ^

1eae520 ^
3df037d ^


0f645e7 ^
920306c ^
920306c ^
920306c ^
29c35a3 ^
920306c ^
61d34d4 ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87