aboutsummaryrefslogtreecommitdiff
path: root/sem3/osc
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-02-15 20:28:59 +0100
committerJulian T <julian@jtle.dk>2020-02-15 20:28:59 +0100
commit8076d839bf69e619f2e504122ec2ceb790a0b978 (patch)
treefa6cb7c649939c3a002676ce142105360d3d2fd6 /sem3/osc
parentf17e8e195bd1ee3f911ae311dce8e8f7df4f8457 (diff)
Changed to a more consistent codestyle
Diffstat (limited to 'sem3/osc')
-rw-r--r--sem3/osc/miniproject/cnasm/ast.c22
-rw-r--r--sem3/osc/miniproject/cnasm/codegen.c27
-rw-r--r--sem3/osc/mm1/mm1/jmod.c9
-rw-r--r--sem3/osc/mm1/mm2/tprog.c14
-rw-r--r--sem3/osc/mm11/regn/symtab.c15
5 files changed, 53 insertions, 34 deletions
diff --git a/sem3/osc/miniproject/cnasm/ast.c b/sem3/osc/miniproject/cnasm/ast.c
index 035b75d..badf090 100644
--- a/sem3/osc/miniproject/cnasm/ast.c
+++ b/sem3/osc/miniproject/cnasm/ast.c
@@ -9,7 +9,8 @@ static ast_node_t *create_empty_node() {
memset(n, 0, sizeof(ast_node_t));
}
-ast_node_t *insert_ctrl(enum ntype t, cond_t *c, ast_node_t *iftrue, ast_node_t *iffalse) {
+ast_node_t *insert_ctrl(enum ntype t, cond_t *c, ast_node_t *iftrue, ast_node_t *iffalse)
+{
ast_node_t *n = create_empty_node();
n->t = t;
@@ -20,7 +21,8 @@ ast_node_t *insert_ctrl(enum ntype t, cond_t *c, ast_node_t *iftrue, ast_node_t
return n;
}
-ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff) {
+ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff)
+{
ast_node_t *n = create_empty_node();
n->t = TFOR;
@@ -32,7 +34,8 @@ ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff) {
return n;
}
-ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list) {
+ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list)
+{
ast_node_t *n = create_empty_node();
n->t = TSTM_LIST;
@@ -41,7 +44,8 @@ ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list) {
return n;
}
-ast_node_t *insert_ident(char *ident) {
+ast_node_t *insert_ident(char *ident)
+{
ast_node_t *n = create_empty_node();
n->t = TIDENT;
@@ -50,7 +54,8 @@ ast_node_t *insert_ident(char *ident) {
return n;
}
-cond_t *insert_cond(uint8_t cmp, char *a, char *b) {
+cond_t *insert_cond(uint8_t cmp, char *a, char *b)
+{
cond_t *c = malloc( sizeof(cond_t));
c->cmp = cmp;
@@ -58,12 +63,13 @@ cond_t *insert_cond(uint8_t cmp, char *a, char *b) {
c->b = b;
}
-void node_print(ast_node_t *node) {
- if( !node ){
+void node_print(ast_node_t *node)
+{
+ if ( !node ){
printf("Nil");
return;
}
- switch(node->t) {
+ switch (node->t) {
case TSTM_LIST:
printf("Stm_list(");
node_print(node->list.children[0]);
diff --git a/sem3/osc/miniproject/cnasm/codegen.c b/sem3/osc/miniproject/cnasm/codegen.c
index c995df9..57584a4 100644
--- a/sem3/osc/miniproject/cnasm/codegen.c
+++ b/sem3/osc/miniproject/cnasm/codegen.c
@@ -5,10 +5,11 @@
#include <stdlib.h>
#include "ast.h"
-static void gencondjmp(FILE *f, cond_t *c, bool neg) {
+static void gencondjmp(FILE *f, cond_t *c, bool neg)
+{
uint8_t cmp = neg ? c->cmp^0x80 : c->cmp;
fprintf(f, "cmp %s %s\n", c->a, c->b);
- switch(cmp) {
+ switch (cmp) {
case CEQ:
fprintf(f, "je ");
break;
@@ -33,11 +34,12 @@ static void gencondjmp(FILE *f, cond_t *c, bool neg) {
}
}
-static void genif(FILE *f, struct genctx *ctx, ast_node_t *n) {
+static void genif(FILE *f, struct genctx *ctx, ast_node_t *n)
+{
bool doElse = n->flowctrl.iffalse;
// Create conditional jump
gencondjmp(f, n->flowctrl.condition, true);
- if( doElse ) {
+ if ( doElse ) {
fprintf(f, "else_%d\n", ctx->nested);
} else {
fprintf(f, "end_%d\n", ctx->nested);
@@ -48,7 +50,7 @@ static void genif(FILE *f, struct genctx *ctx, ast_node_t *n) {
gentree(f, &octx, n->flowctrl.iftrue);
// Do else
- if( doElse ) {
+ if ( doElse ) {
fprintf(f, "jmp end_%d\n", ctx->nested);
fprintf(f, "else_%d:\n", ctx->nested);
gentree(f, &octx, n->flowctrl.iffalse);
@@ -58,7 +60,8 @@ static void genif(FILE *f, struct genctx *ctx, ast_node_t *n) {
fprintf(f, "end_%d:\n", ctx->nested);
}
-static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n) {
+static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n)
+{
// Create loop label
fprintf(f, "loop_%d:\n", ctx->nested);
@@ -77,7 +80,8 @@ static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n) {
fprintf(f, "end_%d:\n", ctx->nested);
}
-static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n) {
+static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n)
+{
// Do pre stuff
fprintf(f, "%s\n", n->forloop.pre);
@@ -101,15 +105,16 @@ static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n) {
fprintf(f, "end_%d:\n", ctx->nested);
}
-void gentree(FILE *f, struct genctx *ctx, ast_node_t *n) {
- if( !n ) {
+void gentree(FILE *f, struct genctx *ctx, ast_node_t *n)
+{
+ if ( !n ) {
return;
}
- if( ctx == NULL ) {
+ if ( ctx == NULL ) {
ctx = malloc(sizeof(struct genctx));
ctx->nested = 0;
}
- switch(n->t) {
+ switch (n->t) {
case TSTM_LIST:
gentree(f, ctx, n->list.children[0]);
gentree(f, ctx, n->list.children[1]);
diff --git a/sem3/osc/mm1/mm1/jmod.c b/sem3/osc/mm1/mm1/jmod.c
index a07077c..472ad46 100644
--- a/sem3/osc/mm1/mm1/jmod.c
+++ b/sem3/osc/mm1/mm1/jmod.c
@@ -8,7 +8,8 @@ static int major_num;
static int busy;
-static int cool_open(struct inode *inode, struct file *file) {
+static int cool_open(struct inode *inode, struct file *file)
+{
/* Check if we are already serving someone */
if (busy) {
return -EBUSY;
@@ -18,13 +19,15 @@ static int cool_open(struct inode *inode, struct file *file) {
return 0;
}
-static int cool_release (struct inode *inode, struct file *file) {
+static int cool_release (struct inode *inode, struct file *file)
+{
busy = 0;
return 0;
}
-static ssize_t cool_read (struct file *filp, char *buffer, size_t len, loff_t *offset) {
+static ssize_t cool_read (struct file *filp, char *buffer, size_t len, loff_t *offset)
+{
char str[12] = "hej med dig";
int i;
diff --git a/sem3/osc/mm1/mm2/tprog.c b/sem3/osc/mm1/mm2/tprog.c
index 377555f..bb04778 100644
--- a/sem3/osc/mm1/mm2/tprog.c
+++ b/sem3/osc/mm1/mm2/tprog.c
@@ -10,7 +10,8 @@
struct itimerval timer;
-void timer_handler(int signum) {
+void timer_handler(int signum)
+{
/* Handy structure reference */
struct timeval *tv = &timer.it_value;
printf("Hey we hit the alarm\n\a");
@@ -22,9 +23,9 @@ void timer_handler(int signum) {
if (tv->tv_usec == 0) {
tv->tv_usec = 999999;
} else if (tv->tv_usec > ENDUSEC) {
- tv->tv_usec *= SPEED;
+ tv->tv_usec *= SPEED;
if (tv->tv_usec < ENDUSEC) {
- tv->tv_usec = ENDUSEC;
+ tv->tv_usec = ENDUSEC;
}
} else {
/* Return letting the timer be set to ENDUSEC */
@@ -36,12 +37,13 @@ void timer_handler(int signum) {
/* Set alarm */
int err = setitimer(ITIMER_REAL, &timer, NULL);
if (err) {
- printf("Hey we got an error guys\n");
- exit(1);
+ printf("Hey we got an error guys\n");
+ exit(1);
}
}
-int main() {
+int main()
+{
/* Setup handler for timer */
struct sigaction sa;
memset(&sa, 0, sizeof(sa)); /* Remeber to set all fields to zero */
diff --git a/sem3/osc/mm11/regn/symtab.c b/sem3/osc/mm11/regn/symtab.c
index 8103203..35799a5 100644
--- a/sem3/osc/mm11/regn/symtab.c
+++ b/sem3/osc/mm11/regn/symtab.c
@@ -2,9 +2,10 @@
#include <stdlib.h>
#include <string.h>
-unsigned int hash(char *s) {
+unsigned int hash(char *s)
+{
uint32_t hv = 0;
- for( int i = 0; s[i] != '\0'; i++ ) {
+ for ( int i = 0; s[i] != '\0'; i++ ) {
// take MSB 6 bits of hv and xors with LSB of s[i]
uint32_t v = ( hv >> 26 ) ^ (s[i] & 0x3f);
@@ -16,7 +17,8 @@ unsigned int hash(char *s) {
}
-symnode_t *sym_insert(char *var) {
+symnode_t *sym_insert(char *var)
+{
unsigned int index = hash(var);
// Save old value
@@ -24,7 +26,7 @@ symnode_t *sym_insert(char *var) {
// Make new
symbolarray[index] = malloc(sizeof(symnode_t));
- if( symbolarray[index] == NULL ) {
+ if ( symbolarray[index] == NULL ) {
// If malloc failed
symbolarray[index] = oldSym;
return NULL;
@@ -37,12 +39,13 @@ symnode_t *sym_insert(char *var) {
return symbolarray[index];
}
-symnode_t *sym_lookup(char *var) {
+symnode_t *sym_lookup(char *var)
+{
unsigned int index = hash(var);
symnode_t *n = symbolarray[index];
// Look trough list
- while(n != NULL && strcmp(n->name, var) != 0) {
+ while (n != NULL && strcmp(n->name, var) != 0) {
n = n->next;
}