Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELF code emitter for Z80 architecture (naiive impl.) #10

Open
wants to merge 21 commits into
base: z80
Choose a base branch
from

Commits on Jun 26, 2020

  1. Configuration menu
    Copy the full SHA
    587fdd3 View commit details
    Browse the repository at this point in the history
  2. Push cast down to usage.

    jacobly0 committed Jun 26, 2020
    Configuration menu
    Copy the full SHA
    0ecf551 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    1d81be5 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    218e7cf View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    2026d17 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    6495989 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    46f9733 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    16dada5 View commit details
    Browse the repository at this point in the history
  9. [cc1] Initialize all of the passes when -mllvm is passed so that -pri…

    …nt-before/after can be parsed correctly.
    jacobly0 committed Jun 26, 2020
    Configuration menu
    Copy the full SHA
    8c9b181 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    acf4d1d View commit details
    Browse the repository at this point in the history
  11. Add OMF object format.

    jacobly0 committed Jun 26, 2020
    Configuration menu
    Copy the full SHA
    5fd5c88 View commit details
    Browse the repository at this point in the history
  12. [Z80] Add Z80 target.

    jacobly0 committed Jun 26, 2020
    Configuration menu
    Copy the full SHA
    b204774 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    92b4a06 View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    2ed6977 View commit details
    Browse the repository at this point in the history
  15. Configuration menu
    Copy the full SHA
    418a0ed View commit details
    Browse the repository at this point in the history
  16. Configuration menu
    Copy the full SHA
    3c72039 View commit details
    Browse the repository at this point in the history
  17. [Z80] Add Z80 tests.

    jacobly0 committed Jun 26, 2020
    Configuration menu
    Copy the full SHA
    3732596 View commit details
    Browse the repository at this point in the history
  18. Use Github Actions.

    jacobly0 committed Jun 26, 2020
    Configuration menu
    Copy the full SHA
    dcd55f9 View commit details
    Browse the repository at this point in the history

Commits on Jul 13, 2020

  1. Add missing Z80 definitions to ELF binary format.

    Signed-off-by: Paul Osmialowski <[email protected]>
    pawosm-arm committed Jul 13, 2020
    Configuration menu
    Copy the full SHA
    2830cfe View commit details
    Browse the repository at this point in the history
  2. Fix things that have hit me hard while working on '-fintegrated-as'

    Signed-off-by: Paul Osmialowski <[email protected]>
    pawosm-arm committed Jul 13, 2020
    Configuration menu
    Copy the full SHA
    e1c4140 View commit details
    Browse the repository at this point in the history

