4-variable-simplifier/get_cached_simplifications.c

194 lines
3.8 KiB
C

#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <defines.h>
#include <debug.h>
#include <cmdln.h>
#include "get_cached_simplifications.h"
#include "calculate_simplifications.h"
static struct path { char data[PATH_MAX]; } get_path(
const struct cmdln_flags* flags)
{
struct path path = {};
strcat(path.data, ".simplifier-cache-1");
if (flags->use_operators.not)
strcat(path.data, "-not");
if (flags->use_operators.or)
strcat(path.data, "-or");
if (flags->use_operators.orn)
strcat(path.data, "-orn");
if (flags->use_operators.nor)
strcat(path.data, "-nor");
if (flags->use_operators.and)
strcat(path.data, "-and");
if (flags->use_operators.andn)
strcat(path.data, "-andn");
if (flags->use_operators.nand)
strcat(path.data, "-nand");
if (flags->use_operators.xor)
strcat(path.data, "-xor");
if (flags->use_operators.nxor)
strcat(path.data, "-nxor");
if (flags->use_operators.lt)
strcat(path.data, "-lt");
if (flags->use_operators.lte)
strcat(path.data, "-lte");
if (flags->use_operators.gt)
strcat(path.data, "-gt");
if (flags->use_operators.gte)
strcat(path.data, "-gte");
if (flags->use_operators.ternary)
strcat(path.data, "-ternary");
if (flags->use_operators.assignment)
strcat(path.data, "-assignment");
strcat(path.data, ".bin");
return path;
}
struct expr (*get_cached_simplifications(const struct cmdln_flags* flags))[N]
{
struct path path = get_path(flags);
int fd = -1;
bool rebuild = flags->force_rebuild;
struct expr (*retval)[N];
if (!rebuild)
{
fd = open(path.data, O_RDONLY);
if (fd < 0)
{
if (errno == ENOENT)
{
rebuild = true;
}
else
{
TODO;
exit(1);
}
}
}
if (rebuild)
{
if (!flags->quiet)
{
puts(""
"I'll have to build up my cache of simplifications" "\n"
"I'll only have to do this once." "\n"
"\n"
"This may take a while." "\n"
"");
}
if (!flags->quiet && !flags->verbose)
{
puts("Re-run with '-v' to watch progress");
}
if (!flags->quiet && !flags->assume_yes)
{
puts("");
puts("Any input to start:"), getchar();
puts("Started.");
}
struct simplifications simps0 = calculate_simplifications(flags);
if (flags->use_operators.assignment)
{
TODO;
}
fd = open(path.data, O_RDWR | O_TRUNC | O_CREAT, 0664);
if (fd < 0)
{
TODO;
exit(1);
}
if (write(fd, &simps0, sizeof(simps0)) < (ssize_t) sizeof(simps0))
{
printf("%s: write() to cache failed: %m\n", flags->argv0);
exit(1);
}
if (flags->use_operators.assignment)
{
TODO;
}
}
assert(fd > 0);
void* ptr = mmap(
/* addr: */ NULL,
/* len: */ flags->use_operators.assignment
? sizeof(struct simplifications) * ((1 << 16) + 1)
: sizeof(struct simplifications),
/* prot: */ PROT_READ,
/* flags: */ MAP_PRIVATE,
/* fildes: */ fd,
/* off: */ 0);
if (ptr == (void*) -1)
{
TODO;
}
close(fd);
return ptr;
}