shithub: MicroHs

Download patch

ref: 28638046f4f3be0644397948d2afc839e6440c4f
parent: 787b9f7567a131c27c10f2152d8059cce6e0757a
author: Lennart Augustsson <lennart.augustsson@epicgames.com>
date: Sun Jan 14 08:23:22 EST 2024

Devolve code to ancient C to be compilable with cc65.

There is still a lot left to do.

--- a/src/runtime/bfile.c
+++ b/src/runtime/bfile.c
@@ -246,7 +246,7 @@
 {
   struct BFILE_lzw *p = (struct BFILE_lzw*)bp;
   char *s;
-  int c, n;
+  int c, n, l;
 
   /* Do we have an ungetb character? */
   if (p->unget >= 0) {
@@ -272,7 +272,7 @@
   if (!s) {
     char *os = p->table[p->old];
     strcpy(p->buf, os);
-    int l = strlen(os);
+    l = strlen(os);
     p->buf[l++] = p->ch;
     p->buf[l] = '\0';
   } else {
@@ -300,8 +300,9 @@
 closeb_lzw(BFILE *bp)
 {
   struct BFILE_lzw *p = (struct BFILE_lzw*)bp;
+  int i;
 
-  for (int i = 0; i < DICTSIZE; i++) {
+  for (i = 0; i < DICTSIZE; i++) {
     if (p->table[i])
       FREE(p->table[i]);
   }
@@ -350,6 +351,7 @@
   int      unget;
 };
 
+/* This is not right with WORD_SIZE==16 */
 int
 getb_utf8(BFILE *bp)
 {
--- a/src/runtime/eval.c
+++ b/src/runtime/eval.c
@@ -90,9 +90,9 @@
 int
 FFS(bits_t x)
 {
+  int i;
   if (!x)
     return 0;
-  int i;
   for(i = 1; !(x & 1); x >>= 1, i++)
     ;
   return i;
@@ -321,6 +321,8 @@
 arr_alloc(size_t sz, NODEPTR e)
 {
   struct ioarray *arr = MALLOC(sizeof(struct ioarray) + (sz-1) * sizeof(NODEPTR));
+  size_t i;
+
   if (!arr)
     memerr();
   arr->next = array_root;
@@ -327,7 +329,7 @@
   array_root = arr;
   arr->marked = 0;
   arr->size = sz;
-  for(size_t i = 0; i < sz; i++)
+  for(i = 0; i < sz; i++)
     arr->array[i] = e;
   //PRINT("arr_alloc(%d, %p) = %p\n", (int)sz, e, arr);
   num_arr_alloc++;
@@ -438,13 +440,16 @@
 static INLINE NODEPTR
 alloc_node(enum node_tag t)
 {
+  heapoffs_t i = next_scan_index / BITS_PER_WORD;
+  int k;                        /* will contain bit pos + 1 */
+  heapoffs_t pos;
+  NODEPTR n;
+
 #if SANITY
   if (num_free <= 0)
     ERR("alloc_node");
 #endif
 
-  heapoffs_t i = next_scan_index / BITS_PER_WORD;
-  int k;                        /* will contain bit pos + 1 */
   for(;;) {
     heapoffs_t word = free_map[i];
     k = FFS(word);
@@ -461,8 +466,8 @@
     }
 #endif
   }
-  heapoffs_t pos = i * BITS_PER_WORD + k - 1; /* first free node */
-  NODEPTR n = HEAPREF(pos);
+  pos = i * BITS_PER_WORD + k - 1; /* first free node */
+  n = HEAPREF(pos);
   mark_used(n);
   next_scan_index = pos;
 
@@ -615,6 +620,10 @@
 void
 init_nodes(void)
 {
+  enum node_tag t;
+  size_t j;
+  NODEPTR n;
+
   ALLOC_HEAP(heap_size);
   free_map_nwords = (heap_size + BITS_PER_WORD - 1) / BITS_PER_WORD; /* bytes needed for free map */
   free_map = MALLOC(free_map_nwords * sizeof(bits_t));
@@ -650,7 +659,7 @@
     }
   }
 #else
-  for(enum node_tag t = T_FREE; t < T_LAST_TAG; t++) {
+  for(t = T_FREE; t < T_LAST_TAG; t++) {
     NODEPTR n = HEAPREF(heap_start++);
     SETTAG(n, t);
     switch (t) {
@@ -672,7 +681,7 @@
     default:
       break;
     }
-    for (int j = 0; j < sizeof primops / sizeof primops[0];j++) {
+    for (j = 0; j < sizeof primops / sizeof primops[0];j++) {
       if (primops[j].tag == t) {
         primops[j].node = n;
       }
@@ -680,7 +689,7 @@
   }
 #endif
 #if GCRED
-  for (uint j = 0; j < sizeof primops / sizeof primops[0]; j++) {
+  for (j = 0; j < sizeof primops / sizeof primops[0]; j++) {
     flip_ops[primops[j].tag] = primops[j].flipped;
   }
 #endif
@@ -690,7 +699,7 @@
    * do not have single constructors.
    * But we can make compound one, since they are irreducible.
    */
-#define NEWAP(c, f, a) do { NODEPTR n = HEAPREF(heap_start++); SETTAG(n, T_AP); FUN(n) = (f); ARG(n) = (a); (c) = n;} while(0)
+#define NEWAP(c, f, a) do { n = HEAPREF(heap_start++); SETTAG(n, T_AP); FUN(n) = (f); ARG(n) = (a); (c) = n;} while(0)
   NEWAP(combLT, combZ,     combFalse);  /* Z B */
   NEWAP(combEQ, combFalse, combFalse);  /* K K */
   NEWAP(combGT, combFalse, combTrue);   /* K A */
@@ -726,8 +735,9 @@
 {
   NODEPTR n;
 #if GCRED
-  value_t i;
+  value_t val;
 #endif
+  size_t i;
   enum node_tag tag;
 
   //  mark_depth++;
@@ -817,9 +827,9 @@
   }
 #endif
 #if INTTABLE
-  if (tag == T_INT && LOW_INT <= (i = GETVALUE(n)) && i < HIGH_INT) {
+  if (tag == T_INT && LOW_INT <= (val = GETVALUE(n)) && val < HIGH_INT) {
     SETTAG(n, T_IND);
-    INDIR(n) = intTable[i - LOW_INT];
+    INDIR(n) = intTable[val - LOW_INT];
     red_int++;
     goto top;
   }
@@ -836,7 +846,7 @@
      */
     if (!arr->marked) {
       arr->marked = 1;
-      for(size_t i = 0; i < arr->size; i++)
+      for(i = 0; i < arr->size; i++)
         mark(&arr->array[i]);
     }
   }
@@ -848,6 +858,9 @@
 void
 gc(void)
 {
+  stackptr_t i;
+  struct ioarray **arrp, *arr;
+
   num_gc++;
   num_marked = 0;
 #if WANT_STDIO
@@ -857,7 +870,7 @@
   gc_mark_time -= GETTIMEMILLI();
   mark_all_free();
   //  mark_depth = 0;
-  for (stackptr_t i = 0; i <= stack_ptr; i++)
+  for (i = 0; i <= stack_ptr; i++)
     mark(&stack[i]);
   gc_mark_time += GETTIMEMILLI();
 
@@ -867,8 +880,8 @@
   if (num_free < heap_size / 50)
     ERR("heap exhausted");
 
-  for (struct ioarray **arrp = &array_root; *arrp; ) {
-    struct ioarray *arr = *arrp;
+  for (arrp = &array_root; *arrp; ) {
+    arr = *arrp;
     if (arr->marked) {
       arr->marked = 0;
       arrp = &arr->next;
@@ -963,7 +976,7 @@
   { "llabs",    (funptr_t)llabs,   FFI_II },
 #elif WORD_SIZE == 32  /* WORD_SIZE */
   { "llabs",    (funptr_t)labs,    FFI_II },
-#elif WORD_SIZE == 32  /* WORD_SIZE */
+#elif WORD_SIZE == 16  /* WORD_SIZE */
   { "llabs",    (funptr_t)abs,    FFI_II },
 #else
 #error Unknown WORD_SIZE
@@ -1042,7 +1055,9 @@
 value_t
 lookupFFIname(const char *name)
 {
-  for(int i = 0; i < sizeof(ffi_table) / sizeof(ffi_table[0]); i++)
+  size_t i;
+
+  for(i = 0; i < sizeof(ffi_table) / sizeof(ffi_table[0]); i++)
     if (strcmp(ffi_table[i].ffi_name, name) == 0)
       return (value_t)i;
   return -1;
@@ -1053,10 +1068,12 @@
 {
   NODEPTR r;
   value_t i = lookupFFIname(buf);
+  char *fun;
+
   if (i < 0) {
     /* lookup failed, generate a node that will dynamically generate an error */
     r = alloc_node(T_BADDYN);
-    char *fun = MALLOC(strlen(buf) + 1);
+    fun = MALLOC(strlen(buf) + 1);
     strcpy(fun, buf);
     CSTR(r) = fun;
   } else {
@@ -1156,7 +1173,9 @@
 NODEPTR *
 find_label(heapoffs_t label)
 {
-  for(int i = (int)label; ; i++) {
+  int i;
+
+  for(i = (int)label; ; i++) {
     i %= shared_table_size;
     if (shared_table[i].node == NIL) {
       /* The slot is empty, so claim and return it */
--