From ed2b91411a5d73bd54fc8f2623ecd1c535689b5d Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sun, 24 May 2020 10:01:46 -0400 Subject: [PATCH] Limit ourselves to 8-bit branching I'm planning on going back to 8-bit branching. 16-bit br cells incur a non-negligible penalty and, while at first 64 words (128 bytes forward or backward) seemed a bit limiting, I now don't see why one would ever construct such a big branch. It would be un-forthy. Also, I looked at using BC instead of IY to hold IP and the transition would be a lot easier with 8-bit branching. In this commit, all I do is add overflow checks in IF. The mechanic below doesn't change. I'll give myself some time to think it over so that I avoid yet another back and forth. --- blk/004 | 1 + blk/023 | 11 +++++++++++ blk/397 | 3 ++- blk/398 | 13 ++++++------- blk/399 | 4 ++-- emul/forth.bin | Bin 6083 -> 6126 bytes 6 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 blk/023 diff --git a/blk/004 b/blk/004 index aea982b..b50a1e8 100644 --- a/blk/004 +++ b/blk/004 @@ -1 +1,2 @@ 21 How blocks are organized 22 Addressed devices +23 Branching diff --git a/blk/023 b/blk/023 new file mode 100644 index 0000000..de7059b --- /dev/null +++ b/blk/023 @@ -0,0 +1,11 @@ +Branching + +Branching in Collapse OS is limited to 8-bit. This represents +64 word references forward or backward. While this might seem +a bit tight at first, having this limit saves us a non- +negligible amount of resource usage. + +The reasoning behind this intentional limit is that huge +branches are generally a indicator that a logic ought to be +simplified. So here's one more constraint for you to help you +towards simplicity. diff --git a/blk/397 b/blk/397 index 80dd387..14955f4 100644 --- a/blk/397 +++ b/blk/397 @@ -1,6 +1,7 @@ ( Now we have "as late as possible" stuff ) +: _bchk DUP 0x7f + 0xff > IF LIT< br-ovfl (print) ABORT THEN ; : DO COMPILE 2>R H@ ; IMMEDIATE -: LOOP COMPILE (loop) H@ - , ; IMMEDIATE +: LOOP COMPILE (loop) H@ - _bchk , ; IMMEDIATE ( LEAVE is implemented in low xcomp ) : LITN 32 , , ( 32 == NUMBER ) ; ( gets its name at the very end. can't comment afterwards ) diff --git a/blk/398 b/blk/398 index 77d6f42..fcf0395 100644 --- a/blk/398 +++ b/blk/398 @@ -1,14 +1,13 @@ -: IF ( -- a | a: br cell addr ) +: IF ( -- a | a: br cell addr ) COMPILE (?br) H@ 2 ALLOT ( br cell allot ) ; IMMEDIATE -: THEN ( a -- | a: br cell addr ) - DUP H@ -^ SWAP ( a-H a ) ! +: THEN ( a -- | a: br cell addr ) + DUP H@ -^ _bchk SWAP ( a-H a ) ! ; IMMEDIATE -: ELSE ( a1 -- a2 | a1: IF cell a2: ELSE cell ) +: ELSE ( a1 -- a2 | a1: IF cell a2: ELSE cell ) COMPILE (br) 2 ALLOT - DUP H@ -^ SWAP ( a-H a ) - ! - H@ 2- ( push a. -2 for allot offset ) + [COMPILE] THEN + H@ 2- ( push a. -2 for allot offset ) ; IMMEDIATE diff --git a/blk/399 b/blk/399 index 0f2a5e5..e0befb6 100644 --- a/blk/399 +++ b/blk/399 @@ -1,6 +1,6 @@ : BEGIN H@ ; IMMEDIATE -: AGAIN COMPILE (br) H@ - , ; IMMEDIATE -: UNTIL COMPILE (?br) H@ - , ; IMMEDIATE +: AGAIN COMPILE (br) H@ - _bchk , ; IMMEDIATE +: UNTIL COMPILE (?br) H@ - _bchk , ; IMMEDIATE : [ INTERPRET ; IMMEDIATE : ] R> DROP ; : LIT< WORD 34 , SCPY 0 C, ; IMMEDIATE diff --git a/emul/forth.bin b/emul/forth.bin index 13b327d5075b975ad7d57faa100e875bb930d5c4..221c9e2acf4e04b8393f6aeb64a9d008b06ec412 100644 GIT binary patch delta 187 zcmX@C|4yItFgN324Mqlrof|p7i0Z{BC1+&!FtGBmFexz9GpH~tF#KmIV@qccU{GR6 zD$>m_OUq$M;x}V+VsPNhYF*BwfW+w($ zpI}#Eh88{*hG2%%oC*v~4C}a$iY;YjVRd3~a&`CglbU>9OiB@ANT^?kr;ivzD^PVh dQ1wz)V`hj%w9sTWaZkpi$pPYKK(bxj1^~VxEMWiu delta 166 zcmaE-e^{UMFgN324MqlrzKxt;M44UuJ11+4$#b6ObYk%F4Dl1594h9`s5N<^n0C4u zQxCHfgR4)ls~|%Qp9(`T!)ZB>S=+fm@=IA+Se+Q0T-`nWlo(q1mI67B x?v9>*TtE&)$5K{fW{~Njej%PdB4FWkpg}O