Skip to content

Commit b0996a4

Browse files
committed
new generated c89 code
1 parent d2a3f01 commit b0996a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+38115
-28294
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ The C Programming language Second Edition 1988
1414

1515
# 🍰 Cake
1616

17-
_Cake is made by C programmers for C programmers._
18-
1917
Cake is a compiler front end written from scratch in C, designed from the C23 language specification.
2018
It allows you to translate newer versions of C, such as C23, to C99. Additionally, Cake provides a platform
2119
for experimenting with new features for the C language, including extensions like lambdas, defer and static [object lifetime](ownership.md) checks.
@@ -171,24 +169,29 @@ This compilation is useful for tracking errors together with the unit tests.
171169

172170
# Differences from CFront
173171

174-
CFront was the original compiler for C++ which converted C++ to C.
172+
Here's a polished version of your text:
173+
174+
---
175175

176-
CFront generated code was used only for direct compilation because it
177-
had all macros expanded making it useless to reuse the generated code in
178-
other platforms.
176+
CFront was the original compiler for C++, designed to convert C++ code into C.
179177

180-
Cake have two modes. One is for direct compilation (like CFront) and the other
181-
preserves macros includes etc.. making it suitable for distribution.
178+
The code generated by CFront was used solely for direct compilation because
179+
it had all macros expanded, making the generated output unsuitable for
180+
reuse on other platforms.
182181

183-
The other difference is that C++ is a second branch of evolution making C++ more
184-
compatible with C89 than C99.
182+
Cake offers two modes. The first is for direct compilation, similar to CFront.
183+
The second mode preserves macros, includes, and other preprocessor directives,
184+
making the output suitable for distribution.
185185

186-
The idea of Cake is to keep the main line of evolution of C and be always 100%
187-
compatible.
186+
Another key difference is that C++ represents a separate
187+
evolutionary branch, maintaining greater compatibility with C89
188+
than with C99.
188189

189-
The added extensions aims to keep the spirit of the language and implement proposed
190-
features in a way they can be experimented even before standardization.
190+
The goal of Cake is to follow the main evolutionary line of C and remain
191+
100% compatible with C.
191192

193+
The added extensions aim to uphold the spirit of the language while allowing
194+
developers to experiment with proposed features even before they become standardized.
192195

193196

194197

manual.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,6 @@ https://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html
241241
242242
## C99 Transformations
243243
244-
C89 is the minimum target.
245-
246-
However the idea if C89 target is NOT support very old compilers, but generate code that can be compiled with C++.
247-
248244
C89
249245
https://port70.net/~nsz/c/c89/c89-draft.html
250246

ownership.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Last Updated 15 Sept 2024
2+
Last Updated 20 Nov 2024
33

44
This is a work in progress. Cake source is currently being used to validate the concepts. It's in the process of transitioning to include annotated nullable checks, which was the last feature added.
55

@@ -173,10 +173,11 @@ void x_destroy(_Opt struct X * p)
173173
174174
#### mutable
175175

176-
Note that this concept also could be applied for const members.
176+
Note that this concept of constructor phase also could be applied for const members.
177177

178178
The introduction of a **mutable** qualifier allows certain exceptions to the usual contract
179179
of immutability and non-nullability during transitional phases, such as in constructors and destructors.
180+
180181
This means that objects marked as **mutable** can temporarily violate their normal constraints,
181182
such as modifying `const` members or assigning null to non-nullable pointers during these phases.
182183

src/build.c

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
WINDOWS
55
cl -DTEST build.c && build
66
7-
Debugging on windows:
7+
Debugging on windows:
88
cl /Zi build.c
99
devenv /DebugExe build.exe
1010
@@ -22,17 +22,19 @@
2222
" parser.h " \
2323
" error.h " \
2424
" fs.h " \
25-
" flow_object.h " \
25+
" object_flow.h " \
2626
" hashmap.h " \
2727
" osstream.h " \
2828
" options.h " \
2929
" token.h " \
3030
" type.h " \
3131
" pre_expressions.h " \
32-
" object.h " \
32+
" object.h " \
3333
" expressions.h " \
3434
" visit.h " \
35-
" format_visit.h "
35+
" visit_il.h " \
36+
" visit_defer.h " \
37+
" visit_fmt.h "
3638

