Skip to content

Commit 36bfffc

Browse files
author
David Gonzalez Martin
committed
Testing format, some basic programs
1 parent 40136cd commit 36bfffc

20 files changed

+1221
-988
lines changed

redflag.vcxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
<ClCompile Include="src\ir.c" />
1616
<ClCompile Include="src\lexer.c" />
1717
<ClCompile Include="src\llvm.c" />
18-
<ClCompile Include="src\llvm_codegen.c" />
1918
<ClCompile Include="src\main.c" />
2019
<ClCompile Include="src\parser.c" />
2120
<ClCompile Include="src\os.c" />
22-
<ClCompile Include="src\red_llvm.c" />
2321
<ClCompile Include="src\src_file.c" />
2422
<ClCompile Include="src\x64_backend.c" />
2523
</ItemGroup>
@@ -30,8 +28,6 @@
3028
<ClInclude Include="src\ir.h" />
3129
<ClInclude Include="src\lexer.h" />
3230
<ClInclude Include="src\llvm.h" />
33-
<ClInclude Include="src\llvm_codegen.h" />
34-
<ClInclude Include="src\red_llvm.h" />
3531
<ClInclude Include="src\os.h" />
3632
<ClInclude Include="src\parser.h" />
3733
<ClInclude Include="src\src_file.h" />

redflag.vcxproj.filters

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
<ClCompile Include="src\os.c" />
88
<ClCompile Include="src\src_file.c" />
99
<ClCompile Include="src\parser.c" />
10-
<ClCompile Include="src\llvm_codegen.c" />
1110
<ClCompile Include="src\x64_backend.c" />
1211
<ClCompile Include="src\ir.c" />
13-
<ClCompile Include="src\red_llvm.c" />
1412
<ClCompile Include="src\llvm.c" />
1513
</ItemGroup>
1614
<ItemGroup>
@@ -22,10 +20,8 @@
2220
<ClInclude Include="src\os.h" />
2321
<ClInclude Include="src\src_file.h" />
2422
<ClInclude Include="src\parser.h" />
25-
<ClInclude Include="src\llvm_codegen.h" />
2623
<ClInclude Include="src\ir.h" />
2724
<ClInclude Include="src\x64_backend.h" />
28-
<ClInclude Include="src\red_llvm.h" />
2925
<ClInclude Include="src\llvm.h" />
3026
</ItemGroup>
3127
<ItemGroup>

redflag.vcxproj.user

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
4-
<LocalDebuggerCommandArguments>test.red</LocalDebuggerCommandArguments>
4+
<LocalDebuggerCommandArguments>
5+
</LocalDebuggerCommandArguments>
56
<LocalDebuggerDebuggerType>NativeOnly</LocalDebuggerDebuggerType>
67
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
8+
<LocalDebuggerWorkingDirectory>$(ProjectDir)/test</LocalDebuggerWorkingDirectory>
79
</PropertyGroup>
810
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
9-
<LocalDebuggerCommandArguments>test.red</LocalDebuggerCommandArguments>
11+
<LocalDebuggerCommandArguments>
12+
</LocalDebuggerCommandArguments>
1013
<LocalDebuggerDebuggerType>NativeOnly</LocalDebuggerDebuggerType>
1114
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
15+
<LocalDebuggerWorkingDirectory>$(ProjectDir)/test</LocalDebuggerWorkingDirectory>
1216
</PropertyGroup>
1317
<PropertyGroup>
1418
<ShowAllFiles>true</ShowAllFiles>

src/bigint.c

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44

55
#include "bigint.h"
66

7+
static u64 BigInt_as_unsigned(const BigInt* bigint)
8+
{
9+
redassert(!bigint->is_negative);
10+
if (bigint->digit_count == 0)
11+
{
12+
return 0;
13+
}
14+
else if (bigint->digit_count == 1)
15+
{
16+
return bigint->digit;
17+
}
18+
else
19+
{
20+
RED_UNREACHABLE;
21+
return 0;
22+
}
23+
}
24+
725
static void BigInt_normalize(BigInt *dst)
826
{
927
const u64* digits = bigint_ptr(dst);
@@ -31,6 +49,78 @@ static void BigInt_normalize(BigInt *dst)
3149
}
3250
}
3351

