Skip to content

Commit c9f4631

Browse files
committed
X86Assembler: Isolate get_asm() and save_bin() for 32 and 64bit
1 parent c6c0000 commit c9f4631

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/libasr/codegen/wasm_to_x64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ Result<int> wasm_to_x64(Vec<uint8_t> &wasm_bytes, Allocator &al,
695695

696696
{
697697
auto t1 = std::chrono::high_resolution_clock::now();
698-
m_a.save_binary(filename);
698+
m_a.save_binary64(filename);
699699
auto t2 = std::chrono::high_resolution_clock::now();
700700
time_save =
701701
std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1)

src/libasr/codegen/x86_assembler.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace LCompilers {
1212

13-
void X86Assembler::save_binary(const std::string &filename) {
13+
void X86Assembler::save_binary64(const std::string &filename) {
1414
Vec<uint8_t> bin = create_elf64_x86_binary(m_al, *this);
1515
{
1616
std::ofstream out;
@@ -26,6 +26,21 @@ void X86Assembler::save_binary(const std::string &filename) {
2626
#endif
2727
}
2828

29+
void X86Assembler::save_binary(const std::string &filename) {
30+
{
31+
std::ofstream out;
32+
out.open(filename);
33+
out.write((const char*) m_code.p, m_code.size());
34+
}
35+
#ifdef LFORTRAN_LINUX
36+
std::string mode = "0755";
37+
int mod = strtol(mode.c_str(), 0, 8);
38+
if (chmod(filename.c_str(),mod) < 0) {
39+
throw AssemblerError("chmod failed");
40+
}
41+
#endif
42+
}
43+
2944
// ELF header structure for 32-bit
3045
struct Elf32_Ehdr {
3146
uint8_t ident[16];

src/libasr/codegen/x86_assembler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,10 @@ class X86Assembler {
446446

447447
#ifdef LFORTRAN_ASM_PRINT
448448
std::string get_asm() {
449+
return m_asm_code;
450+
}
449451

452+
std::string get_asm64() {
450453
std::string header =
451454
R"(BITS 64
452455
org )" + i2s((uint64_t) m_origin) + R"(
@@ -666,6 +669,7 @@ R"(BITS 64
666669

667670
// Saves the generated machine code into a binary file
668671
void save_binary(const std::string &filename);
672+
void save_binary64(const std::string &filename);
669673

670674
void asm_pop_r64(X64Reg r64) {
671675
X86Reg r32 = X86Reg(r64 & 7);

0 commit comments

Comments
 (0)