Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
paule32 committed Nov 4, 2023
1 parent 57b51ba commit 88b63e7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 29 deletions.
18 changes: 9 additions & 9 deletions src/Interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -870,58 +870,58 @@ int main(int argc, char **argv)
// check, if input, and output file is ok
// --------------------------------------
if (file_input_asm.empty()) {
std::cerr << gettext("input assembly file missing.") << std::endl;
std::cerr << "// " << gettext("input assembly file missing.") << std::endl;
return EXIT_FAILURE;
} else {
std::stringstream ss;

// asm input
std::string ext(ExtractFileExtension(file_input_asm));
if (ext != ".asm") {
std::cerr << gettext("input main C++ file must have extension .asm")
std::cerr << "// " << gettext("input main C++ file must have extension .asm")
<< std::endl;
return EXIT_FAILURE;
}

// c++ output.cc
if (file_output_cm.empty()) {
std::cout << gettext("output main C++ file missing, use default.")
std::cout << "// " << gettext("output main C++ file missing, use default.")
<< std::endl;
file_output_cm = "aout_main.cc";
}
else {
std::string ext(ExtractFileExtension(file_output_cm));
if ((ext != ".cc") || (ext != ".cc")) {
std::cout << gettext("output main C++ file must have extension .cc")
std::cout << "// " << gettext("output main C++ file must have extension .cc")
<< std::endl;
return EXIT_FAILURE;
}
}

// c++ header.h
if (file_output_ch.empty()) {
std::cout << gettext("output header C++ file missing, use default.")
std::cout << "// " << gettext("output header C++ file missing, use default.")
<< std::endl;
file_output_ch = "aout_header.h";
}
else {
std::string ext(ExtractFileExtension(file_output_ch));
if ((ext != ".h") || (ext != ".H")) {
std::cout << gettext("output header C++ file must have extension .h")
std::cout << "// " << gettext("output header C++ file must have extension .h")
<< std::endl;
return EXIT_FAILURE;
}
}

// c++ misc.cc
if (file_output_ct.empty()) {
std::cout << "output tool C++ file missing, use default." << std::endl;
std::cout << "// " << "output tool C++ file missing, use default." << std::endl;
file_output_ct = "aout_misc.cc";
}
else {
std::string ext(ExtractFileExtension(file_output_ct));
if ((ext != ".cc") || (ext != ".cc")) {
std::cout << "output misc C++ file must have extension .cc" << std::endl;
std::cout << "// " << "output misc C++ file must have extension .cc" << std::endl;
return EXIT_FAILURE;
}
}
Expand Down Expand Up @@ -987,7 +987,7 @@ AsmParser::AsmParser( const char *filename, bool mode )
// -----------------------------------------------------------------
AsmParser::~AsmParser()
{
std::cout << _("please wait...") << std::endl;
std::cout << "// " << _("please wait...") << std::endl;

// -------------------------------------------------------------
// at terminating application, delete de-packed .mo file.
Expand Down
1 change: 1 addition & 0 deletions src/assembler.lex
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ void asm_parser_main(void)
<< "\t rt.cpuFeatures());" << std::endl
<< std::endl
<< "\tx86::Assembler a(&code); // Create and attach x86::Assembler to code." << std::endl
<< "\tstd::string err_lbl(\"failed bind label.\");" << std::endl
<< std::endl

<< output_stream_labels.str() << std::endl
Expand Down
81 changes: 61 additions & 20 deletions src/assembler.y
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ extern "C" {
std::vector< std::string > section_vector; // section
std::map < std::string, int > address_map; // map string to address

std::stringstream output_stream; // code
std::stringstream output_stream_labels; // label's
std::stringstream output_stream; // code
std::stringstream output_stream_labels; // label's

std::stringstream ts2,ts3;
%}

%union {
Expand Down Expand Up @@ -172,8 +173,7 @@ program_top
ss << "\tLabel lbl_" << str << " = a.newNamedLabel("
<< "\"" << str << "\", SIZE_MAX);"
<< std::endl
<< "\tif (err)" << std::endl
<< "\tyyerror(gettext(\"failed create label\"));"
<< "\tif (err) yyerror(err_lbl.c_str());"
<< std::endl;
output_stream_labels << ss.str();

Expand All @@ -191,8 +191,7 @@ program_top
// -----------------------------------
ss.str(std::string(""));
ss << "\terr = a.bind(lbl_" << str << ");"
<< std::endl << "\tif (err)"
<< std::endl << "\tyyerror(gettext(\"failed bind local label\"));"
<< std::endl << "\tif (err) yyerror(err_lbl.c_str());"
<< std::endl;
} else {
// -----------------------------------
Expand All @@ -202,7 +201,7 @@ program_top
std::stringstream s2;

s2 << std::endl << "\terr = a.bind(lbl_" << str << ");"
<< std::endl << "\tif (err) yyerror(gettext(\"failed bind global label\"));"
<< std::endl << "\tif (err) yyerror(err_lbl.c_str());"
<< std::endl ;

output_stream << s2.str();
Expand Down Expand Up @@ -249,7 +248,7 @@ program_top
| TOK_MOV _tok_reg ',' _tok_imm32 {
std::stringstream ss;
std::string str = $4;
std::cout << "imm32: " << str << std::endl;
std::cout << "// imm32: " << str << std::endl;
ss << "\tx86::mov("
<< $2 << ','
<< str
Expand Down Expand Up @@ -281,13 +280,23 @@ program_top
<< std::endl;
output_stream << ss.str();
}
| TOK_PUSH _tok_reg
| TOK_PUSH _tok_reg {
std::stringstream ss;
ss << "\tx86::push(" << $2 << ");"
<< std::endl;
output_stream << ss.str();
}
| TOK_RET {
std::stringstream ss;
ss << "\tx86::ret();" << std::endl;
output_stream << ss.str();
}
| TOK_SUB _tok_reg ',' _tok_num
| TOK_SUB _tok_reg ',' _tok_num {
std::stringstream ss;
ss << "\tx86::sub(" << $2 << "," << $4 << ");"
<< std::endl;
output_stream << ss.str();
}
| TOK_CALL _tok_id {
std::stringstream ss;
std::string str = $2;
Expand Down Expand Up @@ -428,22 +437,54 @@ _reg8l

_tok_mem
: _mem_ptr { $$ = $1; }
| __mem_ptr { $$ = $1; }
;

_mem_ptr
: _mem_ptr_byte __mem_ptr
| _mem_ptr_word __mem_ptr
| _mem_ptr_dword __mem_ptr
| _mem_ptr_qword __mem_ptr
: _mem_ptr_byte { $$ = $1; }
| _mem_ptr_word { $$ = $1; }
| _mem_ptr_dword { $$ = $1; }
| _mem_ptr_qword { $$ = $1; }
;

_mem_ptr_byte
: TOK_BYTE __mem_ptr {
std::stringstream ss;
ss << "x86::byte_ptr" << $2;
$$ = strdup( ss.str().c_str());
}
;
_mem_ptr_word
: TOK_WORD __mem_ptr {
std::stringstream ss;
ss << "x86::word_ptr" << $2;
$$ = strdup( ss.str().c_str());
}
;

_mem_ptr_byte : TOK_BYTE TOK_PTR { $$ = $1; } | TOK_BYTE ;
_mem_ptr_word : TOK_WORD TOK_PTR { $$ = $1; } | TOK_WORD ;
_mem_ptr_dword : TOK_DWORD TOK_PTR { $$ = $1; } | TOK_DWORD ;
_mem_ptr_qword : TOK_QWORD TOK_PTR { $$ = $1; } | TOK_QWORD ;
_mem_ptr_dword
: TOK_DWORD __mem_ptr {
std::stringstream ss;
ss << "x86::dword_ptr" << $2;
$$ = strdup( ss.str().c_str());
}
;

_mem_ptr_qword
: TOK_QWORD __mem_ptr {
std::stringstream ss;
ss << "x86::qword_ptr" << $2;
$$ = strdup( ss.str().c_str());
}
;

__mem_ptr
: '[' _tok_reg '+' _tok_num ']'
: '[' _tok_reg '+' _tok_num ']' {
std::stringstream ss;
ss << "(" << $2 << ", "
<< std::hex << $4
<< std::dec << ")";
$$ = strdup(ss.str().c_str());
}
;

_tok_imm32
Expand Down

0 comments on commit 88b63e7

Please sign in to comment.