Lots of clean and enhancements to simplify task of writing it in assembly.

This commit is contained in:
Jeremiah Orians 2018-07-19 22:33:02 -04:00
parent 64531def3b
commit dc94afb558
No known key found for this signature in database
GPG Key ID: 7457821534D2ACCD
17 changed files with 8390 additions and 674 deletions

View File

@ -17,15 +17,38 @@
* Current
** Added
Added support for global intializers
Introduced base offset local and argument load/store
Added function pointer requirement into M2-Planet
Added test24 - get_machine
Added General_Recursion to reduce complexity
Added uniqueID to purge all code segments that recreate it's functionality
Added struct Char arrays for structs
** Changed
Made String naming 100% deterministic
Broke up all_expr to simplify any manual implementations
Cleaned up function calls
Simplify variable looksup
Reused a union to reduce globals
Brought back common recursion
Reorged logic tree to reduce complexity
Simplified expression
Changed EOF detection logic to deal with unsigned bits
** Fixed
Correct bug in how \" is treated
Clean up of & when && should have been used
Made test22 for consistent
Fixed !c->a regression
** Removed
Removed need for string copying in M2-Planet
Wasted steps in stack offsets
Need for stack relative offsets
Extra stack walks
Removed need for current_function
Removed unused values
Removed legacy x86 << >> hacks at the cost of more instructions
* 0.2 - 2018-06-21
** Added

3
cc.h
View File

@ -53,6 +53,7 @@ struct token_list
};
union
{
struct token_list* frame;
struct type* type;
char* filename;
};
@ -62,7 +63,7 @@ struct token_list
int linenumber;
};
struct token_list* locals;
int temps;
int depth;
};
/* What types we have */

1066
cc_core.c

File diff suppressed because it is too large Load Diff

View File

@ -151,7 +151,7 @@ reset:
goto reset;
}
}
else if(c < 0)
else if(c == EOF)
{
free(current);
return c;

View File

@ -18,6 +18,7 @@
#include "cc.h"
#include <stdint.h>
void line_error();
int numerate_string(char *a);
/* Initialize default types */
void initialize_types()
@ -50,35 +51,43 @@ void initialize_types()
c->size = 1;
c->type = c;
/* char** is char */
c->indirect = b;
b->indirect = c;
/* Define FILE */
/* Define char** */
struct type* d = calloc(1, sizeof(struct type));
d->name = "FILE";
d->name = "char**";
d->size = 4;
d->type = d;
/* FILE* has the same properties as FILE */
d->type = c;
d->indirect = d;
/* Define FUNCTION */
/*fix up indrects for chars */
c->indirect = b;
b->indirect = d;
/* Define FILE */
struct type* e = calloc(1, sizeof(struct type));
e->name = "FUNCTION";
e->name = "FILE";
e->size = 4;
e->type = e;
/* FUNCTION* has the same properties as FUNCTION */
/* FILE* has the same properties as FILE */
e->indirect = e;
/* Define FUNCTION */
struct type* f = calloc(1, sizeof(struct type));
f->name = "unsigned";
f->name = "FUNCTION";
f->size = 4;
f->type = f;
/* unsigned* has the same properties as unsigned */
/* FUNCTION* has the same properties as FUNCTION */
f->indirect = f;
/* Define FUNCTION */
struct type* g = calloc(1, sizeof(struct type));
g->name = "unsigned";
g->size = 4;
g->type = g;
/* unsigned* has the same properties as unsigned */
g->indirect = g;
/* Finalize type list */
f->next = g;
e->next = f;
d->next = e;
c->next = d;
@ -108,9 +117,22 @@ struct type* build_member(struct type* last, int offset)
struct type* member_type = type_name();
struct type* i = calloc(1, sizeof(struct type));
i->name = global_token->s;
global_token = global_token->next;
i->members = last;
i->size = member_type->size;
member_size = member_type->size;
/* Check to see if array */
if(match( "[", global_token->s))
{
global_token = global_token->next;
i->size = member_type->type->size * numerate_string(global_token->s);
global_token = global_token->next;
require_match("Struct only supports [num] form\n", "]");
}
else
{
i->size = member_type->size;
}
member_size = i->size;
i->type = member_type;
i->offset = offset;
return i;
@ -128,10 +150,10 @@ struct type* build_union(struct type* last, int offset)
{
size = member_size;
}
global_token = global_token->next;
require_match("ERROR in build_union\nMissing ;\n", ";");
}
member_size = size;
global_token = global_token->next;
return last;
}
@ -148,7 +170,7 @@ void create_struct()
global_types = head;
global_token = global_token->next;
i->size = 4;
require_match("ERROR in create_struct\x0A Missing {\x0A", "{");
require_match("ERROR in create_struct\n Missing {\n", "{");
struct type* last = NULL;
while('}' != global_token->s[0])
{
@ -161,12 +183,11 @@ void create_struct()
last = build_member(last, offset);
}
offset = offset + member_size;
global_token = global_token->next;
require_match("ERROR in create_struct\x0A Missing ;\x0A", ";");
require_match("ERROR in create_struct\n Missing ;\n", ";");
}
global_token = global_token->next;
require_match("ERROR in create_struct\x0A Missing ;\x0A", ";");
require_match("ERROR in create_struct\n Missing ;\n", ";");
head->size = offset;
head->members = last;
@ -198,7 +219,7 @@ struct type* type_name()
{
file_print("Unknown type ", stderr);
file_print(global_token->s, stderr);
file_print("\x0A", stderr);
file_print("\n", stderr);
line_error();
exit(EXIT_FAILURE);
}

