From 0af73ad1ca296dedb0f2e114eefb21decf746880 Mon Sep 17 00:00:00 2001
From: David Schmenk <dschmenk@gmail.com>
Date: Mon, 8 Jan 2018 13:16:37 -0800
Subject: [PATCH] disable optimizer on 64K machines

---
 src/samplesrc/test.pla  |  8 +++----
 src/toolsrc/codegen.pla | 53 ++++++++++++++++++++++-------------------
 src/toolsrc/parse.pla   | 24 +++++++++----------
 src/toolsrc/plasm.pla   | 14 +++++++----
 src/vmsrc/cmd.pla       |  9 +++----
 5 files changed, 59 insertions(+), 49 deletions(-)

diff --git a/src/samplesrc/test.pla b/src/samplesrc/test.pla
index eba1af4..915a0b5 100755
--- a/src/samplesrc/test.pla
+++ b/src/samplesrc/test.pla
@@ -119,10 +119,10 @@ export def main(range)#0
       putc('?')
   wend
   putln
-  //printfunc(1, 2, &(a,b) a+b)
-  //printfunc(1, 2, &(a,b) (a-b))
-  //lambda = &(x,y) x * y
-  //puti(lambda(2,3));putln
+  printfunc(1, 2, &(a,b) a+b)
+  printfunc(1, 2, &(a,b) (a-b))
+  lambda = &(x,y) x * y
+  puti(lambda(2,3));putln
   a             = vals123
   drop, b, drop = vals123
   drop, drop, c = vals123
diff --git a/src/toolsrc/codegen.pla b/src/toolsrc/codegen.pla
index 75a9e53..322a1f4 100644
--- a/src/toolsrc/codegen.pla
+++ b/src/toolsrc/codegen.pla
@@ -28,7 +28,7 @@
 //
 def new_tag(type)
     tag_cnt++
-    if tag_cnt >= TAGNUM; exit_err(ERR_OVER|ERR_CODE|ERR_TABLE); fin
+    if tag_cnt >= tag_num; exit_err(ERR_OVER|ERR_CODE|ERR_TABLE); fin
     tag_addr=>[tag_cnt] = 0 // Unresolved, nothing to update yet
     tag_type->[tag_cnt] = type
     return tag_cnt
@@ -101,7 +101,7 @@ def emit_addr(tag, offset)#0
     fixup_tag=>[fixup_cnt]  = tag
     fixup_addr=>[fixup_cnt] = codeptr
     fixup_cnt++
-    if fixup_cnt >= FIXUPNUM; exit_err(ERR_OVER|ERR_ID|ERR_TABLE); fin
+    if fixup_cnt >= fixup_num; exit_err(ERR_OVER|ERR_ID|ERR_TABLE); fin
     emit_word(offset + tag_addr=>[tag])
 end
 def emit_reladdr(tag)#0
@@ -434,7 +434,7 @@ def lookup_idglobal(nameptr, len)
     fin
     return idptr
 end
-def add_iddata(namestr, len, type, size)#0
+def new_iddata(namestr, len, type, size)#0
     if idmatch(namestr, len, idglobal_tbl, globals); exit_err(ERR_DUP|ERR_ID); fin
     nametostr(namestr, len, lastglobal + idname)
     lastglobal=>idtype = type
@@ -450,7 +450,7 @@ def add_iddata(namestr, len, type, size)#0
     fin
     globals++
     lastglobal = lastglobal + t_id + len
-    if lastglobal - idglobal_tbl >  IDGLOBALSZ; exit_err(ERR_OVER|ERR_GLOBAL|ERR_ID|ERR_TABLE); fin
+    if lastglobal - idglobal_tbl >  globalbufsz; exit_err(ERR_OVER|ERR_GLOBAL|ERR_ID|ERR_TABLE); fin
 end
 def size_iddata(type, varsize, initsize)#0
     if varsize > initsize
@@ -460,7 +460,7 @@ def size_iddata(type, varsize, initsize)#0
         datasize = datasize + initsize
     fin
 end
-def add_idglobal(namestr, len, type, value, cparms, cvals)#0
+def new_idglobal(namestr, len, type, value, cparms, cvals)#0
     if idmatch(namestr, len, idglobal_tbl, globals); exit_err(ERR_DUP|ERR_ID); fin
     lastglobal=>idval     = value
     lastglobal=>idtype    = type