3739

3840
#define CAKE_SOURCE_FILES \
@@ -46,12 +48,14 @@
4648
" object.c " \
4749
" expressions.c " \
4850
" pre_expressions.c " \
49-
" flow_object.c " \
51+
" object_flow.c " \
5052
" parser.c " \
53+
" visit_defer.c " \
5154
" visit.c " \
52-
" flow_visit.c " \
55+
" visit_il.c " \
56+
" visit_flow.c " \
5357
" error.c " \
54-
" format_visit.c " \
58+
" visit_fmt.c " \
5559
" type.c "
5660

5761
#define HOEDOWN_SOURCE_FILES \
@@ -142,7 +146,7 @@ int main()
142146
execute_cmd(CC " -D_CRT_SECURE_NO_WARNINGS maketest.c " OUT_OPT "../maketest.exe");
143147
execute_cmd(CC " -D_CRT_SECURE_NO_WARNINGS amalgamator.c " OUT_OPT "../amalgamator.exe");
144148
execute_cmd(CC " -D_CRT_SECURE_NO_WARNINGS -I.. embed.c ../fs.c ../error.c " OUT_OPT "../embed.exe");
145-
149+
146150
echo_chdir("./hoedown");
147151

148152
execute_cmd(CC HOEDOWN_SOURCE_FILES OUT_OPT "../../hoedown.exe");
@@ -161,14 +165,49 @@ int main()
161165

162166
remove("maketest.exe");
163167

164-
execute_cmd(RUN "embed.exe \"./include\" " );
168+
execute_cmd(RUN "embed.exe \"./include\" ");
165169

166170
execute_cmd(RUN "amalgamator.exe -olib.c" CAKE_SOURCE_FILES);
167171
remove("amalgamator.exe");
168172

173+
#ifdef BUILD_WINDOWS_HLC
174+
execute_cmd(CC CAKE_SOURCE_FILES " main.c "
175+
176+
#ifdef DISABLE_COLORS
177+
" /DDISABLE_COLORS "
178+
#endif
179+
180+
#if defined DEBUG
181+
" /Od /MDd /RTC1 "
182+
" /Dstrdup=_strdup" /*nao linka em release*/
183+
#else // RELEASE
184+
" /MT "
185+
" /DNDEBUG "
186+
187+
#endif
188+
" /D_CRT_NONSTDC_NO_WARNINGS "
189+
" /wd4996 "
190+
" /wd4100 " //unreferenced formal paramet
191+
" /wd4068 " //unknown pragma
192+
" /W4 "
193+
#ifdef TEST
194+
"-DTEST"
195+
#endif
196+
" /D_CRT_SECURE_NO_WARNINGS "
197+
" /link "
198+
" ucrt.lib "
199+
" Kernel32.lib User32.lib Advapi32.lib"
200+
" uuid.lib Ws2_32.lib Rpcrt4.lib Bcrypt.lib "
201+
" /out:cake.exe");
202+
203+
//Runs cake on its own source
204+
execute_cmd("cake.exe -sarif -sarif-path \"../vc/.sarif\" -ownership=enable -Wstyle -Wno-unused-parameter -Wno-unused-variable " CAKE_HEADER_FILES CAKE_SOURCE_FILES);
205+
206+
#endif
169207