21
gcc_req.h Normal file
View File

@ -0,0 +1,21 @@
/* Copyright (C) 2016 Jeremiah Orians
* This file is part of stage0.
*
* stage0 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.
*
* stage0 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 stage0. If not, see <http://www.gnu.org/licenses/>.
*/
// Exists only because gcc doesn't support naked Function pointers
// And thus adds just enough support that M2-Plant can leverage the feature
// in its self-host
typedef struct token_list* (*FUNCTION) (struct token_list*, struct token_list*);

View File

@ -18,6 +18,7 @@ M2-Planet-gcc: cc_reader.c cc_strings.c cc_core.c cc.c cc_types.c cc.h | bin
cc_core.c \
cc.c \
cc.h \
gcc_req.h \
-o bin/M2-Planet-gcc
M2-Planet: M2-Planet-gcc | bin results
@ -51,6 +52,7 @@ clean:
./test/test21/cleanup.sh
./test/test22/cleanup.sh
./test/test23/cleanup.sh
./test/test24/cleanup.sh
./test/test99/cleanup.sh
./test/test100/cleanup.sh
@ -86,6 +88,7 @@ test: test00-binary \
test21-binary \
test22-binary \
test23-binary \
test24-binary \
test99-binary \
test100-binary | results
sha256sum -c test/test.answers
@ -162,6 +165,9 @@ test22-binary: M2-Planet | results
test23-binary: M2-Planet | results
test/test23/hello.sh
test24-binary: M2-Planet | results
test/test24/hello.sh
test99-binary: M2-Planet | results
test/test99/hello.sh

View File

