🤯🤯🤯🤯🤯🤯🤯🤯🤯

This commit is contained in:
Scarlett
2025-05-04 23:54:42 -04:00
parent 8b0d191409
commit 0fc796aa25
32 changed files with 867 additions and 1021 deletions

44
genx.sh
View File

@ -44,17 +44,57 @@ if [ ! -d "binaries" ]; then
mkdir -p binaries
fi
appendStd() {
if [ ! -f "$1" ]; then
echo "File not found: $1"
return 1
fi
backup_file="${1}.bak"
cp "$1" "$backup_file"
temp_file=$(mktemp)
while IFS= read -r line || [ -n "$line" ]; do
echo "$line" >> "$temp_file"
if [[ $line =~ ^#include\ \"[^\"]+\" ]]; then
include_file=$(echo "$line" | sed -n 's/.*"\([^"]*\)".*/\1/p')
if [[ $include_file == "std.alpha" ]]; then
echo -e "${GREEN}[GenX] ${WHITE}Including ${BLUE}std.alpha${WHITE}..."
if [ -f "library/std.alpha" ]; then
cat library/std.alpha >> "$temp_file"
else
echo -e "${RED}[GenX] ${YELLOW}File library/std.alpha not found!${WHITE}"
rm "$temp_file"
return 1
fi
fi
fi
done < "$1"
mv "$temp_file" "$1"
}
if [ $# -eq 1 ]; then
if [ -f "$1" ]; then
if [[ "$1" == *.alpha ]]; then
appendStd "$1"
./alpha -cg "$1"
backup_file="${1}.bak"
if [ -f "$backup_file" ]; then
mv "$backup_file" "$1"
echo -e "${GREEN}[GenX] ${WHITE}Reverted $1 to its original state."
fi
filename=$(basename -- "$1")
filename="${filename:0:${#filename}-6}"
s_name="out/${filename}.s"
echo -e "${GREEN}[GenX] ${WHITE}Generated .s file..."
echo -e "${GREEN}[GenX] ${WHITE}Generated .s file."
gcc ${s_name} library/alpha_lib_reg.s library/alpha_driver.s -no-pie -o binaries/${filename}
if [ $? -eq 0 ]; then
echo -e "${GREEN}[GenX] ${WHITE}Generated executable file..."
echo -e "${GREEN}[GenX] ${WHITE}Generated executable file."
echo -e "${GREEN}[GenX] ${WHITE}Executable file: binaries/${filename}"
echo -e "${GREEN}[GenX] ${WHITE}Done!"
else

View File

@ -1,61 +0,0 @@
# Makefile to make executables from alpha source code files
# and also make executables from some sample assembly files execising the alpha library
#
# Carl Alphonce
# April 20, 2024
# The alpha compiler and flags (adjust the flags as needed)
AC := ./alpha
AFLAGS := -tok -asc -tc -st -ir -cg
# The preprocessor and flags (you should not adjust these)
CPP := cpp
CPPFLAGS := -P -x c
# Adjust for the library your team is using (register-based or stack-based parameter passing)
ALPHA_LIB = alpha_lib_reg.s ## Register-based parameter passing
#ALPHA_LIB = alpha_lib_st.s ## Stack-based parameter passing
# Adjust for the parameter passing approach your compiler uses:
# alpha_driver_reg.s for register-based parameter passing
# alpha_driver_st.s for stack-based parameter passing
# This file provides a main function that packages up argv[1] (or "") as an alpha string
# (type 1->character) and calls entry with that argument
ALPHA_DRIVER = alpha_driver_reg.s ## Register-based parameter passing
#ALPHA_DRIVER = alpha_driver_st.s ## Stack-based parameter passing
# Create an assembly (.s) file from an alpha source file
# This involves several steps:
%.s : %.alpha
@mv $< $<._temporary_ # 1. rename input file so we can us it as
@$(CPP) $(CPPFLAGS) $<._temporary_ > $< # 2. input to CPP, writing output to original filename
@$(AC) $(AFLAGS) $< # 3. run the alpha compiler on the pre-processed file
@mv $<._temporary_ $< # 4. restore the original input file
# Examples of assembly code using the alpha library files
# In these examples the calling code is in assembly, and defines main (so the driver is not included here)
# Example #1: calling the printBoolean function
printBoolean : printBoolean_reg.s
@gcc $< $(ALPHA_LIB) -no-pie -o $@
# Example #2: calling the printInt function
printInt : printInt_reg.s
@gcc $< $(ALPHA_LIB) -no-pie -o $@
# Example #3: calling the reserve and release functions
reserve_release : reserve_release_reg.s
@gcc $< $(ALPHA_LIB) -no-pie -o $@
# The rule for assembling .s files and linking them together (using the gcc compiler driver)
# to produce an executable (assuming no earlier make rule triggers first)
% : %.s $(ALPHA_LIB) $(ALPHA_DRIVER)
@gcc $< $(ALPHA_LIB) $(ALPHA_DRIVER) -no-pie -o $@

View File

@ -1,26 +0,0 @@
.file "printBoolean.c"
.text
.globl main
.type main, @function
main:
.LFB0:
pushq %rbp
movq %rsp, %rbp
movb $1, %dil # the representation of 'true'
call printBoolean
movb $10, %dil
call printCharacter
movb $0, %dil # the representation of 'false'
call printBoolean
movb $10, %dil
call printCharacter
movl $0, %eax
popq %rbp
ret
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.4.0"
.section .note.GNU-stack,"",@progbits

View File

@ -1,22 +0,0 @@
.file "printInt.c"
.text
.globl main
.type main, @function
main:
.LFB0:
pushq %rbp
movq %rsp, %rbp
movl $10, %edi # Move the immediate value 10 to %edi
call printInteger # call the alpha_lib function printInteger
movl $10, %edi # Put the \n character (decimal 10) into %edi
call printCharacter # then call the alpha_lib function printCharacter
movl $0, %eax
popq %rbp
ret
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.4.0"
.section .note.GNU-stack,"",@progbits

View File

@ -1,37 +0,0 @@
.file "reserve_release.c"
.text
.globl main
.type main, @function
main:
.LFB0:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $8, %edi # move sizeof(*a) into %edi
call reserve # call alpha_lib_reg function reserve
movq %rax, -8(%rbp) # put the returned pointer on the stack at offset -8
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movl $20, (%rax) # put value 20 into location pointed at by %rax --> (*a).x
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movl $45, 4(%rax) # put value 45 into location 4(%rax) --> (*a).y
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movl 4(%rax), %eax # move (*a).y into %eax
movl %eax, %edi # and then into %edi
call printInteger # call alpha_lib_reg function printInteger
movl $10, %edi # move '\n' into %edi
call printCharacter # call alpha_lib_reg function printCharacter
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movq %rax, %rdi # and then into %rdi
call release # call alpha_lib_reg function reserve
movl $0, %eax
leave
ret
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.4.0"
.section .note.GNU-stack,"",@progbits

24
library/std.alpha Normal file
View File

@ -0,0 +1,24 @@
(* Standard Alpha Library - Provided by Carl *)
type string: 1 -> character
type BooleanXBoolean: [Boolean: x; Boolean: y]
type characterXcharacter: [character: x; character: y]
type integerXinteger: [integer: x; integer: y]
type Boolean2Boolean: Boolean -> Boolean
type integer2integer: integer -> integer
type character2integer: character -> integer
type Boolean2integer: Boolean -> integer
type string2integer: string -> integer
type integerXinteger2integer: integerXinteger -> integer
type integerXinteger2Boolean: integerXinteger -> Boolean
type characterXcharacter2Boolean: characterXcharacter -> Boolean
type BooleanXBoolean2Boolean: BooleanXBoolean -> Boolean
type integer2address: integer -> address
type address2integer: address -> integer
external function printInteger: integer2integer
external function printCharacter: character2integer
external function printBoolean: Boolean2integer
function entry: string2integer

View File

@ -82,8 +82,7 @@ int generate(){
case E_FUNC_START:
generateFunctionStart(i);
break;
default:
;
default:;
}
i = i->next;
}
@ -133,7 +132,6 @@ CGNode *addCG(TableNode *tn, int sp) {
return cg;
}
int generateLabel(Instruction *inst) {
if (inst == NULL) {
return -1;
@ -162,7 +160,6 @@ int generateAdd(Instruction *inst) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -486,7 +483,6 @@ int generateAssign(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL) {
printdebug("generateAssign failed, NULL operand");
return -1;
@ -607,7 +603,6 @@ int generateEqualTo(Instruction *inst){
return 0;
}
int generateCall(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
if (op1 == NULL) {
@ -630,7 +625,6 @@ int generateCall(Instruction *inst){
// return -1;
//}
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
//now for the return
@ -643,13 +637,11 @@ int generateCall(Instruction *inst){
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#store return from call\n", getAddress(cg));
return 0;
}
int generateReturn(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg;
if (op1 == NULL) {
printdebug("generateReturn failed, NULL operand");
return -1;

View File

@ -36,7 +36,6 @@ void * S_Pop(Stack *s) {
return r;
}
void *S_Peek(Stack *s) {
if (s == NULL || S_IsEmpty(s)) {
return NULL;
@ -81,7 +80,6 @@ void emit_backpatch(Stack * s, int l){
char *temp = NULL;
/*
TODO: this is here to bring your attention to the comment bellow.
check if start is NULL if it is assign it to the start globle variable
@ -186,8 +184,7 @@ void emit_binary_op(
Op op,
TableNode *result,
TNodeOrConst *arg1,
TNodeOrConst * arg2
){
TNodeOrConst *arg2) {
emit_helper();
current->opcode = op;
// TODO: create temp and remove result from param list
@ -264,11 +261,13 @@ void emit_conditional_jump(Op condition, int label, ...){
TNodeOrConst *n1;
TNodeOrConst *n2;
switch (condition) {
case E_IF_X_TRUE: case E_IF_X_FALSE:
case E_IF_X_TRUE:
case E_IF_X_FALSE:
n1 = va_arg(argptr, TNodeOrConst *);
current->operand1 = n1;
break;
case E_LESS_THAN: case E_EQUAL_TO:
case E_LESS_THAN:
case E_EQUAL_TO:
n1 = va_arg(argptr, TNodeOrConst *);
n2 = va_arg(argptr, TNodeOrConst *);
current->operand1 = n1;
@ -293,8 +292,7 @@ void emit_parameter(TNodeOrConst * param){
void emit_function_call(
TableNode *result,
int param_count,
TNodeOrConst * name
){
TNodeOrConst *name) {
emit_helper();
current->opcode = E_CALL;
current->operand1 = name;
@ -411,15 +409,13 @@ void emit_as_file(FILE * out_file, Instruction * i){
fprintf(out_file,
"%4.d: func : %s\n",
i->index,
getName(i->result)
);
getName(i->result));
break;
case E_LABEL:
fprintf(out_file,
"%4.d: Label : %d\n",
i->index,
i->label
);
i->label);
break;
case E_ADD:
fprintf(out_file,
@ -427,8 +423,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_SUB:
fprintf(out_file,
@ -436,8 +431,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_MUL:
fprintf(out_file,
@ -445,8 +439,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_DIV:
fprintf(out_file,
@ -454,8 +447,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_MOD:
fprintf(out_file,
@ -463,8 +455,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_OR:
fprintf(out_file,
@ -472,8 +463,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_AND:
fprintf(out_file,
@ -481,55 +471,48 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_NEG:
fprintf(out_file,
"%4.d: %s = -%s\n",
i->index,
getName(i->result),
get_string(i->operand1)
);
get_string(i->operand1));
break;
case E_NOT:
fprintf(out_file,
"%4.d: %s = !%s\n",
i->index,
getName(i->result),
get_string(i->operand1)
);
get_string(i->operand1));
break;
case E_ASSIGN:
fprintf(out_file,
"%4.d: %s = %s\n",
i->index,
getName(i->result),
get_string(i->operand1)
);
get_string(i->operand1));
break;
case E_GOTO:
fprintf(out_file,
"%4.d: GOTO : %d\n",
i->index,
i->label
);
i->label);
break;
case E_IF_X_TRUE:
fprintf(out_file,
"%4.d: if %s True GOTO %d\n",
i->index,
get_string(i->operand1),
i->label
);
i->label);
break;
case E_IF_X_FALSE:
fprintf(out_file,
"%4.d: if %s False GOTO %d\n",
i->index,
get_string(i->operand1),
i->label
);
i->label);
break;
case E_LESS_THAN:
// this feels wrong I need to TODO: this
@ -538,8 +521,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_EQUAL_TO:
// this feels wrong I need to TODO: this
@ -548,30 +530,26 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_CALL:
fprintf(out_file,
"%4.d: call : %s %s\n",
i->index,
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_PARAM:
fprintf(out_file,
"%4.d: param %s \n",
i->index,
get_string(i->operand1)
);
get_string(i->operand1));
break;
case E_RETURN:
fprintf(out_file,
"%4.d: return : %s\n",
i->index,
get_string(i->operand1)
);
get_string(i->operand1));
break;
case E_INDEX_COPY_RIGHT:
fprintf(out_file,
@ -579,8 +557,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
);
get_string(i->operand2));
break;
case E_INDEX_COPY_LEFT:
fprintf(out_file,
@ -595,32 +572,26 @@ void emit_as_file(FILE * out_file, Instruction * i){
"%4.d: %s = &%s\n",
i->index,
getName(i->result),
get_string(i->operand1)
);
get_string(i->operand1));
break;
case E_DEREF_RIGHT:
fprintf(out_file,
"%4.d: %s = *%s\n",
i->index,
getName(i->result),
get_string(i->operand1)
);
get_string(i->operand1));
case E_DEREF_LEFT:
fprintf(out_file,
"%4.d: *%s = %s\n",
i->index,
getName(i->result),
get_string(i->operand1)
);
get_string(i->operand1));
}
emit_as_file(out_file, i->next);
}
TableNode *getTN(TNodeOrConst *tnc) {
if (tnc->d == NODE) {
return tnc->tnc_union->node;

View File

@ -39,7 +39,6 @@ typedef union TNConstUnion TNConstUnion;
typedef struct Instruction Instruction;
typedef struct TNodeOrConst TNodeOrConst;
// these are from page 364
typedef enum { // these are from page 364
@ -106,7 +105,6 @@ typedef struct Instruction {
Instruction *next;
} Instruction;
// NOTE We are not using this We are using the Stack api
typedef struct TFList {
Instruction *i;
@ -121,14 +119,12 @@ typedef struct TFList {
// - backpatch(p,i) function to fill in jump targets
// void bp_temp(int n);
extern Instruction *begin;
extern Instruction *current;
extern int label_count;
extern bool code_gen;
extern FILE *ir_flag;
TNodeOrConst *tn_or_const(Discriminant, void *);
void emit_binary_op(Op op, TableNode *result, TNodeOrConst *arg1, TNodeOrConst *arg2);
void emit_unary_op(Op op, TableNode *result, TNodeOrConst *arg);
@ -152,13 +148,10 @@ void emit_goto(int i);
void emit_detach();
void emit_push_all(Stack *s);
int getLabel(Instruction *i);
TableNode *getTN(TNodeOrConst *tnc);
int getConst(TNodeOrConst *tnc);
TNodeOrConst *getOperand1(Instruction *i);
TNodeOrConst *getOperand2(Instruction *i);
TableNode *getResult(Instruction *i);

View File

@ -25,7 +25,8 @@ int line_number = 1;
int column_number = 1;
int yycolumn = 1;
#define YY_USER_ACTION { \
#define YY_USER_ACTION \
{ \
yylloc.first_line = yylineno; \
yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; \

View File

@ -103,7 +103,6 @@ int run(FILE *alpha) {
int token;
top = cur = init(CreateScope(NULL, 1, 1));
// If file is not found
if (alpha == NULL) {
fprintf(stderr, "INPUT FILE NOT FOUND\n");

View File

@ -1545,7 +1545,13 @@ SymbolTable *getParent(SymbolTable *st) {
ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; }
SymbolTable *getFirstChild(ListOfTable *lt) { return lt->table; }
ListOfTable *getRestOfChildren(ListOfTable *lt) { return lt->next; }
TableNode *getFirstEntry(SymbolTable *st) { return st->entries; }
TableNode *getFirstEntry(SymbolTable *st) {
if (st == NULL || st->entries == NULL) {
printdebug("passed a NULL symbol table to getFirstEntry");
return undefined;
}
return st->entries;
}
// Segfaults when passed an invalid table node!
TableNode *getNextEntry(TableNode *tn) {

View File

@ -0,0 +1 @@
(* TODO: Prints out fib sequence. *)

View File

@ -0,0 +1 @@
(* TODO: creates a linked list and prints out the chain. *)

View File

@ -1,35 +1,14 @@
(* TEST: [-asc -tc -cg -ir] *)
type string: 1 -> character
type BooleanXBoolean: [Boolean: x; Boolean: y]
type characterXcharacter: [character: x; character: y]
type integerXinteger: [integer: x; integer: y]
type Boolean2Boolean: Boolean -> Boolean
type integer2integer: integer -> integer
type character2integer: character -> integer
type Boolean2integer0: Boolean -> integer
type string2integer: string -> integer
type integerXinteger2integer: integerXinteger -> integer
type integerXinteger2Boolean: integerXinteger -> Boolean
type characterXcharacter2Boolean: characterXcharacter -> Boolean
type BooleanXBoolean2Boolean: BooleanXBoolean -> Boolean
type integer2address: integer -> address
type address2integer: address -> integer
external function printInteger: integer2integer
external function printCharacter: character2integer
external function printBoolean: Boolean2integer
function entry: string2integer
#include "std.alpha"
entry (arg) := {
[integer:x; integer:y; integer: result]
y := 1;
x := 3;
y := x + y;
result := printInteger(y);
return y;
}

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[Boolean:b; Boolean: c; Boolean: d]
c := true;
z := true;
d := false;
d := c & d;
return 1;
}

View File

@ -1,8 +0,0 @@
type main: string -> integer
function entry: main
entry(arg) := {
[integer:x]
x := 3 + 2 * 8;
return x;
}

View File

@ -1,9 +0,0 @@
type main: string -> integer
function entry: main
entry(arg) := {
[integer:x; Boolean b]
x := 3 + 2 * 8;
b := x < 1;
return 0;
}

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[integer:x; integer:y]
y := 1;
x := 3;
y := x / y;
return y;
}

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[Boolean: b; integer: x; integer: y]
x := 1;
y := 2;
b := x = y;
return 1;
}

View File

@ -1,18 +0,0 @@

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[Boolean: b; integer: x; integer: y]
character x := 1;
x := 1;
y := 2;
b := x < y;
return 1;
}

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[integer:x; integer:y]
y := 1;
x := 3;
y := x % y;
return y;
}

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[integer:x; integer:y]
y := 1;
x := 3;
y := x * x;
return y;
}

View File

@ -1,10 +1,12 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[integer:x; integer:y]
x := 3;
y := -x;
return y;
}

View File

@ -1,10 +1,12 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[Boolean: c; Boolean: d]
c := true;
d := !c;
return 1;
}

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[Boolean:b; Boolean: c; Boolean: d]
c := true;
d := false;
d := c | d;
return 1;
}

View File

@ -1,11 +1,13 @@
(* TEST: [-asc -tc -cg -ir] *)
type main: integer -> integer
function test: main
test (a) := {
#include "std.alpha"
entry (arg) := {
[integer:x; integer:y]
y := 1;
x := 3;
y := x - y;
return y;
}