Skip to content

Commit

Permalink
[preproc] asm files parseable from stdin, 2nd preproc pass
Browse files Browse the repository at this point in the history
  • Loading branch information
sbird committed Apr 1, 2024
1 parent 7a99226 commit d5acfb4
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 82 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ endif

ifeq ($(NODEP),1)
$(C_BUILDDIR)/%.o: $(C_SUBDIR)/%.s
$(PREPROC) $< charmap.txt | $(CPP) -I include - | $(AS) $(ASFLAGS) -o $@
$(PREPROC) $< charmap.txt | $(CPP) -I include - | $(PREPROC) $$< charmap.txt -i | $(AS) $(ASFLAGS) -o $@
else
define SRC_ASM_DATA_DEP
$1: $2 $$(shell $(SCANINC) -I include -I "" $2)
$$(PREPROC) $$< charmap.txt | $$(CPP) -I include - | $$(AS) $$(ASFLAGS) -o $$@
$$(PREPROC) $$< charmap.txt | $$(CPP) -I include - | $$(PREPROC) $$< charmap.txt -i | $$(AS) $$(ASFLAGS) -o $$@
endef
$(foreach src, $(C_ASM_SRCS), $(eval $(call SRC_ASM_DATA_DEP,$(patsubst $(C_SUBDIR)/%.s,$(C_BUILDDIR)/%.o, $(src)),$(src))))
endif
Expand All @@ -385,7 +385,7 @@ endif

ifeq ($(NODEP),1)
$(DATA_ASM_BUILDDIR)/%.o: $(DATA_ASM_SUBDIR)/%.s
$(PREPROC) $< charmap.txt | $(CPP) -I include - | $(AS) $(ASFLAGS) -o $@
$(PREPROC) $< charmap.txt | $(CPP) -I include - | $(PREPROC) $$< charmap.txt -i | $(AS) $(ASFLAGS) -o $@
else
$(foreach src, $(REGULAR_DATA_ASM_SRCS), $(eval $(call SRC_ASM_DATA_DEP,$(patsubst $(DATA_ASM_SUBDIR)/%.s,$(DATA_ASM_BUILDDIR)/%.o, $(src)),$(src))))
endif
Expand Down
4 changes: 2 additions & 2 deletions tools/preproc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ CXX ?= g++
CXXFLAGS := -std=c++11 -O2 -Wall -Wno-switch -Werror

SRCS := asm_file.cpp c_file.cpp charmap.cpp preproc.cpp string_parser.cpp \
utf8.cpp
utf8.cpp io.cpp

HEADERS := asm_file.h c_file.h char_util.h charmap.h preproc.h string_parser.h \
utf8.h
utf8.h io.h

ifeq ($(OS),Windows_NT)
EXE := .exe
Expand Down
28 changes: 3 additions & 25 deletions tools/preproc/asm_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,11 @@
#include "utf8.h"
#include "string_parser.h"
#include "../../gflib/characters.h"
#include "io.h"

AsmFile::AsmFile(std::string filename) : m_filename(filename)
AsmFile::AsmFile(std::string filename, bool isStdin) : m_filename(filename)
{
FILE *fp = std::fopen(filename.c_str(), "rb");

if (fp == NULL)
FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str());

std::fseek(fp, 0, SEEK_END);

m_size = std::ftell(fp);

if (m_size < 0)
FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str());
else if (m_size == 0)
return; // Empty file

m_buffer = new char[m_size + 1];

std::rewind(fp);

if (std::fread(m_buffer, m_size, 1, fp) != 1)
FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str());

m_buffer[m_size] = 0;

std::fclose(fp);
m_buffer = ReadFileToBuffer(filename.c_str(), isStdin, &m_size);

m_pos = 0;
m_lineNum = 1;
Expand Down
2 changes: 1 addition & 1 deletion tools/preproc/asm_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum class Directive
class AsmFile
{
public:
AsmFile(std::string filename);
AsmFile(std::string filename, bool isStdin);
AsmFile(AsmFile&& other);
AsmFile(const AsmFile&) = delete;
~AsmFile();
Expand Down
48 changes: 4 additions & 44 deletions tools/preproc/c_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,16 @@
#include "char_util.h"
#include "utf8.h"
#include "string_parser.h"
#include "io.h"

CFile::CFile(const char * filenameCStr, bool isStdin)
{
FILE *fp;

if (isStdin) {
fp = stdin;
if (isStdin)
m_filename = std::string{"<stdin>/"}.append(filenameCStr);
} else {
fp = std::fopen(filenameCStr, "rb");
else
m_filename = std::string(filenameCStr);
}

std::string& filename = m_filename;

if (fp == NULL)
FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str());

m_size = 0;
m_buffer = (char *)malloc(CHUNK_SIZE + 1);
if (m_buffer == NULL) {
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename.c_str());
}