@ -23,8 +23,13 @@ DEFINE CALL_IMMEDIATE E8
DEFINE CMP 39C3
DEFINE COPY_eax_to_ebx 89C3
DEFINE COPY_eax_to_ecx 89C1
DEFINE COPY_ebx_to_eax 89D8
DEFINE COPY_ebx_to_edx 89DA
DEFINE COPY_ecx_to_ebp 89CD
DEFINE COPY_edi_to_ebp 89fd
DEFINE COPY_esp_to_ebp 89E5
DEFINE COPY_esp_to_ecx 89E1
DEFINE COPY_esp_to_edi 89E7
DEFINE DIVIDE_eax_by_ebx_into_eax F7FB
DEFINE INT_80 CD80
DEFINE JUMP E9
@ -32,6 +37,7 @@ DEFINE JUMP_EQ 0F84
DEFINE JUMP_NE 0F85
DEFINE JUMP_EQ8 74
DEFINE JUMP_NE8 75
DEFINE LOAD_BASE_ADDRESS_eax 8D85
DEFINE LOAD_BYTE 0FBE00
DEFINE LOAD_EFFECTIVE_ADDRESS 8D8424
DEFINE LOAD_EFFECTIVE_ADDRESS_ebx 8D9C24
@ -56,8 +62,12 @@ DEFINE NOT_eax F7D0
DEFINE OR_eax_ebx 09D8
DEFINE POP_eax 58
DEFINE POP_ebx 5B
DEFINE POP_ebp 5D
DEFINE POP_edi 5F
DEFINE PUSH_eax 50
DEFINE PUSH_ebx 53
DEFINE PUSH_ebp 55
DEFINE PUSH_edi 57
DEFINE RETURN C3
DEFINE SAL_eax_Immediate8 C1E0
DEFINE SAL_eax_cl D3E0

33
test/functions/uname.c Normal file
View File

@ -0,0 +1,33 @@
/* Copyright (C) 2016 Jeremiah Orians
* This file is part of stage0.
*
* stage0 is free software: you an 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.
*
* stage0 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 stage0. If not, see <http://www.gnu.org/licenses/>.
*/
struct utsname
{
char** sysname[65]; /* Operating system name (e.g., "Linux") */
char** nodename[65]; /* Name within "some implementation-defined network" */
char** release[65]; /* Operating system release (e.g., "2.6.28") */
char** version[65]; /* Operating system version */
char** machine[65]; /* Hardware identifier */
};
int uname(struct utsname* unameData)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
"LOAD_IMMEDIATE_eax %109"
"INT_80");
}

View File

