Skip to content

Commit ef0a39c

Browse files
committed
Input / output files
1 parent 3429c86 commit ef0a39c

File tree

9 files changed

+191
-184
lines changed

9 files changed

+191
-184
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/.idea
22
/cmake-*
33
/build
4-
/temp
4+
/temp
5+
6+
/benchmark.yaml

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.15)
22
project(stackvm)
33

4-
set(CMAKE_CXX_STANDARD 23)
4+
set(CMAKE_CXX_STANDARD 20)
55

66
find_package(LLVM REQUIRED CONFIG)
77

config.yaml

+1-23
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,59 @@ benchmarks:
33
name: hello world
44
src: samples/hello.b
55
output: a0b65939670bc2c010f4d5d6a0b3e4e4590fb92b
6-
batch: 46945466
7-
baseline: 111
86

97
-
108
name: semihash
119
src: samples/semihash.b
1210
input: samples/semihash.in
1311
output: 5ddd3bb20b4b20d7cb776adc9823f9286a5a9f96
14-
batch: 2870
15-
baseline: 1267185
1612

1713
-
1814
name: harmonic
1915
src: samples/harmonic.b
2016
output: b6589fc6ab0dc82cf12099d1c2d40ab994e8410c
21-
batch: 650176
22-
baseline: 7438
2317

2418
-
2519
name: 8 bit cell size
2620
src: samples/cell_size.b
2721
width: 8
2822
output: 94cfcfa4ce6f757b8211ef5a2882076d7fc4f7bd
29-
batch: 146388207
30-
baseline: 34
3123

3224
-
3325
name: 16 bit cell size
3426
src: samples/cell_size.b
3527
width: 16
3628
output: d2bb3c52eaf4286637d9a3df1e6024c2aaeb7728
37-
batch: 139989087
38-
baseline: 35
3929

4030
-
4131
name: 32 bit cell size
4232
src: samples/cell_size.b
4333
width: 32
4434
output: 85c954c43002504b11cc5e239de7c6d0cc96e522
45-
batch: 160015413
46-
baseline: 33
4735

4836
-
4937
name: mandlebrot
5038
src: samples/mandlebrot.b
5139
output: b77a017f811831f0b74e0d69c08b78e620dbda2b
52-
batch: 8
53-
baseline: 675272993
5440

5541
-
5642
width: 16
5743
name: synthetic alias
5844
src: samples/synthetic_alias.b
5945
output: 58a74e7d129257f8b0be51f9aa10f8cbb60db621
60-
batch: 17768
61-
baseline: 282086
6246

6347
-
6448
name: self interpreter
6549
src: samples/self_interpreter.b
6650
input: samples/hello.b
6751
output: a0b65939670bc2c010f4d5d6a0b3e4e4590fb92b
68-
batch: 11575
69-
baseline: 437539
7052

7153
-
7254
name: signed math
7355
src: samples/signedmath.b
7456
output: 6a0967cbc5595edf4ef606ea16ca9d314c76b8bc
75-
batch: 1895747
76-
baseline: 2508
7757

7858
-
7959
name: sierpinski
8060
src: samples/sierpinski.b
81-
output: 74e24c1e949d69e2cd47c5905140cc068de61b9e
82-
batch: 63
83-
baseline: 80151435
61+
output: 74e24c1e949d69e2cd47c5905140cc068de61b9e

main.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <iostream>
22
#include <string>
3-
#include <filesystem>
43
#include <clipp.h>
54
#include <fstream>
65

@@ -17,7 +16,7 @@ void printUsageAndExit(group &cli) {
1716

1817
std::cerr << "Usage:\n" << usage_lines(cli, "stackvm", format) << std::endl;
1918
std::cerr << "Parameters:\n" << documentation(cli, format) << std::endl;
20-
exit(1);
19+
std::exit(1);
2120
}
2221

