shithub: femtolisp

Download patch

ref: 478f3c13a0a71bfdb9d7a7e621bf93c61c32f2a0
parent: 815de30dc8eeaee1a426ea62b5411b787e4f742b
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Jan 12 21:39:40 EST 2025

make it work on Quadra 800 and 900 (Mac OS 8.x)

--- a/3rd/fn.h
+++ b/3rd/fn.h
@@ -26,11 +26,11 @@
 static inline uint32_t
 __builtin_popcount(Tbitmap w)
 {
-	w -= (w >> 1) & 0x55555555;
-	w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
-	w = (w + (w >> 4)) & 0x0F0F0F0F;
-	w = (w * 0x01010101) >> 24;
-	return(w);
+	w -= (w >> 1) & 0x55555555U;
+	w = (w & 0x33333333U) + ((w >> 2) & 0x33333333U);
+	w = (w + (w >> 4)) & 0x0F0F0F0FU;
+	w = (w * 0x01010101U) >> 24;
+	return w;
 }
 #endif
 
@@ -104,7 +104,7 @@
 	static inline type \
 	Tindex_##field(Tindex i) \
 	{ \
-		return(Tunmask(field, i)); \
+		return Tunmask(field, i); \
 	} \
 	struct dummy
 
@@ -127,13 +127,13 @@
 static inline Tindex
 Tbitmap_add(Tindex i, Tbitmap bitmap)
 {
-	return(i | Tix_place(bitmap));
+	return i | Tix_place(bitmap);
 }
 
 static inline Tindex
 Tbitmap_del(Tindex i, Tbitmap bitmap)
 {
-	return(i & ~Tix_place(bitmap));
+	return i & ~Tix_place(bitmap);
 }
 
 // sanity checks!
@@ -170,14 +170,14 @@
 {
 	uint32_t off = Tindex_offset(i);
 	if(off >= len)
-		return(0);
-	else return knybble(key, off, Tindex_shift(i));
+		return 0;
+	return knybble(key, off, Tindex_shift(i));
 }
 
 static inline Tbitmap
 twigbit(Tindex i, const char *key, size_t len)
 {
-	return(1U << nibble(i, key, len));
+	return 1U << nibble(i, key, len);
 }
 
 static inline bool
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@
 This is a reanimation of
 https://github.com/lambdaconservatory/femtolisp with bigger plans.
 
