Skip to content

Commit

Permalink
Add long integer support in native and byte code
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Oct 2, 2021
1 parent 3d9302c commit 30becb5
Show file tree
Hide file tree
Showing 19 changed files with 1,935 additions and 201 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ After four weeks, the compiler has now matured significantly. There are still s

### Language

* No long integer
* No struct function return
* Missing const checks for structs and enums
* Missing warnings for all kind of abuses

### Linker

* No explicit sections for code, data bss or stack
* No media file import

### Standard Libraries
Expand All @@ -49,9 +47,7 @@ After four weeks, the compiler has now matured significantly. There are still s

### Optimizing

* All global variables are considered volatile
* Simple loop opmtimization
* Poor bookeeping of callee saved registers
* Partial block domination analysis
* No register use for arguments
* Auto variables placed on fixed stack for known call sequence
Expand All @@ -74,13 +70,15 @@ The compiler is command line driven, and creates an executable .prg file.

* -i : additional include paths
* -o : optional output file name
* -cr : alternative runtime library, replaces the crt.c
* -rt : alternative runtime library, replaces the crt.c
* -e : execute the result in the integrated emulator
* -n : create pure native code for all functions
* -d : define a symbol (e.g. NOFLOAT to avoid float code in printf)
* -d : define a symbol (e.g. NOFLOAT or NOLONG to avoid float/long code in printf)

A list of source files can be provided.



## Implementation Details

The compiler does a full program compile, the linker step is part of the compilation. It knows all functions during the compilation run and includes only reachable code in the output. Source files are added to the build with the help of a pragma:
Expand Down
12 changes: 12 additions & 0 deletions autotest/autotest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ if %errorlevel% neq 0 goto :error
..\release\oscar64 -e -n testint16.c
if %errorlevel% neq 0 goto :error

..\release\oscar64 -e testint32.c
if %errorlevel% neq 0 goto :error

..\release\oscar64 -e -n testint32.c
if %errorlevel% neq 0 goto :error

..\release\oscar64 -e recursiontest.c
if %errorlevel% neq 0 goto :error

Expand Down Expand Up @@ -78,6 +84,12 @@ if %errorlevel% neq 0 goto :error
..\release\oscar64 -e -n testint16cmp.c
if %errorlevel% neq 0 goto :error

..\release\oscar64 -e testint32cmp.c
if %errorlevel% neq 0 goto :error

..\release\oscar64 -e -n testint32cmp.c
if %errorlevel% neq 0 goto :error

..\release\oscar64 -e floatstringtest.c
if %errorlevel% neq 0 goto :error

Expand Down
96 changes: 96 additions & 0 deletions autotest/bitshifttest.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,82 @@ void shr16n(unsigned xu, int xi)
#pragma native(shl16n)
#pragma native(shr16n)


void shl32b(unsigned long xu, long xi)
{
unsigned long ua[32];
long ia[32];
#assign s 0
#repeat
ua[s] = xu << s;
ia[s] = xi << s;
#assign s s + 1
#until s == 32

for(int i=0; i<32; i++)
{
assert(ua[i] == xu << i);
assert(ia[i] == xi << i);
}
}

void shr32b(unsigned long xu, long xi)
{
unsigned long ua[32];
long ia[32];
#assign s 0
#repeat
ua[s] = xu >> s;
ia[s] = xi >> s;
#assign s s + 1
#until s == 32

for(int i=0; i<32; i++)
{
assert(ua[i] == xu >> i);
assert(ia[i] == xi >> i);
}
}

void shl32n(unsigned long xu, long xi)
{
unsigned long ua[32];
long ia[32];
#assign s 0
#repeat
ua[s] = xu << s;
ia[s] = xi << s;
#assign s s + 1
#until s == 32

for(int i=0; i<32; i++)
{
assert(ua[i] == xu << i);
assert(ia[i] == xi << i);
}
}

void shr32n(unsigned long xu, long xi)
{
unsigned long ua[32];
long ia[32];
#assign s 0
#repeat
ua[s] = xu >> s;
ia[s] = xi >> s;
#assign s s + 1
#until s == 32

for(int i=0; i<32; i++)
{
assert(ua[i] == xu >> i);
assert(ia[i] == xi >> i);
}
}

