Implement variable dereferencing.

This commit is contained in:
Andrius Štikonas 2021-11-08 00:11:23 +00:00
parent 6d4d6c56ea
commit 6ebe45f369
7 changed files with 180 additions and 20 deletions

View File

@ -388,7 +388,28 @@ void constant_load(struct token_list* a)
emit_out("\n");
}
void variable_load(struct token_list* a)
void emit_dereference(int load_byte) {
if(load_byte)
{
if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOAD8 R0 R0 0\n");
else if(X86 == Architecture) emit_out("LOAD_BYTE\n");
else if(AMD64 == Architecture) emit_out("LOAD_BYTE\n");
else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD8 R0 MEMORY\n");
else if(AARCH64 == Architecture) emit_out("DEREF_X0_BYTE\n");
else if(RISCV64 == Architecture) emit_out("RD_A0 RS1_A0 LBU\n");
}
else
{
if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOAD R0 R0 0\n");
else if(X86 == Architecture) emit_out("LOAD_INTEGER\n");
else if(AMD64 == Architecture) emit_out("LOAD_INTEGER\n");
else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R0 MEMORY\n");
else if(AARCH64 == Architecture) emit_out("DEREF_X0\n");
else if(RISCV64 == Architecture) emit_out("RD_A0 RS1_A0 LD\n");
}
}
void variable_load(struct token_list* a, int num_dereference)
{
require(NULL != global_token, "incomplete variable load received\n");
if((match("FUNCTION", a->type->name) || match("FUNCTION*", a->type->name)) && match("(", global_token->s))
@ -412,14 +433,23 @@ void variable_load(struct token_list* a)
emit_out("\n");
if(TRUE == Address_of) return;
if(match("=", global_token->s)) return;
if(!match("=", global_token->s)) {
emit_dereference(FALSE);
}
if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOAD R0 R0 0\n");
else if(X86 == Architecture) emit_out("LOAD_INTEGER\n");
else if(AMD64 == Architecture) emit_out("LOAD_INTEGER\n");
else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R0 MEMORY\n");
else if(AARCH64 == Architecture) emit_out("DEREF_X0\n");
else if(RISCV64 == Architecture) emit_out("RD_A0 RS1_A0 LD\n");
while (num_dereference > 0)
{
if(match("char*", current_target->name)) {
/* Load a single byte */
emit_dereference(TRUE);
}
else
{
emit_dereference(FALSE);
}
current_target = current_target->type;
num_dereference = num_dereference - 1;
}
}
void function_load(struct token_list* a)
@ -688,6 +718,11 @@ void primary_expr_number()
void primary_expr_variable()
{
int num_dereference = 0;
while(global_token->s[0] == '*') {
global_token = global_token->next;
num_dereference = num_dereference + 1;
}
char* s = global_token->s;
global_token = global_token->next;
struct token_list* a = sym_lookup(s, global_constant_list);
@ -697,21 +732,21 @@ void primary_expr_variable()
return;
}
a= sym_lookup(s, function->locals);
a = sym_lookup(s, function->locals);
if(NULL != a)
{
variable_load(a);
variable_load(a, num_dereference);
return;
}
a = sym_lookup(s, function->arguments);
if(NULL != a)
{
variable_load(a);
variable_load(a, num_dereference);
return;
}
a= sym_lookup(s, global_function_list);
a = sym_lookup(s, global_function_list);
if(NULL != a)
{
function_load(a);
@ -1310,6 +1345,7 @@ void primary_expr()
else if(global_token->s[0] == '\'') primary_expr_char();
else if(global_token->s[0] == '"') primary_expr_string();
else if(in_set(global_token->s[0], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")) primary_expr_variable();
else if(global_token->s[0] == '*') primary_expr_variable();
else if(in_set(global_token->s[0], "0123456789")) primary_expr_number();
else primary_expr_failure();
}

View File

@ -54,7 +54,6 @@ struct type* new_primitive(char* name0, char* name1, char* name2, int size, int
b->size = register_size;
b->is_signed = sign;
b->indirect = a;
b->type = b;
a->type = b;
struct type* r = calloc(1, sizeof(struct type));
@ -64,6 +63,7 @@ struct type* new_primitive(char* name0, char* name1, char* name2, int size, int
r->is_signed = sign;
r->indirect = b;
r->type = r;
b->type = r;
return r;
}

View File

@ -51,6 +51,7 @@ aarch64-tests: \
test0024-aarch64-binary \
test0025-aarch64-binary \
test0026-aarch64-binary \
test0027-aarch64-binary \
test0100-aarch64-binary \
test0101-aarch64-binary \
test0102-aarch64-binary \
@ -88,6 +89,7 @@ amd64-tests: \
test0024-amd64-binary \
test0025-amd64-binary \
test0026-amd64-binary \
test0027-amd64-binary \
test0100-amd64-binary \
test0101-amd64-binary \
test0102-amd64-binary \
@ -179,6 +181,7 @@ armv7l-tests: \
test0024-armv7l-binary \
test0025-armv7l-binary \
test0026-armv7l-binary \
test0027-armv7l-binary \
test0100-armv7l-binary \
test0101-armv7l-binary \
test0102-armv7l-binary \
@ -216,6 +219,7 @@ x86-tests: \
test0024-x86-binary \
test0025-x86-binary \
test0026-x86-binary \
test0027-x86-binary \
test0100-x86-binary \
test0101-x86-binary \
test0102-x86-binary \
@ -255,6 +259,7 @@ riscv64-tests: \
test0024-riscv64-binary \
test0025-riscv64-binary \
test0026-riscv64-binary \
test0027-riscv64-binary \
test0100-riscv64-binary \
test0101-riscv64-binary \
test0102-riscv64-binary \
@ -345,6 +350,9 @@ test0025-riscv64-binary: M2-Planet | results
test0026-riscv64-binary: M2-Planet | results
test/test0026/run_test.sh riscv64
test0027-riscv64-binary: M2-Planet | results
test/test0027/run_test.sh riscv64
test0100-riscv64-binary: M2-Planet | results
test/test0100/run_test.sh riscv64
@ -450,6 +458,9 @@ test0025-aarch64-binary: M2-Planet | results
test0026-aarch64-binary: M2-Planet | results
test/test0026/run_test.sh aarch64
test0027-aarch64-binary: M2-Planet | results
test/test0027/run_test.sh aarch64
test0100-aarch64-binary: M2-Planet | results
test/test0100/run_test.sh aarch64
@ -555,6 +566,9 @@ test0025-amd64-binary: M2-Planet | results
test0026-amd64-binary: M2-Planet | results
test/test0026/run_test.sh amd64
test0027-amd64-binary: M2-Planet | results
test/test0027/run_test.sh amd64
test0100-amd64-binary: M2-Planet | results
test/test0100/run_test.sh amd64
@ -810,6 +824,9 @@ test0025-armv7l-binary: M2-Planet | results
test0026-armv7l-binary: M2-Planet | results
test/test0026/run_test.sh armv7l
test0027-armv7l-binary: M2-Planet | results
test/test0026/run_test.sh armv7l
test0100-armv7l-binary: M2-Planet | results
test/test0100/run_test.sh armv7l
@ -915,6 +932,9 @@ test0025-x86-binary: M2-Planet | results
test0026-x86-binary: M2-Planet | results
test/test0026/run_test.sh x86
test0027-x86-binary: M2-Planet | results
test/test0027/run_test.sh x86
test0100-x86-binary: M2-Planet | results
test/test0100/run_test.sh x86

View File

@ -176,6 +176,10 @@ bf7204e213a7221aa783a68d841b5dfb6baa2bb0f5d5bf361e1edfe0c6c1b30b test/results/t
f4676ec3d8eade4c0f405a4b66d7069c78b881fd3f654e7e04f3fe56326542b5 test/results/test0026-armv7l-binary
856fdc8a9045f71583337dfc4a0d2a5cd01a72061d292b6d75e6fa52e70c78df test/results/test0026-riscv64-binary
46381cee997ec150a6b3bb7b978908df8d30cf65cc98744451a00f6eb5a70aa6 test/results/test0026-x86-binary
308ff16be0bbd485cb550ee2f7b49f0bc971bcf00cb043fa59f7053fe5ae7a16 test/results/test0027-aarch64-binary
1591d35cf06ee0b2a746f81445b871e19440908c1b49657c85934d7daf131488 test/results/test0027-amd64-binary
f871d50855f0b12aa4925b819e1a67140027737262686bca8d622b42cb22bc43 test/results/test0027-riscv64-binary
04e75e83bcea38ba913117c4a4fd1dac5264321aa34eb40a934d6596b6bf7978 test/results/test0027-x86-binary
29bc8c839549d674dd7717ee31aa7a974fb81163c7a75ec6357ab129f91ba48c test/results/test0100-aarch64-binary
1f51f1fdc444e40917a7fe59d70472b7bca9a623230093c39c55dd5b911e6ed6 test/results/test0100-amd64-binary
bc2666096825062ad5bf840e94fe1fe26b37bef2d024d642cc237f98c65fcccc test/results/test0100-armv7l-binary
@ -217,9 +221,9 @@ eb1f6aab09a1a4966f9f49a518d8aa03ca97aa7c46e443871d758a306671a85e test/results/t
a2cbfd5f76d3be8049c6737e3fa02f191caf29b6885db006fa23f86e6eacc186 test/results/test0106-knight-posix-binary
d75e450e2fcdf19df63f9d6a3fe5e032933e57b33f6f06b39a8ed2f3dc759f17 test/results/test0106-riscv64-binary
473cc504d6cba9eaf648abcf15c83a317ff3f4a7f08f5e8936e3b35b2cc4fbc6 test/results/test0106-x86-binary
3b5ddb395491c54ddb705f22467807d450d6560cecd5e5a1e58a80b0d98cebab test/results/test1000-aarch64-binary
d0db9d1f07d96948e6ffd5a27247ac7cc0d72f70cb4131cdcdf0c2c43a3c76f4 test/results/test1000-amd64-binary
e17764ae5266384f263416333f2fb490d591ed92fa94a6e44dc22d07baaf3700 test/results/test1000-armv7l-binary
7916298f9d8e4e033a0be485c35d3f1194f85f0a8ce73ad3ae34296dcd69ebfa test/results/test1000-knight-posix-binary
d922ad66b74ab8a084d061d7b82877a099a808d51ccc3a9b15eddf23251ab6c1 test/results/test1000-riscv64-binary
53eb324f93c79212bf9a01b2f01b033c3b1149a7ea4a2d12eb6fffe518e65a11 test/results/test1000-x86-binary
b5187732c5dd3c90f07404636c8a929d8ef0f4b0e859228468825eff4dbfd66b test/results/test1000-aarch64-binary
25c32aaa0dcda0aba7f2b0c9a87a923a904161448b50ad5c8c8032fb27e29f51 test/results/test1000-amd64-binary
7676c997da4ca49b204bfb884eec89418e5bb9620c1e686e03ba5064e7406ca6 test/results/test1000-armv7l-binary
f33537743e53ad62849af0675f5d3635ff0f3a1ba8169768289c125a014607c3 test/results/test1000-knight-posix-binary
120e74cb11b9df0fd0793cfae6475cfaf95f8af4242d3b4f8b33247249fff25d test/results/test1000-riscv64-binary
72ee105c0fd1356c029966a7c550c019a7ccc5763ec8c2f2e40affc345e1349b test/results/test1000-x86-binary

View File

@ -0,0 +1,28 @@
/* Copyright (C) 2021 Andrius Štikonas
* This file is part of M2-Planet.
*
* M2-Planet is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* M2-Planet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
*/
int main(int argc, char** argv)
{
char *d = "test\n";
if(*d != 't') {
return 1;
}
int i = 2;
int *j = &i;
*j = 0;
return *j;
}

72
test/test0027/run_test.sh Executable file
View File

@ -0,0 +1,72 @@
#! /bin/sh
## Copyright (C) 2017 Jeremiah Orians
## Copyright (C) 2020-2021 deesix <deesix@tuta.io>
## This file is part of M2-Planet.
##
## M2-Planet is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## M2-Planet is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
set -ex
ARCH="$1"
. test/env.inc.sh
TMPDIR="test/test0027/tmp-${ARCH}"
mkdir -p ${TMPDIR}
# Build the test
bin/M2-Planet \
--architecture ${ARCH} \
--debug \
-f test/test0027/dereference.c \
-o ${TMPDIR}/dereference.M1 \
|| exit 1
# Build debug footer
blood-elf \
${BLOOD_ELF_WORD_SIZE_FLAG} \
-f ${TMPDIR}/dereference.M1 \
${ENDIANNESS_FLAG} \
--entry _start \
-o ${TMPDIR}/dereference-footer.M1 \
|| exit 2
# Macro assemble with libc written in M1-Macro
M1 \
-f M2libc/${ARCH}/${ARCH}_defs.M1 \
-f M2libc/${ARCH}/libc-core.M1 \
-f ${TMPDIR}/dereference.M1 \
-f ${TMPDIR}/dereference-footer.M1 \
${ENDIANNESS_FLAG} \
--architecture ${ARCH} \
-o ${TMPDIR}/dereference.hex2 \
|| exit 2
# Resolve all linkages
hex2 \
-f M2libc/${ARCH}/ELF-${ARCH}-debug.hex2 \
-f ${TMPDIR}/dereference.hex2 \
${ENDIANNESS_FLAG} \
--architecture ${ARCH} \
--base-address ${BASE_ADDRESS} \
-o test/results/test0027-${ARCH}-binary \
|| exit 3
# Ensure binary works if host machine supports test
if [ "$(get_machine ${GET_MACHINE_FLAGS})" = "${ARCH}" ]
then
. ./sha256.sh
# Verify that the resulting file works
./test/results/test0027-${ARCH}-binary || exit 4
fi
exit 0

View File

@ -1 +1 @@
0e01009082d89c270ce3adb147c2c0e248cb963724203188ebcc89ab0042355a test/test1000/proof
a7014fa831a6f837d25bace5fdce16ff16cb155eb6676ad1f4bdc239d4ba361d test/test1000/proof