-Supported OS: [9front](http://9front.org), Unix-like operating systems (OpenBSD, NetBSD, Linux, etc), MacOS 9.x.
+Supported OS: [9front](http://9front.org), Unix-like operating systems (OpenBSD, NetBSD, Linux, etc), MacOS [89].x.
 
 Supported CPUs: any decent 32 or 64-bit, little or big endian.
 
@@ -43,12 +43,15 @@
 
 	mk all test
 
-### MacOS 9.x (PowerPC)
+### MacOS 9.x (PowerPC) or MacOS 8.x (m68k)
 
 Install and build [Retro68](https://github.com/autc04/Retro68).
 
 	ln -s path-to-Retro68-build/toolchain cross/macos-toolchain
-	meson setup build . -Dbuildtype=minsize --cross-file cross/powerpc-apple.txt -Dstrip=true
+	# for PowerPC:
+	meson setup build . -Dbuildtype=minsize --cross-file cross/powerpc-apple.txt
+	# for m68k:
+	meson setup build . -Dbuildtype=minsize --cross-file cross/m68k-apple.txt
 	ninja -C build
 
 `build/flisp.bin` can be then copied to your MacOS, it contains the executable.
--- a/builtins.c
+++ b/builtins.c
@@ -372,7 +372,7 @@
 	if(nargs > 1)
 		argcount(nargs, 1);
 	if(nargs == 0){
-		char buf[1024];
+		char buf[4096];
 		if(getcwd(buf, sizeof(buf)) == nil)
 			lerrorf(FL_IOError, "could not get current dir");
 		return string_from_cstr(buf);
--- a/cross/m68-apple.txt
+++ b/cross/m68-apple.txt
@@ -4,8 +4,10 @@
 prefix = path + 'm68k-apple-macos-'
 
 [built-in options]
-c_args = ['-fdata-sections', '-mcpu=68040', '-ffunction-sections', '-D__macos__']
-cpp_link_args = ['-Wl,-gc-sections', '-mcpu=68040', '-Wl,--mac-strip-macsbug']
+c_args = ['-march=68020', '-fdata-sections', '-ffunction-sections', '-D__macos__']
+c_link_args = ['-march=68020', '-Wl,--mac-single', '-Wl,--mac-strip-macsbug']
+cpp_args = c_args
+cpp_link_args = c_link_args
 
 [properties]
 rincludes = toolchain + 'RIncludes'
--- a/cvalues.c
+++ b/cvalues.c
@@ -5,9 +5,6 @@
 #include "iostream.h"
 #include "equal.h"
 
-// trigger unconditional GC after this many bytes are allocated
-#define ALLOC_LIMIT_TRIGGER 67108864
-
 static void cvalue_init(fltype_t *type, value_t v, void *dest);
 
 void
@@ -843,7 +840,7 @@
 	argcount(nargs, 1);
 	symbol_t *s = tosymbol(args[0]);
 	if(!iscbuiltin(s->binding))
-		lerrorf(FL_ArgError, "function %s not found", s->name);
+		lerrorf(FL_ArgError, "function \"%s\" not found", s->name);
 	return s->binding;
 }
 
--- a/flisp.c
+++ b/flisp.c
@@ -1382,6 +1382,7 @@
 	set(FL_printlengthsym = symbol("*print-length*", false), FL_f);
 	set(FL_printlevelsym = symbol("*print-level*", false), FL_f);
 	FL(lasterror) = FL_nil;
+
 	for(i = 0; i < nelem(builtins); i++){
 		if(builtins[i].name)
 			set(symbol(builtins[i].name, false), builtin(i));
@@ -1400,7 +1401,6 @@
 	const builtinspec_t *b;
 	for(i = 0, b = builtin_fns; i < nelem(builtin_fns); i++, b++)
 		set(symbol(b->name, false), cbuiltin(b->name, b->fptr));
-
 	table_init();
 	iostream_init();
 	compress_init();
--- a/flmain.c
+++ b/flmain.c
@@ -39,15 +39,8 @@
 	ios_init_stdstreams();
 	mpsetminbits(sizeof(fixnum_t)*8);
 
-#if defined(__macos__)
-	if(fl_init(INITIAL_HEAP_SIZE) != 0){
-		fprintf(stderr, "fl_init failed\n");
-		getchar();
-#else
-	if(fl_init(2*1024*1024) != 0){
-#endif
+	if(fl_init(INITIAL_HEAP_SIZE) != 0)
 		exit(1);
-	}
 
 	value_t f = cvalue(FL(iostreamtype), (int)sizeof(ios_t));
 	ios_t *s = value2c(ios_t*, f);
--- /dev/null
+++ b/macos/femtolispm68k.r
@@ -1,0 +1,22 @@
+#include "Retro68APPL.r"
+
+resource 'SIZE' (-1) {
+	reserved,
+	ignoreSuspendResumeEvents,
+	reserved,
+	cannotBackground,
+	needsActivateOnFGSwitch,
+	backgroundAndForeground,
+	dontGetFrontClicks,
+	ignoreChildDiedEvents,
+	is32BitCompatible,
+	notHighLevelEventAware,
+	onlyLocalHLEvents,
+	notStationeryAware,
+	dontUseTextEditServices,
+	reserved,
+	reserved,
+	reserved,
+	4 * 1024 * 1024,
+	2 * 1024 * 1024
+};
--- /dev/null
+++ b/macos/femtolispppc.r
@@ -1,0 +1,22 @@
+#include "RetroPPCAPPL.r"
+
+resource 'SIZE' (-1) {
+	reserved,
+	ignoreSuspendResumeEvents,
+	reserved,
+	cannotBackground,
+	needsActivateOnFGSwitch,
+	backgroundAndForeground,
+	dontGetFrontClicks,
+	ignoreChildDiedEvents,
+	is32BitCompatible,
+	notHighLevelEventAware,
+	onlyLocalHLEvents,
+	notStationeryAware,
+	dontUseTextEditServices,
+	reserved,
+	reserved,
+	reserved,
+	16 * 1024 * 1024,
+	8 * 1024 * 1024
+};
--- a/macos/platform.h
+++ b/macos/platform.h
@@ -1,3 +1,5 @@
+#pragma once
+
 #define _XOPEN_SOURCE 700
 #include <assert.h>
 #include <ctype.h>
--- a/meson.build
+++ b/meson.build
@@ -22,7 +22,6 @@
 	'-Werror=odr',
 	'-Werror=strict-aliasing',
 	'-Wformat=2',
-	'-Wformat-signedness',
 	'-Wint-to-pointer-cast',
 	'-Wmissing-prototypes',
 	'-Wno-format-y2k',
@@ -41,6 +40,25 @@
 	language: 'c',
 )
 
+
+cc = meson.get_compiler('c')
+
+if cc.get_id() == 'clang'
+	add_project_arguments(
+		'-D__wchar_t=__please_no_wchar_t_thank_you',
+		'-Wunused-const-variable',
+		language: 'c',
+	)
+else
+	add_project_arguments(
+		'-Wformat-truncation=2',
+		'-Wlogical-op',
+		'-Wunused-const-variable=2',
+		'-Werror=lto-type-mismatch',
+		language: 'c',
+	)
+endif
+
 platform = 'posix'
 
 if host_machine.system() == 'macos'
@@ -54,16 +72,10 @@
 	cpp = meson.get_compiler('cpp')
 	add_project_arguments(
 		'-DINITIAL_HEAP_SIZE=64*1024',
+		'-DALLOC_LIMIT_TRIGGER=128*1024',
 		'-D__thread=', # does not make sense anyway
 		language: 'c',
 	)
-	if host_machine.cpu() != 'm68k'
-		# computed gotos break m68k build
-		add_project_arguments(
-			'-DCOMPUTED_GOTO',
-			language: 'c',
-		)
-	endif
 	extras = [
 		cpp.find_library('RetroConsole', required: true),
 	]
@@ -70,7 +82,8 @@
 else
 	flisp_exe_name = 'flisp'
 	add_project_arguments(
-		'-DINITIAL_HEAP_SIZE=2*1024*1024',
+		'-DINITIAL_HEAP_SIZE=8*1024*1024',
+		'-DALLOC_LIMIT_TRIGGER=1*1024*1024',
 		'-DCOMPUTED_GOTO',
 		language: 'c',
 	)
@@ -107,26 +120,6 @@
 	'utf8.c',
 ]
 
-cc = meson.get_compiler('c')
-
-if cc.get_id() == 'clang'
-	add_project_arguments(
-		'-D__wchar_t=__please_no_wchar_t_thank_you',
-		'-Wformat-truncation',
-		'-Wno-gnu-offsetof-extensions',
-		'-Wunused-const-variable',
-		language: 'c',
-	)
-else
-	add_project_arguments(
-		'-Wformat-truncation=2',
-		'-Wlogical-op',
-		'-Wunused-const-variable=2',
-		'-Werror=lto-type-mismatch',
-		language: 'c',
-	)
-endif
-
 math = cc.find_library('m', required: false)
 
 boot = custom_target(
@@ -250,12 +243,12 @@
 
 if platform == 'macos'
 	if host_machine.cpu() == 'm68k'
-		template = 'Retro68APPL.r'
+		template = 'femtolispm68k.r'
 		rez_data = '--copy'
 		rezflags = []
 		flisp_code = flisp
 	else
-		template = 'RetroPPCAPPL.r'
+		template = 'femtolispppc.r'
 		rez_data = '--data'
 		rezflags = ['-DTARGET_API_MAC_CARBON=1']
 		flisp_code = custom_target(
@@ -274,11 +267,13 @@
 		)
 	endif
 	rincludes = meson.get_external_property('rincludes')
+	template = join_paths(meson.global_source_root(), 'macos', template)
 
 	flisp_bin = custom_target(
 		'flisp.bin',
 		input: [
 			flisp_code,
+			template,
 		],
 		output: [
 			'flisp.bin',
@@ -288,7 +283,7 @@
 			meson.get_external_property('rez'),
 			'-I' + rincludes,
 		] + rezflags + [
-			rincludes + '/' + template,
+			template,
 			'-DCFRAG_NAME="flisp"',
 			'-o', 'flisp.bin',
 			'-t', 'APPL',
--- a/mkfile
+++ b/mkfile
@@ -2,7 +2,12 @@
 
 BIN=/$objtype/bin
 TARG=flisp
-CFLAGS=$CFLAGS -p -I. -I3rd -I3rd/brieflz -Iplan9 -D__plan9__ -D__${objtype}__ -DNDEBUG
+CFLAGS=$CFLAGS -p -I. -I3rd -I3rd/brieflz -Iplan9 \
+	-D__plan9__ -D__${objtype}__ \
+	-DNDEBUG \
+	-DINITIAL_HEAP_SIZE=8*1024*1024 \
+	-DALLOC_LIMIT_TRIGGER=1*1024*1024 \
+
 CLEANFILES=plan9/flisp.boot.s plan9/builtin_fns.h
 
 HFILES=\
--- a/posix/platform.h
+++ b/posix/platform.h
@@ -1,3 +1,5 @@
+#pragma once
+
 #define _XOPEN_SOURCE 700
 #include <assert.h>
 #include <ctype.h>
--- a/types.c
+++ b/types.c
@@ -13,8 +13,10 @@
 			return ft;
 	}
 	void **bp = equalhash_bp(&FL(TypeTable), (void*)t);
-	if(*bp != HT_NOTFOUND)
+	if(*bp != HT_NOTFOUND){
+		assert(*bp != nil);
 		return *bp;
+	}
 
 	bool isarray = iscons(t) && car_(t) == FL_arraysym && iscons(cdr_(t));
 	size_t sz;
@@ -26,6 +28,7 @@
 	}
 
 	ft = MEM_CALLOC(1, sizeof(fltype_t));
+	assert(ft != nil);
 	ft->type = t;
 	ft->numtype = NONNUMERIC;
 	if(issymbol(t)){
@@ -37,6 +40,7 @@
 	if(iscons(t)){
 		if(isarray){
 			fltype_t *eltype = get_type(car_(cdr_(t)));
+			assert(eltype != nil);
 			if(eltype->size == 0){
 				MEM_FREE(ft);
 				lerrorf(FL_ArgError, "invalid array element type");