170208
#ifdef BUILD_WINDOWS_MSC
171-
execute_cmd("cl " CAKE_SOURCE_FILES " main.c "
209+
210+
execute_cmd(CC CAKE_SOURCE_FILES " main.c "
172211

173212
#ifdef DISABLE_COLORS
174213
" /DDISABLE_COLORS "
@@ -193,7 +232,7 @@ int main()
193232
" /utf-8 "
194233
" /W4 "
195234
" /Zi "
196-
" /Gm- "
235+
" /Gm- "
197236
" /Zc:inline "
198237
//" /WX " //Treats all compiler warnings as errors.
199238
" /Gd "
@@ -253,13 +292,13 @@ int main()
253292
"-DTEST"
254293
#endif
255294
" -Wall "
256-
" -D_DEFAULT_SOURCE "
295+
" -D_DEFAULT_SOURCE "
257296
" -Wno-unknown-pragmas "
258297
" -Wno-multichar "
259-
" -std=c17 "
260-
298+
" -std=c17 "
299+
261300
" -o cake "
262-
CAKE_SOURCE_FILES " main.c ");
301+
CAKE_SOURCE_FILES " main.c ");
263302
#endif
264303

265304
#if defined BUILD_LINUX_GCC || defined BUILD_WINDOWS_GCC || defined BUILD_MACOS_GCC

src/build.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ int system_like(const char* command)
4747
#define OUT_OPT " -o "
4848
#define CC_DESCRIPTION "MINGW "
4949

50+
#elif defined __HLC__
51+
52+
#define BUILD_WINDOWS_HLC
53+
#define CC "hlc "
54+
#define OUT_OPT " -o "
55+
#define CC_DESCRIPTION "HLC MSVC "
56+
5057
#elif defined _MSC_VER
5158

5259
#define BUILD_WINDOWS_MSC

src/cakeconfig.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//This was generated by running cake -autoconfig
2+
//This file was generated reading the variable INCLUDE inside Visual Studio Command Prompt.
3+
//echo %INCLUDE%
4+
#pragma dir "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.40.33807/include/"
5+
#pragma dir "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.40.33807/ATLMFC/include/"
6+
#pragma dir "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Auxiliary/VS/include/"
7+
#pragma dir "C:/Program Files (x86)/Windows Kits/10/include/10.0.22000.0/ucrt/"
8+
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//um/"
9+
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//shared/"
10+
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//winrt/"
11+
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//cppwinrt/"
12+
#pragma dir "C:/Program Files (x86)/Windows Kits/NETFXSDK/4.8/include/um/"

src/expressions.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* https://github.com/thradams/cake
44
*/
55

6-
#pragma safety enable
6+
//#pragma safety enable
77

88
#include "ownership.h"
99
#include <limits.h>
@@ -1976,6 +1976,36 @@ struct expression* _Owner _Opt postfix_expression_type_name(struct parser_ctx* c
19761976
{
19771977
p_expression_node->expression_type = POSTFIX_EXPRESSION_COMPOUND_LITERAL;
19781978
p_expression_node->braced_initializer = braced_initializer(ctx);
1979+
p_expression_node->type = type_dup(&p_expression_node->type_name->type);
1980+
//TODO
1981+
1982+
struct object* _Owner _Opt p_object = make_object_ptr(&p_expression_node->type);
1983+
if (p_object == NULL)
1984+
{
1985+
compiler_diagnostic_message(C_ERROR_STRUCT_IS_INCOMPLETE, ctx, p_expression_node->first_token, NULL, "incomplete struct/union type");
1986+
throw;
1987+
}
1988+
p_expression_node->object = *p_object;
1989+
p_object = NULL;
1990+
1991+
1992+
bool is_constant = type_is_const(&p_expression_node->type) ||
1993+
p_expression_node->type.storage_class_specifier_flags & STORAGE_SPECIFIER_CONSTEXPR;
1994+
1995+
object_default_initialization(&p_expression_node->object, is_constant);
1996+
1997+
//printf("\n");
1998+
//object_print_to_debug(&p_init_declarator->p_declarator->object);
1999+
2000+
struct initializer initializer = {0};
2001+
initializer.braced_initializer = p_expression_node->braced_initializer;
2002+
initializer.first_token = p_expression_node->first_token;
2003+
2004+
initializer_init(ctx,
2005+
&p_expression_node->type,
2006+
&p_expression_node->object,
2007+
&initializer,
2008+
is_constant);
19792009
}
19802010

19812011
if (ctx->previous == NULL)
@@ -5865,7 +5895,7 @@ void check_assigment(struct parser_ctx* ctx,
58655895

58665896
compiler_diagnostic_message(W_ERROR_INCOMPATIBLE_TYPES, ctx,
58675897
p_b_expression->first_token, NULL,
5868-
" incompatible types");
5898+
" incompatible types");
58695899
}
58705900

