Skip to content
Snippets Groups Projects
Commit 94474365 authored by krf41037's avatar krf41037
Browse files

Moved cfg dot printing to print_tac

parent d396c0d9
No related branches found
No related tags found
No related merge requests found
......@@ -111,7 +111,8 @@ int main(int argc, char *argv[]) {
}
struct mcc_tac *tac = mcc_tac_build(prog,symbol_table);
tac_print(tac, stdout);
mcc_tac_print(tac, stdout);
mcc_print_cfg_dot(tac,stdout);
mcc_symbol_table_delete_table(symbol_table);
mcc_symbol_table_delete_error_collector(ec);
......
#ifndef _GFG_H_
#define _GFG_H_
#include "mcc/tac_build.h"
#include "mcc/tac.h"
#include "mcc/symbol_table.h"
struct mcc_cfg {
int block_size;
struct mcc_cfg_basic_block *basic;
struct mcc_cfg *next;
};
struct mcc_cfg_edge {
struct mcc_cfg_basic_block *destination;
struct mcc_cfg_edge *next;
};
struct mcc_cfg_basic_block {
int id;
struct mcc_tac_entry *start;
struct mcc_tac_entry *end;
struct mcc_cfg_edge *incoming;
struct mcc_cfg_edge *outgoing;
struct mcc_cfg_basic_block *next;
};
struct mcc_cfg *mcc_create_cfg(struct mcc_tac *tac);
struct mcc_cfg_basic_block *mcc_create_cfg_basic_block(struct mcc_tac *tac);
#endif
\ No newline at end of file
......@@ -8,6 +8,8 @@
static const char *tac_type(enum mcc_tac_operation type);
void tac_print(struct mcc_tac *tac, FILE *out);
void mcc_tac_print(struct mcc_tac *tac, FILE *out);
void mcc_print_cfg_dot(struct mcc_tac *tac, FILE *out);
#endif
#include "mcc/cfg.h"
#include "mcc/tac_build.h"
#include "mcc/tac.h"
#include "mcc/symbol_table.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct mcc_cfg *mcc_create_cfg(struct mcc_tac *tac)
{
struct mcc_cfg *graph = NULL;
}
// Printing
void mcc_cfg_to_dot (FILE *out, struct mcc_cfg *graph)
{
fprintf(out, "digraph \"Control Flow Graphs\" {\n");
fprintf(out, "\tgraph [pad=\"0.5\", nodesep=\"0.5\", ranksep=\"2\"]\n");
fprintf(out, "\tnode [shape=plaintext]\n");
while(graph != NULL) {
mcc_cfg_visit(graph->basic, mcc_cfg_print_graph_visitor, out );
graph = graph->next;
}
fprintf(out, "}\n");
}
void mcc_cfg_print_graph_visitor(struct mcc_cfg_basic_block *block, void *args)
{
}
\ No newline at end of file
......@@ -6,6 +6,8 @@
#include "mcc/symbol_table_print.h"
#include "mcc/tac.h"
#define LABEL_SIZE 64
static const char *tac_type(enum mcc_tac_operation type)
{
switch (type) {
......@@ -82,7 +84,7 @@ static const char *tac_type(enum mcc_tac_operation type)
return "ERROR";
}
void tac_print(struct mcc_tac *tac, FILE *out)
void mcc_tac_print(struct mcc_tac *tac, FILE *out)
{
fprintf(out,"------------------------------\n");
fprintf(out,"- TAC -\n");
......@@ -243,4 +245,133 @@ void tac_print(struct mcc_tac *tac, FILE *out)
fprintf(out,"ERROR\n");
}
}
}
static void print_dot_begin(FILE *out)
{
assert(out);
fprintf(out, "digraph \"AST\" {\n"
"\tnodesep=0.6\n");
}
static void print_dot_end(FILE *out)
{
assert(out);
fprintf(out, "}\n");
}
static void print_dot_node(FILE *out, const void *node, const char *label)
{
assert(out);
assert(node);
assert(label);
fprintf(out, "\t\"%p\" [shape=box, label=\"%s\"];\n", node, label);
}
static void print_dot_edge(FILE *out, const void *src_node, const void *dst_node, const char *label)
{
assert(out);
assert(src_node);
assert(dst_node);
assert(label);
fprintf(out, "\t\"%p\" -> \"%p\" [label=\"%s\"];\n", src_node, dst_node, label);
}
static void print_dot_node_diamond(FILE *out, const void *node, const char *label)
{
assert(out);
assert(node);
assert(label);
fprintf(out, "\t\"%p\" [shape=diamond, label=\"%s\"];\n", node, label);
}
static void print_dot_node_label(FILE *out, int label)
{
assert(out);
assert(label);
fprintf(out, "\t\"Label%d\" [shape=box, label=\"Label%d\"];\n", label,label);
}
static void print_dot_edge_from_label(FILE *out,
const void *dst_node, int label)
{
assert(out);
assert(dst_node);
assert(label);
fprintf(out, "\t\"Label%d\" -> \"%p\" [label=\"%s\"];\n", label, dst_node,
"");
}
static void print_dot_edge_label(FILE *out, const void *src_node,
int label, bool if_else)
{
assert(out);
assert(src_node);
assert(label);
if(if_else==true){
fprintf(out, "\t\"%p\" -> \"Label%d\" [label=\"%s\"];\n", src_node, label,"YES");
}else{
fprintf(out, "\t\"%p\" -> \"Label%d\" [label=\"%s\"];\n", src_node, label,"");
}
}
void mcc_print_cfg_dot(struct mcc_tac *tac, FILE *out)
{
fprintf(out,"------------------------------\n");
fprintf(out,"- CFG DOT -\n");
fprintf(out,"------------------------------\n");
for(int i = 0; i < tac-> tac_entries->size; i++)
{
struct mcc_tac_entry *entry = tac->tac_entries->arr[i];
struct mcc_tac_entry *next_entry = tac->tac_entries->arr[i+1];
const char *op_type = tac_type(entry->tac_op);
switch (entry->tac_op)
{
case MCC_TAC_BOOL:
case MCC_TAC_IDENTIFIER_BOOL:
case MCC_TAC_BOOL_LITERAL:
case MCC_TAC_INT:
case MCC_TAC_IDENTIFIER_INT:
case MCC_TAC_INT_LITERAL:
case MCC_TAC_FLOAT:
case MCC_TAC_IDENTIFIER_FLOAT:
case MCC_TAC_FLOAT_LITERAL:
case MCC_TAC_STRING:
case MCC_TAC_IDENTIFIER_STRING:
case MCC_TAC_STRING_LITERAL:
/*
* have to do this otherwise 'label can only be part of
* a statement and a declaration is not a statement' error
*/
assert(entry->result);
char label[LABEL_SIZE] = { 0 };
snprintf(label, sizeof(label),"%s = %s (%s)\n", entry->result, entry->arg1, op_type);
print_dot_node(out,entry,label);
if(next_entry->tac_op == MCC_TAC_LABEL){
print_dot_edge_label(out,entry,next_entry->result,false);
}
break;
default: fprintf(out, "error %d\n", op_type);
break;
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment