Skip to content

Instantly share code, notes, and snippets.

@Angold-4
Created April 17, 2022 09:01
Show Gist options
  • Save Angold-4/a5e69555ba6cb8d7ceca4c551e40e8c2 to your computer and use it in GitHub Desktop.
Save Angold-4/a5e69555ba6cb8d7ceca4c551e40e8c2 to your computer and use it in GitHub Desktop.
Bootstrapping
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
// We need 4 arguments "program source -o binary"
if (argc < 4) {
printf("Insufficient arguments\n");
return 1;
}
char * sourceFilename = argv[1];
char * destFilename = argv[3];
FILE * inputFile = fopen(sourceFilename, "r");
if(inputFile == NULL) {
printf("File does not exist");
return 1;
}
// Get source file size
fseek(inputFile, 0 , SEEK_END);
int sourceFileSize = ftell(inputFile);
rewind(inputFile);
// Read source file into buffer with one extra byte to accomodate '\0'
int actualBuffSizeRequired = sourceFileSize + 1;
char * buffer = (char *) malloc(actualBuffSizeRequired);
buffer[sourceFileSize] = '\0';
int read = fread(buffer, sizeof(char), sourceFileSize, inputFile);
fclose(inputFile);
/* We pass the source code to GCC as the backend compiler */
char compileCommand[500];
// Generate compile command, tell GCC to assume C language and get source code via stdin
snprintf(compileCommand, 500, "gcc -o %s -xc -", destFilename);
FILE * gccStdin;
gccStdin = popen(compileCommand, "w");
// Pass source code to GCC via stdin
fwrite(buffer, sizeof(char), sourceFileSize, gccStdin);
// Print actual source code used in compilation for reference
printf("%s\n", buffer);
pclose(gccStdin);
free(buffer);
}
#include <stdio.h>
int main() {
char text[] = "hello world";
printf("%s\n", text);
return 0;
}
#include <stdio.h>
int main() {
uint1 text[] = "hello world";
printf("%s\n", text);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
// We need 4 arguments "program source -o binary"
if(argc < 4){
printf("Insufficient arguments\n");
return 1;
}
char * sourceFilename = argv[1];
char * destFilename = argv[3];
FILE * inputFile = fopen(sourceFilename, "r");
if(inputFile == NULL){
printf("File does not exist");
return 1;
}
// Get source file size
fseek(inputFile, 0 , SEEK_END);
int sourceFileSize = ftell(inputFile);
rewind(inputFile);
// Read source file into buffer with one extra byte to accomodate '\0'
int actualBuffSizeRequired = sourceFileSize + 1;
char * buffer = (char *) malloc(actualBuffSizeRequired);
buffer[sourceFileSize] = '\0';
int read = fread(buffer, sizeof(char), sourceFileSize, inputFile);
fclose(inputFile);
/* Our custom X language with uint1 data type */
char newDataType[6] = {'u', 'i', 'n', 't', '1', '\0'};
char * whereUint;
//Replace all instances of uint1 with char
while ((whereUint = strstr(buffer, newDataType)) != NULL){
strncpy(whereUint, "char ", 5);
}
/* We pass the source code to GCC as the backend compiler */
char compileCommand[500];
// Generate compile command, tell GCC to assume C language and get source code via stdin
snprintf(compileCommand, 500, "gcc -o %s -xc -", destFilename);
FILE * gccStdin;
gccStdin = popen(compileCommand, "w");
// Pass source code to GCC via stdin
fwrite(buffer, sizeof(char), sourceFileSize, gccStdin);
// Print actual source code used in compilation for reference
printf("%s\n", buffer);
pclose(gccStdin);
free(buffer);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, uint1 *argv[]) {
// We need 4 arguments "program source -o binary"
if (argc < 4) {
printf("Insufficient arguments\n");
return 1;
}
uint1 * sourceFilename = argv[1];
uint1 * destFilename = argv[3];
FILE * inputFile = fopen(sourceFilename, "r");
if (inputFile == NULL) {
printf("File does not exist");
return 1;
}
// Get source file size
fseek(inputFile, 0 , SEEK_END);
int sourceFileSize = ftell(inputFile);
rewind(inputFile);
// Read source file into buffer with one extra byte to accomodate '\0'
int actualBuffSizeRequired = sourceFileSize + 1;
uint1 * buffer = (uint1 *) malloc(actualBuffSizeRequired);
buffer[sourceFileSize] = '\0';
int read = fread(buffer, sizeof(uint1), sourceFileSize, inputFile);
fclose(inputFile);
/* Our custom X language with uint1 data type */
uint1 newDataType[6] = {'u', 'i', 'n', 't', '1', '\0'};
uint1 * whereUint;
// Replace all instances of uint1 with char
while ((whereUint = strstr(buffer, newDataType)) != NULL) {
strncpy(whereUint, "char ", 5);
}
/* We pass the source code to GCC as the backend compiler */
uint1 compileCommand[500];
//Generate compile command, tell GCC to assume C language and get source code via stdin
snprintf(compileCommand, 500, "gcc -o %s -xc -", destFilename);
FILE * gccStdin;
gccStdin = popen(compileCommand, "w");
//Pass source code to GCC via stdin
fwrite(buffer, sizeof(uint1), sourceFileSize, gccStdin);
//Print actual source code used in compilation for reference
printf("%s\n", buffer);
pclose(gccStdin);
free(buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment