Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
grnt426 committed Mar 7, 2012
2 parents 48afdda + a1e7cc0 commit a90dbb2
Show file tree
Hide file tree
Showing 18 changed files with 1,047 additions and 259 deletions.
27 changes: 17 additions & 10 deletions Hartz Instruction Set.txt
Expand Up @@ -55,23 +55,30 @@ Instruction Set:
- beq $source
- True: Right
- False: Read current instruction
- ROT <1001>
- ROT <10010(0/1)>
- Rotates Data Ring
- rot $countReg (bit 5 is register, bit six = 1)
- rot $countReg (bit 7 is register, bit 6 = 1)
- rotates by the specified value in the register
- rot (bit 6 = 0)
- implicitly rotates once
- JMP <1010>
- Rotates Text Ring
- jmp $countReg
- NA <1011>
- LROT <10011(0/1)>
- Rotates Data Ring by amount in next value on text ring
- lrot $multiplier_reg (bit 7 is register, bit 6 = 1)
- applies multiplier, then loads and performs rotate
- lrot (bit 7 ignored, bit 6 = 0)
- apply no multiplier
- LJMP <1010>
- Specifies that the next instruction is to be made a longer jump
- ljmp !multiplier
- JMP <1011>
- The literal value to rotate by
- NA <1100>
- NA <1101>
- NOP <1110>
- HLT <11110>
- Halt execution
- NOP <11111>
- No Operation
- does nothing
- HLT <1111>
- Halt execution

Program Execution:
- Load instruction from text ring
Expand Down Expand Up @@ -136,4 +143,4 @@ Higher Level Instruction Breakdown:
ROT 1 (where one is some immediate value)
LW $1



27 changes: 20 additions & 7 deletions Makefile
@@ -1,12 +1,25 @@
CFLAGS=-std=c99
# Used for compiling the project. If no make target is specified, by default
# only the object files necessary to create the Hartz translator are compiled.
CC = gcc
CFLAGS = -std=c99 -Wall -lm
COMMON_FILES = symbols.c idents.c strlib.c generrors.c terms.c
HARTZ_FILES = translator.c
CCODE_FILES = compiler.c
HARTZ_EXEC = translator
CCODE_EXEC = compiler

all: hartz

hartz:
gcc $(CFLAGS) translator.c symbols.c idents.c strlib.c generrors.c -o translator
all: hartz ccode

ccode:
gcc $(CLFAGS) compiler.c symbols.c idents.c strlib.c generrors.c -o compiler
# To translate Hartz assembly into a "binary executable"
hartz: $(HARTZ_FILES) $(COMMON_FILES)
$(CC) $(CFLAGS) -o $(HARTZ_EXEC) $(HARTZ_FILES) $(COMMON_FILES)

# To compile C-Style code into Hartz Assembly
ccode: $(CCODE_FILES) $(COMMON_FILES)
$(CC) $(CFLAGS) -o $(CCODE_EXEC) $(CCODE_FILES) $(COMMON_FILES)

# Just cleans up object files, which aren't needed after the linker creates
# the executable
clean:
rm -rf *.o hartz ccode
rm -f *.o
48 changes: 47 additions & 1 deletion README
@@ -1 +1,47 @@
Will be updated later...
== Description ==
A compiler that translates C-style code into Hartz assembly, which can then be
further compiled into a "binary executable" for a theoretical Minecraft
CPU that is (and probably forever will be) in development. The idea is to
support basic C-Style language constructs such as "if-else" branches,
arithmetic, do-while loops, and function calls. The whole purpose of all this
is to gain an understanding of compilers and to specifically create a working
compiler for a highly limited machine (in both resources and instructions
available).

== Documentation ==
Author: Grant Kurtz
Created: Dec 2012

The following categories breakdown into relevant sections for compilation.

=== Hartz Instruction Set ===
This lays out, albeit crudely, all the definitions of planned and
currently supported hardware instructions, machine contraints, and some
example code.

* Hartz Instruction Set.txt

=== Productions ===
A list of productions that form all the language constructs that the
C-Style compiler will break down.

(Not Yet Added)

== Software Dependencies ==
gcc v4.3.4

== Compiling ==
make [all|hartz|ccode]

== Running ==

=== Hartz Translator ===
./translator (in-file) (out-file)

=== C-Style Code Compiler ===
./compiler

== Compilation Platform ==
Linux Version: 2.6.32-gentoo-r7
gcc (Gentoo 4.3.4 p1.1, pie-10.1.5) 4.3.4

47 changes: 15 additions & 32 deletions compiler.c
Expand Up @@ -9,10 +9,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symbols.c"
#include "symbols.h"
#include "compiler.h"
#include "generrors.c"
#include "strlib.c"
#include "generrors.h"
#include "strlib.h"
#include "idents.h"
#include "terms.h"

