~vdupras/duskos

fa861cdc04e2d98aa158c118b16466a013dc38ab — Virgil Dupras a month ago a2e9e3f
hal: slightly rework the API

Rename some ops and remove <<n, and >>n, which aren't worth their weight in the
Low HAL as they're only used by "<<" and ">>". They're then replaced by High
HAL's "<<," and ">>," and become useless. Might as well move "<<" and ">>" in
the kernel.
M fs/asm/i386.fs => fs/asm/i386.fs +2 -2
@@ 277,9 277,8 @@ $e2 op loop,     $e1 op loopz,   $e0 op loopnz,
op @, ?movzx,      op @!, xchg,        op addr, lea,
: !, <>) @, ;

\ TODO: in 32-bit mode, arithmetics can be done directly, no need for bx
: op doer ' , does> @ ( halop w ) dip halop>dstsrc ( dst src )
  bx swap ?movzx, bx | execute ;
  bi+ 16b? | 8b? or if bx swap ?movzx, bx then | execute ;
op +, add,         op -, sub,          op compare, cmp,

: *,


@@ 290,6 289,7 @@ op +, add,         op -, sub,          op compare, cmp,
  dup imm? not if cx swap mov, cl then | execute ;
op <<, shl,        op >>, shr,

: [+n], ( n halop -- ) halop>dstsrc nip swap i) add, ;
: _ ( halop -- dst src )
  dup 16b? >r dup 8b? >r 32b)
  halop>dstsrc bx swap mov, ( dst )

M fs/comp/c/egen.fs => fs/comp/c/egen.fs +1 -1
@@ 27,7 27,7 @@ alias noop parseFactor ( tok -- res ) \ forward declaration
  over Result :isconst? if
    CELLSZ + @ over Result arg swap execute over to Result arg
    else @ over Result :?>W execute then ( res ) ;
unaryop _neg, negate, neg
unaryop _neg, -W, neg
: _ -1 i) ^, ;
unaryop _not, _ ^
: _ W=0>Z, Z) C>W, ;

M fs/comp/c/fgen.fs => fs/comp/c/fgen.fs +1 -1
@@ 84,7 84,7 @@ code _lookup ( nref lookup -- )
    1 RSP) [+n], CELLSZ A+n, A) @,
    PSP) compare, NZ) branchC, drop \ Z=match
  \ we have a match, add totcnt*CELLSZ to A, dereference. that's our target.
  RSP) 4 +) @, 2 <<n, RSP) !, W<>A, RSP) +, W) @, \ W=target
  RSP) 4 +) @, 2 i) <<, RSP) !, W<>A, RSP) +, W) @, \ W=target
  12 rs+, W>A, nip, branchA,

\ Switch works by constructing a lookup table of all the cases and generating

M fs/doc/design/simple.txt => fs/doc/design/simple.txt +7 -6
@@ 13,13 13,14 @@ innovation compared to other Forths is to include a C compiler, my main
reference when comparing complexity is Fabrice Bellard's Tiny C Compiler.

Tcc enjoys a very good reputation among geeks, and Fabrice Bellard is generally
considered to be a genius. Nevertheless, Dusk's C compiler, excluding backends,
is 1300 lines of code and tcc, excluding backend is roughly 30,000 lines of
code. At the time of this writing, Dusk CC isn't quite completed yet, but there
isn't much left to add, I don't think it will exceed 2000 lines by much.
considered to be a genius. Nevertheless, Dusk's C compiler, excluding
assemblers, is 1400 lines of code and tcc, excluding backends is roughly 30,000
lines of code. At the time of this writing, Dusk CC isn't quite completed yet,
but there isn't much left to add, I don't think it will exceed 2000 lines by
much.

The i386 backend of Dusk CC, including its assembler, is 600 lines of code. In
tcc, the i386 backend weighs in at 1600 lines of code.
The i386 backend (the HAL) of Dusk CC, including its assembler, is 400 lines of
code. In tcc, the i386 backend weighs in at 1600 lines of code.

How can we explain this difference? It's true that Forth code is generally
denser than C, but not by a factor of 15. It's true that I'm sometimes clever,

M fs/doc/hal.txt => fs/doc/hal.txt +10 -12
@@ 194,15 194,15 @@ branchA,   --