std::size_t numAllocatedBytes = CHUNK_SIZE + 1;
std::size_t bufferOffset = 0;
std::size_t count;

while ((count = std::fread(m_buffer + bufferOffset, 1, CHUNK_SIZE, fp)) != 0) {
if (!std::ferror(fp)) {
m_size += count;

if (std::feof(fp)) {
break;
}

numAllocatedBytes += CHUNK_SIZE;
bufferOffset += CHUNK_SIZE;
m_buffer = (char *)realloc(m_buffer, numAllocatedBytes);
if (m_buffer == NULL) {
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename.c_str());
}
} else {
FATAL_ERROR("Failed to read \"%s\". (error: %s)", filename.c_str(), std::strerror(errno));
}
}

m_buffer[m_size] = 0;

std::fclose(fp);
m_buffer = ReadFileToBuffer(filenameCStr, isStdin, &m_size);

m_pos = 0;
m_lineNum = 1;
Expand Down
2 changes: 0 additions & 2 deletions tools/preproc/c_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,4 @@ class CFile
void RaiseWarning(const char* format, ...);
};

#define CHUNK_SIZE 4096

#endif // C_FILE_H
50 changes: 50 additions & 0 deletions tools/preproc/io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "preproc.h"
#include "io.h"
#include <string>
#include <cstring>

char *ReadFileToBuffer(const char *filename, bool isStdin, long *size)
{
FILE *fp;
if (isStdin)
fp = stdin;
else
fp = std::fopen(filename, "rb");

if (fp == NULL)
FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename);

*size = 0;
char *buffer = (char *)malloc(CHUNK_SIZE + 1);
if (buffer == NULL) {
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename);
}

std::size_t numAllocatedBytes = CHUNK_SIZE + 1;
std::size_t bufferOffset = 0;
std::size_t count;

while ((count = std::fread(buffer + bufferOffset, 1, CHUNK_SIZE, fp)) != 0) {
if (!std::ferror(fp)) {
*size += count;

if (std::feof(fp)) {
break;
}

numAllocatedBytes += CHUNK_SIZE;
bufferOffset += CHUNK_SIZE;
buffer = (char *)realloc(buffer, numAllocatedBytes);
if (buffer == NULL) {
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename);
}
} else {
FATAL_ERROR("Failed to read \"%s\". (error: %s)", filename, std::strerror(errno));
}
}

buffer[*size] = 0;

std::fclose(fp);
return buffer;
}
8 changes: 8 additions & 0 deletions tools/preproc/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef IO_H_
#define IO_H_

#define CHUNK_SIZE 4096

char *ReadFileToBuffer(const char *filename, bool isStdin, long *size);

#endif // IO_H_
20 changes: 15 additions & 5 deletions tools/preproc/preproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ void PrintAsmBytes(unsigned char *s, int length)
}
}

void PreprocAsmFile(std::string filename)
void PreprocAsmFile(std::string filename, bool isStdin)
{
std::stack<AsmFile> stack;

stack.push(AsmFile(filename));
stack.push(AsmFile(filename, isStdin));

for (;;)
{
Expand All @@ -66,7 +66,7 @@ void PreprocAsmFile(std::string filename)
switch (directive)
{
case Directive::Include:
stack.push(AsmFile(stack.top().ReadPath()));
stack.push(AsmFile(stack.top().ReadPath(), false));
stack.top().OutputLocation();
break;
case Directive::String:
Expand Down Expand Up @@ -145,8 +145,18 @@ int main(int argc, char **argv)
if (!extension)
FATAL_ERROR("\"%s\" has no file extension.\n", argv[1]);

if ((extension[0] == 's') && extension[1] == 0)
PreprocAsmFile(argv[1]);
if ((extension[0] == 's') && extension[1] == 0) {
if (argc == 4) {
if (argv[3][0] == '-' && argv[3][1] == 'i' && argv[3][2] == '\0') {
PreprocAsmFile(argv[1], true);
} else {
FATAL_ERROR("unknown argument flag \"%s\".\n", argv[3]);
}
} else {
PreprocAsmFile(argv[1], false);
}

}
else if ((extension[0] == 'c' || extension[0] == 'i') && extension[1] == 0) {
if (argc == 4) {
if (argv[3][0] == '-' && argv[3][1] == 'i' && argv[3][2] == '\0') {
Expand Down

0 comments on commit d5acfb4

Please sign in to comment.