int main(){

Expand Down Expand Up @@ -59,7 +61,7 @@ void getInputFile(char *file){

int checkOpenFile(const char *filename){
FILE *file;
if(file = fopen(filename, "r")){
if( (file = fopen(filename, "r")) ){
fclose(file);
return 1;
}
Expand All @@ -77,29 +79,29 @@ int parseFile(struct program *prog){
#ifdef DEBUG
fprintf(stderr, "*** Reading Line %d...\n", prog->line_count);
#endif
fgetpos(prog->in, &prog->str_line);
tok = read_next_token(buf, prog->in, 64);

// fgetpos(prog->in, &prog->str_line);
// tok = read_next_token(buf, prog->in, 64);
break;
if(!tok){
continue;
}
else{
process_token(tok);
// process_token(tok);
}
}while(!prog->error_code && !check_EOF(prog->in));


return 0;
}

void process_token(char *tok, struct program *prog){

if(check_comment(tok)){
if(check_comment(tok, prog)){
process_comment(tok);
}
else{

// must be a variable
process_definition(tok);
process_definition(tok, prog);
}
}

Expand All @@ -108,7 +110,7 @@ void process_definition(char *tok, struct program *prog){
struct symbol *s;

// previously declared
if(s = find_symbol(tok, prog->tbl)){
if( (s = find_symbol(tok, prog->tbl)) ){
// do stuff
}

Expand All @@ -124,7 +126,7 @@ void process_definition(char *tok, struct program *prog){
strncpy(s->iden, tok, strlen(tok));
}

struct term *t = (struct term *) malloc(sizeof(struct term));
struct Term *t = (struct Term *) malloc(sizeof(struct Term));
if(!t){
print_memory_error(prog);
return;
Expand Down Expand Up @@ -189,14 +191,6 @@ void ifStatement(struct Term *term, FILE *input, ProgramData *prog){

}

void addChildTerm(struct Term *child, struct Term *parent){
int i = 0;
while(parent->child_terms[i] != NULL){
++i;
}
parent->child_terms[i] = child; // the compiler throws a warning?
}

void reportCompilerError(char * err_msg, ProgramData * prog){

}
Expand Down Expand Up @@ -235,14 +229,3 @@ int consumeUntil(FILE *input, char *buf, const unsigned int buf_size,
return -1;
}

struct Term* createTerm(char* term, unsigned int term_len){
struct Term * new_term = (struct Term *) calloc(1, sizeof(struct Term));
strncpy(new_term->term, term, term_len);
return new_term;
}

struct Term* createSingleCharTerm(const char term){
struct Term * new_term = (struct Term *) calloc(1, sizeof(struct Term));
new_term->term[0] = term;
return new_term;
}
12 changes: 1 addition & 11 deletions compiler.h
Expand Up @@ -21,11 +21,6 @@
#define TAB '\t'
#define SPACE ' '

struct Term{
char* term;
struct Term **child_terms;
};

typedef struct{
unsigned short line_count;
unsigned short has_error;
Expand All @@ -40,7 +35,7 @@ void printEqualsSeparator();
int checkOpenFile(const char*);
void getInputFile();
char readNonEmptyChar(FILE *, ProgramData *);
int consumeUntil(FILE * input, char * buf, const unsigned int buf_size,
int consumeUntil(FILE * input, char * buf, const unsigned int buf_size,
const char term_char, ProgramData *prog);

// Symbol Table Manipulation Functions
Expand All @@ -63,9 +58,4 @@ void process_definition(char *tok, struct program *prog);
void reportCompilerError(char *, ProgramData *);


// Term Manipulation Functions
void addChildTerm(struct Term *, struct Term *);
struct Term* createTerm(char* term, unsigned int term_len);
struct Term* createSingleCharTerm(const char term);

#endif
6 changes: 6 additions & 0 deletions generrors.c
Expand Up @@ -9,3 +9,9 @@ void print_memory_error(struct program *prog){
prog->error_code = ALLOC_ERR;
}

void print_fault(const char* reason, struct program *prog){
fprintf(stderr, "Whoops! The compiler has a bug! Failure Reason:\n"
"\t%s", reason);
prog->error_code = 4;
}

3 changes: 3 additions & 0 deletions generrors.h
Expand Up @@ -5,10 +5,13 @@
#define GARBAGE 1
#define SYM_ERR 2
#define ALLOC_ERR 3
#define FAULT 4

struct program;

// Compiler Errors generated by the compiler itself
void print_memory_error(struct program *prog);
void print_fault(const char *reason, struct program *prog);

#endif

0 comments on commit a90dbb2

Please sign in to comment.