Skip to content

Commit

Permalink
new generated c89 code
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Nov 29, 2024
1 parent d2a3f01 commit b0996a4
Show file tree
Hide file tree
Showing 45 changed files with 38,115 additions and 28,294 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ The C Programming language Second Edition 1988

# 🍰 Cake

_Cake is made by C programmers for C programmers._

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

# Differences from CFront

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

---

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

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

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

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

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

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



Expand Down
4 changes: 0 additions & 4 deletions manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,6 @@ https://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html
## C99 Transformations
C89 is the minimum target.
However the idea if C89 target is NOT support very old compilers, but generate code that can be compiled with C++.
C89
https://port70.net/~nsz/c/c89/c89-draft.html
Expand Down
5 changes: 3 additions & 2 deletions ownership.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Last Updated 15 Sept 2024
Last Updated 20 Nov 2024

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.

Expand Down Expand Up @@ -173,10 +173,11 @@ void x_destroy(_Opt struct X * p)
#### mutable

Note that this concept also could be applied for const members.
Note that this concept of constructor phase also could be applied for const members.

The introduction of a **mutable** qualifier allows certain exceptions to the usual contract
of immutability and non-nullability during transitional phases, such as in constructors and destructors.

This means that objects marked as **mutable** can temporarily violate their normal constraints,
such as modifying `const` members or assigning null to non-nullable pointers during these phases.

Expand Down
69 changes: 54 additions & 15 deletions src/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
WINDOWS
cl -DTEST build.c && build
Debugging on windows:
Debugging on windows:
cl /Zi build.c
devenv /DebugExe build.exe
Expand All @@ -22,17 +22,19 @@
" parser.h " \
" error.h " \
" fs.h " \
" flow_object.h " \
" object_flow.h " \
" hashmap.h " \
" osstream.h " \
" options.h " \
" token.h " \
" type.h " \
" pre_expressions.h " \
" object.h " \
" object.h " \
" expressions.h " \
" visit.h " \
" format_visit.h "
" visit_il.h " \
" visit_defer.h " \
" visit_fmt.h "


#define CAKE_SOURCE_FILES \
Expand All @@ -46,12 +48,14 @@
" object.c " \
" expressions.c " \
" pre_expressions.c " \
" flow_object.c " \
" object_flow.c " \
" parser.c " \
" visit_defer.c " \
" visit.c " \
" flow_visit.c " \
" visit_il.c " \
" visit_flow.c " \
" error.c " \
" format_visit.c " \
" visit_fmt.c " \
" type.c "

#define HOEDOWN_SOURCE_FILES \
Expand Down Expand Up @@ -142,7 +146,7 @@ int main()
execute_cmd(CC " -D_CRT_SECURE_NO_WARNINGS maketest.c " OUT_OPT "../maketest.exe");
execute_cmd(CC " -D_CRT_SECURE_NO_WARNINGS amalgamator.c " OUT_OPT "../amalgamator.exe");
execute_cmd(CC " -D_CRT_SECURE_NO_WARNINGS -I.. embed.c ../fs.c ../error.c " OUT_OPT "../embed.exe");

echo_chdir("./hoedown");

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

remove("maketest.exe");

execute_cmd(RUN "embed.exe \"./include\" " );
execute_cmd(RUN "embed.exe \"./include\" ");

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

#ifdef BUILD_WINDOWS_HLC
execute_cmd(CC CAKE_SOURCE_FILES " main.c "

#ifdef DISABLE_COLORS
" /DDISABLE_COLORS "
#endif

#if defined DEBUG
" /Od /MDd /RTC1 "
" /Dstrdup=_strdup" /*nao linka em release*/
#else // RELEASE
" /MT "
" /DNDEBUG "

#endif
" /D_CRT_NONSTDC_NO_WARNINGS "
" /wd4996 "
" /wd4100 " //unreferenced formal paramet
" /wd4068 " //unknown pragma
" /W4 "
#ifdef TEST
"-DTEST"
#endif
" /D_CRT_SECURE_NO_WARNINGS "
" /link "
" ucrt.lib "
" Kernel32.lib User32.lib Advapi32.lib"
" uuid.lib Ws2_32.lib Rpcrt4.lib Bcrypt.lib "
" /out:cake.exe");

//Runs cake on its own source
execute_cmd("cake.exe -sarif -sarif-path \"../vc/.sarif\" -ownership=enable -Wstyle -Wno-unused-parameter -Wno-unused-variable " CAKE_HEADER_FILES CAKE_SOURCE_FILES);

#endif

#ifdef BUILD_WINDOWS_MSC
execute_cmd("cl " CAKE_SOURCE_FILES " main.c "

execute_cmd(CC CAKE_SOURCE_FILES " main.c "

#ifdef DISABLE_COLORS
" /DDISABLE_COLORS "
Expand All @@ -193,7 +232,7 @@ int main()
" /utf-8 "
" /W4 "
" /Zi "
" /Gm- "
" /Gm- "
" /Zc:inline "
//" /WX " //Treats all compiler warnings as errors.
" /Gd "
Expand Down Expand Up @@ -253,13 +292,13 @@ int main()
"-DTEST"
#endif
" -Wall "
" -D_DEFAULT_SOURCE "
" -D_DEFAULT_SOURCE "
" -Wno-unknown-pragmas "
" -Wno-multichar "
" -std=c17 "
" -std=c17 "
" -o cake "
CAKE_SOURCE_FILES " main.c ");
CAKE_SOURCE_FILES " main.c ");
#endif