Instructions:

@,       op -- Read source into dest
!,       op -- Write dest to source
@!,      op -- Swap dest and source
+,       op -- *Z* Add source to dest
[@],     op -- Read indirect source into dest
[!],     op -- Write indirect source into dest
compare, op -- Compare source to dest
[+n], n  op -- *Z* Add n to indirect source without affecting dest
&,    op --  Store the effective address of the operand in dest
@,       op --   Read source into dest
!,       op --   Write dest to source
@!,      op --   Swap dest and source
+,       op --   *Z* Add source to dest
[@],     op --   Read indirect source into dest
[!],     op --   Write indirect source into dest
compare, op --   Compare source to dest
[+n],    n op -- *Z* Add n to indirect source without affecting dest
addr,    op --   Store the effective address of the operand in dest

ps+,    n --   Add n to PSP
rs+,    n --   Add n to RSP


@@ 211,9 211,7 @@ A+n,    n --   *Z* Add n to A
W>A,    --     Copy W to A
A>W,    --     Copy A to W
W<>A,   --     Swap W and A
negate, --     dest = -dest
<<n,    n --   Shift dest left by n
>>n,    n --   Shift dest right by n
-W,     --     W = -W

## High HAL


M fs/tests/asm/hal.fs => fs/tests/asm/hal.fs +1 -1
@@ 172,7 172,7 @@ code test17 ( n -- )

\ test &). this returns item "idx" from PSP
code test18 ( ... idx -- n )
  2 <<n, PSP) &) +, W) @,
  2 i) <<, PSP) &) +, W) @,
  exit,

42 12 123 0 test18 123 #eq

M fs/xcomp/bootlo.fs => fs/xcomp/bootlo.fs +2 -4
@@ 4,8 4,6 @@ code : ] code ] ;
: noop ;
code dup dup, exit,
code swap PSP) @!, exit,
code << 1 <<n, exit,
code >> 1 >>n, exit,
: nip, 4 ps+, ;
code nip nip, exit,
: drop, PSP) @, nip, ;


@@ 55,7 53,7 @@ code16b W>A, drop, A) 16b) [!], 2 A) [+n], drop, exit,
code8b W>A, drop, A) 8b) [!], 1 A) [+n], drop, exit,

code + PSP) +, nip, exit,
code - negate, PSP) +, nip, exit,
code - -W, PSP) +, nip, exit,
: -^ swap - ;
: e>w 5 + ;
: w>e 5 - ;


@@ 108,7 106,7 @@ code ?dup W=0>Z, 0 Z) branchC, dup, then exit,
: min ?swap drop ; : max ?swap nip ;
: max0 ( n -- n ) dup 0< if drop 0 then ;
: =><= ( n l h -- f ) over - rot> ( h n l ) - >= ;
code neg negate, exit,
code neg -W, exit,
: ^ -1 xor ;
: and? bool swap bool and ;
: or? or bool ;

M fs/xcomp/i386/kernel.fs => fs/xcomp/i386/kernel.fs +7 -9
@@ 284,14 284,6 @@ xcode litn
  wcall, dup,
  wjmp, LIT>W,

xcode <<n, ( n -- ) \ ax XX i) shl,
  $e0c1 i) wwrite, al cwrite,
  xdrop, ret,

xcode >>n, ( n -- ) \ ax XX i) shr,
  $e8c1 i) wwrite, al cwrite,
  xdrop, ret,

pc 3 nc, $5b $ff $d3 \ bx pop, bx call,
xcode yield ximm
  ( pc ) 3 movewrite, ret,


@@ 304,7 296,7 @@ xcode exit,
  $c3 ( ret ) i) cwrite,
  ret,

xcode negate, ( -- ) \ ax neg,
xcode -W, ( -- ) \ ax neg,
  $d8f7 i) wwrite, ret,

xcode W=0>Z, $c085 i) wwrite, ret, \ ax ax test,


@@ 351,6 343,12 @@ xcode /mod ( a b -- r q )
  si 0 d) dx mov, \ remainder
  ret,

xcode << ( n -- n )
  ax 1 i) shl, ret,

xcode >> ( n -- n )
  ax 1 i) shr, ret,

xcode lshift ( n u -- n )
  cx ax mov, xdrop,
  ax cl shl,