diff options
author | Julian T <julian@jtle.dk> | 2020-10-12 16:26:42 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-10-12 16:54:48 +0200 |
commit | 4e05a55e373bd315e721d534a1711fec4c0054c5 (patch) | |
tree | 4fc0141760c2730ed79aaa4c7aa60ea45cc66cc0 /sem3/osc | |
parent | b7f9cf43c8a9ab3400cbb30d5e1cadb0c6c2cf23 (diff) |
Moved to linux kernel inspired clang-format
Diffstat (limited to 'sem3/osc')
-rw-r--r-- | sem3/osc/miniproject/cnasm/ast.c | 132 | ||||
-rw-r--r-- | sem3/osc/miniproject/cnasm/ast.h | 51 | ||||
-rw-r--r-- | sem3/osc/miniproject/cnasm/codegen.c | 222 | ||||
-rw-r--r-- | sem3/osc/miniproject/cnasm/codegen.h | 4 | ||||
-rw-r--r-- | sem3/osc/mm1/mm1/jmod.c | 77 | ||||
-rw-r--r-- | sem3/osc/mm1/mm2/tprog.c | 28 | ||||
-rw-r--r-- | sem3/osc/mm11/regn/symtab.c | 63 | ||||
-rw-r--r-- | sem3/osc/mm11/regn/symtab.h | 14 |
8 files changed, 293 insertions, 298 deletions
diff --git a/sem3/osc/miniproject/cnasm/ast.c b/sem3/osc/miniproject/cnasm/ast.c index badf090..cc53e24 100644 --- a/sem3/osc/miniproject/cnasm/ast.c +++ b/sem3/osc/miniproject/cnasm/ast.c @@ -4,101 +4,101 @@ #include <string.h> #include <stdio.h> -static ast_node_t *create_empty_node() { - ast_node_t *n = malloc(sizeof(ast_node_t)); - memset(n, 0, sizeof(ast_node_t)); +static ast_node_t *create_empty_node() +{ + ast_node_t *n = malloc(sizeof(ast_node_t)); + 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(); + ast_node_t *n = create_empty_node(); - n->t = t; - n->flowctrl.condition = c; - n->flowctrl.iftrue = iftrue; - n->flowctrl.iffalse = iffalse; + n->t = t; + n->flowctrl.condition = c; + n->flowctrl.iftrue = iftrue; + n->flowctrl.iffalse = iffalse; - return n; + return n; } ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff) { - ast_node_t *n = create_empty_node(); + ast_node_t *n = create_empty_node(); - n->t = TFOR; - n->forloop.condition = c; - n->forloop.pre = pre; - n->forloop.inc = inc; - n->forloop.stuff = stuff; + n->t = TFOR; + n->forloop.condition = c; + n->forloop.pre = pre; + n->forloop.inc = inc; + n->forloop.stuff = stuff; - return n; + return n; } ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list) { - ast_node_t *n = create_empty_node(); + ast_node_t *n = create_empty_node(); - n->t = TSTM_LIST; - n->list.children[0] = stm_list; - n->list.children[1] = stm; + n->t = TSTM_LIST; + n->list.children[0] = stm_list; + n->list.children[1] = stm; - return n; + return n; } ast_node_t *insert_ident(char *ident) { - ast_node_t *n = create_empty_node(); + ast_node_t *n = create_empty_node(); - n->t = TIDENT; - n->ident = ident; + n->t = TIDENT; + n->ident = ident; - return n; + return n; } cond_t *insert_cond(uint8_t cmp, char *a, char *b) { - cond_t *c = malloc( sizeof(cond_t)); + cond_t *c = malloc(sizeof(cond_t)); - c->cmp = cmp; - c->a = a; - c->b = b; + c->cmp = cmp; + c->a = a; + c->b = b; } void node_print(ast_node_t *node) { - if ( !node ){ - printf("Nil"); - return; - } - switch (node->t) { - case TSTM_LIST: - printf("Stm_list("); - node_print(node->list.children[0]); - printf(","); - node_print(node->list.children[1]); - printf(")"); - break; - case TIF: - printf("If"); - printf("(%s %c %s) {", node->flowctrl.condition->a, - node->flowctrl.condition->cmp, - node->flowctrl.condition->b); - node_print(node->flowctrl.iftrue); - printf("}{"); - node_print(node->flowctrl.iffalse); - printf("}"); - break; - case TIDENT: - printf("%s", node->ident); - break; - case TWHILE: - printf("while"); - printf("(%s %c %s) {", node->flowctrl.condition->a, - node->flowctrl.condition->cmp, - node->flowctrl.condition->b); - node_print(node->flowctrl.iftrue); - printf("}"); - break; - default: - printf("invalid"); - } + if (!node) { + printf("Nil"); + return; + } + switch (node->t) { + case TSTM_LIST: + printf("Stm_list("); + node_print(node->list.children[0]); + printf(","); + node_print(node->list.children[1]); + printf(")"); + break; + case TIF: + printf("If"); + printf("(%s %c %s) {", node->flowctrl.condition->a, + node->flowctrl.condition->cmp, node->flowctrl.condition->b); + node_print(node->flowctrl.iftrue); + printf("}{"); + node_print(node->flowctrl.iffalse); + printf("}"); + break; + case TIDENT: + printf("%s", node->ident); + break; + case TWHILE: + printf("while"); + printf("(%s %c %s) {", node->flowctrl.condition->a, + node->flowctrl.condition->cmp, node->flowctrl.condition->b); + node_print(node->flowctrl.iftrue); + printf("}"); + break; + default: + printf("invalid"); + } } diff --git a/sem3/osc/miniproject/cnasm/ast.h b/sem3/osc/miniproject/cnasm/ast.h index 61a8f4f..4f37553 100644 --- a/sem3/osc/miniproject/cnasm/ast.h +++ b/sem3/osc/miniproject/cnasm/ast.h @@ -11,37 +11,38 @@ #define CLEQ CGT ^ 0x80 #define CGEQ CLT ^ 0x80 -enum ntype { TSTM_LIST, TIF, TFOR, TIDENT, TWHILE}; +enum ntype { TSTM_LIST, TIF, TFOR, TIDENT, TWHILE }; typedef struct cond { - uint8_t cmp; - char *a; - char *b; + uint8_t cmp; + char *a; + char *b; } cond_t; typedef struct ast_node { - enum ntype t; - // Dependent on type - union { - struct { - cond_t *condition; - struct ast_node *iftrue; - struct ast_node *iffalse; - } flowctrl; - struct { - cond_t *condition; - char *pre; - char *inc; - struct ast_node *stuff; - } forloop; - char *ident; - struct { - struct ast_node *children[2]; - } list; - }; + enum ntype t; + // Dependent on type + union { + struct { + cond_t *condition; + struct ast_node *iftrue; + struct ast_node *iffalse; + } flowctrl; + struct { + cond_t *condition; + char *pre; + char *inc; + struct ast_node *stuff; + } forloop; + char *ident; + struct { + struct ast_node *children[2]; + } list; + }; } 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 *insert_stm(ast_node_t *stm, ast_node_t *stm_list); ast_node_t *insert_ident(char *ident); ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff); @@ -50,4 +51,4 @@ cond_t *insert_cond(uint8_t cmp, char *a, char *b); void node_print(ast_node_t *node); -#endif +#endif diff --git a/sem3/osc/miniproject/cnasm/codegen.c b/sem3/osc/miniproject/cnasm/codegen.c index 57584a4..6f32982 100644 --- a/sem3/osc/miniproject/cnasm/codegen.c +++ b/sem3/osc/miniproject/cnasm/codegen.c @@ -7,131 +7,131 @@ 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) { - case CEQ: - fprintf(f, "je "); - break; - case CNEQ: - fprintf(f, "jne "); - break; - case CGT: - fprintf(f, "jg "); - break; - case CLT: - fprintf(f, "jl "); - break; - case CLEQ: - fprintf(f, "jle "); - break; - case CGEQ: - fprintf(f, "jge "); - break; - default: - fprintf(stderr, "Invalid cmp type %x", cmp); - fprintf(f, "jmp "); - } + uint8_t cmp = neg ? c->cmp ^ 0x80 : c->cmp; + fprintf(f, "cmp %s %s\n", c->a, c->b); + switch (cmp) { + case CEQ: + fprintf(f, "je "); + break; + case CNEQ: + fprintf(f, "jne "); + break; + case CGT: + fprintf(f, "jg "); + break; + case CLT: + fprintf(f, "jl "); + break; + case CLEQ: + fprintf(f, "jle "); + break; + case CGEQ: + fprintf(f, "jge "); + break; + default: + fprintf(stderr, "Invalid cmp type %x", cmp); + fprintf(f, "jmp "); + } } 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 ) { - fprintf(f, "else_%d\n", ctx->nested); - } else { - fprintf(f, "end_%d\n", ctx->nested); - } - - struct genctx octx = { ctx->nested+1 }; - // Paste code - gentree(f, &octx, n->flowctrl.iftrue); - - // Do else - if ( doElse ) { - fprintf(f, "jmp end_%d\n", ctx->nested); - fprintf(f, "else_%d:\n", ctx->nested); - gentree(f, &octx, n->flowctrl.iffalse); - } - - // End block - fprintf(f, "end_%d:\n", ctx->nested); + bool doElse = n->flowctrl.iffalse; + // Create conditional jump + gencondjmp(f, n->flowctrl.condition, true); + if (doElse) { + fprintf(f, "else_%d\n", ctx->nested); + } else { + fprintf(f, "end_%d\n", ctx->nested); + } + + struct genctx octx = { ctx->nested + 1 }; + // Paste code + gentree(f, &octx, n->flowctrl.iftrue); + + // Do else + if (doElse) { + fprintf(f, "jmp end_%d\n", ctx->nested); + fprintf(f, "else_%d:\n", ctx->nested); + gentree(f, &octx, n->flowctrl.iffalse); + } + + // End block + fprintf(f, "end_%d:\n", ctx->nested); } static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n) { - // Create loop label - fprintf(f, "loop_%d:\n", ctx->nested); - - // Create conditional jump - gencondjmp(f, n->flowctrl.condition, true); - fprintf(f, "end_%d\n", ctx->nested); - - struct genctx octx = { ctx->nested+1 }; - // Paste code - gentree(f, &octx, n->flowctrl.iftrue); - - // Jump to start - fprintf(f, "jmp loop_%d\n", ctx->nested); - - // End block - fprintf(f, "end_%d:\n", ctx->nested); + // Create loop label + fprintf(f, "loop_%d:\n", ctx->nested); + + // Create conditional jump + gencondjmp(f, n->flowctrl.condition, true); + fprintf(f, "end_%d\n", ctx->nested); + + struct genctx octx = { ctx->nested + 1 }; + // Paste code + gentree(f, &octx, n->flowctrl.iftrue); + + // Jump to start + fprintf(f, "jmp loop_%d\n", ctx->nested); + + // End block + fprintf(f, "end_%d:\n", ctx->nested); } static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n) { - // Do pre stuff - fprintf(f, "%s\n", n->forloop.pre); - - // Create loop label - fprintf(f, "loop_%d:\n", ctx->nested); - - // Create conditional jump - gencondjmp(f, n->flowctrl.condition, true); - fprintf(f, "end_%d\n", ctx->nested); - - struct genctx octx = { ctx->nested+1 }; - // Paste code - gentree(f, &octx, n->forloop.stuff); - - // Do inc stuff - fprintf(f, "%s\n", n->forloop.inc); - // Jump to start - fprintf(f, "jmp loop_%d\n", ctx->nested); - - // End block - fprintf(f, "end_%d:\n", ctx->nested); + // Do pre stuff + fprintf(f, "%s\n", n->forloop.pre); + + // Create loop label + fprintf(f, "loop_%d:\n", ctx->nested); + + // Create conditional jump + gencondjmp(f, n->flowctrl.condition, true); + fprintf(f, "end_%d\n", ctx->nested); + + struct genctx octx = { ctx->nested + 1 }; + // Paste code + gentree(f, &octx, n->forloop.stuff); + + // Do inc stuff + fprintf(f, "%s\n", n->forloop.inc); + // Jump to start + fprintf(f, "jmp loop_%d\n", ctx->nested); + + // End block + fprintf(f, "end_%d:\n", ctx->nested); } void gentree(FILE *f, struct genctx *ctx, ast_node_t *n) { - if ( !n ) { - return; - } - if ( ctx == NULL ) { - ctx = malloc(sizeof(struct genctx)); - ctx->nested = 0; - } - switch (n->t) { - case TSTM_LIST: - gentree(f, ctx, n->list.children[0]); - gentree(f, ctx, n->list.children[1]); - break; - case TIF: - genif(f, ctx, n); - break; - case TIDENT: - fprintf(f, "%s\n", n->ident); - break; - case TFOR: - genfor(f, ctx, n); - break; - case TWHILE: - genwhile(f, ctx, n); - break; - default: - return; - } + if (!n) { + return; + } + if (ctx == NULL) { + ctx = malloc(sizeof(struct genctx)); + ctx->nested = 0; + } + switch (n->t) { + case TSTM_LIST: + gentree(f, ctx, n->list.children[0]); + gentree(f, ctx, n->list.children[1]); + break; + case TIF: + genif(f, ctx, n); + break; + case TIDENT: + fprintf(f, "%s\n", n->ident); + break; + case TFOR: + genfor(f, ctx, n); + break; + case TWHILE: + genwhile(f, ctx, n); + break; + default: + return; + } } diff --git a/sem3/osc/miniproject/cnasm/codegen.h b/sem3/osc/miniproject/cnasm/codegen.h index 24ad6c4..14c68fe 100644 --- a/sem3/osc/miniproject/cnasm/codegen.h +++ b/sem3/osc/miniproject/cnasm/codegen.h @@ -6,9 +6,9 @@ #include "ast.h" struct genctx { - unsigned int nested; + unsigned int nested; }; void gentree(FILE *f, struct genctx *ctx, ast_node_t *n); -#endif +#endif diff --git a/sem3/osc/mm1/mm1/jmod.c b/sem3/osc/mm1/mm1/jmod.c index 472ad46..05eb7c1 100644 --- a/sem3/osc/mm1/mm1/jmod.c +++ b/sem3/osc/mm1/mm1/jmod.c @@ -7,67 +7,62 @@ static int major_num; static int busy; - static int cool_open(struct inode *inode, struct file *file) { - /* Check if we are already serving someone */ - if (busy) { - return -EBUSY; - } + /* Check if we are already serving someone */ + if (busy) { + return -EBUSY; + } - busy = 1; - return 0; + busy = 1; + return 0; } -static int cool_release (struct inode *inode, struct file *file) +static int cool_release(struct inode *inode, struct file *file) { - busy = 0; + busy = 0; - return 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; - char str[12] = "hej med dig"; - int i; - - for (i = 0; i < len; i++) { - put_user(str[i % 12], buffer++); - } + for (i = 0; i < len; i++) { + put_user(str[i % 12], buffer++); + } - return i; + return i; } -static struct file_operations file_ops = { - .owner = THIS_MODULE, - .read = cool_read, - .open = cool_open, - .release = cool_release -}; +static struct file_operations file_ops = { .owner = THIS_MODULE, + .read = cool_read, + .open = cool_open, + .release = cool_release }; -static int __init jmod_init(void) +static int __init jmod_init(void) { - printk(KERN_INFO "COOL_MODULE: Registering cooldev\n"); - - major_num = register_chrdev(0, "cooldev", &file_ops); - if (major_num < 0) { - printk(KERN_ERR "COOL_MODULE: Could not register major\n"); - return 1; - } + printk(KERN_INFO "COOL_MODULE: Registering cooldev\n"); - printk(KERN_INFO "COOL_MODULE: Got major %d\n", major_num); + major_num = register_chrdev(0, "cooldev", &file_ops); + if (major_num < 0) { + printk(KERN_ERR "COOL_MODULE: Could not register major\n"); + return 1; + } - return 0; -} + printk(KERN_INFO "COOL_MODULE: Got major %d\n", major_num); + return 0; +} -static void __exit jmod_exit(void) +static void __exit jmod_exit(void) { - printk(KERN_INFO "COOL_MODULE: Nou moe\n"); - unregister_chrdev(major_num, "cooldev"); + printk(KERN_INFO "COOL_MODULE: Nou moe\n"); + unregister_chrdev(major_num, "cooldev"); } -module_init( jmod_init ); -module_exit( jmod_exit ); - +module_init(jmod_init); +module_exit(jmod_exit); diff --git a/sem3/osc/mm1/mm2/tprog.c b/sem3/osc/mm1/mm2/tprog.c index bb04778..82c9fb2 100644 --- a/sem3/osc/mm1/mm2/tprog.c +++ b/sem3/osc/mm1/mm2/tprog.c @@ -19,26 +19,26 @@ void timer_handler(int signum) /* Calculate new alarm */ tv->tv_sec *= SPEED; if (tv->tv_sec == 0) { - /* If tv_usec is 0 set i to 1 sec otherwise half it */ - if (tv->tv_usec == 0) { - tv->tv_usec = 999999; - } else if (tv->tv_usec > ENDUSEC) { - tv->tv_usec *= SPEED; - if (tv->tv_usec < ENDUSEC) { - tv->tv_usec = ENDUSEC; - } - } else { - /* Return letting the timer be set to ENDUSEC */ - return; - } + /* If tv_usec is 0 set i to 1 sec otherwise half it */ + if (tv->tv_usec == 0) { + tv->tv_usec = 999999; + } else if (tv->tv_usec > ENDUSEC) { + tv->tv_usec *= SPEED; + if (tv->tv_usec < ENDUSEC) { + tv->tv_usec = ENDUSEC; + } + } else { + /* Return letting the timer be set to ENDUSEC */ + return; + } } printf("Set to %d and %d\n", timer.it_value.tv_sec, timer.it_value.tv_usec); /* 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); } } diff --git a/sem3/osc/mm11/regn/symtab.c b/sem3/osc/mm11/regn/symtab.c index 35799a5..9edcf72 100644 --- a/sem3/osc/mm11/regn/symtab.c +++ b/sem3/osc/mm11/regn/symtab.c @@ -4,50 +4,49 @@ unsigned int hash(char *s) { - uint32_t hv = 0; - 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); - - // Push those back on hv - hv = (hv << 4) | v; - } - // Return appropriate size - return hv % HASHSIZE; + uint32_t hv = 0; + 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); + + // Push those back on hv + hv = (hv << 4) | v; + } + // Return appropriate size + return hv % HASHSIZE; } - symnode_t *sym_insert(char *var) { - unsigned int index = hash(var); + unsigned int index = hash(var); - // Save old value - symnode_t *oldSym = symbolarray[index]; + // Save old value + symnode_t *oldSym = symbolarray[index]; - // Make new - symbolarray[index] = malloc(sizeof(symnode_t)); - if ( symbolarray[index] == NULL ) { - // If malloc failed - symbolarray[index] = oldSym; - return NULL; - } + // Make new + symbolarray[index] = malloc(sizeof(symnode_t)); + if (symbolarray[index] == NULL) { + // If malloc failed + symbolarray[index] = oldSym; + return NULL; + } - // Link old one - symbolarray[index]->next = oldSym; - symbolarray[index]->name = var; + // Link old one + symbolarray[index]->next = oldSym; + symbolarray[index]->name = var; - return symbolarray[index]; + return symbolarray[index]; } symnode_t *sym_lookup(char *var) { - unsigned int index = hash(var); - symnode_t *n = symbolarray[index]; + unsigned int index = hash(var); + symnode_t *n = symbolarray[index]; - // Look trough list - while (n != NULL && strcmp(n->name, var) != 0) { - n = n->next; - } + // Look trough list + while (n != NULL && strcmp(n->name, var) != 0) { + n = n->next; + } - return n; + return n; } diff --git a/sem3/osc/mm11/regn/symtab.h b/sem3/osc/mm11/regn/symtab.h index c61f3a8..b7b93ec 100644 --- a/sem3/osc/mm11/regn/symtab.h +++ b/sem3/osc/mm11/regn/symtab.h @@ -2,19 +2,19 @@ #define HASHSIZE 100 typedef struct symnode_struct { - char *name; - struct symnode_struct *next; - double value; + char *name; + struct symnode_struct *next; + double value; } symnode_t; symnode_t *sym_insert(char *var); symnode_t *sym_lookup(char *var); struct symnode { - struct symnode *next; - char *name; - int type; - double value; + struct symnode *next; + char *name; + int type; + double value; }; symnode_t *symbolarray[HASHSIZE]; |