52+
static u8 digit_to_char(u8 digit, bool uppercase)
53+
{
54+
if (digit <= 9)
55+
{
56+
return digit + '0';
57+
}
58+
else if (digit <= 35)
59+
{
60+
return (digit - 10) + (uppercase ? 'A' : 'a');
61+
}
62+
else
63+
{
64+
RED_UNREACHABLE;
65+
return 0;
66+
}
67+
}
68+
69+
static bool bit_at_index(const BigInt* bi, size_t index)
70+
{
71+
usize digit_index = index / 64;
72+
if (digit_index >= bi->digit_count)
73+
{
74+
return false;
75+
}
76+
77+
usize digit_bit_index = index % 64;
78+
const u64* digits = bigint_ptr(bi);
79+
u64 digit = digits[digit_bit_index];
80+
return ((digit >> digit_bit_index) & 0x1) == 0x1;
81+
}
82+
83+
size_t BigInt_clz(const BigInt* big_int, size_t bit_count)
84+
{
85+
if (big_int->is_negative || bit_count == 0)
86+
{
87+
return 0;
88+
}
89+
90+
if (big_int->digit_count == 0)
91+
{
92+
return bit_count;
93+
}
94+
95+
usize count = 0;
96+
97+
for (usize i = bit_count - 1;;)
98+
{
99+
if (bit_at_index(big_int, i))
100+
{
101+
return count;
102+
}
103+
104+
count++;
105+
106+
if (i == 0)
107+
{
108+
break;
109+
}
110+
i--;
111+
}
112+
113+
return count;
114+
}
115+
116+
size_t BigInt_bits_needed(const BigInt* op)
117+
{
118+
usize full_bits = op->digit_count * 64;
119+
usize leading_zero_count = BigInt_clz(op, full_bits);
120+
usize bits_needed = full_bits - leading_zero_count;
121+
return bits_needed + op->is_negative;
122+
}
123+
34124
void BigInt_init_unsigned(BigInt* dst, u64 x)
35125
{
36126
if (x == 0)
@@ -196,10 +286,175 @@ void BigInt_add(BigInt *dst, const BigInt *op1, const BigInt *op2)
196286
BigInt_normalize(dst);
197287
}
198288

289+
static void mul_overflow(u64 op1, u64 op2, u64* lo, u64* hi)
290+
{
291+
u64 u1 = (op1 & 0xffffffff);
292+
u64 v1 = (op2 & 0xffffffff);
293+
u64 t = (u1 * v1);
294+
u64 w3 = (t & 0xffffffff);
295+
u64 k = (t >> 32);
296+
297+
op1 >>= 32;
298+
t = (op1 * v1) + k;
299+
k = (t & 0xffffffff);
300+
u64 w1 = (t >> 32);
301+
302+
op2 >>= 32;
303+
t = (u1 * op2) + k;
304+
k = (t >> 32);
305+
306+
*hi = (op1 * op2) + w1 + k;
307+
*lo = (t << 32) + w3;
308+
}
309+
310+
void BigInt_shl(BigInt* dst, const BigInt* op1, const BigInt* op2)
311+
{
312+
redassert(!op2->is_negative);
313+
314+
if (op2->digit_count == 0)
315+
{
316+
BigInt_init_bigint(dst, op1);
317+
return;
318+
}
319+
320+
if (op2->digit_count != 1)
321+
{
322+
RED_UNREACHABLE;
323+
}
324+
325+
const u64* op1_digits = bigint_ptr(op1);
326+
u64 shift_amt = BigInt_as_unsigned(op2);
327+
328+
if (op1->digit_count == 1 && shift_amt < 64)
329+
{
330+
dst->digit = op1_digits[0] << shift_amt;
331+
if (dst->digit > op1_digits[0])
332+
{
333+
dst->digit_count = 1;
334+
dst->is_negative = op1->is_negative;
335+
return;
336+
}
337+
}
338+
339+
u64 digit_shift_count = shift_amt / 64;
340+
u64 leftover_shift_count = shift_amt % 64;
341+
342+
dst->digits = NEW(u64, op1->digit_count + digit_shift_count + 1);
343+
dst->digit_count = digit_shift_count;
344+
u64 carry = 0;
345+
346+
for (usize i = 0; i < op1->digit_count; i++)
347+
{
348+
u64 digit = op1_digits[i];
349+
dst->digits[dst->digit_count] = carry | (digit << leftover_shift_count);
350+
dst->digit_count++;
351+
352+
if (leftover_shift_count > 0)
353+
{
354+
carry = digit >> (64 - leftover_shift_count);
355+
}
356+
else
357+
{
358+
carry = 0;
359+
}
360+
}
361+
362+
dst->digits[dst->digit_count] = carry;
363+
dst->digit_count++;
364+
dst->is_negative = op1->is_negative;
365+
BigInt_normalize(dst);
366+
}
367+
368+
static void mul_scalar(BigInt* dst, const BigInt* op, u64 scalar)
369+
{
370+
BigInt_init_unsigned(dst, 0);
371+
372+
BigInt bi64;
373+
BigInt_init_unsigned(&bi64, 64);
374+
375+
const u64* op_digits = bigint_ptr(op);
376+
usize i = op->digit_count - 1;
377+
378+
for (;;)
379+
{
380+
BigInt shifted;
381+
BigInt_shl(&shifted, dst, &bi64);
382+
u64 result_scalar;
383+
u64 carry_scalar;
384+
385+
mul_overflow(scalar, op_digits[i], &result_scalar, &carry_scalar);
386+
387+
BigInt result;
388+
BigInt_init_unsigned(&result, result_scalar);
389+
390+
BigInt carry;
391+
BigInt_init_unsigned(&carry, carry_scalar);
392+
393+
BigInt carry_shifted;
394+
BigInt_shl(&carry_shifted, &carry, &bi64);
395+
396+
BigInt tmp;
397+
BigInt_add(&tmp, &shifted, &carry_shifted);
398+
399+
BigInt_add(dst, &tmp, &result);
400+
401+
if (i == 0)
402+
{
403+
break;
404+
}
405+
406+
i--;
407+
}
408+
}
199409