#pragma native(shl32n)
#pragma native(shr32n)

int main(void)
{
for(int i=0; i<32; i++)
Expand Down Expand Up @@ -192,5 +268,25 @@ int main(void)
shr16n(0x1234, 0x1234);
shr16n(0xfedc, 0xfedc);

shl32b(0x00000000UL, 0x00000000L);
shl32b(0xffffffffUL, 0xffffffffL);
shl32b(0x12345678UL, 0x12345678L);
shl32b(0xfedcba98UL, 0xfedcba98L);

shr32b(0x00000000UL, 0x00000000L);
shr32b(0xffffffffUL, 0xffffffffL);
shr32b(0x12345678UL, 0x12345678L);
shr32b(0xfedcba98UL, 0xfedcba98L);

shl32n(0x00000000UL, 0x00000000L);
shl32n(0xffffffffUL, 0xffffffffL);
shl32n(0x12345678UL, 0x12345678L);
shl32n(0xfedcba98UL, 0xfedcba98L);

shr32n(0x00000000UL, 0x00000000L);
shr32n(0xffffffffUL, 0xffffffffL);
shr32n(0x12345678UL, 0x12345678L);
shr32n(0xfedcba98UL, 0xfedcba98L);

return 0;
}
123 changes: 123 additions & 0 deletions autotest/testint32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <assert.h>

void testmuli(long a, long b, long ab)
{
assert (a * b == ab);
}

void testdivi(long a, long b, long ab)
{
assert (a / b == ab);
}

void shltesti(long a, long b, long ab)
{
assert (a << b == ab);
}

void shrtesti(long a, long b, long ab)
{
assert (a >> b == ab);
}

long sieve(long size)
{
bool sieve[1000];

for(long i=0; i<size; i+=2)
{
sieve[i] = false;
sieve[i+1] = true;
}
sieve[2] = true;

for (long i = 3; i * i < size;)
{
long j = i * i;
while (j < size)
{
sieve[j] = false;
j += 2 * i;
}
do {i++;} while (i < size && !sieve[i]);
}

long num = 0;
for(long i=0; i<size; i++)
{
if (sieve[i])
num++;
}

return num;
}

int main(void)
{

testmuli(0, 0, 0);
testmuli(1, 0, 0);
testmuli(0, 1, 0);

testmuli( 1, 1, 1);
testmuli(-1, 1, -1);
testmuli(-1, -1, 1);
testmuli( 1, -1, -1);

testmuli(5, 5, 25);
testmuli( 127, 255, 32385);
testmuli(-127, 255, -32385);
testmuli( 127, -255, -32385);
testmuli(-127, -255, 32385);

testdivi( 1, 1, 1);
testdivi(-1, 1, -1);
testdivi( 1, -1, -1);
testdivi(-1, -1, 1);

testdivi( 11, 4, 2);
testdivi(-11, 4, -2);
testdivi( 11, -4, -2);
testdivi(-11, -4, 2);

shltesti( 17, 1, 34);
shltesti(-17, 1, -34);
shltesti( 1700, 1, 3400);
shltesti(-1700, 1, -3400);

shrtesti( 34, 1, 17);
shrtesti(-34, 1, -17);
shrtesti( 3400, 1, 1700);
shrtesti(-3400, 1, -1700);

shrtesti( -1, 15, -1);
shrtesti(32767, 15, 0);
shrtesti( -1, 14, -1);
shrtesti(32767, 14, 1);

shltesti( -1, 14, -16384);
shltesti( 1, 14, 16384);

assert(sieve(200) == 47);
assert(sieve(1000) == 169);

long a = 0, b = 0;
for(long i=0; i<10000; i++)
{
assert( 177 * i == a);
assert(-177 * i == b);
a += 177;
b -= 177;
}

long c = 0, d = 0;
for(long i=0; i<177; i++)
{
assert( 10000 * i == c);
assert(-10000 * i == d);
c += 10000;
d -= 10000;
}

return 0;
}
Loading

0 comments on commit 30becb5

Please sign in to comment.