Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes - assembling & tests #1

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ OUT := .
LIB_SRC?=./holyc-lib
SYNTAX?=./syntax-highlighting
PREFIX?=/usr/local
BUILT_IN_LOCATION?=~/.holyc-lib
BUILT_IN_LOCATION?=$(HOME)/.holyc-lib

.PHONY: unit-test

Expand Down Expand Up @@ -63,7 +63,7 @@ install:
prsasm-test:
gcc -O0 -g -DPRSASM_TEST ./prsasm.c ./cctrl.c ./dict.c ./ast.c ./aostr.c ./list.c ./lexer.c ./prsutil.c

unit-test: install
unit-test:
cd ../tests && ./tests.sh

$(OUT)/dict.o: dict.c dict.h aostr.h
Expand Down
6 changes: 6 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ AstType *AstTypeNew(void) {
return at;
}

AstType *AstTypeCopy(AstType *type) {
AstType *copy = AstTypeNew();
memcpy(copy,type,sizeof(AstType));
return copy;
}

Ast *AstUnaryOperator(AstType *type, long kind, Ast *operand) {
Ast *ast = AstNew();
ast->type = type;
Expand Down
1 change: 1 addition & 0 deletions src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ extern AstType *ast_void_type;
extern Ast *placeholder_arg;

Ast *AstNew(void);
AstType *AstTypeCopy(AstType *type);
void AstRelease(Ast *ast);
void AstReleaseList(List *ast_list);

Expand Down
20 changes: 0 additions & 20 deletions src/cctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,6 @@ lexeme *CctrlTokenGet(Cctrl *cc) {
return NULL;
}

/* Look ahead by `n` */
lexeme *CctrlTokenPeekBy(Cctrl *cc, int steps) {
List *ll = cc->tkit->cur;
/* We are already positioned one ahead */
while (--steps && ll != cc->tkit->tokens) {
if (ll == cc->tkit->tokens) {
loggerWarning("All tokens consume."
" Possible unexpected end of input\n");
return NULL;
}
ll = ll->next;
}
if (ll == cc->tkit->tokens) {
loggerWarning("All tokens consume."
" Possible unexpected end of input\n");
return NULL;
}
return ll->value;
}

/* Assert the token we are currently pointing at is a TK_PUNCT and the 'i64'
* matches 'expected'. Then consume this token else throw an error */
void CctrlTokenExpect(Cctrl *cc, long expected) {
Expand Down
1 change: 0 additions & 1 deletion src/cctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ Cctrl *CctrlNew(void);
void CctrlInitTokenIter(Cctrl *cc, List *tokens);
lexeme *CctrlTokenGet(Cctrl *cc);
lexeme *CctrlTokenPeek(Cctrl *cc);
lexeme *CctrlTokenPeekBy(Cctrl *cc, int steps);
void CctrlTokenIterSetCur(Cctrl *cc, List *cur);
void CctrlTokenRewind(Cctrl *cc);
void CctrlTokenExpect(Cctrl *cc, long expected);
Expand Down
21 changes: 4 additions & 17 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ int CompileToAst(Cctrl *cc, char *filepath, int lexer_flags) {
lexer l;
aoStr *file_path;
char *src, *strpath;
Dict *seen_files = DictNew(&default_table_type);

Dict *seen_files;

seen_files = DictNew(&default_table_type);
tokens = ListNew();
code_list = ListNew();

Expand All @@ -101,7 +102,6 @@ int CompileToAst(Cctrl *cc, char *filepath, int lexer_flags) {

while ((file_path = ListPop(l.files)) != NULL) {
if (DictGetLen(seen_files,file_path->data,file_path->len) != NULL) {
loggerDebug("saw: %s\n",file_path->data);
aoStrRelease(file_path);
continue;
}
Expand All @@ -110,6 +110,7 @@ int CompileToAst(Cctrl *cc, char *filepath, int lexer_flags) {
ListAppend(code_list,src);
l.ptr = src;
l.lineno = 1;
loggerDebug("lexing: %s\n",file_path->data);
next_tokens = lexToLexemes(cc->macro_defs,&l);

if (!next_tokens || next_tokens->next == next_tokens) {
Expand Down Expand Up @@ -153,17 +154,3 @@ aoStr *CompileCode(Cctrl *cc, char *code, int lexer_flags) {
free(l.files);
return asm_str;
}

/* Essentially uses GCC's assembler on the generated assembly in asmvbuf */
void CompileAssembleToFile(aoStr *asmbuf, char *filename) {
int fd = open(filename, O_CREAT|O_RDWR|O_TRUNC, 0666);
write(fd,asmbuf->data,asmbuf->len);

aoStr *cmd = aoStrNew();
aoStrCatPrintf(cmd, "gcc-13 ./%s -o ./a.out", filename);
printf("%s\n", cmd->data);
system(cmd->data);
aoStrRelease(cmd);
aoStrRelease(asmbuf);
close(fd);
}
5 changes: 3 additions & 2 deletions src/holyc-lib/io.HC
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public U8 *FileRead(U8 *path, I64 *_size=NULL)
public Bool FileWrite(U8 *filename, U8 *buf, I64 size,
I64 flags=O_CREAT|O_RDWR)
{
I64 towrite = size, total=0, nwritten = 0;
I64 towrite = size, nwritten = 0;
I32 fd;

if ((fd = open(filename,flags,438)) == -1) {
Expand All @@ -104,7 +104,8 @@ public Bool FileWrite(U8 *filename, U8 *buf, I64 size,
close(fd);
return FALSE;
}
total += nwritten;

buf += nwritten;
towrite -= nwritten;
}
close(fd);
Expand Down
51 changes: 44 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -9,6 +11,8 @@
#include "cctrl.h"
#include "util.h"

#define ASM_TMP_FILE "/tmp/holyc-asm.s"

typedef struct mccOptions {
int print_ast;
int print_tokens;
Expand Down Expand Up @@ -67,24 +71,57 @@ void execGcc(char *filename, aoStr *asmbuf, aoStr *cmd) {
system(cmd->data);
}

int writeAsmToTmp(aoStr *asmbuf) {
int fd;
ssize_t written;
size_t towrite = 0;
char *ptr;
ptr = asmbuf->data;

if ((fd = open(ASM_TMP_FILE,O_RDWR|O_TRUNC|O_CREAT,0644)) == -1) {
loggerPanic("Failed to create file for intermediary assembly: %s\n",
strerror(errno));
}

towrite = asmbuf->len;
ptr = asmbuf->data;

while (towrite > 0) {
written = write(fd,ptr,towrite);
if (written < 0) {
if (written == EINTR) {
continue;
}
close(fd);
loggerPanic("Failed to create file for intermediary assembly: %s\n",
strerror(errno));
}
printf("%ld\n",written);
towrite -= written;
ptr += written;
}
close(fd);
return 1;
}

void emitFile(aoStr *asmbuf, mccOptions *opts) {
aoStr *cmd = aoStrNew();

if (opts->emit_object) {
aoStr *escaped = aoStrEscapeString(asmbuf);
aoStrCatPrintf(cmd, "echo \"%s\" | gcc -x assembler -c -o ./%s -",
escaped->data, opts->obj_outfile);
aoStrRelease(escaped);
writeAsmToTmp(asmbuf);
aoStrCatPrintf(cmd, "gcc -c %s -lm -lc -o ./%s",
ASM_TMP_FILE,opts->obj_outfile);
system(cmd->data);
} else if (opts->asm_outfile && opts->assemble_only) {
int fd = open(opts->asm_outfile, O_CREAT|O_RDWR|O_TRUNC, 0666);
write(fd,asmbuf->data,asmbuf->len);
close(fd);
} else {
aoStr *escaped = aoStrEscapeString(asmbuf);
aoStrCatPrintf(cmd, "echo \"%s\" | gcc -x assembler -o ./a.out -", escaped->data);
aoStrRelease(escaped);
writeAsmToTmp(asmbuf);
aoStrCatPrintf(cmd, "gcc %s -lm -lc -o ./a.out", ASM_TMP_FILE);
system(cmd->data);
remove(ASM_TMP_FILE);

}
aoStrRelease(cmd);
aoStrRelease(asmbuf);
Expand Down
20 changes: 14 additions & 6 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,20 @@ Ast *ParseDeclArrayInitInt(Cctrl *cc, AstType *type) {
return AstArrayInit(initlist);
}

void ParseFlattenAnnonymous(AstType *anon, Dict *fields_dict, int offset) {
void ParseFlattenAnnonymous(AstType *anon, Dict *fields_dict,
int offset, int make_copy)
{
AstType *base, *type;
for (int i = 0; i < anon->fields->capacity; ++i) {
for (DictNode *dn = anon->fields->body[i]; dn; dn = dn->next) {
AstType *type = (AstType *)dn->val;
base = (AstType *)dn->val;
if (make_copy) {
type = AstTypeCopy(base);
} else {
type = base;
}
type->offset += offset;
DictSet(fields_dict,type->clsname->data,type);
DictSet(fields_dict,dn->key,type);
}
}
}
Expand Down Expand Up @@ -268,7 +276,7 @@ Dict *ParseClassOffsets(int *real_size, List *fields, AstType *base_class,

if (base_class) {
offset = base_class->size;
ParseFlattenAnnonymous(base_class,fields_dict,0);
ParseFlattenAnnonymous(base_class,fields_dict,0,1);
} else {
offset = 0;
}
Expand All @@ -291,7 +299,7 @@ Dict *ParseClassOffsets(int *real_size, List *fields, AstType *base_class,
field = it->value;
if (field->clsname == NULL && ParseIsClassOrUnion(field->kind)) {
offset += CalcPadding(offset,field->size);
ParseFlattenAnnonymous(field,fields_dict,offset);
ParseFlattenAnnonymous(field,fields_dict,offset,0);
offset += field->size;
continue;
} else {
Expand Down Expand Up @@ -329,7 +337,7 @@ Dict *ParseUnionOffsets(int *real_size, List *fields) {
max_size = field->size;
}
if (field->clsname == NULL && ParseIsClassOrUnion(field->kind)) {
ParseFlattenAnnonymous(field,fields_dict,0);
ParseFlattenAnnonymous(field,fields_dict,0,0);
continue;
}

Expand Down
10 changes: 3 additions & 7 deletions src/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -1088,9 +1088,9 @@ void AsmAddr(Cctrl *cc, aoStr *buf, Ast *ast) {
case AST_LVAR: {
if (ast->operand->type->kind == AST_TYPE_POINTER) {
/* XXX: this feels extremely hacky */
aoStrCatPrintf(buf, "# ADDR of %s\n\t",
AstKindToString(ast->operand->type->ptr->kind));
switch (ast->operand->type->ptr->kind) {
aoStrCatPrintf(buf, "# ADDR of %s\n\t",
AstKindToString(ast->operand->type->ptr->kind));
case AST_TYPE_ARRAY:
case AST_TYPE_CHAR:
case AST_TYPE_CLASS:
Expand Down Expand Up @@ -1938,11 +1938,7 @@ void AsmDataSection(Cctrl *cc, aoStr *buf) {
ptr = str;
while (*ptr) {
switch (*ptr) {
case '\\': {
/* XXX: this is weird but works */
aoStrCatPrintf(escaped,"\\\\");
break;
}
case '\\': aoStrCatPrintf(escaped,"\\"); break;
case '\n': aoStrCatPrintf(escaped,"\\n"); break;
case '\t': aoStrCatPrintf(escaped,"\\t"); break;
case '\r': aoStrCatPrintf(escaped,"\\r"); break;
Expand Down
1 change: 1 addition & 0 deletions tests/02_pointer_simple.HC
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ U0 Main()
I64 *p = &e->x;
Assign(p);
PrintResult(*p == 9, 1);
Free(e);
"====\n";
}
1 change: 1 addition & 0 deletions tests/03_malloc_array.HC
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Bool ArrayJuggling()
{
I64 *ptr = 0;
I64 arr[5][5];
arr[0][0] = 0;
arr[0][0] += 1; // arr[0][0] = 1
arr[0][0]++; // arr[0][0] = 2
++(arr[0][0]); // arr[0][0] = 3
Expand Down
5 changes: 3 additions & 2 deletions tests/17_varargs.HC
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ I64 Wrap(I64 i)

U0 Main()
{
"Test - Variable arguments: ";
I64 res, correct=0, tests = 15;
if (!StrNCmp("foo bar baz", Concat("foo", "bar", "baz"), 11)) {
correct++;
Expand All @@ -82,8 +83,8 @@ U0 Main()
res = Arg3(5,6,7,1);
if (res == 19) correct++;

if (AddS(2,2,2,4,4) == 14) correct++;
res = AddS(2,2,2,4,4);
if (AddS(2,2,2,4,4,0) == 14) correct++;
res = AddS(2,2,2,4,4,0);
if (res == 14) correct++;

/* assign and check */
Expand Down