diff --git a/Gemfile b/Gemfile deleted file mode 100644 index 97bdde4..0000000 --- a/Gemfile +++ /dev/null @@ -1,9 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public License, -# v. 2.0. If a copy of the MPL was not distributed with this file, You can -# obtain one at https://mozilla.org/MPL/2.0/. -# frozen_string_literal: true - -source 'https://rubygems.org' - -gem 'term-ansicolor', '~> 1.2' -gem 'rake', '~> 13.2' diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 3521a74..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,22 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - bigdecimal (3.1.9) - rake (13.2.1) - sync (0.5.0) - term-ansicolor (1.11.2) - tins (~> 1.0) - tins (1.38.0) - bigdecimal - sync - -PLATFORMS - ruby - x86_64-linux - -DEPENDENCIES - rake (~> 13.2) - term-ansicolor (~> 1.2) - -BUNDLED WITH - 2.6.7 diff --git a/Rakefile b/Rakefile index e6dc163..2b61d70 100644 --- a/Rakefile +++ b/Rakefile @@ -6,8 +6,6 @@ require 'open3' require 'rake/clean' -SYSROOT = '../eugenios/build/sysroot' -QEMU = 'qemu-riscv32' STAGES = Dir.glob('boot/stage*.elna').collect { |stage| File.basename stage, '.elna' }.sort CLEAN.include 'build/boot', 'build/valid' @@ -19,6 +17,10 @@ def compile(input, output) sh ENV.fetch('CC', 'gcc'), '-nostdlib', '-o', output, input end +def run(exe) + ENV.fetch('QEMU', '').split << exe +end + task default: :boot desc 'Final stage' @@ -31,9 +33,8 @@ task boot: "boot/#{STAGES.last}.elna" do |t| source = groupped['.elna'] cat_arguments = ['cat', source] - compiler_arguments = [QEMU, '-L', SYSROOT, exe] diff_arguments = ['diff', '-Nur', '--text', expected, '-'] - Open3.pipeline(cat_arguments, compiler_arguments, diff_arguments) + Open3.pipeline(cat_arguments, run(exe), diff_arguments) end desc 'Convert previous stage language into the current stage language' @@ -60,8 +61,7 @@ STAGES.each do |stage| exe, source = t.prerequisites cat_arguments = ['cat', source] - compiler_arguments = [QEMU, '-L', SYSROOT, exe] - last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) + last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe)) IO.copy_stream last_stdout, t.name end @@ -70,8 +70,7 @@ STAGES.each do |stage| exe, source = t.prerequisites cat_arguments = ['cat', source] - compiler_arguments = [QEMU, '-L', SYSROOT, exe] - last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) + last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe)) IO.copy_stream last_stdout, t.name end @@ -85,8 +84,7 @@ file 'build/valid/stage1.s' => ['build/boot/stage1', 'boot/stage1.s', 'build/val source, exe, = t.prerequisites.partition { |prerequisite| prerequisite.end_with? '.s' } cat_arguments = ['cat', *source] - compiler_arguments = [QEMU, '-L', SYSROOT, *exe] - last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) + last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe.first)) IO.copy_stream last_stdout, t.name end diff --git a/boot/stage1.s b/boot/stage1.s index b4dbfed..870952b 100644 --- a/boot/stage1.s +++ b/boot/stage1.s @@ -2,7 +2,7 @@ # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -# Stage1 compiler. +# Stage 1 compiler. # # It supports declaring and calling procedures without arguments. # A procedure name should start with an underscore. diff --git a/boot/stage2.elna b/boot/stage2.elna index ce9de8a..0423b3b 100644 --- a/boot/stage2.elna +++ b/boot/stage2.elna @@ -2,7 +2,7 @@ # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -# Stage2 compiler. +# Stage 2 compiler. # # - Procedures without none or one argument. # - Goto statements. diff --git a/boot/stage3.elna b/boot/stage3.elna index 2c79295..aec9832 100644 --- a/boot/stage3.elna +++ b/boot/stage3.elna @@ -2,7 +2,7 @@ # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -# Stage3 compiler. +# Stage 3 compiler. # # - Procedures with multiple arguments. # - Character literals with and without escaping. diff --git a/boot/stage4.elna b/boot/stage4.elna index 1d98c3a..d873b9a 100644 --- a/boot/stage4.elna +++ b/boot/stage4.elna @@ -2,7 +2,7 @@ # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -# Stage4 compiler. +# Stage 4 compiler. # # - Taking value of local and global variables. Variables that doesn't begin # with "v" are considered global. diff --git a/boot/stage5.elna b/boot/stage5.elna index 88b407f..69623db 100644 --- a/boot/stage5.elna +++ b/boot/stage5.elna @@ -2,7 +2,7 @@ # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -# Stage5 compiler. +# Stage 5 compiler. # # - Stack size increased to 128 bytes per procedure. # 7 words on the stack, 92 - 120, are reversed for procedure arguments (caller side). diff --git a/boot/stage6.elna b/boot/stage6.elna index f412cc6..7d426f9 100644 --- a/boot/stage6.elna +++ b/boot/stage6.elna @@ -1,9 +1,10 @@ -# ihis Source Code Form is subject to the terms of the Mozilla Public License, +# This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -# Stage6 compiler. +# Stage 6 compiler. # +# - String literals. .section .rodata @@ -43,6 +44,9 @@ asm_type_directive: .string ".type " .type asm_type_function, @object asm_type_function: .string ", @function\n" +.type asm_type_object, @object +asm_type_object: .string ", @object\n" + .type asm_colon, @object asm_colon: .string ":\n" @@ -112,19 +116,110 @@ asm_comma: .string ", " .type asm_sp, @object asm_sp: .string "sp" +.type asm_rodata, @object +asm_rodata: .string ".section .rodata\n" + +.type asm_strings, @object +asm_strings: .string "strings" + +.type asm_ascii, @object +asm_ascii: .string " .ascii " + .section .bss # When modifiying also change the read size in the entry point procedure. .type source_code, @object source_code: .zero 81920 +.type compiler_strings, @object +compiler_strings: .zero 8192 + .section .data +.type compiler_strings_position, @object +compiler_strings_position: .word compiler_strings + +.type compiler_strings_length, @object +compiler_strings_length: .word 0 + .type source_code_position, @object source_code_position: .word source_code .section .text +# Calculates and returns the string token length between quotes, including the +# escaping slash characters. +# +# Parameters: +# a0 - String token pointer. +# +# Returns the length in a0. +proc _string_length(); +begin + # Reset the counter. + v0 := 0; + +.string_length_loop: + v88 := v88 + 1; + + lw t0, 88(sp) + lb t0, (t0) + + li t1, '"' + beq t0, t1, .string_length_end + + v0 := v0 + 1; + goto .string_length_loop; + +.string_length_end: + return v0 +end; + +# Adds a string to the global, read-only string storage. +# +# Parameters: +# a0 - String token. +# +# Returns the offset from the beginning of the storage to the new string in a0. +proc _add_string(); +begin + v0 := v88 + 1; + v4 := compiler_strings_length; + +.add_string_loop: + lw t0, 0(sp) + lb t1, (t0) + li t2, '"' + + beq t1, t2, .add_string_end + + la t2, compiler_strings_position + lw t3, (t2) + sb t1, (t3) + + addi t3, t3, 1 + sw t3, (t2) + + addi t0, t0, 1 + sw t0, 0(sp) + + li t2, '\\' + bne t1, t2, .add_string_increment + + goto .add_string_loop; + +.add_string_increment: + la t2, compiler_strings_length + lw t4, (t2) + addi t4, t4, 1 + sw t4, (t2) + + goto .add_string_loop; + +.add_string_end: + return v4 +end; + # Reads standard input into a buffer. # a0 - Buffer pointer. # a1 - Buffer size. @@ -595,6 +690,37 @@ begin end; +proc _compile_string_literal(); +begin + _string_length(source_code_position); + sw a0, 0(sp) + + _add_string(source_code_position); + sw a0, 4(sp) + + _advance_token(v0 + 2); + + _write_z(@asm_la); + _write_register('t', 0); + _write_z(@asm_comma); + _write_z(@asm_strings); + _write_c('\n'); + + _write_z(@asm_li); + _write_register('t', 1); + _write_z(@asm_comma); + _write_i(v4); + _write_c('\n'); + + _write_z(@asm_add); + _write_register('t', 0); + _write_z(@asm_comma); + _write_register('t', 0); + _write_z(@asm_comma); + _write_register('t', 1); + _write_c('\n'); +end; + proc _compile_term(); begin la t0, source_code_position @@ -614,6 +740,9 @@ begin li t1, '~' beq a0, t1, .compile_term_not + li t1, '"' + beq a0, t1, .compile_term_string_literal + _is_digit(v0); bnez a0, .compile_term_integer_literal @@ -639,6 +768,10 @@ begin _compile_not_expression(); goto .compile_term_end; +.compile_term_string_literal: + _compile_string_literal(); + goto .compile_term_end; + .compile_term_variable: _compile_variable_expression(); goto .compile_term_end; @@ -1336,63 +1469,99 @@ begin end; # Process the source code and print the generated code. -proc _compile(); +proc _compile_module(); begin -.compile_loop: +.compile_module_loop: _skip_newlines(); la t0, source_code_position lw t0, (t0) lb t0, (t0) - beqz t0, .compile_end + beqz t0, .compile_module_end li t1, '#' - beq t0, t1, .compile_comment + beq t0, t1, .compile_module_comment # 8 is ".section" length. _memcmp(source_code_position, @keyword_section, 8); - beqz a0, .compile_section + beqz a0, .compile_module_section # 5 is ".type" length. _memcmp(source_code_position, @keyword_type, 5); - beqz a0, .compile_type + beqz a0, .compile_module_type # 5 is "proc " length. Space is needed to distinguish from "procedure". _memcmp(source_code_position, @keyword_proc, 5); - beqz a0, .compile_procedure + beqz a0, .compile_module_procedure # 6 is ".globl" length. _memcmp(source_code_position, @keyword_global, 6); - beqz a0, .compile_global + beqz a0, .compile_module_global # Not a known token, exit. - goto .compile_end; + goto .compile_module_end; -.compile_section: +.compile_module_section: _compile_section(); - goto .compile_loop; + goto .compile_module_loop; -.compile_type: +.compile_module_type: _compile_type(); - goto .compile_loop; + goto .compile_module_loop; -.compile_global: +.compile_module_global: _compile_line(); - goto .compile_loop; + goto .compile_module_loop; -.compile_comment: +.compile_module_comment: _skip_comment(); - goto .compile_loop; + goto .compile_module_loop; -.compile_procedure: +.compile_module_procedure: _compile_procedure(); - goto .compile_loop; + goto .compile_module_loop; + +.compile_module_end: +end; + +proc _compile(); +begin + _compile_module(); + + _write_z(@asm_rodata); + _write_z(@asm_type_directive); + _write_z(@asm_strings); + _write_z(@asm_type_object); + _write_z(@asm_strings); + _write_c(':'); + _write_z(@asm_ascii); + _write_c('"'); + + la t0, compiler_strings + sw t0, 0(sp) + +.compile_loop: + lw t0, 0(sp) + la t1, compiler_strings_position + lw t1, (t1) + bge t0, t1, .compile_end + + lb a0, (t0) + + addi t0, t0, 1 + sw t0, 0(sp) + + _write_c(); + + j .compile_loop .compile_end: + _write_c('"'); + _write_c('\n'); end; # Terminates the program. a0 contains the return code. diff --git a/boot/stage7.elna b/boot/stage7.elna new file mode 100644 index 0000000..6973963 --- /dev/null +++ b/boot/stage7.elna @@ -0,0 +1,1260 @@ +# This Source Code Form is subject to the terms of the Mozilla Public License, +# v. 2.0. If a copy of the MPL was not distributed with this file, You can +# obtain one at https://mozilla.org/MPL/2.0/. + +# Stage 7 compiler. +# +# - String literals. + +.section .bss + +# When modifiying also change the read size in the entry point procedure. +.type source_code, @object +source_code: .zero 81920 + +.type compiler_strings, @object +compiler_strings: .zero 8192 + +.section .data + +.type compiler_strings_position, @object +compiler_strings_position: .word compiler_strings + +.type compiler_strings_length, @object +compiler_strings_length: .word 0 + +.type source_code_position, @object +source_code_position: .word source_code + +.section .text + +# Calculates and returns the string token length between quotes, including the +# escaping slash characters. +# +# Parameters: +# a0 - String token pointer. +# +# Returns the length in a0. +proc _string_length(); +begin + # Reset the counter. + v0 := 0; + +.string_length_loop: + v88 := v88 + 1; + + lw t0, 88(sp) + lb t0, (t0) + + li t1, '"' + beq t0, t1, .string_length_end + + v0 := v0 + 1; + goto .string_length_loop; + +.string_length_end: + return v0 +end; + +# Adds a string to the global, read-only string storage. +# +# Parameters: +# a0 - String token. +# +# Returns the offset from the beginning of the storage to the new string in a0. +proc _add_string(); +begin + v0 := v88 + 1; + v4 := compiler_strings_length; + +.add_string_loop: + lw t0, 0(sp) + lb t1, (t0) + li t2, '"' + + beq t1, t2, .add_string_end + + la t2, compiler_strings_position + lw t3, (t2) + sb t1, (t3) + + addi t3, t3, 1 + sw t3, (t2) + + addi t0, t0, 1 + sw t0, 0(sp) + + li t2, '\\' + bne t1, t2, .add_string_increment + + goto .add_string_loop; + +.add_string_increment: + la t2, compiler_strings_length + lw t4, (t2) + addi t4, t4, 1 + sw t4, (t2) + + goto .add_string_loop; + +.add_string_end: + return v4 +end; + +# Reads standard input into a buffer. +# a0 - Buffer pointer. +# a1 - Buffer size. +# +# Returns the amount of bytes written in a0. +proc _read_file(); +begin + mv a2, a1 + mv a1, a0 + # STDIN. + li a0, 0 + li a7, 63 # SYS_READ. + ecall +end; + +# Writes to the standard output. +# +# Parameters: +# a0 - Buffer. +# a1 - Buffer length. +proc _write_s(); +begin + mv a2, a1 + mv a1, a0 + # STDOUT. + li a0, 1 + li a7, 64 # SYS_WRITE. + ecall +end; + +# Writes a number to a string buffer. +# +# t0 - Local buffer. +# t1 - Constant 10. +# t2 - Current character. +# t3 - Whether the number is negative. +# +# Parameters: +# a0 - Whole number. +# a1 - Buffer pointer. +# +# Sets a0 to the length of the written number. +proc _print_i(); +begin + li t1, 10 + addi t0, s0, -9 + + li t3, 0 + bgez a0, .print_i_digit10 + li t3, 1 + neg a0, a0 + +.print_i_digit10: + rem t2, a0, t1 + addi t2, t2, '0' + sb t2, 0(t0) + div a0, a0, t1 + addi t0, t0, -1 + bne zero, a0, .print_i_digit10 + + beq zero, t3, .print_i_write_call + addi t2, zero, '-' + sb t2, 0(t0) + addi t0, t0, -1 + +.print_i_write_call: + mv a0, a1 + addi a1, t0, 1 + sub a2, s0, t0 + addi a2, a2, -9 + sw a2, 0(sp) + + _memcpy(); + + return v0 +end; + +# Writes a number to the standard output. +# +# Parameters: +# a0 - Whole number. +proc _write_i(); +begin + _print_i(v88, @v0); + + mv a1, a0 + addi a0, sp, 0 + _write_s(); + +end; + +# Writes a character from a0 into the standard output. +proc _write_c(); +begin + _write_s(@v88, 1); +end; + +# Write null terminated string. +# +# Parameters: +# a0 - String. +proc _write_z(); +begin +.write_z_loop: + # Check for 0 character. + lw a0, 88(sp) + lb a0, (a0) + beqz a0, .write_z_end + + # Print a character. + _write_c(); + + # Advance the input string by one byte. + v88 := v88 + 1; + + goto .write_z_loop; + +.write_z_end: +end; + +# Detects if a0 is an uppercase character. Sets a0 to 1 if so, otherwise to 0. +proc _is_upper(); +begin + v0 := v88 >= 'A'; + v4 := v88 <= 'Z'; + + return v0 & v4 + +end; + +# Detects if a0 is an lowercase character. Sets a0 to 1 if so, otherwise to 0. +proc _is_lower(); +begin + v0 := v88 >= 'a'; + v4 := v88 <= 'z'; + + return v0 & v4 + +end; + +# Detects if the passed character is a 7-bit alpha character or an underscore. +# +# Paramters: +# a0 - Tested character. +# +# Sets a0 to 1 if the character is an alpha character or underscore, sets it to 0 otherwise. +proc _is_alpha(); +begin + sw a0, 0(sp) + + _is_upper(); + sw a0, 4(sp) + + _is_lower(v0); + + lw t0, 0(sp) + xori t1, t0, '_' + seqz t1, t1 + + lw t0, 4(sp) + or a0, a0, t0 + or a0, a0, t1 +end; + +# Detects whether the passed character is a digit +# (a value between 0 and 9). +# +# Parameters: +# a0 - Exemined value. +# +# Sets a0 to 1 if it is a digit, to 0 otherwise. +proc _is_digit(); +begin + v0 := v88 >= '0'; + v4 := v88 <= '9'; + + return v0 & v4 +end; + +proc _is_alnum(); +begin + sw a0, 4(sp) + + _is_alpha(); + sw a0, 0(sp) + + _is_digit(v4); + + lw a1, 0(sp) + or a0, a0, a1 +end; + +# Reads the next token. +# +# Returns token length in a0. +proc _read_token(); +begin + # Current token position. + v0 := source_code_position; + # Token length. + v4 := 0; + +.read_token_loop: + lw t0, 0(sp) + lb t0, (t0) # Current character. + + # First we try to read a derictive. + # A derictive can contain a dot and characters. + li t1, '.' + beq t0, t1, .read_token_next + + lw a0, 0(sp) + lb a0, (a0) + _is_alnum(); + bnez a0, .read_token_next + + goto .read_token_end; + +.read_token_next: + # Advance the source code position and token length. + v4 := v4 + 1; + v0 := v0 + 1; + + goto .read_token_loop; + +.read_token_end: + return v4 +end; + +# a0 - First pointer. +# a1 - Second pointer. +# a2 - The length to compare. +# +# Returns 0 in a0 if memory regions are equal. +proc _memcmp(); +begin + mv t0, a0 + li a0, 0 + +.memcmp_loop: + beqz a2, .memcmp_end + + lbu t1, (t0) + lbu t2, (a1) + sub a0, t1, t2 + + bnez a0, .memcmp_end + + addi t0, t0, 1 + addi a1, a1, 1 + addi a2, a2, -1 + + goto .memcmp_loop; + +.memcmp_end: +end; + +# Copies memory. +# +# Parameters: +# a0 - Destination. +# a1 - Source. +# a2 - Size. +# +# Preserves a0. +proc _memcpy(); +begin + mv t0, a0 + +.memcpy_loop: + beqz a2, .memcpy_end + + lbu t1, (a1) + sb t1, (a0) + + addi a0, a0, 1 + addi a1, a1, 1 + addi a2, a2, -1 + + goto .memcpy_loop + +.memcpy_end: + mv a0, t0 +end; + +# Advances the token stream by a0 bytes. +proc _advance_token(); +begin + la t0, source_code_position + lw t1, (t0) + add t1, t1, a0 + sw t1, (t0) +end; + +# Prints the current token. +# +# Parameters: +# a0 - Token length. +# +# Returns a0 unchanged. +proc _write_token(); +begin + _write_s(source_code_position, v88); + return v88 +end; + +proc _compile_section(); +begin + # Print and skip the ".section" (8 characters) directive and a space after it. + _write_token(9); + _advance_token(); + + # Read the section name. + _read_token(); + addi a0, a0, 1 + + _write_token(); + _advance_token(); +end; + +# Prints and skips a line. +proc _skip_comment(); +begin + la t0, source_code_position + lw t1, (t0) + +.skip_comment_loop: + # Check for newline character. + lb t2, (t1) + li t3, '\n' + beq t2, t3, .skip_comment_end + + # Advance the input string by one byte. + addi t1, t1, 1 + sw t1, (t0) + + goto .skip_comment_loop; + +.skip_comment_end: + # Skip the newline. + addi t1, t1, 1 + sw t1, (t0) +end; + +# Prints and skips a line. +proc _compile_line(); +begin +.compile_line_loop: + la a0, source_code_position + lw a1, (a0) + + lb t0, (a1) + li t1, '\n' + beq t0, t1, .compile_line_end + + # Print a character. + lw a0, (a1) + _write_c(); + + # Advance the input string by one byte. + _advance_token(1); + + goto .compile_line_loop; + +.compile_line_end: + _write_c('\n'); + + _advance_token(1); +end; + +proc _compile_integer_literal(); +begin + _write_z("\tli t0, \0"); + + _read_token(); + _write_token(); + _advance_token(); + + _write_c('\n'); +end; + +proc _compile_character_literal(); +begin + _write_z("\tli t0, \0"); + + _write_c('\''); + _advance_token(1); + + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + li t1, '\\' + bne a0, t1, .compile_character_literal_end + + _write_c('\\'); + _advance_token(1); + +.compile_character_literal_end: + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + _write_c(); + + _write_c('\''); + _write_c('\n'); + + _advance_token(2); + +end; + +proc _compile_variable_expression(); +begin + _compile_designator(); + _write_z("\tlw t0, (t0)\n\0"); +end; + +proc _compile_address_expression(); +begin + # Skip the "@" sign. + _advance_token(1); + _compile_designator(); + +end; + +proc _compile_negate_expression(); +begin + # Skip the "-" sign. + _advance_token(1); + _compile_term(); + + _write_z("\tneg t0, t0\n\0"); +end; + +proc _compile_not_expression(); +begin + # Skip the "~" sign. + _advance_token(1); + _compile_term(); + + _write_z("\tnot t0, t0\n\0"); +end; + +proc _compile_string_literal(); +begin + _string_length(source_code_position); + sw a0, 0(sp) + + _add_string(source_code_position); + sw a0, 4(sp) + + _advance_token(v0 + 2); + _write_z("\tla t0, strings\n\0"); + + _write_z("\tli t1, \0"); + _write_i(v4); + _write_c('\n'); + + _write_z("\tadd t0, t0, t1\n\0"); +end; + +proc _compile_term(); +begin + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + sw a0, 0(sp) + + li t1, '\'' + beq a0, t1, .compile_term_character_literal + + li t1, '@' + beq a0, t1, .compile_term_address + + li t1, '-' + beq a0, t1, .compile_term_negation + + li t1, '~' + beq a0, t1, .compile_term_not + + li t1, '"' + beq a0, t1, .compile_term_string_literal + + _is_digit(v0); + bnez a0, .compile_term_integer_literal + + goto .compile_term_variable; + +.compile_term_character_literal: + _compile_character_literal(); + goto .compile_term_end; + +.compile_term_integer_literal: + _compile_integer_literal(); + goto .compile_term_end; + +.compile_term_address: + _compile_address_expression(); + goto .compile_term_end; + +.compile_term_negation: + _compile_negate_expression(); + goto .compile_term_end; + +.compile_term_not: + _compile_not_expression(); + goto .compile_term_end; + +.compile_term_string_literal: + _compile_string_literal(); + goto .compile_term_end; + +.compile_term_variable: + _compile_variable_expression(); + goto .compile_term_end; + +.compile_term_end: +end; + +proc _compile_binary_rhs(); +begin + # Skip the whitespace after the binary operator. + _advance_token(1); + _compile_term(); + + # Load the left expression from the stack; + _write_z("\tlw t1, 24(sp)\n\0"); +end; + +proc _compile_expression(); +begin + _compile_term(); + + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + + li t1, ' ' + bne a0, t1, .compile_expression_end + + # It is a binary expression. + + # Save the value of the left expression on the stack. + _write_z("sw t0, 24(sp)\n\0"); + + # Skip surrounding whitespace in front of the operator. + _advance_token(1); + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, '+' + beq t0, t1, .compile_expression_add + + li t1, '*' + beq t0, t1, .compile_expression_mul + + li t1, '&' + beq t0, t1, .compile_expression_and + + li t1, 'o' + beq t0, t1, .compile_expression_or + + li t1, 'x' + beq t0, t1, .compile_expression_xor + + li t1, '=' + beq t0, t1, .compile_expression_equals + + li t1, '<' + beq t0, t1, .compile_expression_less + + li t1, '>' + beq t0, t1, .compile_expression_greater + + # Unknown binary operator. + unimp + +.compile_expression_add: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("add t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_mul: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tmul t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_and: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tand t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_or: + _advance_token(2); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("or t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_xor: + _advance_token(3); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("xor t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_equals: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("xor t0, t0, t1\nseqz t0, t0\n\0"); + + goto .compile_expression_end; + +.compile_expression_less: + _advance_token(1); + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, '>' + beq t0, t1, .compile_expression_not_equal + + li t1, '=' + beq t0, t1, .compile_expression_less_equal + + _compile_binary_rhs(); + + # Execute the operation. + _write_z("slt t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_not_equal: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\txor t0, t0, t1\nsnez t0, t0\n\0"); + + goto .compile_expression_end; + +.compile_expression_less_equal: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tslt t0, t0, t1\nxori t0, t0, 1\n\0"); + + goto .compile_expression_end; + +.compile_expression_greater: + _advance_token(1); + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, '=' + beq t0, t1, .compile_expression_greater_equal + + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tslt t0, t1, t0\n\0"); + + goto .compile_expression_end; + +.compile_expression_greater_equal: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tslt t0, t1, t0\nxori t0, t0, 1\n\0"); + + goto .compile_expression_end; + +.compile_expression_end: +end; + +proc _compile_call(); +begin + # Stack variables: + # v0 - Procedure name length. + # v4 - Procedure name pointer. + # v8 - Argument count. + + _read_token(); + sw a0, 0(sp) + v4 := source_code_position; + v8 := 0; + + # Skip the identifier and left paren. + _advance_token(v0 + 1); + + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, ')' + beq t0, t1, .compile_call_finalize + +.compile_call_loop: + _compile_expression(); + + # Save the argument on the stack. + _write_z("\tsw t0, \0"); + + # Calculate the stack offset: 116 - (4 * argument_counter) + v12 := v8 * 4; + v12 := 116 + -v12; + _write_i(v12); + + _write_z("(sp)\n\0"); + + # Add one to the argument counter. + v8 := v8 + 1; + + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, ',' + bne t0, t1, .compile_call_finalize + + _advance_token(2); + goto .compile_call_loop; + +.compile_call_finalize: + # Load the argument from the stack. + + lw t0, 8(sp) + beqz t0, .compile_call_end + + # Decrement the argument counter. + v8 := v8 + -1; + + _write_z("\tlw a\0"); + _write_i(v8); + + _write_z(", \0"); + + # Calculate the stack offset: 116 - (4 * argument_counter) + v12 := v8 * 4; + v12 := 116 + -v12; + _write_i(v12); + + _write_z("(sp)\n\0"); + + goto .compile_call_finalize; + +.compile_call_end: + _write_z("\tcall \0"); + _write_s(v4, v0); + + # Skip the right paren. + _advance_token(1); +end; + +proc _compile_goto(); +begin + _advance_token(5); + + _read_token(); + sw a0, 0(sp) + + _write_z("\tj \0"); + + _write_token(v0); + _advance_token(); +end; + +proc _compile_local_designator(); +begin + # Skip "v" in the local variable name. + _advance_token(1); + _write_z("\t addi t0, sp, \0"); + + # Read local variable stack offset and save it. + _read_token(); + _write_token(); + _advance_token(); + _write_c('\n'); +end; + +proc _compile_global_designator(); +begin + _write_z("\tla t0, \0"); + + _read_token(); + _write_token(); + _advance_token(); + + _write_c('\n'); +end; + +proc _compile_designator(); +begin + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + + li t1, 'v' + beq a0, t1, .compile_designator_local + + goto .compile_designator_global; + +.compile_designator_local: + _compile_local_designator(); + goto .compile_designator_end; + +.compile_designator_global: + _compile_global_designator(); + goto .compile_designator_end; + +.compile_designator_end: +end; + +proc _compile_assignment(); +begin + _compile_designator(); + + # Save the assignee address on the stack. + _write_z("\tsw t0, 20(sp)\n\0"); + + # Skip the assignment sign (:=) with surrounding whitespaces. + _advance_token(4); + + # Compile the assignment. + _compile_expression(); + + _write_z("\tlw t1, 20(sp)\nsw t0, (t1)\n\0"); +end; + +proc _compile_return_statement(); +begin + # Skip "return" keyword and whitespace after it. + _advance_token(7); + _compile_expression(); + + _write_z("mv a0, t0\n\0"); +end; + +proc _compile_statement(); +begin + # This is a call if the statement starts with an underscore. + la t0, source_code_position + lw t0, (t0) + # First character after alignment tab. + addi t0, t0, 1 + lb t0, (t0) + + li t1, '_' + beq t0, t1, .compile_statement_call + + li t1, 'g' + beq t0, t1, .compile_statement_goto + + li t1, 'v' + beq t0, t1, .compile_statement_assignment + + # keyword_ret contains "\tret", so it's 4 bytes long. + _memcmp(source_code_position, "\treturn", 7); + beqz a0, .compile_statement_return + + _compile_line(); + goto .compile_statement_end; + +.compile_statement_call: + _advance_token(1); + _compile_call(); + + goto .compile_statement_semicolon; + +.compile_statement_goto: + _advance_token(1); + _compile_goto(); + + goto .compile_statement_semicolon; + +.compile_statement_assignment: + _advance_token(1); + _compile_assignment(); + + goto .compile_statement_semicolon; + +.compile_statement_return: + _advance_token(1); + _compile_return_statement(); + _write_c('\n'); + + goto .compile_statement_end; + +.compile_statement_semicolon: + _advance_token(2); + _write_c('\n'); + +.compile_statement_end: +end; + +proc _compile_procedure_body(); +begin +.compile_procedure_body_loop: + # 3 is "end" length. + _memcmp(source_code_position, "end", 3); + + beqz a0, .compile_procedure_body_epilogue + + _compile_statement(); + goto .compile_procedure_body_loop; + +.compile_procedure_body_epilogue: +end; + +# Writes a regster name to the standard output. +# +# Parameters: +# a0 - Register character. +# a1 - Register number. +proc _write_register(); +begin + _write_c(v88); + v84 := v84 + '0'; + _write_c(v84); +end; + +proc _compile_procedure_prologue(); +begin + _write_z("\taddi sp, sp, -128\n\tsw ra, 124(sp)\n\tsw s0, 120(sp)\n\taddi s0, sp, 128\n\0"); + v0 := 0; + +.compile_procedure_prologue_loop: + _write_z("\tsw a\0"); + _write_i(v0); + _write_z(", \0"); + + # Calculate the stack offset: 88 - (4 * parameter_counter) + v4 := v0 * 4; + v4 := 88 + -v4; + _write_i(v4); + + _write_z("(sp)\n\0"); + + v0 := v0 + 1; + lw a0, 0(sp) + + li t0, 8 + bne a0, t0, .compile_procedure_prologue_loop +end; + +proc _compile_procedure(); +begin + # Skip "proc ". + _advance_token(5); + + _read_token(); + sw a0, 0(sp) # Save the procedure name length. + + # Write .type _procedure_name, @function. + _write_z(".type \0"); + + _write_token(v0); + _write_z(", @function\n\0"); + + # Write procedure label, _procedure_name: + _write_token(v0); + _write_z(":\n\0"); + + # Skip the function name and trailing parens, semicolon, "begin" and newline. + _advance_token(v0 + 10); + + _compile_procedure_prologue(); + _compile_procedure_body(); + + # Write the epilogue. + _write_z("\tlw ra, 124(sp)\n\tlw s0, 120(sp)\n\taddi sp, sp, 128\n\tret\n\0"); + + # Skip the "end" keyword, semicolon and newline. + _advance_token(5); +end; + +proc _compile_type(); +begin + # Print and skip the ".type" (5 characters) directive and a space after it. + _write_token(6); + _advance_token(); + + # Read and print the symbol name. + _read_token(); + + # Print and skip the symbol name, comma, space and @. + addi a0, a0, 3 + _write_token(); + _advance_token(); + + # Read the symbol type. + _read_token(); + + # Print the symbol type and newline. + addi a0, a0, 1 + _write_token(); + _advance_token(); + + # Write the object definition itself. + _compile_line(); + +.compile_type_end: +end; + +proc _skip_newlines(); +begin + # Skip newlines. + la t0, source_code_position + lw t1, (t0) + +.skip_newlines_loop: + lb t2, (t1) + li t3, '\n' + bne t2, t3, .skip_newlines_end + beqz t2, .skip_newlines_end + + addi t1, t1, 1 + sw t1, (t0) + + goto .skip_newlines_loop; + +.skip_newlines_end: +end; + +# Process the source code and print the generated code. +proc _compile_module(); +begin +.compile_module_loop: + _skip_newlines(); + + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + beqz t0, .compile_module_end + li t1, '#' + beq t0, t1, .compile_module_comment + + # 8 is ".section" length. + _memcmp(source_code_position, ".section", 8); + beqz a0, .compile_module_section + + # 5 is ".type" length. + _memcmp(source_code_position, ".type", 5); + beqz a0, .compile_module_type + + # 5 is "proc " length. Space is needed to distinguish from "procedure". + _memcmp(source_code_position, "proc ", 5); + beqz a0, .compile_module_procedure + + # 6 is ".globl" length. + _memcmp(source_code_position, ".globl", 6); + beqz a0, .compile_module_global + + # Not a known token, exit. + goto .compile_module_end; + +.compile_module_section: + _compile_section(); + + goto .compile_module_loop; + +.compile_module_type: + _compile_type(); + + goto .compile_module_loop; + +.compile_module_global: + _compile_line(); + + goto .compile_module_loop; + +.compile_module_comment: + _skip_comment(); + + goto .compile_module_loop; + +.compile_module_procedure: + _compile_procedure(); + + goto .compile_module_loop; + +.compile_module_end: +end; + +proc _compile(); +begin + _compile_module(); + + _write_z(".section .rodata\n.type strings, @object\nstrings: .ascii \0"); + _write_c('"'); + + la t0, compiler_strings + sw t0, 0(sp) + +.compile_loop: + lw t0, 0(sp) + la t1, compiler_strings_position + lw t1, (t1) + bge t0, t1, .compile_end + + lb a0, (t0) + + addi t0, t0, 1 + sw t0, 0(sp) + + _write_c(); + + j .compile_loop + +.compile_end: + _write_c('"'); + _write_c('\n'); +end; + +# Terminates the program. a0 contains the return code. +# +# Parameters: +# a0 - Status code. +proc _exit(); +begin + li a7, 93 # SYS_EXIT + ecall +end; + +# Entry point. +.globl _start +proc _start(); +begin + # Read the source from the standard input. + # Second argument is buffer size. Modifying update the source_code definition. + _read_file(@source_code, 81920); + _compile(); + + _exit(0); + +end; diff --git a/boot/stage8.elna b/boot/stage8.elna new file mode 100644 index 0000000..6973963 --- /dev/null +++ b/boot/stage8.elna @@ -0,0 +1,1260 @@ +# This Source Code Form is subject to the terms of the Mozilla Public License, +# v. 2.0. If a copy of the MPL was not distributed with this file, You can +# obtain one at https://mozilla.org/MPL/2.0/. + +# Stage 7 compiler. +# +# - String literals. + +.section .bss + +# When modifiying also change the read size in the entry point procedure. +.type source_code, @object +source_code: .zero 81920 + +.type compiler_strings, @object +compiler_strings: .zero 8192 + +.section .data + +.type compiler_strings_position, @object +compiler_strings_position: .word compiler_strings + +.type compiler_strings_length, @object +compiler_strings_length: .word 0 + +.type source_code_position, @object +source_code_position: .word source_code + +.section .text + +# Calculates and returns the string token length between quotes, including the +# escaping slash characters. +# +# Parameters: +# a0 - String token pointer. +# +# Returns the length in a0. +proc _string_length(); +begin + # Reset the counter. + v0 := 0; + +.string_length_loop: + v88 := v88 + 1; + + lw t0, 88(sp) + lb t0, (t0) + + li t1, '"' + beq t0, t1, .string_length_end + + v0 := v0 + 1; + goto .string_length_loop; + +.string_length_end: + return v0 +end; + +# Adds a string to the global, read-only string storage. +# +# Parameters: +# a0 - String token. +# +# Returns the offset from the beginning of the storage to the new string in a0. +proc _add_string(); +begin + v0 := v88 + 1; + v4 := compiler_strings_length; + +.add_string_loop: + lw t0, 0(sp) + lb t1, (t0) + li t2, '"' + + beq t1, t2, .add_string_end + + la t2, compiler_strings_position + lw t3, (t2) + sb t1, (t3) + + addi t3, t3, 1 + sw t3, (t2) + + addi t0, t0, 1 + sw t0, 0(sp) + + li t2, '\\' + bne t1, t2, .add_string_increment + + goto .add_string_loop; + +.add_string_increment: + la t2, compiler_strings_length + lw t4, (t2) + addi t4, t4, 1 + sw t4, (t2) + + goto .add_string_loop; + +.add_string_end: + return v4 +end; + +# Reads standard input into a buffer. +# a0 - Buffer pointer. +# a1 - Buffer size. +# +# Returns the amount of bytes written in a0. +proc _read_file(); +begin + mv a2, a1 + mv a1, a0 + # STDIN. + li a0, 0 + li a7, 63 # SYS_READ. + ecall +end; + +# Writes to the standard output. +# +# Parameters: +# a0 - Buffer. +# a1 - Buffer length. +proc _write_s(); +begin + mv a2, a1 + mv a1, a0 + # STDOUT. + li a0, 1 + li a7, 64 # SYS_WRITE. + ecall +end; + +# Writes a number to a string buffer. +# +# t0 - Local buffer. +# t1 - Constant 10. +# t2 - Current character. +# t3 - Whether the number is negative. +# +# Parameters: +# a0 - Whole number. +# a1 - Buffer pointer. +# +# Sets a0 to the length of the written number. +proc _print_i(); +begin + li t1, 10 + addi t0, s0, -9 + + li t3, 0 + bgez a0, .print_i_digit10 + li t3, 1 + neg a0, a0 + +.print_i_digit10: + rem t2, a0, t1 + addi t2, t2, '0' + sb t2, 0(t0) + div a0, a0, t1 + addi t0, t0, -1 + bne zero, a0, .print_i_digit10 + + beq zero, t3, .print_i_write_call + addi t2, zero, '-' + sb t2, 0(t0) + addi t0, t0, -1 + +.print_i_write_call: + mv a0, a1 + addi a1, t0, 1 + sub a2, s0, t0 + addi a2, a2, -9 + sw a2, 0(sp) + + _memcpy(); + + return v0 +end; + +# Writes a number to the standard output. +# +# Parameters: +# a0 - Whole number. +proc _write_i(); +begin + _print_i(v88, @v0); + + mv a1, a0 + addi a0, sp, 0 + _write_s(); + +end; + +# Writes a character from a0 into the standard output. +proc _write_c(); +begin + _write_s(@v88, 1); +end; + +# Write null terminated string. +# +# Parameters: +# a0 - String. +proc _write_z(); +begin +.write_z_loop: + # Check for 0 character. + lw a0, 88(sp) + lb a0, (a0) + beqz a0, .write_z_end + + # Print a character. + _write_c(); + + # Advance the input string by one byte. + v88 := v88 + 1; + + goto .write_z_loop; + +.write_z_end: +end; + +# Detects if a0 is an uppercase character. Sets a0 to 1 if so, otherwise to 0. +proc _is_upper(); +begin + v0 := v88 >= 'A'; + v4 := v88 <= 'Z'; + + return v0 & v4 + +end; + +# Detects if a0 is an lowercase character. Sets a0 to 1 if so, otherwise to 0. +proc _is_lower(); +begin + v0 := v88 >= 'a'; + v4 := v88 <= 'z'; + + return v0 & v4 + +end; + +# Detects if the passed character is a 7-bit alpha character or an underscore. +# +# Paramters: +# a0 - Tested character. +# +# Sets a0 to 1 if the character is an alpha character or underscore, sets it to 0 otherwise. +proc _is_alpha(); +begin + sw a0, 0(sp) + + _is_upper(); + sw a0, 4(sp) + + _is_lower(v0); + + lw t0, 0(sp) + xori t1, t0, '_' + seqz t1, t1 + + lw t0, 4(sp) + or a0, a0, t0 + or a0, a0, t1 +end; + +# Detects whether the passed character is a digit +# (a value between 0 and 9). +# +# Parameters: +# a0 - Exemined value. +# +# Sets a0 to 1 if it is a digit, to 0 otherwise. +proc _is_digit(); +begin + v0 := v88 >= '0'; + v4 := v88 <= '9'; + + return v0 & v4 +end; + +proc _is_alnum(); +begin + sw a0, 4(sp) + + _is_alpha(); + sw a0, 0(sp) + + _is_digit(v4); + + lw a1, 0(sp) + or a0, a0, a1 +end; + +# Reads the next token. +# +# Returns token length in a0. +proc _read_token(); +begin + # Current token position. + v0 := source_code_position; + # Token length. + v4 := 0; + +.read_token_loop: + lw t0, 0(sp) + lb t0, (t0) # Current character. + + # First we try to read a derictive. + # A derictive can contain a dot and characters. + li t1, '.' + beq t0, t1, .read_token_next + + lw a0, 0(sp) + lb a0, (a0) + _is_alnum(); + bnez a0, .read_token_next + + goto .read_token_end; + +.read_token_next: + # Advance the source code position and token length. + v4 := v4 + 1; + v0 := v0 + 1; + + goto .read_token_loop; + +.read_token_end: + return v4 +end; + +# a0 - First pointer. +# a1 - Second pointer. +# a2 - The length to compare. +# +# Returns 0 in a0 if memory regions are equal. +proc _memcmp(); +begin + mv t0, a0 + li a0, 0 + +.memcmp_loop: + beqz a2, .memcmp_end + + lbu t1, (t0) + lbu t2, (a1) + sub a0, t1, t2 + + bnez a0, .memcmp_end + + addi t0, t0, 1 + addi a1, a1, 1 + addi a2, a2, -1 + + goto .memcmp_loop; + +.memcmp_end: +end; + +# Copies memory. +# +# Parameters: +# a0 - Destination. +# a1 - Source. +# a2 - Size. +# +# Preserves a0. +proc _memcpy(); +begin + mv t0, a0 + +.memcpy_loop: + beqz a2, .memcpy_end + + lbu t1, (a1) + sb t1, (a0) + + addi a0, a0, 1 + addi a1, a1, 1 + addi a2, a2, -1 + + goto .memcpy_loop + +.memcpy_end: + mv a0, t0 +end; + +# Advances the token stream by a0 bytes. +proc _advance_token(); +begin + la t0, source_code_position + lw t1, (t0) + add t1, t1, a0 + sw t1, (t0) +end; + +# Prints the current token. +# +# Parameters: +# a0 - Token length. +# +# Returns a0 unchanged. +proc _write_token(); +begin + _write_s(source_code_position, v88); + return v88 +end; + +proc _compile_section(); +begin + # Print and skip the ".section" (8 characters) directive and a space after it. + _write_token(9); + _advance_token(); + + # Read the section name. + _read_token(); + addi a0, a0, 1 + + _write_token(); + _advance_token(); +end; + +# Prints and skips a line. +proc _skip_comment(); +begin + la t0, source_code_position + lw t1, (t0) + +.skip_comment_loop: + # Check for newline character. + lb t2, (t1) + li t3, '\n' + beq t2, t3, .skip_comment_end + + # Advance the input string by one byte. + addi t1, t1, 1 + sw t1, (t0) + + goto .skip_comment_loop; + +.skip_comment_end: + # Skip the newline. + addi t1, t1, 1 + sw t1, (t0) +end; + +# Prints and skips a line. +proc _compile_line(); +begin +.compile_line_loop: + la a0, source_code_position + lw a1, (a0) + + lb t0, (a1) + li t1, '\n' + beq t0, t1, .compile_line_end + + # Print a character. + lw a0, (a1) + _write_c(); + + # Advance the input string by one byte. + _advance_token(1); + + goto .compile_line_loop; + +.compile_line_end: + _write_c('\n'); + + _advance_token(1); +end; + +proc _compile_integer_literal(); +begin + _write_z("\tli t0, \0"); + + _read_token(); + _write_token(); + _advance_token(); + + _write_c('\n'); +end; + +proc _compile_character_literal(); +begin + _write_z("\tli t0, \0"); + + _write_c('\''); + _advance_token(1); + + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + li t1, '\\' + bne a0, t1, .compile_character_literal_end + + _write_c('\\'); + _advance_token(1); + +.compile_character_literal_end: + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + _write_c(); + + _write_c('\''); + _write_c('\n'); + + _advance_token(2); + +end; + +proc _compile_variable_expression(); +begin + _compile_designator(); + _write_z("\tlw t0, (t0)\n\0"); +end; + +proc _compile_address_expression(); +begin + # Skip the "@" sign. + _advance_token(1); + _compile_designator(); + +end; + +proc _compile_negate_expression(); +begin + # Skip the "-" sign. + _advance_token(1); + _compile_term(); + + _write_z("\tneg t0, t0\n\0"); +end; + +proc _compile_not_expression(); +begin + # Skip the "~" sign. + _advance_token(1); + _compile_term(); + + _write_z("\tnot t0, t0\n\0"); +end; + +proc _compile_string_literal(); +begin + _string_length(source_code_position); + sw a0, 0(sp) + + _add_string(source_code_position); + sw a0, 4(sp) + + _advance_token(v0 + 2); + _write_z("\tla t0, strings\n\0"); + + _write_z("\tli t1, \0"); + _write_i(v4); + _write_c('\n'); + + _write_z("\tadd t0, t0, t1\n\0"); +end; + +proc _compile_term(); +begin + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + sw a0, 0(sp) + + li t1, '\'' + beq a0, t1, .compile_term_character_literal + + li t1, '@' + beq a0, t1, .compile_term_address + + li t1, '-' + beq a0, t1, .compile_term_negation + + li t1, '~' + beq a0, t1, .compile_term_not + + li t1, '"' + beq a0, t1, .compile_term_string_literal + + _is_digit(v0); + bnez a0, .compile_term_integer_literal + + goto .compile_term_variable; + +.compile_term_character_literal: + _compile_character_literal(); + goto .compile_term_end; + +.compile_term_integer_literal: + _compile_integer_literal(); + goto .compile_term_end; + +.compile_term_address: + _compile_address_expression(); + goto .compile_term_end; + +.compile_term_negation: + _compile_negate_expression(); + goto .compile_term_end; + +.compile_term_not: + _compile_not_expression(); + goto .compile_term_end; + +.compile_term_string_literal: + _compile_string_literal(); + goto .compile_term_end; + +.compile_term_variable: + _compile_variable_expression(); + goto .compile_term_end; + +.compile_term_end: +end; + +proc _compile_binary_rhs(); +begin + # Skip the whitespace after the binary operator. + _advance_token(1); + _compile_term(); + + # Load the left expression from the stack; + _write_z("\tlw t1, 24(sp)\n\0"); +end; + +proc _compile_expression(); +begin + _compile_term(); + + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + + li t1, ' ' + bne a0, t1, .compile_expression_end + + # It is a binary expression. + + # Save the value of the left expression on the stack. + _write_z("sw t0, 24(sp)\n\0"); + + # Skip surrounding whitespace in front of the operator. + _advance_token(1); + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, '+' + beq t0, t1, .compile_expression_add + + li t1, '*' + beq t0, t1, .compile_expression_mul + + li t1, '&' + beq t0, t1, .compile_expression_and + + li t1, 'o' + beq t0, t1, .compile_expression_or + + li t1, 'x' + beq t0, t1, .compile_expression_xor + + li t1, '=' + beq t0, t1, .compile_expression_equals + + li t1, '<' + beq t0, t1, .compile_expression_less + + li t1, '>' + beq t0, t1, .compile_expression_greater + + # Unknown binary operator. + unimp + +.compile_expression_add: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("add t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_mul: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tmul t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_and: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tand t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_or: + _advance_token(2); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("or t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_xor: + _advance_token(3); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("xor t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_equals: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("xor t0, t0, t1\nseqz t0, t0\n\0"); + + goto .compile_expression_end; + +.compile_expression_less: + _advance_token(1); + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, '>' + beq t0, t1, .compile_expression_not_equal + + li t1, '=' + beq t0, t1, .compile_expression_less_equal + + _compile_binary_rhs(); + + # Execute the operation. + _write_z("slt t0, t0, t1\n\0"); + + goto .compile_expression_end; + +.compile_expression_not_equal: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\txor t0, t0, t1\nsnez t0, t0\n\0"); + + goto .compile_expression_end; + +.compile_expression_less_equal: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tslt t0, t0, t1\nxori t0, t0, 1\n\0"); + + goto .compile_expression_end; + +.compile_expression_greater: + _advance_token(1); + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, '=' + beq t0, t1, .compile_expression_greater_equal + + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tslt t0, t1, t0\n\0"); + + goto .compile_expression_end; + +.compile_expression_greater_equal: + _advance_token(1); + _compile_binary_rhs(); + + # Execute the operation. + _write_z("\tslt t0, t1, t0\nxori t0, t0, 1\n\0"); + + goto .compile_expression_end; + +.compile_expression_end: +end; + +proc _compile_call(); +begin + # Stack variables: + # v0 - Procedure name length. + # v4 - Procedure name pointer. + # v8 - Argument count. + + _read_token(); + sw a0, 0(sp) + v4 := source_code_position; + v8 := 0; + + # Skip the identifier and left paren. + _advance_token(v0 + 1); + + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, ')' + beq t0, t1, .compile_call_finalize + +.compile_call_loop: + _compile_expression(); + + # Save the argument on the stack. + _write_z("\tsw t0, \0"); + + # Calculate the stack offset: 116 - (4 * argument_counter) + v12 := v8 * 4; + v12 := 116 + -v12; + _write_i(v12); + + _write_z("(sp)\n\0"); + + # Add one to the argument counter. + v8 := v8 + 1; + + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + + li t1, ',' + bne t0, t1, .compile_call_finalize + + _advance_token(2); + goto .compile_call_loop; + +.compile_call_finalize: + # Load the argument from the stack. + + lw t0, 8(sp) + beqz t0, .compile_call_end + + # Decrement the argument counter. + v8 := v8 + -1; + + _write_z("\tlw a\0"); + _write_i(v8); + + _write_z(", \0"); + + # Calculate the stack offset: 116 - (4 * argument_counter) + v12 := v8 * 4; + v12 := 116 + -v12; + _write_i(v12); + + _write_z("(sp)\n\0"); + + goto .compile_call_finalize; + +.compile_call_end: + _write_z("\tcall \0"); + _write_s(v4, v0); + + # Skip the right paren. + _advance_token(1); +end; + +proc _compile_goto(); +begin + _advance_token(5); + + _read_token(); + sw a0, 0(sp) + + _write_z("\tj \0"); + + _write_token(v0); + _advance_token(); +end; + +proc _compile_local_designator(); +begin + # Skip "v" in the local variable name. + _advance_token(1); + _write_z("\t addi t0, sp, \0"); + + # Read local variable stack offset and save it. + _read_token(); + _write_token(); + _advance_token(); + _write_c('\n'); +end; + +proc _compile_global_designator(); +begin + _write_z("\tla t0, \0"); + + _read_token(); + _write_token(); + _advance_token(); + + _write_c('\n'); +end; + +proc _compile_designator(); +begin + la t0, source_code_position + lw t0, (t0) + lb a0, (t0) + + li t1, 'v' + beq a0, t1, .compile_designator_local + + goto .compile_designator_global; + +.compile_designator_local: + _compile_local_designator(); + goto .compile_designator_end; + +.compile_designator_global: + _compile_global_designator(); + goto .compile_designator_end; + +.compile_designator_end: +end; + +proc _compile_assignment(); +begin + _compile_designator(); + + # Save the assignee address on the stack. + _write_z("\tsw t0, 20(sp)\n\0"); + + # Skip the assignment sign (:=) with surrounding whitespaces. + _advance_token(4); + + # Compile the assignment. + _compile_expression(); + + _write_z("\tlw t1, 20(sp)\nsw t0, (t1)\n\0"); +end; + +proc _compile_return_statement(); +begin + # Skip "return" keyword and whitespace after it. + _advance_token(7); + _compile_expression(); + + _write_z("mv a0, t0\n\0"); +end; + +proc _compile_statement(); +begin + # This is a call if the statement starts with an underscore. + la t0, source_code_position + lw t0, (t0) + # First character after alignment tab. + addi t0, t0, 1 + lb t0, (t0) + + li t1, '_' + beq t0, t1, .compile_statement_call + + li t1, 'g' + beq t0, t1, .compile_statement_goto + + li t1, 'v' + beq t0, t1, .compile_statement_assignment + + # keyword_ret contains "\tret", so it's 4 bytes long. + _memcmp(source_code_position, "\treturn", 7); + beqz a0, .compile_statement_return + + _compile_line(); + goto .compile_statement_end; + +.compile_statement_call: + _advance_token(1); + _compile_call(); + + goto .compile_statement_semicolon; + +.compile_statement_goto: + _advance_token(1); + _compile_goto(); + + goto .compile_statement_semicolon; + +.compile_statement_assignment: + _advance_token(1); + _compile_assignment(); + + goto .compile_statement_semicolon; + +.compile_statement_return: + _advance_token(1); + _compile_return_statement(); + _write_c('\n'); + + goto .compile_statement_end; + +.compile_statement_semicolon: + _advance_token(2); + _write_c('\n'); + +.compile_statement_end: +end; + +proc _compile_procedure_body(); +begin +.compile_procedure_body_loop: + # 3 is "end" length. + _memcmp(source_code_position, "end", 3); + + beqz a0, .compile_procedure_body_epilogue + + _compile_statement(); + goto .compile_procedure_body_loop; + +.compile_procedure_body_epilogue: +end; + +# Writes a regster name to the standard output. +# +# Parameters: +# a0 - Register character. +# a1 - Register number. +proc _write_register(); +begin + _write_c(v88); + v84 := v84 + '0'; + _write_c(v84); +end; + +proc _compile_procedure_prologue(); +begin + _write_z("\taddi sp, sp, -128\n\tsw ra, 124(sp)\n\tsw s0, 120(sp)\n\taddi s0, sp, 128\n\0"); + v0 := 0; + +.compile_procedure_prologue_loop: + _write_z("\tsw a\0"); + _write_i(v0); + _write_z(", \0"); + + # Calculate the stack offset: 88 - (4 * parameter_counter) + v4 := v0 * 4; + v4 := 88 + -v4; + _write_i(v4); + + _write_z("(sp)\n\0"); + + v0 := v0 + 1; + lw a0, 0(sp) + + li t0, 8 + bne a0, t0, .compile_procedure_prologue_loop +end; + +proc _compile_procedure(); +begin + # Skip "proc ". + _advance_token(5); + + _read_token(); + sw a0, 0(sp) # Save the procedure name length. + + # Write .type _procedure_name, @function. + _write_z(".type \0"); + + _write_token(v0); + _write_z(", @function\n\0"); + + # Write procedure label, _procedure_name: + _write_token(v0); + _write_z(":\n\0"); + + # Skip the function name and trailing parens, semicolon, "begin" and newline. + _advance_token(v0 + 10); + + _compile_procedure_prologue(); + _compile_procedure_body(); + + # Write the epilogue. + _write_z("\tlw ra, 124(sp)\n\tlw s0, 120(sp)\n\taddi sp, sp, 128\n\tret\n\0"); + + # Skip the "end" keyword, semicolon and newline. + _advance_token(5); +end; + +proc _compile_type(); +begin + # Print and skip the ".type" (5 characters) directive and a space after it. + _write_token(6); + _advance_token(); + + # Read and print the symbol name. + _read_token(); + + # Print and skip the symbol name, comma, space and @. + addi a0, a0, 3 + _write_token(); + _advance_token(); + + # Read the symbol type. + _read_token(); + + # Print the symbol type and newline. + addi a0, a0, 1 + _write_token(); + _advance_token(); + + # Write the object definition itself. + _compile_line(); + +.compile_type_end: +end; + +proc _skip_newlines(); +begin + # Skip newlines. + la t0, source_code_position + lw t1, (t0) + +.skip_newlines_loop: + lb t2, (t1) + li t3, '\n' + bne t2, t3, .skip_newlines_end + beqz t2, .skip_newlines_end + + addi t1, t1, 1 + sw t1, (t0) + + goto .skip_newlines_loop; + +.skip_newlines_end: +end; + +# Process the source code and print the generated code. +proc _compile_module(); +begin +.compile_module_loop: + _skip_newlines(); + + la t0, source_code_position + lw t0, (t0) + lb t0, (t0) + beqz t0, .compile_module_end + li t1, '#' + beq t0, t1, .compile_module_comment + + # 8 is ".section" length. + _memcmp(source_code_position, ".section", 8); + beqz a0, .compile_module_section + + # 5 is ".type" length. + _memcmp(source_code_position, ".type", 5); + beqz a0, .compile_module_type + + # 5 is "proc " length. Space is needed to distinguish from "procedure". + _memcmp(source_code_position, "proc ", 5); + beqz a0, .compile_module_procedure + + # 6 is ".globl" length. + _memcmp(source_code_position, ".globl", 6); + beqz a0, .compile_module_global + + # Not a known token, exit. + goto .compile_module_end; + +.compile_module_section: + _compile_section(); + + goto .compile_module_loop; + +.compile_module_type: + _compile_type(); + + goto .compile_module_loop; + +.compile_module_global: + _compile_line(); + + goto .compile_module_loop; + +.compile_module_comment: + _skip_comment(); + + goto .compile_module_loop; + +.compile_module_procedure: + _compile_procedure(); + + goto .compile_module_loop; + +.compile_module_end: +end; + +proc _compile(); +begin + _compile_module(); + + _write_z(".section .rodata\n.type strings, @object\nstrings: .ascii \0"); + _write_c('"'); + + la t0, compiler_strings + sw t0, 0(sp) + +.compile_loop: + lw t0, 0(sp) + la t1, compiler_strings_position + lw t1, (t1) + bge t0, t1, .compile_end + + lb a0, (t0) + + addi t0, t0, 1 + sw t0, 0(sp) + + _write_c(); + + j .compile_loop + +.compile_end: + _write_c('"'); + _write_c('\n'); +end; + +# Terminates the program. a0 contains the return code. +# +# Parameters: +# a0 - Status code. +proc _exit(); +begin + li a7, 93 # SYS_EXIT + ecall +end; + +# Entry point. +.globl _start +proc _start(); +begin + # Read the source from the standard input. + # Second argument is buffer size. Modifying update the source_code definition. + _read_file(@source_code, 81920); + _compile(); + + _exit(0); + +end;