@@ -469,13 +469,13 @@ def add_idglobal(namestr, len, type, value, cparms, cvals)#0
     nametostr(namestr, len, lastglobal + idname)
     globals++
     lastglobal = lastglobal + t_id + len
-    if lastglobal - idglobal_tbl > IDGLOBALSZ; exit_err(ERR_OVER|ERR_GLOBAL|ERR_ID|ERR_TABLE); fin
+    if lastglobal - idglobal_tbl > globalbufsz; exit_err(ERR_OVER|ERR_GLOBAL|ERR_ID|ERR_TABLE); fin
 end
-def add_idconst(namestr, len, value)#0
-    add_idglobal(namestr, len, CONST_TYPE, value, 0, 0)
+def new_idconst(namestr, len, value)#0
+    new_idglobal(namestr, len, CONST_TYPE, value, 0, 0)
 end
-def add_idfunc(namestr, len, type, tag, cfnparms, cfnvals)#0
-    add_idglobal(namestr, len, type|FUNC_TYPE, tag, cfnparms, cfnvals)
+def new_idfunc(namestr, len, type, tag, cfnparms, cfnvals)#0
+    new_idglobal(namestr, len, type|FUNC_TYPE, tag, cfnparms, cfnvals)
 end
 def set_idfunc(namestr, len, tag, cparms, cvals)#0
     word idptr
@@ -494,19 +494,24 @@ def init_idglobal#0
     word op
     byte i
 
-    puts("Mem free: "); puth(heapavail); putln
-    tag_addr     = heapalloc(TAGNUM*2)
-    tag_type     = heapalloc(TAGNUM)
-    puts("Mem free after tag allocate: "); puth(heapavail); putln
-    fixup_tag    = heapalloc(FIXUPNUM*2)
-    fixup_addr   = heapalloc(FIXUPNUM*2)
-    puts("Mem free after fixup allocate: "); puth(heapavail); putln
-    idglobal_tbl = heapalloc(IDGLOBALSZ)
-    idlocal_tbl  = heapalloc(IDLOCALSZ)
-    puts("Mem free after global allocate: "); puth(heapavail); putln
+    tag_num     = TAGNUM
+    fixup_num   = FIXUPNUM
+    globalbufsz = IDGLOBALSZ
+    localbufsz  = IDLOCALSZ
+    if isult(heapavail, $4000)
+        tag_num     = TAGNUM/2
+        fixup_num   = FIXUPNUM/2
+        globalbufsz = IDGLOBALSZ/2
+        localbufsz  = IDLOCALSZ/2
+    fin
+    tag_addr     = heapalloc(tag_num*2)
+    tag_type     = heapalloc(tag_num)
+    fixup_tag    = heapalloc(fixup_num*2)
+    fixup_addr   = heapalloc(fixup_num*2)
+    idglobal_tbl = heapalloc(globalbufsz)
+    idlocal_tbl  = heapalloc(localbufsz)
     codebufsz    = heapavail - 4096
     codebuff     = heapalloc(codebufsz)
-    puts("Mem free after data+code allocate: "); puth(heapavail); putln
     codeptr      = codebuff
     lastglobal   = idglobal_tbl
     puts("Data+Code buffer size = "); puti(codebufsz); putln
@@ -521,14 +526,14 @@ def init_idglobal#0
     next
     op=>opnext = NULL
 end
-def add_idlocal(namestr, len, type, size)#0
+def new_idlocal(namestr, len, type, size)#0
     if idmatch(namestr, len, @idlocal_tbl, locals); exit_err(ERR_DUP|ERR_ID); fin
     lastlocal=>idval  = framesize
     lastlocal=>idtype = type | LOCAL_TYPE
     nametostr(namestr, len, lastlocal + idname)
     locals++
     lastlocal = lastlocal + t_id + len
-    if lastlocal - idlocal_tbl > IDLOCALSZ; exit_err(ERR_OVER|ERR_LOCAL|ERR_TABLE); fin
+    if lastlocal - idlocal_tbl > localbufsz; exit_err(ERR_OVER|ERR_LOCAL|ERR_TABLE); fin
     framesize = framesize + size
     if framesize > 255; exit_err(ERR_OVER|ERR_LOCAL|ERR_FRAME); fin
 end
