Skip to content

Commit

Permalink
(crt) More crt cleanup for heap + atexit
Browse files Browse the repository at this point in the history
  • Loading branch information
suborb committed Mar 12, 2024
1 parent c8298ab commit cd92276
Show file tree
Hide file tree
Showing 155 changed files with 579 additions and 720 deletions.
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ z88dk v2.4 - xx.xx.2024

Future release changes:

- [classic] Support more ways of configuring the heap size
- [classic] Additional pragma support for interrupts added to targets
- [cpm] Stack now setup correctly when running on CP/M Plus
- [zsdcc] Upgraded to SDCC 4.4.0 r14648
- [z80asm] process file.asm.m4 by m4 and assemble resulting file.asm

Expand Down
13 changes: 3 additions & 10 deletions include/byteswap.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@
#define _BYTESWAP_H

#include <sys/compiler.h>
#include <intrinsic.h>


unsigned int bswap_16 (unsigned int __x)
{
return (__x >> 8) | (__x << 8);
}

unsigned long bswap_32 (unsigned long __x)
{
return (bswap_16 (__x & 0xffff) << 16) | (bswap_16 (__x >> 16));
}

#define bswap_32(a) intrinsic_swap_endian_32(a)
#define bswap_16(a) intrinsic_swap_endian_16(a)


#endif /*_BYTESWAP_H*/
Expand Down
8 changes: 4 additions & 4 deletions include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@

// Automatic Preset for malloc: 3/4 of the free memory
#ifdef AMALLOC
#pragma define CRT_HEAP_ENABLE = 0x01
#pragma define CRT_HEAP_AMALLOC = 0x01
#endif
#ifdef AMALLOC3
#pragma define CRT_HEAP_ENABLE = 0x01
#pragma define CRT_HEAP_AMALLOC = 0x01
#endif

// Automatic Preset for malloc: 2/4 of the free memory
#ifdef AMALLOC2
#pragma define CRT_HEAP_ENABLE = 0x03
#pragma define CRT_HEAP_AMALLOC = 0x03
#endif

// Automatic Preset for malloc: 1/4 of the free memory
#ifdef AMALLOC1
#pragma define CRT_HEAP_ENABLE = 0x07
#pragma define CRT_HEAP_AMALLOC = 0x07
#endif

extern void __LIB__ mallinit(void);
Expand Down
2 changes: 1 addition & 1 deletion lib/crt/classic/crt_cpm_fcntl.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


; Control whether extending a file will write an empty record
IF !DEFINED_CPM_WRITE_EMPTY_RECORD
IFNDEF CPM_WRITE_EMPTY_RECORD
DEFC CPM_WRITE_EMPTY_RECORD = 1
ENDIF
PUBLIC __CPM_WRITE_EMPTY_RECORD
Expand Down
3 changes: 3 additions & 0 deletions lib/crt/classic/crt_defaults.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
defc DEF__crt_enable_rst = 0 ;; if non-zero and in some crts with code org = 0, set bits indicate which rst locations are implemented with user code
defc DEF__crt_enable_nmi = 0 ;; if non-zero and in some crts with code org = 0, a jump to user code to service the nmi is inserted

defc DEF__crt_stack_size = -1 ;; Overhead for stack if heap required
defc DEF__clib_malloc_heap_size = -1 ;; Default heap size (bytes)


; clib defaults
defc DEF__clib_exit_stack_size = 32 ;; max number of functions that can be registered with atexit()
Expand Down
17 changes: 17 additions & 0 deletions lib/crt/classic/crt_init_atexit.inc
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@

PUBLIC __clib_exit_stack_size
EXTERN exitsp

IF __clib_exit_stack_size > 0
ld hl, -(__clib_exit_stack_size * 2)
add hl,sp
ld sp,hl
ENDIF
IF __CPU_GBZ80__
ld hl,sp+0
ld d,h
ld e,l
ld hl,exitsp
ld a,l
ld (hl+),a
ld a,h
ld (hl+),a
ELIF __CPU_INTEL__
ld hl,0
add hl,sp
ld (exitsp),hl
ELSE
ld (exitsp),sp
ENDIF

53 changes: 39 additions & 14 deletions lib/crt/classic/crt_init_heap.inc
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@

; Optional definition for auto MALLOC init
; it assumes we have free space between the end of
; the compiled program and the stack pointer

; It works only with a _heap pointer defined somewhere else in the crt0.
; Such (long) pointer will hold, at startup
; Heap setup. We have a few ways to setup the heap
;
; 1. -pragma-define:CLIB_MALLOC_HEAP_SIZE - creates a heap in BSS
; 2. -pragma-define:CRT_STACK_SIZE - leaves x for stack, rest is heap
; 3. -DAMALLOC etc - divides up the space. Last address either programmatic or defined by
; CRT_MAX_HEAP_ADDRESS
; 4. Some ports call in with fixed addresses to use
;
; For 2 and 3, heap runs from either _BSS_TAIL or CRT_HEAP_ADDRESS
;
; Note: This code must be called after crt0_init for two reasons:
; 1. The heap pointer is within bss (so would get wiped)
; 2. Some targets have a himem blob sitting after bss on load