#if defined BUILD_LINUX_GCC || defined BUILD_WINDOWS_GCC || defined BUILD_MACOS_GCC
Expand Down
7 changes: 7 additions & 0 deletions src/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ int system_like(const char* command)
#define OUT_OPT " -o "
#define CC_DESCRIPTION "MINGW "

#elif defined __HLC__

#define BUILD_WINDOWS_HLC
#define CC "hlc "
#define OUT_OPT " -o "
#define CC_DESCRIPTION "HLC MSVC "

#elif defined _MSC_VER

#define BUILD_WINDOWS_MSC
Expand Down
12 changes: 12 additions & 0 deletions src/cakeconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//This was generated by running cake -autoconfig
//This file was generated reading the variable INCLUDE inside Visual Studio Command Prompt.
//echo %INCLUDE%
#pragma dir "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.40.33807/include/"
#pragma dir "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.40.33807/ATLMFC/include/"
#pragma dir "C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Auxiliary/VS/include/"
#pragma dir "C:/Program Files (x86)/Windows Kits/10/include/10.0.22000.0/ucrt/"
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//um/"
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//shared/"
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//winrt/"
#pragma dir "C:/Program Files (x86)/Windows Kits/10//include/10.0.22000.0//cppwinrt/"
#pragma dir "C:/Program Files (x86)/Windows Kits/NETFXSDK/4.8/include/um/"
34 changes: 32 additions & 2 deletions src/expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://github.com/thradams/cake
*/

#pragma safety enable
//#pragma safety enable

#include "ownership.h"
#include <limits.h>
Expand Down Expand Up @@ -1976,6 +1976,36 @@ struct expression* _Owner _Opt postfix_expression_type_name(struct parser_ctx* c
{
p_expression_node->expression_type = POSTFIX_EXPRESSION_COMPOUND_LITERAL;
p_expression_node->braced_initializer = braced_initializer(ctx);
p_expression_node->type = type_dup(&p_expression_node->type_name->type);
//TODO

struct object* _Owner _Opt p_object = make_object_ptr(&p_expression_node->type);
if (p_object == NULL)
{
compiler_diagnostic_message(C_ERROR_STRUCT_IS_INCOMPLETE, ctx, p_expression_node->first_token, NULL, "incomplete struct/union type");
throw;
}
p_expression_node->object = *p_object;
p_object = NULL;


bool is_constant = type_is_const(&p_expression_node->type) ||
p_expression_node->type.storage_class_specifier_flags & STORAGE_SPECIFIER_CONSTEXPR;

object_default_initialization(&p_expression_node->object, is_constant);

//printf("\n");
//object_print_to_debug(&p_init_declarator->p_declarator->object);

struct initializer initializer = {0};
initializer.braced_initializer = p_expression_node->braced_initializer;
initializer.first_token = p_expression_node->first_token;

initializer_init(ctx,
&p_expression_node->type,
&p_expression_node->object,
&initializer,
is_constant);
}

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

compiler_diagnostic_message(W_ERROR_INCOMPATIBLE_TYPES, ctx,
p_b_expression->first_token, NULL,
" incompatible types");
" incompatible types");
}

if (type_is_pointer(&lvalue_right_type) && type_is_pointer(&t2))
Expand Down
65 changes: 65 additions & 0 deletions src/f.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
The objective of this output is to generate a simple yet compatible
version of C89, which can serve as an intermediate language for backends.
*/

/*forward declarations*/
struct _iobuf;
struct __crt_locale_data;
struct __crt_locale_pointers;
struct __crt_multibyte_data;


/*declarations*/
struct _iobuf
{
void * _Placeholder;
};

struct __crt_locale_data;
struct __crt_locale_pointers
{
struct __crt_locale_data * locinfo;
struct __crt_multibyte_data * mbcinfo;
};

struct __crt_multibyte_data;




int __stdio_common_vfprintf(unsigned __int64 _Options, struct _iobuf * _Stream, char * _Format, struct __crt_locale_pointers * _Locale, char * _ArgList);

inline unsigned __int64 *__local_stdio_printf_options(void)
{
static unsigned __int64 _OptionsStorage;
return &_OptionsStorage;
}
inline int _vfprintf_l(struct _iobuf * _Stream, char * _Format, struct __crt_locale_pointers * _Locale, char * _ArgList)
{
return __stdio_common_vfprintf(( *__local_stdio_printf_options()),_Stream,_Format,_Locale,_ArgList);
}
struct _iobuf *__acrt_iob_func(unsigned int _Ix);
inline int printf(char * _Format, ...)
{
int _Result;
char * _ArgList;
((void)(_ArgList = (char *)(&(_Format)) + ((sizeof (_Format) + sizeof (int) - 1) & ~(sizeof (int) - 1))));
_Result = _vfprintf_l((__acrt_iob_func(1)),_Format,((void *)0),_ArgList);
((void)(_ArgList = (char *)0));
return _Result;
}
int isalnum(int _C);
char *setlocale(int _Category, char * _Locale);


int main(void)
{
unsigned char c = 223;
printf("isalnum('\\xdf') in default C locale returned %d\n", ! !isalnum(c));
if (setlocale(2,"de_DE.iso88591"))
{
printf("isalnum('\\xdf') in ISO-8859-1 locale returned %d\n", ! !isalnum(c));
}
}

22 changes: 6 additions & 16 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
#pragma safety enable
enum X : short {
A
};

int main() {
enum X x = A;
}

_Owner int socket();
void close(_Owner int fd);

int main()
{
_Owner int fd;

fd = socket();
if (fd < 0)
{
static_set(fd, "null");
static_debug(fd);
return 1;
}
close(fd);
}
Loading

0 comments on commit b0996a4

Please sign in to comment.