@@ -553,7 +558,7 @@ end
 //
 // Module dependency list
 //
-def add_moddep(strptr, strlen)#0
+def new_moddep(strptr, strlen)#0
     if strlen > 15; strlen = 15; fin
     memcpy(@moddep_tbl[moddep_cnt*16] + 1, strptr, strlen)
     moddep_tbl[moddep_cnt*16] = strlen
diff --git a/src/toolsrc/parse.pla b/src/toolsrc/parse.pla
index b7180f0..809370b 100644
--- a/src/toolsrc/parse.pla
+++ b/src/toolsrc/parse.pla
@@ -909,7 +909,7 @@ def parse_var(type, basesize)#0
     if token == SET_TKN
         if type & (EXTERN_TYPE|LOCAL_TYPE); exit_err(ERR_INVAL|ERR_LOCAL|ERR_INIT); fin
         if idlen
-            add_iddata(idptr, idlen, type, 0)
+            new_iddata(idptr, idlen, type, 0)
         fin
         constval, constsize, consttype = parse_constexpr
         arraysize = emit_data(type, consttype, constval, constsize)
@@ -920,9 +920,9 @@ def parse_var(type, basesize)#0
         size_iddata(PTR_TYPE, size, arraysize)
     elsif idlen
         if infunc
-            add_idlocal(idptr, idlen, type, size)
+            new_idlocal(idptr, idlen, type, size)
         else
-            add_iddata(idptr, idlen, type, size)
+            new_iddata(idptr, idlen, type, size)
         fin
     fin
 end
@@ -967,14 +967,14 @@ def parse_struc#0
                     size = size * 2
                 fin
                 if idlen
-                    add_idconst(idstr, idlen, offset)
+                    new_idconst(idstr, idlen, offset)
                 fin
                 offset = offset + size
             until token <> COMMA_TKN
         fin
     loop
     if struclen
-        add_idconst(@strucid, struclen, offset)
+        new_idconst(@strucid, struclen, offset)
     fin
     if token <> END_TKN; exit_err(ERR_MISS|ERR_CLOSE|ERR_STATE); fin
     scan
@@ -994,7 +994,7 @@ def parse_vars(type)
             idlen = tknlen
             if scan <> SET_TKN; exit_err(ERR_INVAL|ERR_CONST); fin
             value, size, type = parse_constexpr
-            add_idconst(idptr, idlen, value)
+            new_idconst(idptr, idlen, value)
             break
         is STRUC_TKN
             parse_struc
@@ -1043,7 +1043,7 @@ def parse_vars(type)
                         if not parse_const(@cfnvals); exit_err(ERR_INVAL|ERR_CONST); fin
                         scan
                     fin
-                    add_idfunc(idptr, idlen, type, new_tag(type & EXTERN_TYPE ?? EXTERN_FIXUP|WORD_FIXUP :: WORD_FIXUP), cfnparms, cfnvals)
+                    new_idfunc(idptr, idlen, type, new_tag(type & EXTERN_TYPE ?? EXTERN_FIXUP|WORD_FIXUP :: WORD_FIXUP), cfnparms, cfnvals)
                 else
                     exit_err(ERR_MISS|ERR_ID)
                 fin
@@ -1059,7 +1059,7 @@ end
 def parse_mods
     if token == IMPORT_TKN
         if scan <> ID_TKN; exit_err(ERR_MISS|ERR_ID); fin
-        add_moddep(tknptr, tknlen)
+        new_moddep(tknptr, tknlen)
         scan
         while parse_vars(EXTERN_TYPE); nextln; loop
         if token <> END_TKN; exit_err(ERR_MISS|ERR_CLOSE|ERR_STATE); fin
@@ -1081,7 +1081,7 @@ def parse_lambda
         repeat
             if scan == ID_TKN
                 cfnparms++
-                add_idlocal(tknptr, tknlen, WORD_TYPE, 2)
+                new_idlocal(tknptr, tknlen, WORD_TYPE, 2)
                 scan
             fin
         until token <> COMMA_TKN