200410
void BigInt_mul(BigInt *dst, const BigInt *op1, const BigInt *op2)
201411
{
412+
if (op1->digit_count == 0 || op2->digit_count == 0)
413+
{
414+
return BigInt_init_unsigned(dst, 0);
415+
}
416+
417+
const u64* op1_digits = bigint_ptr(op1);
418+
const u64* op2_digits = bigint_ptr(op2);
419+
420+
u64 carry;
421+
mul_overflow(op1_digits[0], op2_digits[0], &dst->digit, &carry);
422+
if (carry == 0 && op1->digit_count == 1 && op2->digit_count == 1)
423+
{
424+
dst->is_negative = op1->is_negative != op2->is_negative;
425+
dst->digit_count = 1;
426+
BigInt_normalize(dst);
427+
return;
428+
}
429+
430+
BigInt_init_unsigned(dst, 0);
202431

432+
BigInt bi_64;
433+
434+
BigInt_init_unsigned(&bi_64, 0);
435+
436+
usize i = op2->digit_count - 1;
437+
438+
for (;;)
439+
{
440+
BigInt shifted;
441+
BigInt_shl(&shifted, dst, &bi_64);
442+
443+
BigInt scalar_result;
444+
mul_scalar(&scalar_result, op1, op2_digits[i]);
445+
446+
BigInt_add(dst, &scalar_result, &shifted);
447+
448+
if (i == 0)
449+
{
450+
break;
451+
}
452+
453+
i--;
454+
}
455+
456+
dst->is_negative = op1->is_negative != op2->is_negative;
457+
BigInt_normalize(dst);
203458
}
204459

205460
void BigInt_init_bigint(BigInt *dst, const BigInt *src)

src/compiler_types.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ typedef struct Symbol
538538

539539
typedef enum RedTypeKind
540540
{
541+
INVALID,
542+
VOID,
541543
PRIMITIVE,
542544
FUNCTION,
543545
//*****
@@ -559,6 +561,28 @@ typedef enum RedTypePrimitive
559561
RED_TYPE_PRIMITIVE_POINTER,
560562
} RedTypePrimitive;
561563

564+
static inline const char* primitive_type_str(RedTypePrimitive primitive_type_id)
565+
{
566+
switch (primitive_type_id)
567+
{
568+
CASE_TO_STR(RED_TYPE_PRIMITIVE_U8);
569+
CASE_TO_STR(RED_TYPE_PRIMITIVE_U16);
570+
CASE_TO_STR(RED_TYPE_PRIMITIVE_U32);
571+
CASE_TO_STR(RED_TYPE_PRIMITIVE_U64);
572+
CASE_TO_STR(RED_TYPE_PRIMITIVE_S8);
573+
CASE_TO_STR(RED_TYPE_PRIMITIVE_S16);
574+
CASE_TO_STR(RED_TYPE_PRIMITIVE_S32);
575+
CASE_TO_STR(RED_TYPE_PRIMITIVE_S64);
576+
CASE_TO_STR(RED_TYPE_PRIMITIVE_F32);
577+
CASE_TO_STR(RED_TYPE_PRIMITIVE_F64);
578+
CASE_TO_STR(RED_TYPE_PRIMITIVE_F128);
579+
CASE_TO_STR(RED_TYPE_PRIMITIVE_POINTER);
580+
default:
581+
RED_NOT_IMPLEMENTED;
582+
return null;
583+
}
584+
}
585+
562586
typedef struct RedType
563587
{
564588
RedTypeKind kind;
@@ -620,8 +644,8 @@ typedef struct CompoundStatement
620644
typedef struct BranchExpr
621645
{
622646
Node* condition;
623-
Node* true_block;
624-
Node* false_block;
647+
Node* if_block;
648+
Node* else_block;
625649
} BranchExpr;
626650

627651
typedef struct FnProto

0 commit comments

Comments
 (0)