@ -1,26 +1,27 @@
c359c568a0c31a0559ed2a9a8232d3d8f439903066e31dac843908b1d79008c6 test/results/test00-binary
85db8b71becd83c68331561eb565b7a187cf40019bb84dcd4142371901569489 test/results/test01-binary
968dca052b05c2b2e82eec4b0b453de1acd81e4a0a6d5178334a3212ee91db13 test/results/test02-binary
53501e16b035f635c68bd3202ac7d815e0a5d9ba7e1c3b76a6e25b6badf58ca5 test/results/test03-binary
fc50b56e4115369438187fa3e31a7ae7a20040e033afe0a3159b9e8e69205f7e test/results/test04-binary
bda47233fdac376fbd558e4917281d0f717bea4ed3f84f79244a78752e8d2192 test/results/test05-binary
a2c36f93cc736a5bc352c74d55bc3f029b8522f83a8422a06a3916a742ca5a2c test/results/test06-binary
d27eb315d694324650b11a421d6990eee60ac5921a5625bbccb43d806f09e156 test/results/test07-binary
8cc38294fb1261843cfc3956fad5a451c95bbc2ed687435d4e2d59df2c4a8567 test/results/test08-binary
cc8f252877a85c0d7832094ff574d7173ac33940917bc1591358b8970651a81c test/results/test09-binary
3857aee4183de41bd00b014d616a5d73f4bfc57aa60a6073bb4113d6ff2fb8d5 test/results/test10-binary
99d9fb3fb50b8f03576508f01819542c584ce4b623c1a4a7ef3ecd3b4c9c265b test/results/test100-binary
dce2f0b35323cf6a2b01f74a9335100f2d8626028af545832dbdb503573db0e5 test/results/test11-binary
88602970fa07b5da7a42b4f2b2486fe03accc6796e05453c4ab934e986790bef test/results/test12-binary
c85a57b5b1d65288efd47a3b12c6fca1efade9e7ec91e65efda5531d2c40d293 test/results/test13-binary
ef2b0fd3acd8f478f5f1ea51ab165503e2f22a4c330e5db134e162cf8b624cf8 test/results/test14-binary
ca2c1321cbcbf3f551860fc0857d0a816660ba541987f9ed7f92f8553cd6b06b test/results/test15-binary
7b0ae42a9ac9e8387900c1a95b3310e9c7fbe4f244d8d7522285dc1cef2f38ec test/results/test16-binary
15f1fc51d559b74d7515d3e978a835f7edc765f0763fdbae328f4b03a6b2b92b test/results/test17-binary
6405e331d3626c0ed1eca16eecc7fb628d8e1dc54dff3a6106e5f5bb063c896c test/results/test18-binary
33f7802c581d3b6382a1b63211564529419769a9788b5a5cac856e45b9eac57c test/results/test19-binary
6fa44153ee3f27f0df49b282c0bb3017dbfaea906073c8c02b8bf5ad4d4a7860 test/results/test20-binary
5eca573b21ddb66f85adc3b85a6faec6c2bf9c6bb6b814360631072f6348a8da test/results/test21-binary
29e782e265b0c2cb807ac94d15336be7785ddb9ebc4b175b5ef9241812a24235 test/results/test22-binary
5c288c9d788980e8c50b5261d05bae94a65950e3a14c8287f091e0278523a97e test/results/test23-binary
ba3d9623c6512bc327cf934d994e5327e5fad3f7500896e2d8458467fae9b9e9 test/results/test99-binary
12977ab3adfa80dd4e98068e3cf30a660bfbd5fc1c2e5ad4b33fb72ef8cb11a4 test/results/test00-binary
76b75af92dbaba02222ca1a2ae1ec28d8ea075828039821b445c2368ee31d46d test/results/test01-binary
02c8454596a4fddc5d659a547a12386bdde54f1d3e05df2558b0e89d676f2142 test/results/test02-binary
735ce831b4c0f9909977d659fe2b5f83920cfd38606a1d833cab4d89f9619398 test/results/test03-binary
62cafe3e96763862e802b83666b457ceaf4b3e4032b530349253b95c5c1e7bb3 test/results/test04-binary
2cbd9acf2e8069f5d93764e1fe235f0e1606a36e058bad8a976a6d4e97b4b8fe test/results/test05-binary
c921f545c7baebe05e1ce60c777d6d7782ba626fdf7520f576d3ee1b849a8bbf test/results/test06-binary
b45fae655b7f848b28ebdb8eb2e30ae789fbcf7920bc315395d53986bb1adae4 test/results/test07-binary
d511db73158a9544a5b5f828a79751e3de8a04b81c143fd0c146fc22c938aa9f test/results/test08-binary
6831ba0c4e01cea5fb524d811e75542875512fb417baa03d2515278d5b0ee6a5 test/results/test09-binary
ef179cd359ba1d61d45089e314cd4ac2069c8dc4dd7494d7c766344ea3c8cf88 test/results/test10-binary
21a87d7ec6f90ed4c47b9951aed7d28dff1dca20556c8ff4027921338ba2d55f test/results/test100-binary
5aaf399fe706d4a8c85c121c75ada29a65c293b57c98e8999961a2ef0bab0d62 test/results/test11-binary
4f8111e73e07255ae203963438c82ea8bcff7474e1594b52b426c58a03cb30eb test/results/test12-binary
dd74dabfdce8657ff440c1eef531cbf67a64854f2020d4d6bcb65c9cc2d199cb test/results/test13-binary
ed0960dc5809d8cda84e7df8bf02bd0cd2aeeba4df8c752d68af434408555f8c test/results/test14-binary
e216869c3fb06de7a41578517c797169e219b20a5697a822ba11eeef0d04f181 test/results/test15-binary
315ae5cc5c9d5bdcae0eddd55371128e53e3e9267a2a7c53832ed0af51693bea test/results/test16-binary
fdce9856f885418a7b2f69fc24a6cc0c85922313b49694d8030c544e4b2ad16f test/results/test17-binary
9a426972b6df90a158aebe3b8f3eb9ef8a63ce317d764afb92be4fce16542743 test/results/test18-binary
33b528e79793f7dc89490c386d96c9be82501a01ed795bb991b6404a472df4c8 test/results/test19-binary
48d845d20fff86183047342641cc8a6174e71c0ca004be882f0195a141bd64ea test/results/test20-binary
fd7bd7f28151e503d475ec59608ab3c7d0853e237255cf0a1e418694ff061d25 test/results/test21-binary
dffc0dbe1d99fd156ab406d0b71ce48e6d91072f75b8103a208510293e37735a test/results/test22-binary
c745adaa7c5ba2230877fc8d2137a87d25597212069660813460cb6d764de2a0 test/results/test23-binary
50215e4b4e2ce22a959ea7bcfc77c4d6ac45464455f5103afcaea0e84f9bf1d1 test/results/test24-binary
0013b8786068520e386a0cf2ce39c5145462439f17c264e62a9eddb1eed0433b test/results/test99-binary