@@ -1118,7 +1118,7 @@ def parse_lambda
         func_tag                  = new_tag(WORD_FIXUP)
         lambda_tag[lambda_cnt]    = func_tag
         lambda_cparms[lambda_cnt] = cfnparms
-        add_idfunc(@lambda_id[lambda_cnt * 4 + 1], 3, FUNC_TYPE, func_tag, cfnparms, 1)
+        new_idfunc(@lambda_id[lambda_cnt * 4 + 1], 3, FUNC_TYPE, func_tag, cfnparms, 1)
     fin
     lambda_cnt++
     if lambda_cnt >= LAMBDANUM; parse_warn("Lambda function overflow"); fin
@@ -1148,7 +1148,7 @@ def parse_defs
             repeat
                 if scan == ID_TKN
                     cfnparms++
-                    add_idlocal(tknptr, tknlen, WORD_TYPE, 2)
+                    new_idlocal(tknptr, tknlen, WORD_TYPE, 2)
                     scan
                 fin
             until token <> COMMA_TKN
@@ -1167,7 +1167,7 @@ def parse_defs
             idptr=>idtype = idptr=>idtype | type
         else
             func_tag = new_tag(WORD_FIXUP)
-            add_idfunc(idstr, idlen, type, func_tag, cfnparms, infuncvals)
+            new_idfunc(idstr, idlen, type, func_tag, cfnparms, infuncvals)
         fin
         emit_tag(func_tag)
         while parse_vars(LOCAL_TYPE); nextln; loop
diff --git a/src/toolsrc/plasm.pla b/src/toolsrc/plasm.pla
index 08e8d46..c4fab9b 100644
--- a/src/toolsrc/plasm.pla
+++ b/src/toolsrc/plasm.pla
@@ -232,7 +232,7 @@ end
 // Generated code buffers
 //
 const OPSEQNUM          = 256
-const TAGNUM            = 2048
+const TAGNUM            = 1024
 const FIXUPNUM          = 2048
 const IDGLOBALSZ        = 2048
 const IDLOCALSZ         = 512
@@ -242,7 +242,8 @@ word tag_addr, tag_type
 word idglobal_tbl, idlocal_tbl
 word pending_seq
 word globals, lastglobal, lastlocal, savelast
-word codebufsz, datasize, framesize, savesize
+word tag_num, fixup_num, globalbufsz, localbufsz, codebufsz
+word datasize, framesize, savesize
 byte locals, savelocals
 word codebuff, codeptr, entrypoint
 word modsysflags
@@ -420,9 +421,12 @@ if ^arg and ^(arg + 1) == '-'
             //
             // Load optimizer module here
             //
-            if modexec("CODEOPT") >= 0
-                outflags = outflags | OPTIMIZE
-            else
+            if MACHID & $30 == $30
+                if modexec("CODEOPT") >= 0
+                    outflags = outflags | OPTIMIZE
+                fin
+            fin
+            if not (outflags & OPTIMIZE)
                 puts("\nOptimizer disabled\n")
             fin
             opt++
diff --git a/src/vmsrc/cmd.pla b/src/vmsrc/cmd.pla
index a930b67..c1d063d 100755
--- a/src/vmsrc/cmd.pla
+++ b/src/vmsrc/cmd.pla
@@ -1063,7 +1063,7 @@ def loadmod(mod)#1
                     fin
                     if ^rld & $10 // EXTERN reference.
                         fixup = fixup + lookupextern(esd, rld->3)
-                    else      // INTERN fixup.
+                    else          // INTERN fixup.
                         fixup = fixup + modfix - MODADDR
                         if uword_isge(fixup, bytecode)
                             //
@@ -1121,18 +1121,19 @@ def loadmod(mod)#1
     //
     fixup = 0 // This is repurposed for the return code
     if init
-        fixup = adddef(defbank, init - defofst + defaddr, @deflast)()
+        init  = init - defofst + defaddr
+        fixup = adddef(defbank, init, @deflast)()
         if fixup < modinitkeep
             //
             // Free init routine unless initkeep
             //
             if defbank
-                xheap  = init - defofst + defaddr
+                xheap = init
             else
                 //
                 // Free up init code in main memory.
                 //
-                releaseheap(init - defofst + defaddr)
+                releaseheap(init)
             fin
             if fixup < 0
                 perr = -fixup