Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:disintar/ton into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tvorogme committed May 12, 2024
2 parents f2b82c3 + cc5d3f6 commit 0836d5f
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test/regression-tests.cache/
**/*build*/
.idea
.vscode
.DS_Store
zlib/
libsodium/
libmicrohttpd-0.9.77-w32-bin/
Expand Down
14 changes: 11 additions & 3 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,17 @@ target_include_directories(ton_block PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SO
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/block> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(ton_block PUBLIC ton_crypto tdutils tdactor tl_api)

add_executable(func func/func-main.cpp ${FUNC_LIB_SOURCE})
target_include_directories(func PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(func PUBLIC ton_crypto src_parser git ton_block)
add_library(func-lib STATIC ${FUNC_LIB_SOURCE})
target_include_directories(func-lib PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(func-lib PUBLIC ton_crypto src_parser ton_block)
if (USE_EMSCRIPTEN)
target_link_options(func-lib PRIVATE -fexceptions)
target_compile_options(func-lib PRIVATE -fexceptions)
endif()
set_target_properties(func-lib PROPERTIES OUTPUT_NAME func)

add_executable(func func/func-main.cpp)
target_link_libraries(func PUBLIC func-lib git)
if (WINGETOPT_FOUND)
target_link_libraries_system(func wingetopt)
endif()
Expand Down
36 changes: 33 additions & 3 deletions crypto/func/func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace funC {
int verbosity, indent, opt_level = 2;
bool stack_layout_comments, op_rewrite_comments, program_envelope, asm_preamble;
bool interactive = false;
bool interactive_from_string = false;
GlobalPragma pragma_allow_post_modification{"allow-post-modification"};
GlobalPragma pragma_compute_asm_ltr{"compute-asm-ltr"};
std::string generated_from, boc_output_filename;
Expand Down Expand Up @@ -217,20 +218,49 @@ int func_proceed(const std::vector<std::string> &sources, std::ostream &outs, st
funC::indent = 1;
}

sym::symbols.clear();
for(int i = 0; i < sym::symbols.hprime; i++){
sym::sym_def[i] = nullptr;
sym::global_sym_def[i] = nullptr;
}
sym::symbol_stack.clear();
sym::scope_opened_at.clear();
sym::scope_level = 0;

while(!funC::inclusion_locations.empty()){
funC::inclusion_locations.pop();
}
funC::source_files.clear();
funC::source_fdescr.clear();

funC::glob_func_cnt = 0;
funC::undef_func_cnt = 0;
funC::glob_var_cnt = 0;
funC::glob_func.clear();
funC::glob_func.clear();
funC::generated_from = "";

funC::define_keywords();
funC::define_builtins();

int ok = 0, proc = 0;
try {
for (auto src : sources) {
ok += funC::parse_source_file(src.c_str(), {}, true);
proc++;
if (!funC::interactive_from_string) {
for (auto src : sources) {
ok += funC::parse_source_file(src.c_str(), {}, true);
proc++;
}
}
if (funC::interactive) {
funC::generated_from += "stdin ";
ok += funC::parse_source_stdin();
proc++;
}
if (funC::interactive_from_string) {
funC::generated_from += "string ";
ok = funC::parse_source_string(sources[0]);
proc++;
}
if (ok < proc) {
throw src::Fatal{"output code generation omitted because of errors"};
}
Expand Down
7 changes: 6 additions & 1 deletion crypto/func/func.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#pragma once
#include <vector>
#include <map>
#include <string>
#include <set>
#include <stack>
Expand Down Expand Up @@ -888,9 +889,13 @@ class ReadCallback {
bool parse_source(std::istream* is, const src::FileDescr* fdescr);
bool parse_source_file(const char* filename, src::Lexem lex = {}, bool is_main = false);
bool parse_source_stdin();
bool parse_source_string(const std::string &source);

extern std::vector<const src::FileDescr*> source_fdescr;
extern std::map<std::string, src::FileDescr*> source_files;
extern std::stack<src::SrcLocation> inclusion_locations;


/*
*
* EXPRESSIONS
Expand Down Expand Up @@ -1738,7 +1743,7 @@ void define_builtins();


extern int verbosity, indent, opt_level;
extern bool stack_layout_comments, op_rewrite_comments, program_envelope, asm_preamble, interactive;
extern bool stack_layout_comments, op_rewrite_comments, program_envelope, asm_preamble, interactive, interactive_from_string;
extern std::string generated_from, boc_output_filename;
extern ReadCallback::Callback read_callback;

Expand Down
11 changes: 11 additions & 0 deletions crypto/func/parse-func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,7 @@ bool parse_source_file(const char* filename, src::Lexem lex, bool is_main) {
}

auto path_res = read_callback(ReadCallback::Kind::Realpath, filename);

if (path_res.is_error()) {
auto error = path_res.move_as_error();
lex.error(error.message().c_str());
Expand Down Expand Up @@ -1815,4 +1816,14 @@ bool parse_source_stdin() {
return parse_source(&std::cin, cur_source);
}

bool parse_source_string(const std::string &source) {
src::FileDescr* cur_source = new src::FileDescr{"string"};
cur_source->is_main = true;
source_fdescr.push_back(cur_source);

std::stringstream stream{source};
return parse_source(&stream, cur_source);
}


} // namespace funC
6 changes: 5 additions & 1 deletion tvm-python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_library(combined INTERFACE)
target_link_libraries(combined INTERFACE
tonlib overlay tdutils tdactor adnl tl_api dht terminal
rldp catchain validatorsession full-node emulator emulator_static validator-disk validator-hardfork ton_validator
validator-hardfork fift-lib memprof)
validator-hardfork fift-lib func-lib memprof)
bundle_static_library_deps(combined combined_bundled)

get_target_property(deps combined INTERFACE_LINK_LIBRARIES)
Expand Down Expand Up @@ -48,6 +48,8 @@ set(PYTHON_EMULATOR_SOURCE
PyTVM.h
PyFift.cpp
PyFift.h
PyFunc.cpp
PyFunc.h
PyStack.h
PyStack.cpp
PyEmulator.cpp
Expand All @@ -69,6 +71,8 @@ target_link_libraries(python_ton PRIVATE -static-libstdc++
${BLST_LIB} ${SECP256K1_LIBRARY} ${JEMALLOC_LIBRARIES}
OpenSSL::Crypto ${SODIUM_LIBRARY_RELEASE})

target_link_libraries(python_ton PUBLIC func-lib git)

target_include_directories(python_ton PRIVATE ${include_dirs})

install(TARGETS python_ton
Expand Down
62 changes: 62 additions & 0 deletions tvm-python/PyFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 Disintar LLP / [email protected]

#include "PyFunc.h"

class CompilationException : public std::runtime_error {
public:
explicit CompilationException(const std::string& message)
: std::runtime_error(message) {}
};

std::string compile_from_sources(const std::vector<std::string>& sources) {
std::ostringstream output;
funC::read_callback = funC::fs_read_callback;
int result = funC::func_proceed(sources, output, std::cerr);
if (result != 0) {
throw CompilationException("Compilation failed!");
}
return output.str();
}

std::string func_to_asm(const std::vector<std::string>& sources,
bool preamble,
int indent,
bool verbosity,
int optimization,
bool envelope,
bool stack_comments,
bool op_comments
) {
funC::asm_preamble = preamble;
funC::indent = indent;
if (verbosity) ++funC::verbosity;
funC::opt_level = optimization;
funC::program_envelope = envelope;
funC::stack_layout_comments = stack_comments;
funC::op_rewrite_comments = op_comments;

std::ostringstream err;
std::string res = compile_from_sources(sources);
return res;
}

std::string func_string_to_asm(const std::string& source,
bool preamble,
int indent,
bool verbosity,
int optimization,
bool envelope,
bool stack_comments,
bool op_comments
) {
funC::asm_preamble = preamble;
funC::indent = indent;
if (verbosity) ++funC::verbosity;
funC::opt_level = optimization;
funC::program_envelope = envelope;
funC::stack_layout_comments = stack_comments;
funC::op_rewrite_comments = op_comments;
funC::interactive_from_string = true;
std::string res = compile_from_sources(std::vector<std::string>{source});
return res;
}
34 changes: 34 additions & 0 deletions tvm-python/PyFunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <string>
#include <stdexcept>
#include "td/utils/Status.h"
#include <utility>
#include "crypto/func/func.h"
#include "crypto/fift/Fift.h"
#include "crypto/fift/utils.h"
#include "PyCell.h"


#ifndef TON_FUNC_H
#define TON_FUNC_H

std::string compile_from_sources(const std::vector<std::string>& sources, std::ostringstream& errors);
std::string func_to_asm(const std::vector<std::string>& sources,
bool preamble = true,
int indent = 0,
bool verbosity = false,
int optimization = 0,
bool envelope = true,
bool stack_comments = true,
bool op_comments = false
);
std::string func_string_to_asm(const std::string& source,
bool preamble = true,
int indent = 0,
bool verbosity = false,
int optimization = 0,
bool envelope = true,
bool stack_comments = true,
bool op_comments = false
);

#endif //TON_FIFT_H
21 changes: 20 additions & 1 deletion tvm-python/python_ton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include "tvm-python/PyTools.h"
#include "tvm-python/PyTVM.h"
#include "tvm-python/PyFift.h"
#include "tvm-python/PyFunc.h"
#include "tvm-python/PyStack.h"
#include "tvm-python/PySmcAddress.h"
#include "tvm-python/PyKeys.h"
#include "tvm-python/PyLiteClient.h"
#include "crypto/tl/tlbc-data.h"
#include "crypto/func/func.h"
#include "td/utils/optional.h"
#include "tl/generate/auto/tl/tonlib_api.h"

Expand Down Expand Up @@ -247,6 +249,23 @@ PYBIND11_MODULE(python_ton, m) {
m.def("deserialize_stack_entry", deserialize_stack_entry, py::arg("cell_slice"));
m.def("deserialize_stack", deserialize_stack, py::arg("cell_slice"));

m.def("func_to_asm", func_to_asm, py::arg("source_files: list[str]"),
py::arg("preamble") = false,
py::arg("indent") = 0,
py::arg("verbosity") = false,
py::arg("optimization") = 2,
py::arg("envelope") = true,
py::arg("stack_comments") = false,
py::arg("op_comments") = false);
m.def("func_string_to_asm", func_string_to_asm, py::arg("source_string: str"),
py::arg("preamble") = false,
py::arg("indent") = 0,
py::arg("verbosity") = false,
py::arg("optimization") = 2,
py::arg("envelope") = true,
py::arg("stack_comments") = false,
py::arg("op_comments") = false);

py::class_<PyStackInfo>(m, "PyStackInfo", py::module_local())
.def_readwrite("stack", &PyStackInfo::stack)
.def_readwrite("gas_consumed", &PyStackInfo::gas_consumed)
Expand Down Expand Up @@ -419,7 +438,7 @@ PYBIND11_MODULE(python_ton, m) {
})
.def_property_readonly("last",
[](const ton::lite_api::liteServer_masterchainInfoExt& obj) -> const ton::BlockIdExt {
return std::move(ton::create_block_id(std::move(obj.last_)));
return ton::create_block_id(std::move(obj.last_));
});

py::class_<block::AccountState::Info>(m, "block_AccountState_Info", py::module_local())
Expand Down

0 comments on commit 0836d5f

Please sign in to comment.