-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtolk-main.cpp
285 lines (256 loc) · 10.3 KB
/
tolk-main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
This file is part of TON Blockchain source code.
TON Blockchain is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
TON Blockchain is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
You must obey the GNU General Public License in all respects for all
of the code used other than OpenSSL. If you modify file(s) with this
exception, you may extend this exception to your version of the file(s),
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version. If you delete this exception statement
from all source files in the program, then also delete it here.
*/
#include "tolk.h"
#include "tolk-version.h"
#include "compiler-state.h"
#include "td/utils/port/path.h"
#include <getopt.h>
#include <fstream>
#include <sys/stat.h>
#ifdef TD_DARWIN
#include <mach-o/dyld.h>
#elif TD_WINDOWS
#include <windows.h>
#else // linux
#include <unistd.h>
#endif
#include "git.h"
using namespace tolk;
void usage(const char* progname) {
std::cerr
<< "usage: " << progname << " [options] <filename.tolk>\n"
"\tGenerates Fift TVM assembler code from a .tolk file\n"
"-o<fif-filename>\tWrites generated code into specified .fif file instead of stdout\n"
"-b<boc-filename>\tGenerate Fift instructions to save TVM bytecode into .boc file\n"
"-O<level>\tSets optimization level (2 by default)\n"
"-x<option-names>\tEnables experimental options, comma-separated\n"
"-S\tDon't include stack layout comments into Fift output\n"
"-e\tIncreases verbosity level (extra output into stderr)\n"
"-v\tOutput version of Tolk and exit\n";
std::exit(2);
}
static bool stdlib_folder_exists(const char* stdlib_folder) {
struct stat f_stat;
int res = stat(stdlib_folder, &f_stat);
return res == 0 && (f_stat.st_mode & S_IFMT) == S_IFDIR;
}
// getting current executable path is a complicated and not cross-platform task
// for instance, we can't just use argv[0] or even filesystem::canonical
// https://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe/1024937
static bool get_current_executable_filename(std::string& out) {
#ifdef TD_DARWIN
char name_buf[1024];
unsigned int size = 1024;
if (0 == _NSGetExecutablePath(name_buf, &size)) { // may contain ../, so normalize it
char *exe_path = realpath(name_buf, nullptr);
if (exe_path != nullptr) {
out = exe_path;
return true;
}
}
#elif TD_WINDOWS
char exe_path[1024];
if (GetModuleFileNameA(nullptr, exe_path, 1024)) {
out = exe_path;
std::replace(out.begin(), out.end(), '\\', '/'); // modern Windows correctly deals with / separator
return true;
}
#else // linux
char exe_path[1024];
ssize_t res = readlink("/proc/self/exe", exe_path, 1024 - 1);
if (res >= 0) {
exe_path[res] = 0;
out = exe_path;
return true;
}
#endif
return false;
}
// simple join "/some/folder/" (guaranteed to end with /) and "../relative/path"
static std::string join_path(std::string dir, const char* relative) {
while (relative[0] == '.' && relative[1] == '.' && relative[2] == '/') {
size_t slash_pos = dir.find_last_of('/', dir.size() - 2); // last symbol is slash, find before it
if (slash_pos != std::string::npos) {
dir = dir.substr(0, slash_pos + 1);
}
relative += 3;
}
return dir + relative;
}
static std::string auto_discover_stdlib_folder() {
// if the user launches tolk compiler from a package installed (e.g. /usr/bin/tolk),
// locate stdlib in /usr/share/ton/smartcont (this folder exists on package installation)
// (note, that paths are not absolute, they are relative to the launched binary)
// consider https://github.com/ton-blockchain/packages for actual paths
std::string executable_filename;
if (!get_current_executable_filename(executable_filename)) {
return {};
}
// extract dirname to concatenate with relative paths (separator / is ok even for windows)
size_t slash_pos = executable_filename.find_last_of('/');
std::string executable_dir = executable_filename.substr(0, slash_pos + 1);
#ifdef TD_DARWIN
std::string def_location = join_path(executable_dir, "../share/ton/ton/smartcont/tolk-stdlib");
#elif TD_WINDOWS
std::string def_location = join_path(executable_dir, "smartcont/tolk-stdlib");
#else // linux
std::string def_location = join_path(executable_dir, "../share/ton/smartcont/tolk-stdlib");
#endif
if (stdlib_folder_exists(def_location.c_str())) {
return def_location;
}
// so, the binary is not from a system package
// maybe it's just built from sources? e.g. ~/ton/cmake-build-debug/tolk/tolk
// then, check the ~/ton/crypto/smartcont folder
std::string near_when_built_from_sources = join_path(executable_dir, "../../crypto/smartcont/tolk-stdlib");
if (stdlib_folder_exists(near_when_built_from_sources.c_str())) {
return near_when_built_from_sources;
}
// no idea of where to find stdlib; let's show an error for the user, he should provide env var above
return {};
}
td::Result<std::string> fs_read_callback(CompilerSettings::FsReadCallbackKind kind, const char* query) {
switch (kind) {
case CompilerSettings::FsReadCallbackKind::Realpath: {
td::Result<std::string> res_realpath;
if (query[0] == '@' && strlen(query) > 8 && !strncmp(query, "@stdlib/", 8)) {
// import "@stdlib/filename" or import "@stdlib/filename.tolk"
std::string path = G.settings.stdlib_folder + static_cast<std::string>(query + 7);
if (strncmp(path.c_str() + path.size() - 5, ".tolk", 5) != 0) {
path += ".tolk";
}
res_realpath = td::realpath(td::CSlice(path.c_str()));
} else {
// import "relative/to/cwd/path.tolk"
res_realpath = td::realpath(td::CSlice(query));
}
if (res_realpath.is_error()) {
// note, that for non-existing files, `realpath()` on Linux/Mac returns an error,
// whereas on Windows, it returns okay, but fails after, on reading, with a message "cannot open file"
return td::Status::Error(std::string{"cannot find file "} + query);
}
return res_realpath;
}
case CompilerSettings::FsReadCallbackKind::ReadFile: {
struct stat f_stat;
int res = stat(query, &f_stat); // query here is already resolved realpath
if (res != 0 || (f_stat.st_mode & S_IFMT) != S_IFREG) {
return td::Status::Error(std::string{"cannot open file "} + query);
}
size_t file_size = static_cast<size_t>(f_stat.st_size);
std::string str;
str.resize(file_size);
FILE* f = fopen(query, "rb");
fread(str.data(), file_size, 1, f);
fclose(f);
return std::move(str);
}
default: {
return td::Status::Error("unknown query kind");
}
}
}
class StdCoutRedirectToFile {
std::unique_ptr<std::fstream> output_file;
std::streambuf* backup_sbuf = nullptr;
public:
explicit StdCoutRedirectToFile(const std::string& output_filename) {
if (!output_filename.empty()) {
output_file = std::make_unique<std::fstream>(output_filename, std::fstream::trunc | std::fstream::out);
if (output_file->is_open()) {
backup_sbuf = std::cout.rdbuf(output_file->rdbuf());
}
}
}
~StdCoutRedirectToFile() {
if (backup_sbuf) {
std::cout.rdbuf(backup_sbuf);
}
}
bool is_failed() const { return output_file && !output_file->is_open(); }
};
int main(int argc, char* const argv[]) {
int i;
while ((i = getopt(argc, argv, "o:b:O:x:Sevh")) != -1) {
switch (i) {
case 'o':
G.settings.output_filename = optarg;
break;
case 'b':
G.settings.boc_output_filename = optarg;
break;
case 'O':
G.settings.optimization_level = std::max(0, atoi(optarg));
break;
case 'x':
G.settings.parse_experimental_options_cmd_arg(optarg);
break;
case 'S':
G.settings.stack_layout_comments = false;
break;
case 'e':
G.settings.verbosity++;
break;
case 'v':
std::cout << "Tolk compiler v" << TOLK_VERSION << std::endl;
std::cout << "Build commit: " << GitMetadata::CommitSHA1() << std::endl;
std::cout << "Build date: " << GitMetadata::CommitDate() << std::endl;
std::exit(0);
case 'h':
default:
usage(argv[0]);
}
}
StdCoutRedirectToFile redirect_cout(G.settings.output_filename);
if (redirect_cout.is_failed()) {
std::cerr << "Failed to create output file " << G.settings.output_filename << std::endl;
return 2;
}
// locate tolk-stdlib/ based on env or default system paths
if (const char* env_var = getenv("TOLK_STDLIB")) {
std::string stdlib_filename = static_cast<std::string>(env_var) + "/common.tolk";
td::Result<std::string> res = td::realpath(td::CSlice(stdlib_filename.c_str()));
if (res.is_error()) {
std::cerr << "Environment variable TOLK_STDLIB is invalid: " << res.move_as_error().message().c_str() << std::endl;
return 2;
}
G.settings.stdlib_folder = env_var;
} else {
G.settings.stdlib_folder = auto_discover_stdlib_folder();
}
if (G.settings.stdlib_folder.empty()) {
std::cerr << "Failed to discover Tolk stdlib.\n"
"Probably, you have a non-standard Tolk installation.\n"
"Please, provide env variable TOLK_STDLIB referencing to tolk-stdlib/ folder.\n";
return 2;
}
if (G.is_verbosity(2)) {
std::cerr << "stdlib folder: " << G.settings.stdlib_folder << std::endl;
}
if (optind != argc - 1) {
std::cerr << "invalid usage: should specify exactly one input file.tolk" << std::endl;
return 2;
}
G.settings.read_callback = fs_read_callback;
int exit_code = tolk_proceed(argv[optind]);
return exit_code;
}