aforth

FORTH for Linux x86-64, written in assembly.
git clone git://henryandlizzy.uk/aforth
Log | Files | Refs | README

macros.i (2188B)


      1 .macro	do label
      2 	.quad	\label
      3 .endm
      4 
      5 .macro	forthword immediate
      6 .ifnb	\immediate
      7 	.quad	0x8000000000000000 + enter
      8 .else
      9 	.quad	enter
     10 .endif
     11 .endm
     12 
     13 .macro	endword
     14 	do	EXIT
     15 .endm
     16 
     17 .macro	const val
     18 	do	docon
     19 	.quad	\val
     20 .endm
     21 
     22 .macro	variable init
     23 	do	dovar
     24 .ifnb	\init
     25 	.quad	\init
     26 .else
     27 	.quad	0
     28 .endif
     29 .endm
     30 
     31 .macro	get var
     32 	do	\var
     33 	do	fetch
     34 .endm
     35 
     36 .macro	set var:req value
     37 .ifnb	\value
     38 	const	\value
     39 .endif
     40 	do	\var
     41 	do	store
     42 .endm
     43 
     44 .macro	strlit msg
     45 	.quad	9f - 8f
     46 8:	.ascii	"\msg\()"
     47 9:
     48 .endm
     49 
     50 .macro	string msg
     51 	do	dostr
     52 	strlit	"\msg\()"
     53 .endm
     54 
     55 .macro	say msg
     56 	string	"\msg\()"
     57 	do	PRINT
     58 .endm
     59 
     60 .macro	saycr msg
     61 	say	"\msg\()\n"
     62 .endm
     63 
     64 .macro	escape val
     65 	say	"\x1B[\val\()m"
     66 .endm
     67 
     68 .macro	scratch length
     69 	const	8f
     70 	goto	9f
     71 8:	.skip	\length
     72 9:
     73 .endm
     74 
     75 .macro	goto label
     76 	do	dogoto
     77 	.quad	\label
     78 .endm
     79 
     80 .macro	if label
     81 	do	dobranch
     82 	.quad	\label
     83 .endm
     84 
     85 .macro	test compare:req value:req target:req
     86 	do	DUP
     87 	const	\value
     88 	do	\compare
     89 	if	\target
     90 .endm
     91 
     92 .macro	unless label
     93 	do	iszero
     94 	if	\label
     95 .endm
     96 
     97 .macro	debug
     98 	do	DUP
     99 	do	dot
    100 	do	CR
    101 .endm
    102 
    103 .macro	offset var distance
    104 	do	\var
    105 	const	\distance
    106 	do	plus
    107 .endm
    108 
    109 .macro	verb type:req name:req altname immediate
    110 	.quad	7b - 8
    111 .ifnb	\altname
    112 7:	strlit	"\altname\()"
    113 .else
    114 7:	strlit	"\name\()"
    115 .endif
    116 .ifnb	\immediate
    117 \name\():	\type\()word	\immediate
    118 .else
    119 \name\():	\type\()word
    120 .endif
    121 .endm
    122 
    123 .macro	noverb type:req name:req immediate
    124 .ifnb	\immediate
    125 \name\():	\type\()word	\immediate
    126 .else
    127 \name\():	\type\()word
    128 .endif
    129 .endm
    130 
    131 #	Codeword macros
    132 
    133 .macro	codeword immediate
    134 .ifnb	\immediate
    135 	.quad	0x8000000000000008 + .
    136 .else
    137 	.quad	. + 8
    138 .endif
    139 .endm
    140 
    141 .macro	advance	register
    142 	add	$8,	\register
    143 .endm
    144 
    145 .macro	retreat	register
    146 	sub	$8,	\register
    147 .endm
    148 
    149 #	Stack manipulation
    150 
    151 .macro	_dup
    152 	retreat	SP
    153 	mov	TOS,	(SP)
    154 .endm
    155 
    156 #	Comparison
    157 
    158 .macro	compare	op	name	altname
    159 verb	code	\name	"\altname\()"
    160 	minstk	2
    161 	cmp	TOS,	(SP)
    162 	\op	truecmp
    163 	movq	$0,	(SP)
    164 	jmp	_drop
    165 .endm
    166 
    167 .macro	cmpaddr	op	name	altname
    168 verb	code	\name	"\altname\()"
    169 	minstk	2
    170 	mov	(SP),	ACC
    171 	mov	(ACC),	ACC
    172 	cmp	(TOS),	ACC
    173 	\op	truecmp
    174 	movq	$0,	(SP)
    175 	jmp	_drop
    176 .endm
    177 
    178 .macro	minstk	depth:req
    179 	cmp	$stack - ( \depth * 8 ), SP
    180 	jle	1f
    181 		mov	$_uflow,	IP
    182 		jmp	next
    183 1:
    184 .endm