2322
int main(int argc, char** argv) {
@@ -30,6 +29,8 @@ int main(int argc, char** argv) {
3029

3130
auto cli = (
3231
option("-h", "--help").set(help) % "print this help message",
32+
(option("-i", "--input") & value("file", config.inputFile)) % "the file to read from",
33+
(option("-o", "--output") & value("file", config.outputFile)) % "the file to write to",
3334
(option("-w", "--width") & value("bits", config.cellWidth)) % "width of cells in bits\ndefault = 8",
3435
(option("-e", "--eof") & value("value", config.cellWidth)) % "value of getchar when eof is reached\ndefault = 0",
3536
(option("-m", "--memory") & value("size", memory)) % "how much virtual memory (in bytes) to reserve to the left and right\ndefault = 128MiB,128MiB",
@@ -71,7 +72,7 @@ int main(int argc, char** argv) {
7172
std::cerr << std::system_error(
7273
errno, std::system_category(), "Error: Failed to open \"" + program + "\""
7374
).what() << std::endl;
74-
exit(1);
75+
std::exit(1);
7576
}
7677

7778
std::stringstream contents;

src/bfvm.cc

+22-10
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ struct CommandLineDiag : Diag {
9393
void artifact(const std::string &name, const DiagCollector &contents) override {
9494
if (config.dump.empty()) return;
9595
std::ofstream file = Util::openFile(config.dump + "/" + name, true);
96-
auto contentsStr = contents();
97-
file.write(contentsStr.data(), contentsStr.size());
96+
auto contentsStr = contents();
97+
file.write(contentsStr.data(), contentsStr.size());
9898
file.close();
9999
}
100100
};
@@ -117,6 +117,8 @@ struct IO {
117117
std::string outputRecording;
118118
InputState inputState = IS_NONE;
119119
#endif
120+
FILE *inputFile;
121+
FILE *outputFile;
120122
int eofValue = 0;
121123
};
122124

@@ -139,7 +141,7 @@ struct CompileContext {
139141
!std::filesystem::create_directory(config.dump)
140142
) {
141143
std::cerr << "Error: Failed to create output directory \"" + config.dump + "\"";
142-
exit(1);
144+
std::exit(1);
143145
}
144146
diag->timeline = Util::openFile(config.dump + "/timeline.txt", false);
145147
diag->timeline << "Time,Event,Label" << "\r\n";
@@ -192,6 +194,16 @@ struct CompileContext {
192194

193195
void run(BFVM::Handle &handle) {
194196
IO io;
197+
if (config.inputFile.empty()) {
198+
io.inputFile = stdin;
199+
} else {
200+
io.inputFile = fopen(config.inputFile.c_str(), "r");
201+
}
202+
if (config.outputFile.empty()) {
203+
io.outputFile = stdout;
204+
} else {
205+
io.outputFile = fopen(config.outputFile.c_str(), "w");
206+
}
195207
io.eofValue = config.eofValue;
196208
#ifndef NDIAG
197209
if (config.profile >= 0) {
@@ -243,7 +255,7 @@ int bfGetchar(IO *io) {
243255
return io->inputRecording[io->inputIndex++];
244256
}
245257
case IS_RECORDING: {
246-
int c = getchar();
258+
int c = fgetc(io->inputFile);
247259
if (c == -1) {
248260
return io->eofValue;
249261
} else {
@@ -253,23 +265,23 @@ int bfGetchar(IO *io) {
253265
} default: break;
254266
}
255267
#endif
256-
int c = getchar();
268+
int c = fgetc(io->inputFile);
257269
if (c == -1) {
258270
return io->eofValue;
259271
} else {
260272
return c;
261273
}
262274
}
263275

264-
void bfPutchar(IO *context, int x) {
276+
void bfPutchar(IO *io, int x) {
265277
#ifndef NDIAG
266-
if (context->inputState == IS_READING) {
278+
if (io->inputState == IS_READING) {
267279
return;
268-
} else if (context->inputState == IS_RECORDING) {
269-
context->outputRecording.push_back(x);
280+
} else if (io->inputState == IS_RECORDING) {
281+
io->outputRecording.push_back(x);
270282
}
271283
#endif
272-
putchar(x);
284+
fputc(x, io->outputFile);
273285
}
274286

275287
struct InterpreterImpl : public BFVM::Interpreter {

src/bfvm.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace BFVM {
1111
std::string dump;
1212
int cellWidth = 8;
1313
uint32_t eofValue = 0;
14+
std::string inputFile;
15+
std::string outputFile;
1416
#ifndef NDIAG
1517
int profile = -1;
1618
bool quiet = false;

src/diagnostics.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ std::ofstream Util::openFile(const std::string &path, bool binary) {
5959
std::cerr << std::system_error(
6060
errno, std::system_category(), "Error: Failed to open \"" + path + "\""
6161
).what() << std::endl;
62-
exit(1);
62+
std::exit(1);
6363
}
6464
return file;
6565
}

0 commit comments

Comments
 (0)