Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
fasm backend replaced nasm
Browse files Browse the repository at this point in the history
  • Loading branch information
l1mey112 committed Aug 9, 2022
1 parent fd5de33 commit 8b6d327
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
17 changes: 17 additions & 0 deletions asm/helloworld_fasm.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
format ELF64 executable
entry _start

segment readable executable

_start:
mov rax, 1
mov rdi, 1
mov rsi, string
mov rdx, 13
syscall
mov rax, 60
xor rdi, rdi
syscall

segment readable writeable
string db "Hello world!",10
32 changes: 11 additions & 21 deletions src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ fn main(){
pref_tut := fp.bool('tutor', 0, false, 'activate tutor mode. program will stop at the checker and display information relating to the stack, used for learning the language')
pref_bat := fp.bool('show', `s`, false, 'open nasm assembly output in a bat process')
//pref_ir := fp.string('show_ir', 0, '', '')
mut pref_out := fp.string('', `o`, '', 'output to file (accepts *.asm, *.S, *.o, *)')
pref_asm := if fp.bool('', `g`, false, 'compile with debug symbols') { ' -F dwarf -g' } else { '' }
mut pref_out := fp.string('', `o`, '', 'output to file (accepts *.asm, *.S, *)')
pref_ver := fp.bool('version', `v`, false, fp.default_version_label)
//pref_deb := fp.bool('debug', `d`, false, 'run the compiler in debug mode')

Expand All @@ -43,7 +42,7 @@ fn main(){
filename := args[0]

// -----------------------------------------------
source := stas.compile_nasm(filename, pref_tut)
source := stas.compile_fasm(filename, pref_tut)
// -----------------------------------------------

pref_ext := os.file_ext(pref_out)
Expand All @@ -62,32 +61,23 @@ fn main(){

if pref_bat {
mut run_process := os.new_process('/usr/bin/bat')
run_process.set_args(['-l','nasm','${file_write_tmp}.asm'])
run_process.set_args(['-l','fasm','${file_write_tmp}.asm'])
run_process.wait()
os.rm('${file_write_tmp}.asm') or {}
exit(0)
}

object_file_out := if pref_ext == '.o' {pref_out} else {'${file_write_tmp}.o'}
nasm_res := os.execute('nasm -a -O0 -felf64$pref_asm -o $object_file_out ${file_write_tmp}.asm')
// no preprocessing, minimal optimisation

if nasm_res.exit_code != 0 {
eprintln(term.red("NASM error, this should never happen"))
eprintln(nasm_res.output)
exit(1)
}
if pref_ext == '.o' {
exit(0)
}

if pref_out == '' {
pref_out = 'a.out'
} // default output
os.execute_or_panic('ld ${file_write_tmp}.o -o $pref_out')
}
fasm_res := os.execute('fasm ${file_write_tmp}.asm')

if fasm_res.exit_code != 0 {
eprintln(term.red("FASM error, this should never happen"))
eprintln(fasm_res.output)
exit(1)
}
os.cp(file_write_tmp,pref_out) or {}
os.rm('${file_write_tmp}.asm') or {}
os.rm('${file_write_tmp}.o') or {}

if pref_run {
exefile := os.abs_path(pref_out)
Expand Down
2 changes: 1 addition & 1 deletion src/stas/entrypoint.v
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn check_entirety (fns map[string]&Function) &Checker {
return checker
}

pub fn compile_nasm (filename string, is_tutor bool)string{
pub fn compile_fasm (filename string, is_tutor bool)string{

fns := parse_entirety(filename)
mut checker := check_entirety(fns)
Expand Down
8 changes: 4 additions & 4 deletions src/stas/gen.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mut:

const header =

'[BITS 64]
[global _start]'
'format ELF64 executable
entry _start'

const builtin_entry =

Expand Down Expand Up @@ -51,9 +51,9 @@ fn (mut g Gen) gen_all() string {
// -- HEADER --
g.header = strings.new_builder(60)
g.header.writeln(header)
g.header.writeln('[section .rodata]')
g.header.writeln('segment readable writeable')
//g.file.drain_builder(mut s_rodata, 0)
g.file.writeln('[section .text]')
g.file.writeln('segment readable executable')
g.file.writeln(builtin_entry)
// -- START PROGRAM --

Expand Down

0 comments on commit 8b6d327

Please sign in to comment.