Move map into the data module.

This commit is contained in:
David Given 2016-09-26 23:03:04 +02:00
parent c4b8e00ae2
commit f552c9c7c6
6 changed files with 97 additions and 94 deletions

View file

@ -1,58 +0,0 @@
#include "mcg.h"
static void extend(struct map_node** map, int* count, int* max)
{
if (*count == *max)
{
int newmax = (*max == 0) ? 8 : (*max * 2);
struct map_node* newmap = realloc(*map, newmax * sizeof(struct map_node));
if (!newmap)
fatal("memory allocation failure");
*max = newmax;
*map = newmap;
}
}
void map_set(struct map_node** map, int* count, int* max, void* left, void* right)
{
int i;
struct map_node* node;
for (i=0; i<*count; i++)
{
node = &(*map)[i];
if (node->left == left)
{
node->right = right;
return;
}
}
extend(map, count, max);
node = &(*map)[*count];
node->left = left;
node->right = right;
(*count)++;
}
void map_add(struct map_node** map, int* count, int* max, void* left, void* right)
{
int i;
struct map_node* node;
for (i=0; i<*count; i++)
{
node = &(*map)[i];
if ((node->left == left) && (node->right == right))
return;
}
extend(map, count, max);
node = &(*map)[*count];
node->left = left;
node->right = right;
(*count)++;
}
/* vim: set sw=4 ts=4 expandtab : */

View file

@ -1,26 +0,0 @@
#ifndef MAP_H
#define MAP_H
struct map_node
{
void* left;
void* right;
};
#define _MAP(MODIFIER, NAME) \
MODIFIER struct map_node* NAME; \
MODIFIER int NAME##_count; \
MODIFIER int NAME##_max
#define MAP(NAME) _MAP(, NAME)
#define STATICMAP(NAME) _MAP(static, NAME)
#define MAP_SET(MAP, LEFT, RIGHT) \
map_set(&MAP, &MAP##_count, &MAP##_max, LEFT, RIGHT)
extern void map_set(struct map_node** map, int* count, int* max, void* left, void* right);
extern void map_add(struct map_node** map, int* count, int* max, void* left, void* right);
#endif

View file

@ -1,6 +1,6 @@
#include "mcg.h" #include "mcg.h"
STATICMAP(graph); static MAPOF(struct basicblock, struct basicblock) graph;
static ARRAYOF(struct ir) pops; static ARRAYOF(struct ir) pops;
static ARRAYOF(struct ir) pushes; static ARRAYOF(struct ir) pushes;
@ -46,7 +46,7 @@ static bool collect_outputs_cb(struct ir* ir, void* user)
struct basicblock* caller = user; struct basicblock* caller = user;
if (ir->opcode == IR_BLOCK) if (ir->opcode == IR_BLOCK)
MAP_SET(graph, caller, ir->u.bvalue); map_addp(&graph, caller, ir->u.bvalue);
return false; return false;
} }
@ -54,7 +54,7 @@ static void make_bb_graph(struct procedure* proc)
{ {
int i, j; int i, j;
graph_count = 0; graph.count = 0;
for (i=0; i<proc->blocks.count; i++) for (i=0; i<proc->blocks.count; i++)
{ {
struct basicblock* bb = proc->blocks.item[i]; struct basicblock* bb = proc->blocks.item[i];
@ -78,11 +78,11 @@ static void convert_block(struct procedure* proc, struct basicblock* bb)
/* Abort unless *every* successor block of this one starts with a pop /* Abort unless *every* successor block of this one starts with a pop
* of the same size... */ * of the same size... */
for (i=0; i<graph_count; i++) for (i=0; i<graph.count; i++)
{ {
if (graph[i].left == bb) if (graph.item[i].left == bb)
{ {
struct basicblock* outbb = graph[i].right; struct basicblock* outbb = graph.item[i].right;
ir = get_first_pop(outbb); ir = get_first_pop(outbb);
if (!ir || (ir->size != lastpush->size)) if (!ir || (ir->size != lastpush->size))
@ -92,11 +92,11 @@ static void convert_block(struct procedure* proc, struct basicblock* bb)
/* Also abort unless *every* predecessor block of the one we've /* Also abort unless *every* predecessor block of the one we've
* just found *also* ends in a push of the same size. */ * just found *also* ends in a push of the same size. */
for (j=0; j<graph_count; j++) for (j=0; j<graph.count; j++)
{ {
if (graph[j].right == outbb) if (graph.item[j].right == outbb)
{ {
struct basicblock* inbb = graph[j].left; struct basicblock* inbb = graph.item[j].left;
ir = get_last_push(inbb); ir = get_last_push(inbb);
if (!ir || (ir->size != lastpush->size)) if (!ir || (ir->size != lastpush->size))

View file

@ -6,4 +6,3 @@ clibrary {
}, },
} }

58
modules/src/data/map.c Normal file
View file

@ -0,0 +1,58 @@
#include <stdlib.h>
#include <stdbool.h>
#include "map.h"
static void append(void* mapp, void* left, void* right)
{
struct map* map = mapp;
struct map_node* node;
if (map->count == map->max)
{
int newmax = (map->max == 0) ? 8 : (map->max * 2);
struct map_node* newarray = realloc(map->item, newmax * sizeof(*newarray));
map->max = newmax;
map->item = newarray;
}
node = &map->item[map->count];
map->count++;
node->left = left;
node->right = right;
}
void map_putp(void* mapp, void* left, void* right)
{
struct map* map = mapp;
int i;
for (i=0; i<map->count; i++)
{
struct map_node* node = &map->item[i];
if (node->left == left)
{
node->right = right;
return;
}
}
append(map, left, right);
}
void map_addp(void* mapp, void* left, void* right)
{
struct map* map = mapp;
int i;
for (i=0; i<map->count; i++)
{
struct map_node* node = &map->item[i];
if ((node->left == left) && (node->right == right))
return;
}
append(map, left, right);
}

30
modules/src/data/map.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef MAP_H
#define MAP_H
struct map_node
{
void* left;
void* right;
};
/* Danger, Will Robinson! The type and the macro must be compatible. */
struct map
{
struct map_node* item;
int count;
int max;
};
#define MAPOF(LEFT, RIGHT) \
struct { \
struct { LEFT* left; RIGHT* right; }* item; \
int count; \
int max; \
}
extern void map_putp(void* map, void* left, void* right);
extern void map_addp(void* map, void* left, void* right);
#endif