Start adding binary output

This commit is contained in:
Melon 2021-05-05 13:22:14 +01:00
parent 1b7373b617
commit 33f19530d4
4 changed files with 90 additions and 16 deletions

View File

@ -1,4 +1,4 @@
sourcefiles := $(shell find src/ -name *.c) sourcefiles := $(shell find src/impl/ -name *.c)
all: all:
@ -6,7 +6,7 @@ all:
build-main: build-main:
mkdir -p dist/ && \ mkdir -p dist/ && \
gcc -o dist/bf2c ${sourcefiles} gcc -o dist/bf2c -I src/intf/ ${sourcefiles}
package: package:
./scripts/package-bf2c.sh ./scripts/package-bf2c.sh

View File

@ -1,39 +1,74 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "popen2.h"
int is_arg(int ac, char **argv, char *arg) { int is_arg(int ac, char **argv, char *arg) {
if (ac < 2) { if (ac < 2) return 0;
return 0; for(int x=1; x < ac; x++)
} if (0 == strcmp(argv[x], arg))
for(int x=1; x < ac; x++) {
if (0 == strcmp(argv[x], arg)) {
return x; // return position of arg return x; // return position of arg
}
}
return 0; // arg not present return 0; // arg not present
} }
void addValueChange(FILE *out, char *variable, bool increment, int value) { void addValueChange(FILE *out, char *variable, bool increment, int value) {
if(increment) { if(increment) {
if(value==1) fprintf(out, "++%s;", variable); if(value==1) fprintf(out, "++%s;\n", variable);
else fprintf(out, "%s+=%d;", variable, value); else fprintf(out, "%s+=%d;\n", variable, value);
} else { } else {
if(value==1) fprintf(out, "--%s;", variable); if(value==1) fprintf(out, "--%s;\n", variable);
else fprintf(out, "%s-=%d;", variable); else fprintf(out, "%s-=%d;\n", variable);
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv) {
{
FILE *in = stdin, *out = stdout; FILE *in = stdin, *out = stdout;
int a; int a;
int b; int b;
int c; int c;
int cellsize = 30000; int cellsize = 30000;
bool optimise = false; bool optimise = false;
bool binary = false;
if(is_arg(argc, argv, "-h") || is_arg(argc, argv, "--help")) {
printf("=== bf2c ===\n");
printf("Usage: bf2c [-o] [-b] [-f filename]\n");
printf("\n");
printf(" -o Optimise add, subtract and pointer operations\n");
printf(" -b Compile generated C code to binary using gcc\n");
printf(" -f filename Output to a file instead of stdout\n");
return 0;
}
if(is_arg(argc, argv, "-o")) optimise = true; if(is_arg(argc, argv, "-o")) optimise = true;
if(is_arg(argc, argv, "-b")) binary = true;
char *z = "a.out";
int y=is_arg(argc, argv, "-f");
if(y) {
if(argc>y+1 && argv[y+1][0]!='-') {
z = argv[y+1];
} else {
fprintf(stderr, "Argument '-f' requires a value to specific the filename");
return 1;
}
out = fopen(z, "w");
}
if(binary) {
char *cmd = "gcc";
char *ar[7] = {0};
ar[0] = cmd;
ar[1] = "-o";
ar[2] = z;
ar[3] = "-x";
ar[4] = "c";
ar[5] = "-";
ar[6] = NULL;
int *fwd;
popen2(ar,fwd,NULL);
out = fdopen(*fwd, "w");
}
fprintf(out, fprintf(out,
"#include <stdio.h>\n" "#include <stdio.h>\n"
@ -84,7 +119,7 @@ int main(int argc, char **argv)
} }
// Reset for next loop // Reset for next loop
a=1; a = 1;
b = c; b = c;
} }
break; break;

36
src/impl/popen2.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define READ 0
#define WRITE 1
// omg thanks
// https://stackoverflow.com/a/12788169/10719432
pid_t popen2(char *const *command, int *infp, int *outfp) {
int p_stdin[2], p_stdout[2];
pid_t pid;
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0) return -1;
pid = fork();
if (pid < 0) return pid;
else if (pid == 0) {
close(p_stdin[WRITE]);
dup2(p_stdin[READ], READ);
close(p_stdout[READ]);
dup2(p_stdout[WRITE], WRITE);
execvp(command[0], command);
exit(1);
}
if (infp == NULL) close(p_stdin[WRITE]);
else *infp = p_stdin[WRITE];
if (outfp == NULL) close(p_stdout[READ]);
else *outfp = p_stdout[READ];
return pid;
}

3
src/intf/popen2.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
#include <unistd.h>
pid_t popen2(char *const *command, int *infp, int *outfp);