Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link capi #998

Merged
merged 3 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion include/coreir-c/coreir.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void* CORENewMap(
uint len,
COREMapKind kind);


// Context COREreater/deleters
extern COREContext* CORENewContext();
extern void COREDeleteContext(COREContext*);
Expand All @@ -33,7 +34,6 @@ extern COREValueType* COREContextBool(COREContext* context);
extern COREValueType* COREContextInt(COREContext* context);
extern COREValueType* COREContextBitVector(COREContext* context);
extern COREValueType* COREContextString(COREContext* context);
extern COREValueType* COREContextString(COREContext* context);

extern bool COREContextRunPasses(
COREContext* ctx,
Expand Down Expand Up @@ -62,6 +62,16 @@ extern void CORESaveContext(
COREBool* nocoreir,
COREBool* no_default_libs,
COREBool* err);


extern void CORESerializeToFile(COREContext* cc, char* filename, COREBool* err);

extern void CORELoadHeader(COREContext* cc, char* filename, char*** modules, uint* num_modules, COREBool* err);
extern void CORELinkDefinitions(COREContext* cc, char* filename, COREBool* err);

extern void CORESerializeHeader(COREContext* context, char* filename, char** modules, uint num_modules, COREBool* err);
extern void CORESerializeDefinitions(COREContext* cc, char* filename, char** modules, uint num_modules, COREBool* err);

extern CORENamespace* COREGetGlobal(COREContext* c);
extern CORENamespace* COREGetNamespace(COREContext* c, char* name);
extern CORENamespace* CORENewNamespace(COREContext* c, char* name);
Expand Down
64 changes: 64 additions & 0 deletions src/coreir-c/coreir-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ void CORESaveContext(
return;
}

void CORESerializeToFile(COREContext* cc, char* filename, COREBool* err) {
string file(filename);
Context* c = rcast<Context*>(cc);
bool correct = serializeToFile(c, file);
*err = !correct;
return;
}


// bool saveToFile(Namespace* ns, string filename,Module* top=nullptr);
void CORESaveModule(COREModule* module, char* filename, bool* err) {
string file(filename);
Expand All @@ -293,6 +302,61 @@ void CORESaveModule(COREModule* module, char* filename, bool* err) {
return;
}

void CORESerializeHeader(COREContext* cc, char* filename, char** modules, uint num_modules, COREBool* err) {
Context* c = rcast<Context*>(cc);
string file(filename);
vector<string> vec_modules;
for (uint i=0; i< num_modules; ++i) {
vec_modules.emplace_back(modules[i]);
}
bool correct = serializeHeader(c, file, vec_modules);
*err = !correct;
return;
}

void CORESerializeDefinitions(COREContext* cc, char* filename, char** modules, uint num_modules, COREBool* err) {
Context* c = rcast<Context*>(cc);
string file(filename);
vector<string> vec_modules;
for (uint i = 0; i < num_modules; ++i) {
vec_modules.emplace_back(modules[i]);
}
bool correct = serializeDefinitions(c, file, vec_modules);
*err = !correct;
return;
}

void CORELoadHeader(COREContext* cc, char* filename, char*** modules, uint* num_modules, COREBool* err) {
Context* c = rcast<Context*>(cc);
std::string file(filename);
std::vector<Module*> loaded_modules;
bool correct = loadHeader(c, file, loaded_modules);
*err = !correct;
if (correct) {
*num_modules = loaded_modules.size();
*modules = c->newStringArray(loaded_modules.size());
int count = 0;
for (auto m : loaded_modules) {
std::string mref = m->getRefName();
std::size_t name_length = mref.size();
(*modules)[count] = c->newStringBuffer(name_length + 1);
memcpy((*modules)[count], mref.c_str(), name_length + 1);
count++;
}
}
else {
*num_modules = 0;
}
}

void CORELinkDefinitions(COREContext* cc, char* filename, COREBool* err) {
Context* c = rcast<Context*>(cc);
std::string file(filename);
bool correct = linkDefinitions(c, file);
*err = !correct;
}


CORENamespace* COREGetGlobal(COREContext* c) {
return rcast<CORENamespace*>(rcast<Context*>(c)->getGlobal());
}
Expand Down
17 changes: 15 additions & 2 deletions src/ir/fileWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,21 @@ bool saveToFile(
}

bool serializeToFile(Context* c, string filename) {
ASSERT(c->hasTop(), "Missing top");
ASSERT(endsWith(filename, ".json"), filename + "Needs to be a json file");
if (!c->hasTop()) {
Error e;
e.message("Missing Top " + filename);
e.fatal();
c->error(e);
return false;
}
if (!endsWith(filename, ".json")) {
Error e;
e.message(filename + "Needs to be a json file");
e.fatal();
c->error(e);
return false;
}

std::ofstream file(filename);
if (!file.is_open()) {
Error e;
Expand Down