Commits on Jul 14, 2020

  1. ELF code emitter for Z80 architecture (naiive impl.)

    Some caveat:
    
    - The Z80 backend emits a lot of compiler library calls as specified in
      llvm/lib/Target/Z80/Z80ISelLowering.cpp making it slightly harder for
      practical use
    
    The story:
    
    Following the ability of the other 8-bit platform supported by LLVM (AVR) to
    generate ELF object files, I have prepared this crude ELF code emitter
    for Z80. It was not extensively tested, mostly due to the lack of a compatible
    runtime library (e.g. for CP/M system). Yet for the testing purposes, I have
    prepared something rudimentary, some minimalistic mockup of CP/M's libc allowing
    me to prepare a simple executable .COM file. It consists of the following files:
    
    1. _bdos.s
    
      .text
    
      .globl __csave
    __csave:
      pop   hl      ; get the return address
      push  iy      ; preserve iy
      push  ix      ; preserve ix
      ld    ix, 0
      add   ix, sp  ; new frame pointer
      jp    (hl)    ; jump to return address
    
      .globl __crestore
    __crestore:
      ld    sp, ix
      pop   ix      ; restore ix
      pop   iy      ; restore iy
      ret
    
    ; uint16_t _bdos(uint8_t function, uint16_t arg)
      .globl __bdos
    __bdos:
      call  __csave
      ld    c, (ix + 6) ; ix + 6: function
      ld    e, (ix + 8) ; ix + 8: arg (LO)
      ld    d, (ix + 9) ; ix + 9: arg (HI)
      push  iy      ; preserve iy
      push  ix      ; preserve ix
      call  0x05    ; call BDOS
      pop   ix      ; restore ix
      pop   iy      ; restore iy
      ld    l, a    ; return the result in l
      rla           ; get the highest bit to the carry flag
      sbc   a, a    ; keep the carry flag
      ld    h, a    ; return the highest bit in h
      jp    __crestore
    
    Compile this with the GNU 'as' from binutils built with --target=z80-none-elf
    'configure' flag, e.g.:
    
    z80-none-elf-as -o _bdos.o _bdos.s
    
    2. _exit.s
    
      .text
      .globl __Exit
    __Exit:
      pop hl        ; get rid of the return address
      pop hl        ; exit status
      ld (0x80), hl ; store exit status at the address 0x80
      jp 0          ; return to CP/M
    
    Similarly:
    
    z80-none-elf-as -o _exit.o _exit.s
    
    3. crtcpm.c
    
    _Noreturn void _Exit(int);
    
    int main(int, char **);
    
    _Noreturn void start(void) __attribute__((no_builtin))
    {
      // TODO: parse whatever there is at 0x80
      static char *argv[] =  { (char *)(0x80) };
    
      _Exit(main(1, argv));
    }
    
    Compile this (to 'crtcpm.o') with the LLVM compiler built with
    -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="Z80", -DLLVM_TARGETS_TO_BUILD="" and
    -DLLVM_DEFAULT_TARGET_TRIPLE="z80-none-elf" 'cmake' flags, namely:
    
    clang -Wall -O0 -fintegrated-as -fomit-frame-pointer -c crtcpm.c
    
    4. main.c
    
    uint16_t _bdos(uint8_t, uint16_t);
    
    static void putch(int c)
    {
      if ('\n' == c)
        _bdos(0x02U, '\r');
      _bdos(0x02U, c);
    }
    
    int main(int argc, char **argv)
    {
      int i;
      int c;
      const int n = ((unsigned char)(argv[0][0]));
    
      for (i = 0; i < n; i++) {
        c = ((unsigned char)(argv[0][1 + i]));
        putch('[');
        if (' ' == c) {
          putch('s');
          putch('p');
        } else
          putch(c);
        putch(']');
      }
      putch('\n');
      return argc;
    }
    
    Similarly, build 'main.o':
    
    clang -Wall -fintegrated-as -c main.c
    
    Create rudimentary 'libc.a' library from '_bdos.o' and '_exit.o' using GNU 'ar'
    and 'runlib' from binutils built as described earlier:
    
    z80-none-elf-ar r libc.a _bdos.o _exit.o
    z80-none-elf-ranlib libc.a
    
    Link the final .COM file, e.g.:
    
    z80-none-elf-ld -o main.com --oformat binary -Ttext=0x100 crtcpm.o main.o -L. -lc
    
    Every CP/M program should start at 0x100, hence the .text segment is set to
    start at that address. The .data segment will be placed right after last byte of
    the .text segment. Note that 'crtcpm.o' is kept separately from 'libc.a' and is
    listed as the very first file needed to link the final program. This ensures
    that the only function defined in 'crtcpm.o' (the _start() function) will be the
    very first thing placed at the address 0x100.
    
    Copy the 'main.com' file to a disk image and execute it in CP/M. Basically,
    the 'main.com' program should print whatever was given as parameters with each
    character put into brackets (and spaces printed as [sp]):
    
    A>main hello, world
    [sp][H][E][L][L][O][,][sp][W][O][R][L][D]
    
    Note that CP/M turns all the characters in the command line arguments
    into upper-case.
    
    Signed-off-by: Paul Osmialowski <[email protected]>
    pawosm-arm committed Jul 14, 2020
    Configuration menu
    Copy the full SHA
    e9a373b View commit details
    Browse the repository at this point in the history