summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2019-09-30 01:56:15 +0100
committerTom Harley2019-09-30 01:56:15 +0100
commitfbe39f58fc0d757f8ffe54c6dc96bbc690862ded (patch)
tree94d861774ded89f82363bcc2877ec2cc3f94c024
downloadsinter-fbe39f58fc0d757f8ffe54c6dc96bbc690862ded.tar.gz
sinter-fbe39f58fc0d757f8ffe54c6dc96bbc690862ded.zip

Initial commit

-rw-r--r--.gitignore11
-rw-r--r--Makefile22
-rw-r--r--basic.c113
-rw-r--r--basic.h28
-rw-r--r--lex.l38
-rw-r--r--parse.y51
6 files changed, 263 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..89ef181
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,11 @@
+*.o
+*.bc
+*.ll
+
+lex.c
+parse.c
+parse.h
+
+basic
+
+*.sw*
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..56b2f72
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,22 @@
+CC = c++
+
+CFLAGS += -x c --std=c99 -Wpedantic -Wall -Werror
+
+CFLAGS += $(shell llvm-config --cppflags)
+LDFLAGS += $(shell llvm-config --ldflags)
+LDLIBS += $(shell llvm-config --libs core analysis bitwriter --system-libs)
+
+.PHONY: default all clean
+default: all
+all: basic
+basic: basic.o parse.o lex.o
+
+basic.o lex.o: parse.h basic.h
+parse.o: basic.h
+
+parse.h: parse.y
+ $(YACC) -d -o /dev/null $<
+ mv -f y.tab.h $@
+
+clean:
+ $(RM) basic y.tab.* *.o lex.c parse.c parse.h
diff --git a/basic.c b/basic.c
new file mode 100644
index 0000000..4e1a8be
--- /dev/null
+++ b/basic.c
@@ -0,0 +1,113 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <llvm-c/Core.h>
+#include <llvm-c/Analysis.h>
+#include <llvm-c/BitWriter.h>
+
+#include "basic.h"
+#include "parse.h"
+
+int yyparse();
+
+sexpr *new_sexpr(int type, contents val) {
+ /* TODO: unchecked memory allocation! */
+ sexpr *ret = malloc(sizeof (*ret));
+ ret->type = type;
+ ret->contents = val;
+ return ret;
+}
+
+sexpr *new_int(int num) {
+ contents c;
+ c.i = num;
+ return new_sexpr(INT, c);
+}
+
+sexpr *new_float(float num) {
+ contents c;
+ c.f = num;
+ return new_sexpr(FLOAT, c);
+}
+
+sexpr *new_id(char *id) {
+ contents c;
+ c.s = id;
+ return new_sexpr(ID, c);
+}
+
+sexpr *new_node(sexpr *l, sexpr *r) {
+ contents c;
+ c.n.l = l;
+ c.n.r = r;
+ return new_sexpr(BRANCH, c);
+}
+
+void _print_sexpr(sexpr *c) {
+ if (!c) {
+ printf("NIL");
+ } else {
+ switch (c->type) {
+ case INT:
+ printf("INT %i", c->contents.i);
+ break;
+ case FLOAT:
+ printf("FLOAT %f", c->contents.f);
+ break;
+ case ID:
+ printf("ID %s", c->contents.s);
+ break;
+ case BRANCH:
+ printf("(");
+ _print_sexpr(c->contents.n.l);
+ printf(";");
+ _print_sexpr(c->contents.n.r);
+ printf(")");
+ break;
+ case NIL:
+ default:
+ printf("ERROR");
+ }
+ }
+}
+
+void print_sexpr(sexpr *c) {
+ _print_sexpr(c);
+ printf("\n");
+}
+
+int module_id = 0;
+
+void codegen(sexpr *sexpr) {
+ char module_name[50];
+ snprintf(module_name, 50, "module_%d", module_id++);
+ LLVMModuleRef module = LLVMModuleCreateWithName(module_name);
+
+ LLVMBuilderRef builder = LLVMCreateBuilder();
+
+ LLVMTypeRef func_type = LLVMFunctionType(LLVMInt32Type(), 0, 0, 0);
+ LLVMValueRef new_func = LLVMAddFunction(module, "main", func_type);
+
+ LLVMBasicBlockRef entry = LLVMAppendBasicBlock(new_func, "entry");
+
+ LLVMPositionBuilderAtEnd(builder, entry);
+ LLVMValueRef x = LLVMConstInt(LLVMInt32Type(), 4, 0);
+ LLVMValueRef add_output = LLVMBuildAdd(builder, x, x, "add_output");
+ LLVMBuildRet(builder, add_output);
+
+ char *err = 0;
+ LLVMVerifyModule(module, LLVMAbortProcessAction, &err);
+ LLVMDisposeMessage(err);
+
+ char *module_status = LLVMPrintModuleToString(module);
+ printf("%s\n", module_status);
+ LLVMDisposeMessage(module_status);
+
+ LLVMDisposeBuilder(builder);
+ LLVMDisposeModule(module);
+}
+
+int main() {
+ yyparse();
+ return 0;
+}
diff --git a/basic.h b/basic.h
new file mode 100644
index 0000000..679bf89
--- /dev/null
+++ b/basic.h
@@ -0,0 +1,28 @@
+#ifndef _BASIC_H
+#define _BASIC_H
+
+typedef struct sexpr {
+ int type;
+ union contents {
+ float f;
+ char *s;
+ int i;
+ struct node {
+ struct sexpr *l;
+ struct sexpr *r;
+ } n;
+ } contents;
+} sexpr;
+
+typedef struct node node;
+typedef union contents contents;
+
+sexpr *new_int(int num);
+sexpr *new_float(float num);
+sexpr *new_id(char *id);
+sexpr *new_node(sexpr *l, sexpr *r);
+
+void print_sexpr(sexpr *contents);
+void codegen(sexpr *sexpr);
+
+#endif /* _BASIC_H */
diff --git a/lex.l b/lex.l
new file mode 100644
index 0000000..e7591d4
--- /dev/null
+++ b/lex.l
@@ -0,0 +1,38 @@
+%option noyywrap
+%option nodefault
+%option yylineno
+%option noinput nounput
+
+%{
+
+#include <string.h>
+
+#include "basic.h"
+#include "parse.h"
+
+void yyerror(const char *s, ...);
+
+%}
+
+ID [-A-Za-z_+*!/][-A-Za-z0-9_+*!]*
+FLOAT [+-]?([0-9]+[.][0-9]*([Ee][+-]?[0-9]+)?|[.][0-9]+([Ee][+-]?[0-9]+)?)
+INT [+-]?[0-9]+
+
+%%
+
+"(" |
+")" |
+";" { return yytext[0]; }
+
+"()" { return NIL; }
+
+{ID} { /* TODO: strdup allocates memory! */
+ yylval.s = strdup(yytext); return ID; }
+
+{INT} { yylval.i = atoi(yytext); return INT; }
+
+{FLOAT} { yylval.f = atof(yytext); return FLOAT; }
+
+[ \t\n] /* ignore whitespace */
+
+. { yyerror("Unexpected character %c\n", yytext[0]); }
diff --git a/parse.y b/parse.y
new file mode 100644
index 0000000..1810c51
--- /dev/null
+++ b/parse.y
@@ -0,0 +1,51 @@
+%{
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "basic.h"
+
+extern int yylineno;
+int yylex();
+void yyerror(char *s, ...);
+
+%}
+
+%union {
+ float f;
+ int i;
+ char *s;
+ sexpr *u;
+}
+
+%token NIL
+%token <f> FLOAT
+%token <i> INT
+%token <s> ID
+%token BRANCH
+
+%type <u> sexpr
+
+%%
+
+cmds:
+ | cmds sexpr { print_sexpr($2); }
+
+sexpr: '(' sexpr ';' sexpr ')' { $$ = new_node($2, $4); }
+ | '(' sexpr sexpr ')' { $$ = new_node($2, $3); }
+ | INT { $$ = new_int($1); }
+ | FLOAT { $$ = new_float($1); }
+ | ID { $$ = new_id($1); }
+ | NIL { $$ = 0; }
+
+%%
+
+void yyerror(char *s, ...) {
+ va_list ap;
+ va_start(ap, s);
+
+ fprintf(stderr, "%d: error: ", yylineno);
+ vfprintf(stderr, s, ap);
+ fprintf(stderr, "\n");
+}