Skip to content

Commit

Permalink
Fix write generation to write-as-... in function call and return stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nkchuykin committed Jun 20, 2022
1 parent 8857df1 commit e0f07a6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
41 changes: 21 additions & 20 deletions project/src/transpiler/memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ Variable MemoryManager::Add(const VarDecl *id, size_t size,
alias += "-" + to_string(duplicates[alias]);
}
duplicates[alias]++;
string type_postfix = type.substr(2);
// todo char!?
if (!((type_postfix.length() < 3 ||
(type_postfix.substr(0, 3) != "st-" &&
type_postfix.substr(0, 3) != "un-")) // todo recordDecl check
&& type_postfix != "undefinedtype" && type_postfix != "char")) {
type_postfix = "";
}
Variable var = {id,
pointer_,
size,
Expand All @@ -25,7 +33,7 @@ Variable MemoryManager::Add(const VarDecl *id, size_t size,
std::move(value),
std::move(local_name),
shift,
type.substr(2),
type_postfix,
is_initialized};
// TODO fix this plug (rework for check value == EoObject::PLUG)
if (var.value.name.empty()) {
Expand All @@ -40,6 +48,14 @@ Variable MemoryManager::AddExternal(
const VarDecl *id, size_t size, const std::string &type, std::string alias,
EOObject value, std::string local_name, size_t shift,
__attribute__((unused)) bool is_initialized) {
string type_postfix = type.substr(2);
// todo char!?
if (!((type_postfix.length() < 3 ||
(type_postfix.substr(0, 3) != "st-" &&
type_postfix.substr(0, 3) != "un-")) // todo recordDecl check
&& type_postfix != "undefinedtype" && type_postfix != "char")) {
type_postfix = "";
}
Variable var = {id,
some_non_zero_position,
size,
Expand All @@ -48,7 +64,7 @@ Variable MemoryManager::AddExternal(
std::move(value),
std::move(local_name),
shift,
type.substr(2),
type_postfix,
false};
// TODO fix this plug (rework for check value == EoObject::PLUG)
if (var.value.name.empty()) {
Expand All @@ -69,15 +85,7 @@ Variable MemoryManager::AddExternal(

bool MemoryManager::Empty() { return variables_.empty(); }

size_t MemoryManager::RealMemorySize() {
size_t result = 0;
for (const auto &v : variables_) {
if (v.alias.substr(0, 2) != "e-") {
result += v.size;
}
}
return result;
}
size_t MemoryManager::GetFreeSpacePointer() { return pointer_; }

std::vector<Variable>::const_iterator MemoryManager::begin() const {
return variables_.begin();
Expand Down Expand Up @@ -142,15 +150,8 @@ EOObject Variable::GetInitializer() const {
return EOObject(EOObjectType::EO_EMPTY);
}
EOObject res("write");
if ((type_postfix.length() < 3 ||
(type_postfix.substr(0, 3) != "st-" &&
type_postfix.substr(0, 3) != "un-")) // todo recordDecl check
&& type_postfix != "undefinedtype" && type_postfix != "char") {
{
{ // todo char!?
res.name += "-as-" + type_postfix;
}
}
if (!type_postfix.empty()) {
res.name += "-as-" + type_postfix;
}
res.nested.emplace_back(alias);
if (value.type == EOObjectType::EO_PLUG) {
Expand Down
2 changes: 1 addition & 1 deletion project/src/transpiler/memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MemoryManager {

bool Empty();

size_t RealMemorySize();
size_t GetFreeSpacePointer();

const Variable &GetVarById(const clang::VarDecl *id) const;

Expand Down
53 changes: 48 additions & 5 deletions project/src/transpiler/transpile_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ EOObject GetFunctionBody(const clang::FunctionDecl *FD) {
if (func_body == nullptr) {
return EOObject(EOObjectType::EO_EMPTY);
}
size_t shift = transpiler.glob_.RealMemorySize();
size_t shift = transpiler.glob_.GetFreeSpacePointer();
size_t param_memory_size = GetParamMemorySize(FD->parameters());
vector<Variable> all_param = ProcessFunctionParams(FD->parameters(), shift);
vector<EOObject> all_types = PrecessRecordTypes(func_body);
Expand All @@ -92,7 +92,7 @@ EOObject GetFunctionBody(const clang::FunctionDecl *FD) {
local_start.nested.emplace_back("param-start");
local_start.nested.emplace_back("param-size");
func_body_eo.nested.push_back(local_start);
size_t free_pointer = transpiler.glob_.RealMemorySize();
size_t free_pointer = transpiler.glob_.GetFreeSpacePointer();
EOObject local_empty_position("plus", "empty-local-position");
local_empty_position.nested.emplace_back("local-start");
local_empty_position.nested.emplace_back(
Expand Down Expand Up @@ -672,7 +672,11 @@ EOObject GetFunctionCallEOObject(const CallExpr *op) {
int i = 0;
if (op != nullptr && op->getNumArgs() <= var_sizes.size()) {
for (const auto *arg : op->arguments()) {
EOObject param{"write-as-" + GetTypeName(arg->getType())};
EOObject param{"write"};
string postfix = GetPostfix(arg->getType());
if (!postfix.empty()) {
param.name += "-as-" + postfix;
}
EOObject address{"address"};
address.nested.emplace_back("global-ram");
EOObject add{"plus"};
Expand Down Expand Up @@ -1016,7 +1020,11 @@ EOObject GetReturnStmtEOObject(const ReturnStmt *p_stmt) {
}
const auto *ret_value = p_stmt->getRetValue();
if (ret_value != nullptr) {
EOObject ret{"write-as-" + GetTypeName(ret_value->getType())};
EOObject ret{"write"};
string postfix = GetPostfix(ret_value->getType());
if (!postfix.empty()) {
ret.name += "-as-" + postfix;
}
EOObject address{"return"};
ret.nested.push_back(address);
ret.nested.push_back(GetStmtEOObject(ret_value));
Expand Down Expand Up @@ -1133,6 +1141,42 @@ uint64_t GetTypeSize(QualType qual_type) {
return type_size / byte_size;
}

std::string GetPostfix(QualType qual_type) {
extern ASTContext *context; // NOLINT(readability-redundant-declaration)
const clang::Type *type_ptr = qual_type.getTypePtr();
TypeInfo type_info = context->getTypeInfo(type_ptr);
uint64_t type_size = type_info.Width;
std::string str;

if (type_ptr->isBooleanType()) {
str += "bool";
return str;
}

if (type_ptr->isPointerType()) {
str += "ptr";
return str;
}

if (type_ptr->isFloatingType()) {
str += "float" + std::to_string(type_size);
return str;
}

if (!type_ptr->isSignedIntegerType()) {
str += "u";
}
if (type_ptr->isCharType()) {
str += "char";
return str;
}
if (type_ptr->isIntegerType()) {
str += "int" + std::to_string(type_size);
return str;
}
return "";
}

std::string GetTypeName(QualType qual_type) {
extern ASTContext *context; // NOLINT(readability-redundant-declaration)
const clang::Type *type_ptr = qual_type.getTypePtr();
Expand All @@ -1146,7 +1190,6 @@ std::string GetTypeName(QualType qual_type) {
}

if (type_ptr->isPointerType()) {
// str += "int64";
str += "ptr";
return str;
}
Expand Down
2 changes: 2 additions & 0 deletions project/src/transpiler/transpile_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

std::string GetTypeName(clang::QualType qual_type);

std::string GetPostfix(clang::QualType qual_type);

EOObject GetFunctionBody(const clang::FunctionDecl *FD);

EOObject GetCompoundStmt(const clang::CompoundStmt *CS, bool is_decorator);
Expand Down
2 changes: 1 addition & 1 deletion project/src/transpiler/unit_transpiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void UnitTranspiler::GenerateResult() {
EOObject main_call("main");
extern UnitTranspiler transpiler;
main_call.nested.emplace_back(
std::to_string(transpiler.glob_.RealMemorySize()),
std::to_string(transpiler.glob_.GetFreeSpacePointer()),
EOObjectType::EO_LITERAL);
main_call.nested.emplace_back("0", EOObjectType::EO_LITERAL);
init_seq.nested.push_back(main_call);
Expand Down

0 comments on commit e0f07a6

Please sign in to comment.