MACRO MALLSUB_HLDE
IF __CPU_INTEL__ || __CPU_GBZ80__
ld a,l ; leave 2/4 of the free memory for the stack
ld a,l
sub e
ld l,a
ld a,h
Expand All @@ -21,15 +28,33 @@ MACRO MALLSUB_HLDE
ENDM


IF DEFINED_CLIB_MALLOC_HEAP_SIZE && CLIB_MALLOC_HEAP_SIZE > 0
IF __clib_malloc_heap_size > 0
;; The heap is in BSS section already, just need to register it
ld bc,__autoheap
ld hl,CLIB_MALLOC_HEAP_SIZE
ld hl,__clib_malloc_heap_size
push bc ; main address for malloc area
push hl ; area size
EXTERN sbrk_callee
call sbrk_callee
ELIF DEFINED_CRT_HEAP_ENABLE

ELIF __crt_stack_size > 0
;; Support the newlib model where we set how much stack we want and then the rest is heap
ld hl,sp
ld de,__crt_stack_size
and a
MALLSUB_HLDE
IFDEF CRT_HEAP_ADDRESS
ld de,CRT_HEAP_ADDRESS
ELSE
ld de,__BSS_END_tail
ENDIF
and a
MALLSUB_HLDE
push de ; main address for malloc area
push hl ; area size
EXTERN sbrk_callee
call sbrk_callee
ELIF DEFINED_CRT_HEAP_AMALLOC
;; -DAMALLOC handling
IF CRT_MAX_HEAP_ADDRESS
ld hl,CRT_MAX_HEAP_ADDRESS
defc __crt_skip_heapsize_calcs = 1
Expand Down Expand Up @@ -60,7 +85,7 @@ ELIF DEFINED_CRT_HEAP_ENABLE
ld h,a
ELSE
sbc hl,bc ; hl = total free memory
ENDIF
ENDIF


IFNDEF __crt_skip_heapsize_calcs
Expand Down Expand Up @@ -100,11 +125,11 @@ ELIF DEFINED_CRT_HEAP_ENABLE
; Reduce to heap size to 75% of available memory
MALLSUB_HLDE
; Reduce to 50% if needed
IF CRT_HEAP_ENABLE & 2
IF CRT_HEAP_AMALLOC & 2
MALLSUB_HLDE
ENDIF
; Reduce to 25% if needed
IF CRT_HEAP_ENABLE & 4
IF CRT_HEAP_AMALLOC & 4
MALLSUB_HLDE
ENDIF
ENDIF
Expand Down
21 changes: 21 additions & 0 deletions lib/crt/classic/crt_rules.inc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@
ENDIF
ENDIF

;; Some heapsize definitions
IFDEF CRT_STACK_SIZE
defc __crt_stack_size = CRT_STACK_SIZE
ELSE
IFDEF TAR__crt_stack_size
defc __crt_stack_size = TAR__crt_stack_size
ELSE
defc __crt_stack_size = DEF__crt_stack_size
ENDIF
ENDIF

IFDEF CLIB_MALLOC_HEAP_SIZE
defc __clib_malloc_heap_size = CLIB_MALLOC_HEAP_SIZE
ELSE
IFDEF TAR__clib_malloc_heap_size
defc __clib_malloc_heap_size = TAR__clib_malloc_heap_size
ELSE
defc __clib_malloc_heap_size = DEF__clib_malloc_heap_size
ENDIF
ENDIF


PUBLIC __CRT_KEY_DEL
IFDEF CRT_KEY_DEL
Expand Down
20 changes: 5 additions & 15 deletions lib/crt/classic/crt_section.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ENDIF
; The classic CRTs need some things setup, so do it

SECTION code_crt_init
crt0_init_bss:
crt0_init:
EXTERN __BSS_head
EXTERN __BSS_END_tail
IF CRT_INITIALIZE_BSS = 1
Expand All @@ -43,12 +43,8 @@ init_8080:
dec b
jr NZ,init_8080
ENDIF
ELSE
xor a
ENDIF

; a = 0 - reset exitcount
ld (exitcount),a
IF CRT_ENABLE_STDIO = 1 && CLIB_FOPEN_MAX > 0
; Setup std* streams
ld hl,__sgoioblk+2
Expand Down Expand Up @@ -105,21 +101,15 @@ IF !DEFINED_basegraphics
base_graphics:
defw 0 ;Address of graphics map
ENDIF
PUBLIC exitsp
PUBLIC exitcount
exitsp:
defw 0 ;atexit() stack
exitcount:
defb 0 ;Number of atexit() routines

