From 3f87432d36160f2724796eb2016ecdb02de0f5e9 Mon Sep 17 00:00:00 2001 From: Gtker Date: Fri, 17 Jan 2025 19:39:29 +0100 Subject: [PATCH 1/4] Add support for west-const in function arguments West-const is `const int` whereas east-const is `int const`. They mean the same thing and both can be present at the same time (`const int const` means the same). This doesn't allow west-const in local/global variables. --- cc_types.c | 6 ++++++ test/test.answers | 14 +++++++------- test/test0014/basic_args.c | 2 +- test/test1000/proof.answer | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cc_types.c b/cc_types.c index 64c86fc9..c0581ad8 100644 --- a/cc_types.c +++ b/cc_types.c @@ -299,6 +299,12 @@ struct type* type_name(void) require(NULL != global_token, "unfinished type definition in extern\n"); } + if(match("const", global_token->s)) + { + global_token = global_token->next; + require(NULL != global_token, "unfinished type definition in const\n"); + } + if(match("struct", global_token->s)) { global_token = global_token->next; diff --git a/test/test.answers b/test/test.answers index d88bcf75..d70810c3 100644 --- a/test/test.answers +++ b/test/test.answers @@ -287,10 +287,10 @@ e9fa237a2c4d945490563795fee3adeb394171d65a6025c952c818941d1b0ee6 test/results/t e6bb2bfffabcb09bdf681fc63d03d039484e4ce3a8f6dcb4d616d6d02dc3d8c9 test/results/test0106-riscv32-binary ee0044cd67ad632f99d532f1cbfca9ac6c29f2e07eedf28a71110bd67edc6fae test/results/test0106-riscv64-binary d731bc662f99ba7413a10b112662083a3e1bf31e959e08dc84b57b23f3ef31a7 test/results/test0106-x86-binary -3a5f3fd5c4f8ff2f6770cb3d7484537f2defd306071323020f78a93b60be973f test/results/test1000-aarch64-binary -1dbb08aec01b951819b03eb3feb253da3d67b021609d756569a9885cde1dfcdf test/results/test1000-amd64-binary -60a9c2c9678d805576ed6a84a20807f4a3ee64dded3e1b4839caf6ebc493828d test/results/test1000-armv7l-binary -9946ce4f52de35f6234833a5fd382632b97be438dccf67db1f41d2e60998b013 test/results/test1000-knight-posix-binary -4a071222fc53e3270dc197eebe21d91439c8af510497bb0e24535da09a4bdfc5 test/results/test1000-riscv32-binary -e50eebfa59b5eea601554f12243bb1d0fee0d382f0652d6c681bc43a36aa68eb test/results/test1000-riscv64-binary -e355316450658199c0ce764dde449953dc4c10aa6e2b081c7c8c96fb0b7786f8 test/results/test1000-x86-binary +33da326c081aec467970777f3f0398a323a660ffd1eac4e58da497d133f48c94 test/results/test1000-aarch64-binary +7eabc4fa17cd826253666db749b29fb8ff6d8ae4bfd201b7fe240fb8234d0762 test/results/test1000-amd64-binary +7b9653534d46da6f2bccf4be2ab1dc6a643b3cec4ea3fb78a083fdb1f9b2ee7d test/results/test1000-armv7l-binary +b6fd07541fe69bc5b9376fa7ab98e3de44d6dcfc73ec48663e829624f027ab26 test/results/test1000-knight-posix-binary +101127c452a65f1489bc7ed687446769626db87a55e07d919f10324c7165754b test/results/test1000-riscv32-binary +37637351854a6163aff980fdf13f843abc83124e742c786b7a07f6ab840fc322 test/results/test1000-riscv64-binary +e83770287ee672fb3b91f9b597ed6cb4a95a08e1fee0adbf02974c9cd6e65588 test/results/test1000-x86-binary diff --git a/test/test0014/basic_args.c b/test/test0014/basic_args.c index c0b4fa2f..304211a3 100644 --- a/test/test0014/basic_args.c +++ b/test/test0014/basic_args.c @@ -17,7 +17,7 @@ #include #include -void write_string(char* a) +void write_string(const char* a) { while(0 != a[0]) { diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer index 4328a488..4d37ab7f 100644 --- a/test/test1000/proof.answer +++ b/test/test1000/proof.answer @@ -1 +1 @@ -e17a29adcf2dddd2b270da9b078303242e49cc32990f6875fe2fd8e009a9eb05 test/test1000/proof +56f3c36219f77119146207d170d962e87ba66ab5284070800620a2519e3c4986 test/test1000/proof From 5f22f14b273f7976f324bf147df43c01f8bba15b Mon Sep 17 00:00:00 2001 From: Gtker Date: Fri, 17 Jan 2025 20:54:31 +0100 Subject: [PATCH 2/4] Add support for west-const for variables West-const is `const int` whereas east-const is `int const`. They mean the same thing and both can be present at the same time (`const int const` means the same). Global variables that are initialized do not seem to work for knight-native failing with `Target label 1 is not valid` when running `hex2`. --- cc_core.c | 3 ++- test/test.answers | 30 +++++++++++++++--------------- test/test0028/assignment.c | 8 ++++++-- test/test1000/proof.answer | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/cc_core.c b/cc_core.c index 69f6cdd4..0ae210d0 100644 --- a/cc_core.c +++ b/cc_core.c @@ -2711,7 +2711,8 @@ void statement(void) global_token = global_token->next; } else if((NULL != lookup_type(global_token->s, prim_types)) || - match("struct", global_token->s)) + match("struct", global_token->s) || + match("const", global_token->s)) { collect_local(); } diff --git a/test/test.answers b/test/test.answers index d70810c3..09711668 100644 --- a/test/test.answers +++ b/test/test.answers @@ -209,14 +209,14 @@ fb69ed45b3dbb9da3a2425cde3acbc10746b90d367d998cd22fb495cd8498735 test/results/t 6881582a03436d319d498ee7e8507dac10ddeef51c99a3c3972eaeb4ff8efc57 test/results/test0027-riscv32-binary 0d1c90e68389fa6d23eac22aa3cdc990772eb0c0d9a7faa5e135b45162d38b4d test/results/test0027-riscv64-binary 04e75e83bcea38ba913117c4a4fd1dac5264321aa34eb40a934d6596b6bf7978 test/results/test0027-x86-binary -889558c55dc9900546957d789f72e7322dc6b34b03678f99be9165017e0c35b1 test/results/test0028-aarch64-binary -85e061d85dc5a89c1419d1fe5544c9faecc874629e8e10f92f9fd250adcf38a4 test/results/test0028-amd64-binary -cddae954ec3486707f9c7ea6643093c71cdf31feef6db33ad359432bffcc8252 test/results/test0028-armv7l-binary -7818c4382355883e524b3b5d54d0b8a57b7a627f8b858d22cac231ed96b6a549 test/results/test0028-knight-native-binary -9ed5dc1fb8881d2abb44ebb323d3f5656e60972e2b30f59e7cbc395f20a794fd test/results/test0028-knight-posix-binary -d9d621632b1a50de329b38a39cfff4a18abe47f2cd89a497345f4792407612b9 test/results/test0028-riscv32-binary -cd74ccf3a4cb56fe52c0106da92a8befd8628993efd9a74765125ff132653533 test/results/test0028-riscv64-binary -5d48068861e37244acbab80d2a59e5c4f947b2d638d6b5788e473c587ecbc674 test/results/test0028-x86-binary +e7a27a9ad2f740bde6cf935a6f8946fcda4b56a07c4db3597fdfe9b0a5cf62b7 test/results/test0028-aarch64-binary +e838d696f395c47265acc284a52171445b0f182545ae76868259838fc31a1c2c test/results/test0028-amd64-binary +924075757b04c31849ef849db242a03219d17fbf16be742e8c1f2192cf6b9fdc test/results/test0028-armv7l-binary +ca46f103807d43f4e582c2d4785693664dfcf8a3dcc7a84b81dd831b82c2de55 test/results/test0028-knight-native-binary +18a267639ed743a229ce1d2f90f568cccc29d3e6d48c4b0bf3b1d6e713dade4b test/results/test0028-knight-posix-binary +ec23867d7f738134fef47dbd37ab677da1660d433cb33cef89d2b34db67d96e5 test/results/test0028-riscv32-binary +d8a2960f6250fcc684011251005f8d291f8ec99f44aaee64fbcb5425188175c1 test/results/test0028-riscv64-binary +1ee9516f1f2a40cba30328edbf1bde251279161c5d9dd8608bbfaf785b1277a7 test/results/test0028-x86-binary e369398fe85d31d4f20171fbad3a354e03a5e7000e51f3fb40926dbe21300cf6 test/results/test0029-aarch64-binary 4481e92fd98af077c0f8f9ae2d6849a077979657ff7dab58c0bfacbb34c9cb20 test/results/test0029-amd64-binary b7bf774fa05b78b5abfbe90b1a4e3775e997f61ab586b6bf9a54a030045e356e test/results/test0029-armv7l-binary @@ -287,10 +287,10 @@ e9fa237a2c4d945490563795fee3adeb394171d65a6025c952c818941d1b0ee6 test/results/t e6bb2bfffabcb09bdf681fc63d03d039484e4ce3a8f6dcb4d616d6d02dc3d8c9 test/results/test0106-riscv32-binary ee0044cd67ad632f99d532f1cbfca9ac6c29f2e07eedf28a71110bd67edc6fae test/results/test0106-riscv64-binary d731bc662f99ba7413a10b112662083a3e1bf31e959e08dc84b57b23f3ef31a7 test/results/test0106-x86-binary -33da326c081aec467970777f3f0398a323a660ffd1eac4e58da497d133f48c94 test/results/test1000-aarch64-binary -7eabc4fa17cd826253666db749b29fb8ff6d8ae4bfd201b7fe240fb8234d0762 test/results/test1000-amd64-binary -7b9653534d46da6f2bccf4be2ab1dc6a643b3cec4ea3fb78a083fdb1f9b2ee7d test/results/test1000-armv7l-binary -b6fd07541fe69bc5b9376fa7ab98e3de44d6dcfc73ec48663e829624f027ab26 test/results/test1000-knight-posix-binary -101127c452a65f1489bc7ed687446769626db87a55e07d919f10324c7165754b test/results/test1000-riscv32-binary -37637351854a6163aff980fdf13f843abc83124e742c786b7a07f6ab840fc322 test/results/test1000-riscv64-binary -e83770287ee672fb3b91f9b597ed6cb4a95a08e1fee0adbf02974c9cd6e65588 test/results/test1000-x86-binary +1c27f1b811509e39035d6c9a18090c9ea2d9844f53b7f56b843e421afded2558 test/results/test1000-aarch64-binary +330fd30fd995ade25c60b87a3a6c1bbef19d0a1f99b3810b56e95d0e046f2b74 test/results/test1000-amd64-binary +2a7468ce8cd16d1edf37b1750b5f78cae45777d884b6a6c028499f900ed072b3 test/results/test1000-armv7l-binary +9ee0b557b568cc3e62c9df0d0b75f53cc3a5906948cd05cae2099f99b84ed2d0 test/results/test1000-knight-posix-binary +e765bf3961463adb8da900309a5c0e9200e486afde8dce0c25dfefeb928b6b67 test/results/test1000-riscv32-binary +077922fd5eaf5e0ab588f7bd9f1af8776ac19293753f588c41efbd9ec1f71e73 test/results/test1000-riscv64-binary +9a98eab4bbee19eb4f1ca1e56a9eaf5d72e45e4cd57c07b05d8319a8d0956e6c test/results/test1000-x86-binary diff --git a/test/test0028/assignment.c b/test/test0028/assignment.c index 60d74247..49712cc3 100644 --- a/test/test0028/assignment.c +++ b/test/test0028/assignment.c @@ -15,10 +15,14 @@ * along with M2-Planet. If not, see . */ +const int unused; + int main() { + const int one = 1; + const int three = 3; int a = 44; - a += 1; - a *= 3; + a += one; + a *= three; a /= 4; a %= 7; a <<= 8; diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer index 4d37ab7f..65b3e9e1 100644 --- a/test/test1000/proof.answer +++ b/test/test1000/proof.answer @@ -1 +1 @@ -56f3c36219f77119146207d170d962e87ba66ab5284070800620a2519e3c4986 test/test1000/proof +3ac3701b3629e29275d53fca8481bda33cd976776323bf08c851c4ed31563ae7 test/test1000/proof From 75697711b7add3af5f882a1265123af5353dcee8 Mon Sep 17 00:00:00 2001 From: gtker Date: Fri, 17 Jan 2025 23:19:43 +0100 Subject: [PATCH 3/4] Add _Bool type As requested in https://github.com/oriansj/M2libc/pull/67. The standard (C17) says in 6.2.5.2: ```text An object declared as type _Bool is large enough to store the values 0 and 1 ``` It could technically be an int but all major compilers have `sizeof(bool) == 1`. --- cc_types.c | 4 ++++ test/test.answers | 14 +++++++------- test/test1000/proof.answer | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cc_types.c b/cc_types.c index c0581ad8..d89ca1b0 100644 --- a/cc_types.c +++ b/cc_types.c @@ -122,6 +122,10 @@ void initialize_types(void) hold = new_primitive("char", "char*", "char**", 1, TRUE); prim_types = add_primitive(hold); + /* Define _Bool */ + hold = new_primitive("_Bool", "_Bool*", "_Bool**", 1, TRUE); + prim_types = add_primitive(hold); + /* Define FUNCTION */ hold = new_primitive("FUNCTION", "FUNCTION*", "FUNCTION**", register_size, FALSE); prim_types = add_primitive(hold); diff --git a/test/test.answers b/test/test.answers index 09711668..5548b500 100644 --- a/test/test.answers +++ b/test/test.answers @@ -287,10 +287,10 @@ e9fa237a2c4d945490563795fee3adeb394171d65a6025c952c818941d1b0ee6 test/results/t e6bb2bfffabcb09bdf681fc63d03d039484e4ce3a8f6dcb4d616d6d02dc3d8c9 test/results/test0106-riscv32-binary ee0044cd67ad632f99d532f1cbfca9ac6c29f2e07eedf28a71110bd67edc6fae test/results/test0106-riscv64-binary d731bc662f99ba7413a10b112662083a3e1bf31e959e08dc84b57b23f3ef31a7 test/results/test0106-x86-binary -1c27f1b811509e39035d6c9a18090c9ea2d9844f53b7f56b843e421afded2558 test/results/test1000-aarch64-binary -330fd30fd995ade25c60b87a3a6c1bbef19d0a1f99b3810b56e95d0e046f2b74 test/results/test1000-amd64-binary -2a7468ce8cd16d1edf37b1750b5f78cae45777d884b6a6c028499f900ed072b3 test/results/test1000-armv7l-binary -9ee0b557b568cc3e62c9df0d0b75f53cc3a5906948cd05cae2099f99b84ed2d0 test/results/test1000-knight-posix-binary -e765bf3961463adb8da900309a5c0e9200e486afde8dce0c25dfefeb928b6b67 test/results/test1000-riscv32-binary -077922fd5eaf5e0ab588f7bd9f1af8776ac19293753f588c41efbd9ec1f71e73 test/results/test1000-riscv64-binary -9a98eab4bbee19eb4f1ca1e56a9eaf5d72e45e4cd57c07b05d8319a8d0956e6c test/results/test1000-x86-binary +f8a998e12f1c9f310485de2a580360b7f7a983aa75d34c7320d8bf42c427f6bc test/results/test1000-aarch64-binary +c0df4059b3964e143c6df9cc2fcc76adc6bdba7d2c012149e31c91d2ee9869fc test/results/test1000-amd64-binary +df17bf73f027d0a824df5c12b6feaa8ea795e0c8ad2547e618fd117970b52773 test/results/test1000-armv7l-binary +b44c0a66ccb57ba7ea504b5a864aadd8a28bd0a53c80ec3a8f039d32823b4e8e test/results/test1000-knight-posix-binary +e8b235af255d4e576cfa0773291e1ac2a8b431ca09477188c5c32471efef8ddf test/results/test1000-riscv32-binary +3851d9d51d4d0c86f58db3a0c0817607c3be0e85155ada82672e6dc01c2adf25 test/results/test1000-riscv64-binary +d634d26d3d00f0f529190a68871791cd9a4e032def85ade46d318c0c12481b8d test/results/test1000-x86-binary diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer index 65b3e9e1..39213f84 100644 --- a/test/test1000/proof.answer +++ b/test/test1000/proof.answer @@ -1 +1 @@ -3ac3701b3629e29275d53fca8481bda33cd976776323bf08c851c4ed31563ae7 test/test1000/proof +51a59b17d1858b5503cbb31fc6e3cae97d556c82d02e1978bcf276d07061fa83 test/test1000/proof From 6a8c9be5b6d4a5c6aeacaa9bb9a2063d4aac4890 Mon Sep 17 00:00:00 2001 From: gtker Date: Sat, 18 Jan 2025 00:44:42 +0100 Subject: [PATCH 4/4] Change test0028 spaces to tabs --- test/test0028/assignment.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test0028/assignment.c b/test/test0028/assignment.c index 49712cc3..908275a6 100644 --- a/test/test0028/assignment.c +++ b/test/test0028/assignment.c @@ -18,8 +18,8 @@ const int unused; int main() { - const int one = 1; - const int three = 3; + const int one = 1; + const int three = 3; int a = 44; a += one; a *= three;