Compare commits

...

16 commits

Author SHA1 Message Date
Igor M 118de2a37e fixed int typ in positive overflow detection 2024-03-18 15:05:58 +02:00
Igor M f556bb56ac remove unused variable 2024-03-18 14:57:45 +02:00
Igor M 1ded3630f4 Add hello-name example, fix backslash offset 2024-03-18 14:27:09 +02:00
Igor M 68cb60e342 comment hello world 2024-03-17 22:02:56 +02:00
m1el 3393d19cac Merge pull request #11 from m1el/example-hello
added hello example
2024-03-17 21:32:27 +02:00
Igor M 23729438a2 added hello example 2024-03-17 21:31:42 +02:00
m1el b745c4621c Merge pull request #10 from m1el/db-align
Implement db, align, fix some bugs
2024-03-17 19:26:21 +02:00
m1el 81c505cd75 Merge pull request #9 from m1el/some-refactor
Some refactor
2024-03-13 22:06:37 +02:00
m1el aa45a8eba4 Merge pull request #8 from m1el/clang-format
added clang format
2024-03-11 16:01:52 +02:00
m1el f64528736b Merge pull request #7 from m1el/m1el-patch-2
Update c-cpp.yml, upgrade checkout action
2024-03-11 14:01:42 +02:00
m1el 5dec93f774 Update c-cpp.yml, upgrade checkout action 2024-03-11 14:01:15 +02:00
m1el 3aef796bd5 Merge pull request #6 from m1el/m1el-patch-1
Create gitgub workflow
2024-03-11 13:46:07 +02:00
m1el c57f615e01 Update Makefile -- fix path to binary 2024-03-11 13:45:44 +02:00
m1el 94c5192c92 Create git workflow 2024-03-11 13:42:46 +02:00
m1el a9c0cbeb8f Merge pull request #5 from m1el/fix-build
Fix build: change paths, add missing error
2024-03-11 13:41:34 +02:00
m1el 642ba58eeb Merge pull request #4 from m1el/int_comments_label_deref 2024-03-11 13:22:55 +02:00
6 changed files with 138 additions and 5 deletions

19
.github/workflows/c-cpp.yml vendored Normal file
View file

@ -0,0 +1,19 @@
name: Cee-lang CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: make
run: make
- name: example
run: make example

View file

@ -7,6 +7,7 @@ CLANG_FORMAT_STYLE = '{ BasedOnStyle: Google, IndentWidth: 4 }'
hbas: build/hbas
example: build/example.hbf
hello: build/hello.hbf
format:
clang-format --style=${CLANG_FORMAT_STYLE} -i src/*
@ -24,8 +25,12 @@ build/example.hbf: build build/hbas examples/example.S
./build/hbas < examples/example.S > build/example.hbf
xxd build/example.hbf
build/hello.hbf: build build/hbas examples/hello.S
./build/hbas < examples/hello.S > build/hello.hbf
xxd build/hello.hbf
clean:
rm -rf build
all:
hbas
hbas

103
examples/hello-name.S Normal file
View file

@ -0,0 +1,103 @@
jmp entry
puts:
; Write string to console
; r2: [IN] *const u8 String pointer
; r3: [IN] usize String length
li8 r1, 0x1 ; Write syscall
brc r2, r3, 2 ; Copy parameters
li8 r2, 0x1 ; STDOUT
eca
jal r0, r31, 0
gets:
; Read string until end of buffer or LF
; r2: [IN] *mut u8 Buffer
; r3: [IN] usize Buffer length
; Register allocations:
; r33: *mut u8 Buffer end
; r34: u8 Immediate char
; r35: u8 Const [0x0A = LF]
li8 r35, 0x0A
add64 r33, r2, r3
; Setup syscall
li8 r2, 0x1 ; Stdin
cp r3, r2
li8 r4, 0x1 ; Read one char
jeq r3, r33, end
loop:
li8 r1, 0x1 ; Read syscall
eca
addi64 r3, r3, 1
ld r34, r3, 0, 1
jeq r34, r35, end
jne r3, r33, loop
end:
; Set copied amount
sub64 r1, r33, r3
addi64 r1, r1, -1
jal r0, r31, 0
alloc_pages:
; Allocate pages
; r1: [OUT] *mut u8 Pointer to page
; r2: [IN] u16 Page count
muli16 r3, r2, 4096 ; page count
li8 r1, 0x9 ; mmap syscall
li8 r2, 0x0 ; no address set, kernel chosen
li8 r4, 0x2 ; PROT_WRITE
li8 r5, 0x20 ; MAP_ANONYMOUS
li64 r6, -1 ; Doesn't map file
li8 r7, 0x0 ; Doesn't map file
eca
jal r0, r31, 0
entry:
; Program entrypoint
; Register allocations:
; r32: *mut u8 Buffer
; r36: usize Read buffer length
; Allocate one page (4096 KiB)
li8 r2, 1
jal r31, r0, alloc_pages
cp r32, r1
; Print message
lra16 r2, r0, enter_your_name
li8 r3, 17
jal r31, r0, puts
; Read name
cp r2, r32
li16 r3, 4096
jal r31, r0, gets
cp r36, r1
; Print your name is
lra16 r2, r0, your_name_is
li8 r3, 15
jal r31, r0, puts
; And now print the name
cp r2, r32
cp r3, r36
jal r31, r0, puts
tx
enter_your_name:
.db "Enter your name: "
your_name_is:
.db "\nYour name is: "

8
examples/hello.S Normal file
View file

@ -0,0 +1,8 @@
li8 r1, 1 ; 1->sys::write
li8 r2, 1 ; fildes=stdout
lra16 r3, r0, hello_string ; buf=hello_string
li8 r4, 0x11 ; nbyte=0x11
eca ; sys::write(stdout, hello_string, 0x11)
tx
hello_string:
.db "Hello, AbleCorp!\n"

View file

@ -8,7 +8,6 @@ AsmError push_string(char *buf, char *input, size_t len) {
}
pos += 1;
chr = input[pos];
size_t offset = 1;
switch (chr) {
case '\\':
chr = '\\';
@ -34,16 +33,15 @@ AsmError push_string(char *buf, char *input, size_t len) {
}
char high = get_hex(input[pos + 1]);
char low = get_hex(input[pos + 2]);
offset = 2;
if (high > 15 || low > 15) {
return ErrStringBadHex;
}
pos += 2;
chr = high << 4 | low;
break;
default:
return ErrBadStringEscape;
}
pos += offset;
}
buf[ndata] = chr;
ndata += 1;

View file

@ -207,7 +207,7 @@ static AsmError assemble_instr(InstHt ht, char *input, size_t len, Token *tok,
return ErrBadNumOverflow;
}
num_to_write = (uint64_t)tmp;
} else if (meta.sign == 2 && (int)num_to_write < 0) {
} else if (meta.sign == 2 && (int64_t)num_to_write < 0) {
return ErrBadNumOverflow;
}
AsmError err = push_int_le(&rv->buf[rv->len], num_to_write,