aboutsummaryrefslogtreecommitdiff
path: root/docs/history/15-o3/code/examples/selfpatch.zc
diff options
context:
space:
mode:
Diffstat (limited to 'docs/history/15-o3/code/examples/selfpatch.zc')
-rw-r--r--docs/history/15-o3/code/examples/selfpatch.zc17
1 files changed, 17 insertions, 0 deletions
diff --git a/docs/history/15-o3/code/examples/selfpatch.zc b/docs/history/15-o3/code/examples/selfpatch.zc
new file mode 100644
index 0000000..074ccec
--- /dev/null
+++ b/docs/history/15-o3/code/examples/selfpatch.zc
@@ -0,0 +1,17 @@
+// code is data: find the literal 'A' inside f's compiled words and rewrite it.
+// on the jit the store lands on translated code, which gets dropped and
+// retranslated on the next call.
+fn f() { putc('A'); return 0; }
+fn litw(v) { return v + v + 1; } // how the vm encodes a literal push
+fn main()
+{
+ let p = f;
+ f();
+ while (*p != litw('A')) p++;
+ *p = litw('B');
+ f();
+ *p = litw('C');
+ f();
+ putc(10);
+ return 0;
+}