View File

@ -1 +1 @@
4935363ad875dd93f002191f40123eca7af8061318b0a4f5b658fb760cd4a4bb test/test100/proof
d0dcdb8e9024afacbd25289838c5bd274c25447d5b7260e3052dfc77c9bc94aa test/test100/proof

View File

@ -45,7 +45,7 @@ then
# Verify that the resulting file works
./test/results/test22-binary -f test/common_x86/ELF-i386-debug.hex2 \
-f test/test22/hex2_linker.hex2 \
-f test/test22/test.hex2 \
--LittleEndian \
--Architecture 1 \
--BaseAddress 0x8048000 \

7689
test/test22/test.hex2 Normal file

File diff suppressed because it is too large Load Diff

7
test/test24/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# Ignore the files created by script
*.M1
*.hex2
proof
# A place to put a good run for comparison
actual.M1

6
test/test24/cleanup.sh Executable file
View File

@ -0,0 +1,6 @@
#! /bin/sh
rm -f test/test24/get_machine.M1
rm -f test/test24/get_machine-footer.M1
rm -f test/test24/get_machine.hex2
rm -f test/test24/proof
exit 0

34
test/test24/get_machine.c Normal file
View File

@ -0,0 +1,34 @@
/* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
/* Copyright (C) 2017 Jeremiah Orians
* This file is part of stage0.
*
* stage0 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.
*
* stage0 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 stage0. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
void file_print(char* s, FILE* f);
char* numerate_number(int a);
/* Standard C main program */
int main()
{
struct utsname* unameData = calloc(1, sizeof(struct utsname));
uname(unameData);
file_print(unameData->machine, stdout);
file_print("\n", stdout);
return EXIT_SUCCESS;
}

44
test/test24/hello.sh Executable file
View File

@ -0,0 +1,44 @@
#! /bin/sh
set -x
# Build the test
./bin/M2-Planet -f test/functions/exit.c \
-f test/functions/file.c \
-f test/functions/file_print.c \
-f test/functions/malloc.c \
-f test/functions/calloc.c \
-f test/functions/uname.c \
-f test/test24/get_machine.c \
--debug \
-o test/test24/get_machine.M1 || exit 1
# Build debug footer
blood-elf -f test/test24/get_machine.M1 \
-o test/test24/get_machine-footer.M1 || exit 2
# Macro assemble with libc written in M1-Macro
M1 -f test/common_x86/x86_defs.M1 \
-f test/functions/libc-core.M1 \
-f test/test24/get_machine.M1 \
-f test/test24/get_machine-footer.M1 \
--LittleEndian \
--Architecture 1 \
-o test/test24/get_machine.hex2 || exit 3
# Resolve all linkages
hex2 -f test/common_x86/ELF-i386-debug.hex2 \
-f test/test24/get_machine.hex2 \
--LittleEndian \
--Architecture 1 \
--BaseAddress 0x8048000 \
-o test/results/test24-binary \
--exec_enable || exit 4
# Ensure binary works if host machine supports test
if [ "$(get_machine)" = "x86_64" ]
then
# Verify that the compiled program returns the correct result
out=$(./test/results/test24-binary 2>&1 )
[ 0 = $? ] || exit 5
[ "$out" = "x86_64" ] || exit 6
fi
exit 0