58715901
if (type_is_pointer(&lvalue_right_type) && type_is_pointer(&t2))

src/f.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
The objective of this output is to generate a simple yet compatible
3+
version of C89, which can serve as an intermediate language for backends.
4+
*/
5+
6+
/*forward declarations*/
7+
struct _iobuf;
8+
struct __crt_locale_data;
9+
struct __crt_locale_pointers;
10+
struct __crt_multibyte_data;
11+
12+
13+
/*declarations*/
14+
struct _iobuf
15+
{
16+
void * _Placeholder;
17+
};
18+
19+
struct __crt_locale_data;
20+
struct __crt_locale_pointers
21+
{
22+
struct __crt_locale_data * locinfo;
23+
struct __crt_multibyte_data * mbcinfo;
24+
};
25+
26+
struct __crt_multibyte_data;
27+
28+
29+
30+
31+
int __stdio_common_vfprintf(unsigned __int64 _Options, struct _iobuf * _Stream, char * _Format, struct __crt_locale_pointers * _Locale, char * _ArgList);
32+
33+
inline unsigned __int64 *__local_stdio_printf_options(void)
34+
{
35+
static unsigned __int64 _OptionsStorage;
36+
return &_OptionsStorage;
37+
}
38+
inline int _vfprintf_l(struct _iobuf * _Stream, char * _Format, struct __crt_locale_pointers * _Locale, char * _ArgList)
39+
{
40+
return __stdio_common_vfprintf(( *__local_stdio_printf_options()),_Stream,_Format,_Locale,_ArgList);
41+
}
42+
struct _iobuf *__acrt_iob_func(unsigned int _Ix);
43+
inline int printf(char * _Format, ...)
44+
{
45+
int _Result;
46+
char * _ArgList;
47+
((void)(_ArgList = (char *)(&(_Format)) + ((sizeof (_Format) + sizeof (int) - 1) & ~(sizeof (int) - 1))));
48+
_Result = _vfprintf_l((__acrt_iob_func(1)),_Format,((void *)0),_ArgList);
49+
((void)(_ArgList = (char *)0));
50+
return _Result;
51+
}
52+
int isalnum(int _C);
53+
char *setlocale(int _Category, char * _Locale);
54+
55+
56+
int main(void)
57+
{
58+
unsigned char c = 223;
59+
printf("isalnum('\\xdf') in default C locale returned %d\n", ! !isalnum(c));
60+
if (setlocale(2,"de_DE.iso88591"))
61+
{
62+
printf("isalnum('\\xdf') in ISO-8859-1 locale returned %d\n", ! !isalnum(c));
63+
}
64+
}
65+

src/file.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
#pragma safety enable
1+
enum X : short {
2+
A
3+
};
24

5+
int main() {
6+
enum X x = A;
7+
}
38

4-
_Owner int socket();
5-
void close(_Owner int fd);
6-
7-
int main()
8-
{
9-
_Owner int fd;
109

11-
fd = socket();
12-
if (fd < 0)
13-
{
14-
static_set(fd, "null");
15-
static_debug(fd);
16-
return 1;
17-
}
18-
close(fd);
19-
}

0 commit comments

Comments
 (0)