Add support for global char foo[12]; definitions

- only enabled in --bootstrap-mode

- allocates space for both the char array and the pointer to the start of
  the array
This commit is contained in:
Sanne Wouda 2021-01-09 23:34:25 +00:00
parent 674a6f5569
commit 0e3d5b609a
13 changed files with 411 additions and 7 deletions

View File

@ -124,6 +124,17 @@ void require_match(char* message, char* required)
require(NULL != global_token, "EOF after require match occurred\n");
}
void maybe_bootstrap_error(char* feature)
{
if (BOOTSTRAP_MODE)
{
line_error();
file_print(feature, stderr);
file_print(" is not supported in --bootstrap-mode\n", stderr);
exit(EXIT_FAILURE);
}
}
void expression();
void function_call(char* s, int bool)
{
@ -1813,6 +1824,7 @@ void program()
function = NULL;
Address_of = FALSE;
struct type* type_size;
int size;
new_type:
if (NULL == global_token) return;
@ -1857,10 +1869,45 @@ new_type:
{
goto new_type;
}
require(NULL != global_token->next, "Unterminated global\n");
if(match("[", global_token->next->s))
{
maybe_bootstrap_error("global array definitions");
globals_list = emit(":GLOBAL_", globals_list);
globals_list = emit(global_token->s, globals_list);
globals_list = emit("\n&GLOBAL_STORAGE_", globals_list);
globals_list = emit(global_token->s, globals_list);
if (AARCH64 == Architecture || AMD64 == Architecture)
{
globals_list = emit(" %0", globals_list);
}
globals_list = emit("\n:GLOBAL_STORAGE_", globals_list);
globals_list = emit(global_token->s, globals_list);
global_symbol_list = sym_declare(global_token->s, type_size->indirect, global_symbol_list);
require(NULL != global_token->next->next, "Unterminated global\n");
global_token = global_token->next->next;
/* length */
size = numerate_string(global_token->s);
globals_list = emit("\n'", globals_list);
while (0 != size)
{
globals_list = emit(" 00", globals_list);
size = size - 1;
}
globals_list = emit("'\n", globals_list);
global_token = global_token->next;
require_match("missing close bracket\n", "]");
require_match("missing ;\n", ";");
goto new_type;
}
/* Add to global symbol table */
global_symbol_list = sym_declare(global_token->s, type_size, global_symbol_list);
global_token = global_token->next;
require(NULL != global_token, "Unterminated global\n");
if(match(";", global_token->s))
{
/* Ensure 4 bytes are allocated for the global */

View File

@ -95,6 +95,7 @@ clean:
./test/test0022/cleanup.sh
./test/test0023/cleanup.sh
./test/test0024/cleanup.sh
./test/test0025/cleanup.sh
./test/test0100/cleanup.sh
./test/test0101/cleanup.sh
./test/test0102/cleanup.sh
@ -141,6 +142,7 @@ aarch64-tests: \
test0022-aarch64-binary \
test0023-aarch64-binary \
test0024-aarch64-binary \
test0025-aarch64-binary \
test0100-aarch64-binary \
test0101-aarch64-binary \
test0102-aarch64-binary \
@ -176,6 +178,7 @@ amd64-tests: \
test0022-amd64-binary \
test0023-amd64-binary \
test0024-amd64-binary \
test0025-amd64-binary \
test0100-amd64-binary \
test0101-amd64-binary \
test0102-amd64-binary \
@ -265,6 +268,7 @@ armv7l-tests: \
test0022-armv7l-binary \
test0023-armv7l-binary \
test0024-armv7l-binary \
test0025-armv7l-binary \
test0100-armv7l-binary \
test0101-armv7l-binary \
test0102-armv7l-binary \
@ -300,6 +304,7 @@ x86-tests: \
test0022-x86-binary \
test0023-x86-binary \
test0024-x86-binary \
test0025-x86-binary \
test0100-x86-binary \
test0101-x86-binary \
test0102-x86-binary \
@ -384,6 +389,9 @@ test0023-aarch64-binary: M2-Planet | results
test0024-aarch64-binary: M2-Planet | results
test/test0024/hello-aarch64.sh
test0025-aarch64-binary: M2-Planet | results
test/test0025/hello-aarch64.sh
test0100-aarch64-binary: M2-Planet | results
test/test0100/hello-aarch64.sh
@ -483,6 +491,9 @@ test0023-amd64-binary: M2-Planet | results
test0024-amd64-binary: M2-Planet | results
test/test0024/hello-amd64.sh
test0025-amd64-binary: M2-Planet | results
test/test0025/hello-amd64.sh
test0100-amd64-binary: M2-Planet | results
test/test0100/hello-amd64.sh
@ -732,6 +743,9 @@ test0023-armv7l-binary: M2-Planet | results
test0024-armv7l-binary: M2-Planet | results
test/test0024/hello-armv7l.sh
test0025-armv7l-binary: M2-Planet | results
test/test0025/hello-armv7l.sh
test0100-armv7l-binary: M2-Planet | results
test/test0100/hello-armv7l.sh
@ -831,6 +845,9 @@ test0023-x86-binary: M2-Planet | results
test0024-x86-binary: M2-Planet | results
test/test0024/hello-x86.sh
test0025-x86-binary: M2-Planet | results
test/test0025/hello-x86.sh
test0100-x86-binary: M2-Planet | results
test/test0100/hello-x86.sh

View File

@ -141,6 +141,10 @@ deb80d26727f797c397b927938360f846eb982dc0722903cb07947737332c443 test/results/t
0c93110e4cf2f8f69eb3a308162478e644ee63fd4881762161be5de50fbe9f06 test/results/test0024-knight-native-binary
4862ffbb8e6265bdbf3d5fd824f7b7581206a627f5e7fe83cf5b7f4b30b8ebd4 test/results/test0024-knight-posix-binary
69c060eae9ffb12992d89eb02f066c517c5acfd1d9f80152e279fccce7b77f6a test/results/test0024-x86-binary
1e0b151b972847e3ed249b64e6739d13ac4f09e933e6ce8efa59f6b3c7b0a3a9 test/results/test0025-aarch64-binary
73e0279fe7f43f647500ecbcc850a54fc377cc603fd61392a7349473a14e1770 test/results/test0025-amd64-binary
aa1dd88e35833e17b1fc3fa1f7caac72803836470347c4c91f266cb676477d3d test/results/test0025-armv7l-binary
169cc50975e9e36aa2efc2692f82f73b429298a17abbb66fcc73345096033531 test/results/test0025-x86-binary
65a25f4ee09e7ad4a0c44f6dad7e5a64aa1e62ac98d8f8d58d91a8b3a34ebe00 test/results/test0100-aarch64-binary
4d4d7dc249a4fc1788f4443a7a5feba51392a73dc1b80297a510cbca41afd71e test/results/test0100-amd64-binary
98ebb4ac496ed255d769383d027b0bf358f280b248a0994b30c047d2d5506ec4 test/results/test0100-armv7l-binary
@ -175,8 +179,8 @@ a2a83f42119e646b389b98647cf6cf2aa9597185997c9453db746178c8c4c0bf test/results/t
698853b79efb30865a663c4863c050639eb21c7400008f7840830503928973d4 test/results/test0106-knight-native-binary
45c2ba61dc209d7ffa39de9ff0f0a7f8f3ea4d7e38598c72f982fcaf9a05c84a test/results/test0106-knight-posix-binary
944580ff4aae38aafac139faf6eed5bfe4ff68b01a7a3adfa346de8803101182 test/results/test0106-x86-binary
26e9a6dd65d2cd9f7ad45b0dc145239b9317c77029c6b69faf922a9af65a0678 test/results/test1000-aarch64-binary
421f97cb2c0ea0fbdc6ac366a340657682bf1ed9f36f7edad9972f61eb9ec2a8 test/results/test1000-amd64-binary
7486674615d8165e5e0365070a1e20142c12d334db7393fbeafa46d14c5c74c5 test/results/test1000-armv7l-binary
8e7f3f18441a8f5762741f1362d6789098fbb8c9f7ac722b7036416e7e602b25 test/results/test1000-knight-posix-binary
02f26abda516b431ea6e01cfd135e45cd387fa71470495aba005bc121e23c425 test/results/test1000-x86-binary
2acc8a2f5317cb809e0d523cb997e8ab91f90ac95881288d874ed9156a3dcfb4 test/results/test1000-aarch64-binary
abac486fb04c067876058e16782f42e202f56cc8a013362e8a57743869f20aac test/results/test1000-amd64-binary
e575170b6ebf2c3c2fd130943f7c3b28c52c77e9c2a65dde13b471f92286f64f test/results/test1000-armv7l-binary
092ff610f2dc8d643545643d7d2a7423c37b8a88c4ffecd0f4df91e2210115a3 test/results/test1000-knight-posix-binary
1893b8088334aa54a06a4c90ff384b9f499684055e6911843651c2f14d8aacb0 test/results/test1000-x86-binary

23
test/test0025/.gitignore vendored Normal file
View File

@ -0,0 +1,23 @@
## Copyright (C) 2017 Jeremiah Orians
## 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/>.
# Ignore the files created by script
*.M1
*.hex2
proof
# A place to put a good run for comparison
actual.M1

46
test/test0025/array.c Normal file
View File

@ -0,0 +1,46 @@
/* Copyright (C) 2016 Jeremiah Orians
* 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/>.
*/
#include<stdio.h>
char global[32];
void write_string(char* c, FILE* f)
{
while(0 != c[0])
{
fputc(c[0], f);
c = c + 1;
}
}
void strcpy(char* a, char* b)
{
while(0 != b[0])
{
a[0] = b[0];
a = a + 1;
b = b + 1;
}
}
int main()
{
strcpy(global, "hello global\n");
write_string(global, stderr);
return 0;
}

21
test/test0025/cleanup.sh Executable file
View File

@ -0,0 +1,21 @@
#! /bin/sh
## Copyright (C) 2017 Jeremiah Orians
## 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/>.
rm -f test/test0025/array.M1
rm -f test/test0025/array.hex2
rm -f test/test0025/proof
exit 0

62
test/test0025/hello-aarch64.sh Executable file
View File

@ -0,0 +1,62 @@
#! /bin/sh
## Copyright (C) 2017 Jeremiah Orians
## Copyright (C) 2020 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
# Build the test
bin/M2-Planet --architecture aarch64 \
-f M2libc/AArch64/Linux/unistd.h \
-f M2libc/stdlib.c \
-f M2libc/AArch64/Linux/fcntl.h \
-f M2libc/stdio.c \
-f test/test0025/array.c \
--debug \
-o test/test0025/array.M1 || exit 1
# Build debug footer
blood-elf --64 -f test/test0025/array.M1 \
--entry _start \
-o test/test0025/array-footer.M1 || exit 2
# Macro assemble with libc written in M1-Macro
M1 -f M2libc/AArch64/aarch64_defs.M1 \
-f M2libc/AArch64/libc-full.M1 \
-f test/test0025/array.M1 \
-f test/test0025/array-footer.M1 \
--LittleEndian \
--architecture aarch64 \
-o test/test0025/array.hex2 || exit 2
# Resolve all linkages
hex2 -f M2libc/AArch64/ELF-aarch64-debug.hex2 \
-f test/test0025/array.hex2 \
--LittleEndian \
--architecture aarch64 \
--BaseAddress 0x400000 \
-o test/results/test0025-aarch64-binary \
--exec_enable || exit 3
# Ensure binary works if host machine supports test
if [ "$(get_machine ${GET_MACHINE_FLAGS})" = "aarch64" ]
then
. ./sha256.sh
# Verify that the resulting file works
./test/results/test0025-aarch64-binary > test/test0025/proof || exit 4
out=$(sha256_check test/test0025/proof.answer)
[ "$out" = "test/test0025/proof: OK" ] || exit 5
fi
exit 0

60
test/test0025/hello-amd64.sh Executable file
View File

@ -0,0 +1,60 @@
#! /bin/sh
## Copyright (C) 2017 Jeremiah Orians
## 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
bin/M2-Planet --architecture amd64 \
-f M2libc/amd64/Linux/unistd.h \
-f M2libc/stdlib.c \
-f M2libc/amd64/Linux/fcntl.h \
-f M2libc/stdio.c \
-f test/test0025/array.c \
--debug \
-o test/test0025/array.M1 || exit 1
# Build debug footer
blood-elf --64 -f test/test0025/array.M1 \
--entry _start \
-o test/test0025/array-footer.M1 || exit 2
# Macro assemble with libc written in M1-Macro
M1 -f M2libc/amd64/amd64_defs.M1 \
-f M2libc/amd64/libc-full.M1 \
-f test/test0025/array.M1 \
-f test/test0025/array-footer.M1 \
--LittleEndian \
--architecture amd64 \
-o test/test0025/array.hex2 || exit 2
# Resolve all linkages
hex2 -f M2libc/amd64/ELF-amd64-debug.hex2 \
-f test/test0025/array.hex2 \
--LittleEndian \
--architecture amd64 \
--BaseAddress 0x400000 \
-o test/results/test0025-amd64-binary \
--exec_enable || exit 3
# Ensure binary works if host machine supports test
if [ "$(get_machine ${GET_MACHINE_FLAGS})" = "amd64" ]
then
. ./sha256.sh
# Verify that the resulting file works
./test/results/test0025-amd64-binary > test/test0025/proof || exit 4
out=$(sha256_check test/test0025/proof.answer)
[ "$out" = "test/test0025/proof: OK" ] || exit 5
fi
exit 0

61
test/test0025/hello-armv7l.sh Executable file
View File

@ -0,0 +1,61 @@
#! /bin/sh
## Copyright (C) 2017 Jeremiah Orians
## 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
# Build the test
bin/M2-Planet --architecture armv7l \
-f M2libc/armv7l/Linux/unistd.h \
-f M2libc/stdlib.c \
-f M2libc/armv7l/Linux/fcntl.h \
-f M2libc/stdio.c \
-f test/test0025/array.c \
--debug \
-o test/test0025/array.M1 || exit 1
# Build debug footer
blood-elf -f test/test0025/array.M1 \
--entry _start \
-o test/test0025/array-footer.M1 || exit 2
# Macro assemble with libc written in M1-Macro
M1 -f M2libc/armv7l/armv7l_defs.M1 \
-f M2libc/armv7l/libc-full.M1 \
-f test/test0025/array.M1 \
-f test/test0025/array-footer.M1 \
--LittleEndian \
--architecture armv7l \
-o test/test0025/array.hex2 || exit 2
# Resolve all linkages
hex2 -f M2libc/armv7l/ELF-armv7l-debug.hex2 \
-f test/test0025/array.hex2 \
--LittleEndian \
--architecture armv7l \
--BaseAddress 0x400000 \
-o test/results/test0025-armv7l-binary \
--exec_enable || exit 3
# Ensure binary works if host machine supports test
if [ "$(get_machine ${GET_MACHINE_FLAGS})" = "armv7l" ]
then
. ./sha256.sh
# Verify that the resulting file works
./test/results/test0025-armv7l-binary > test/test0025/proof || exit 4
out=$(sha256_check test/test0025/proof.answer)
[ "$out" = "test/test0025/proof: OK" ] || exit 5
fi
exit 0

61
test/test0025/hello-x86.sh Executable file
View File

@ -0,0 +1,61 @@
#! /bin/sh
## Copyright (C) 2017 Jeremiah Orians
## 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
# Build the test
bin/M2-Planet --architecture x86 \
-f M2libc/x86/Linux/unistd.h \
-f M2libc/stdlib.c \
-f M2libc/x86/Linux/fcntl.h \
-f M2libc/stdio.c \
-f test/test0025/array.c \
--debug \
-o test/test0025/array.M1 || exit 1
# Build debug footer
blood-elf --64 -f test/test0025/array.M1 \
--entry _start \
-o test/test0025/array-footer.M1 || exit 2
# Macro assemble with libc written in M1-Macro
M1 -f M2libc/x86/x86_defs.M1 \
-f M2libc/x86/libc-full.M1 \
-f test/test0025/array.M1 \
-f test/test0025/array-footer.M1 \
--LittleEndian \
--architecture x86 \
-o test/test0025/array.hex2 || exit 2
# Resolve all linkages
hex2 -f M2libc/x86/ELF-i386-debug.hex2 \
-f test/test0025/array.hex2 \
--LittleEndian \
--architecture x86 \
--BaseAddress 0x400000 \
-o test/results/test0025-x86-binary \
--exec_enable || exit 3
# Ensure binary works if host machine supports test
if [ "$(get_machine ${GET_MACHINE_FLAGS})" = "x86" ]
then
. ./sha256.sh
# Verify that the resulting file works
./test/results/test0025-x86-binary > test/test0025/proof || exit 4
out=$(sha256_check test/test0025/proof.answer)
[ "$out" = "test/test0025/proof: OK" ] || exit 5
fi
exit 0

1
test/test0025/input Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 test/test0025/proof

View File

@ -1 +1 @@
61dde3b8a913296de7da40208428ff28ff741908ea9e98c7856626b19666288f test/test1000/proof
612c951c26337ca96dfcf50ccf79b9e94e9f3b083500d5897b4b7cdc66acbb6e test/test1000/proof