-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeman.c
66 lines (56 loc) · 1.26 KB
/
codeman.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
This file contains the code for string manipulations required for asm
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "enums.h"
codeBlock * genCode()
{
codeBlock *c;
c = (codeBlock *)calloc(1, sizeof(codeBlock));
c->code = (char *)calloc(256, sizeof(char));
c->len = 0;
return c;
}
// Returns a codeblock with the given string
codeBlock * Assign(const char *format, ...)
{
codeBlock *c = genCode();
va_list args;
va_start(args, format);
vsprintf(c->code, format, args);
va_end(args);
c->len = strlen(c->code);
return c;
}
// Concats two codeblocks
void Concat(codeBlock *c1, codeBlock *c2)
{
if (c2 == NULL)
{
return;
}
unsigned long int len = c1->len + c2->len+1;
if(len>c1->len){
c1->code = (char *)realloc(c1->code, len);
}
c1->len = len;
strcat(c1->code, c2->code);
free(c2->code);
free(c2);
}
// Function for adding label to codeblock
// Returns the label string for referencing the same
// in some other place
char *Label(codeBlock *c)
{
codeBlock *temp;
char *label = (char *)calloc(12, sizeof(char));
sprintf(label, "L%lu", label_count);
temp = Assign("L%lu:\n", label_count);
Concat(c, temp);
label_count++;
return label;
}