IF DEFINED_CLIB_MALLOC_HEAP_SIZE && CLIB_MALLOC_HEAP_SIZE > 0

IF __clib_malloc_heap_size > 0
PUBLIC _heap
_heap:
defw 0,0 ;populated by crt_heap_init.inc
__autoheap:
defs CLIB_MALLOC_HEAP_SIZE
defs __clib_malloc_heap_size

ELIF DEFINED_CRT_HEAP_ENABLE
ELIF DEFINED_CRT_HEAP_AMALLOC || __crt_stack_size > 0
PUBLIC _heap
_heap:
defw 0
Expand Down
7 changes: 3 additions & 4 deletions lib/target/abc80/classic/abc80_crt0.asm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
EXTERN _main


PUBLIC cleanup
PUBLIC __Exit
PUBLIC l_dcal

defc CONSOLE_ROWS = 24
Expand All @@ -34,17 +34,16 @@ ENDIF
start:
ld (__restore_sp_onexit+1),sp
INCLUDE "crt/classic/crt_init_sp.inc"
call crt0_init
INCLUDE "crt/classic/crt_init_atexit.inc"
call crt0_init_bss
ld (exitsp),sp


INCLUDE "crt/classic/crt_init_heap.inc"
INCLUDE "crt/classic/crt_init_eidi.inc"

call _main
cleanup:
__Exit:
push hl
call crt0_exit
pop bc
Expand Down
11 changes: 5 additions & 6 deletions lib/target/ace/classic/ace_crt0.asm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

EXTERN _main

PUBLIC cleanup
PUBLIC __Exit
PUBLIC l_dcal


Expand Down Expand Up @@ -58,10 +58,9 @@ ace_ramtest:
ELSE
ld (__restore_sp_onexit+1),sp
INCLUDE "crt/classic/crt_init_sp.inc"
INCLUDE "crt/classic/crt_init_atexit.inc"
ENDIF
call crt0_init_bss
ld (exitsp),sp
call crt0_init
INCLUDE "crt/classic/crt_init_atexit.inc"

IF DEFINED_CRT_FONT
defc CHAR_TABLE = 0x2C00
Expand Down Expand Up @@ -102,7 +101,7 @@ nobit2: rrca
inc l
jr nz,gfx_bloop

; a bit of cleanup (we should load a font, here!)
; a bit of __Exit (we should load a font, here!)
xor a
blankloop:
ld (hl),a
Expand All @@ -124,7 +123,7 @@ ELSE
ENDIF

call _main
cleanup:
__Exit:
push hl
call crt0_exit
pop bc
Expand Down
7 changes: 3 additions & 4 deletions lib/target/agon/classic/agon_crt0.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


EXTERN _main ;main() is always external to crt0 code
PUBLIC cleanup ;jp'd to by exit()
PUBLIC __Exit ;jp'd to by exit()
PUBLIC l_dcal ;jp(hl)
EXTERN asm_im1_handler
EXTERN asm_nmi_handler
Expand Down Expand Up @@ -102,10 +102,9 @@ copy_done:
ld (__cmdline_length+1),a
ENDIF
INCLUDE "crt/classic/crt_init_sp.inc"
call crt0_init
; Make room for the atexit() stack
INCLUDE "crt/classic/crt_init_atexit.inc"
call crt0_init_bss
ld (exitsp),sp

ld (__agon_mbase),a

Expand Down Expand Up @@ -138,7 +137,7 @@ ENDIF
pop bc
pop bc
; Exit code is in hl
cleanup:
__Exit:
push hl
call crt0_exit
pop hl
Expand Down
10 changes: 4 additions & 6 deletions lib/target/alphatro/classic/alphatro_crt0.asm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

EXTERN _main ; main() is always external to crt0 code

PUBLIC cleanup ; jp'd to by exit()
PUBLIC __Exit ; jp'd to by exit()
PUBLIC l_dcal ; jp(hl)


Expand All @@ -35,18 +35,16 @@
jp start

start:
ld (__restore_sp_onexit+1),sp ; Save entry stack
INCLUDE "crt/classic/crt_init_sp.inc"
call crt0_init
INCLUDE "crt/classic/crt_init_atexit.inc"

ld (__restore_sp_onexit+1),sp ; Save entry stack
call crt0_init_bss
ld (exitsp),sp

INCLUDE "crt/classic/crt_init_heap.inc"
INCLUDE "crt/classic/crt_init_eidi.inc"

call _main ; Call user program
cleanup:
__Exit:
push hl ; return code
call crt0_exit
pop bc ; return code (still not sure it is teh right one !)
Expand Down
Loading

0 comments on commit cd92276

Please sign in to comment.