summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/expand/asm.cpp42
-rw-r--r--src/mir/from_hir.cpp10
-rw-r--r--src/trans/codegen_c.cpp40
3 files changed, 81 insertions, 11 deletions
diff --git a/src/expand/asm.cpp b/src/expand/asm.cpp
index 6ca90f3e..336321f7 100644
--- a/src/expand/asm.cpp
+++ b/src/expand/asm.cpp
@@ -49,9 +49,12 @@ class CAsmExpander:
::std::vector<::std::string> flags;
// Outputs
- if( lex.lookahead(0) == TOK_COLON )
+ if( lex.lookahead(0) == TOK_COLON || lex.lookahead(0) == TOK_DOUBLE_COLON )
{
GET_TOK(tok, lex);
+ if( tok.type() == TOK_DOUBLE_COLON ) {
+ lex.putback(Token(TOK_COLON));
+ }
while( lex.lookahead(0) == TOK_STRING )
{
@@ -73,9 +76,12 @@ class CAsmExpander:
}
// Inputs
- if( lex.lookahead(0) == TOK_COLON )
+ if( lex.lookahead(0) == TOK_COLON || lex.lookahead(0) == TOK_DOUBLE_COLON )
{
GET_TOK(tok, lex);
+ if( tok.type() == TOK_DOUBLE_COLON ) {
+ lex.putback(Token(TOK_COLON));
+ }
while( lex.lookahead(0) == TOK_STRING )
{
@@ -95,9 +101,12 @@ class CAsmExpander:
}
// Clobbers
- if( lex.lookahead(0) == TOK_COLON )
+ if( lex.lookahead(0) == TOK_COLON || lex.lookahead(0) == TOK_DOUBLE_COLON )
{
GET_TOK(tok, lex);
+ if( tok.type() == TOK_DOUBLE_COLON ) {
+ lex.putback(Token(TOK_COLON));
+ }
while( lex.lookahead(0) == TOK_STRING )
{
@@ -111,9 +120,12 @@ class CAsmExpander:
}
// Flags
- if( lex.lookahead(0) == TOK_COLON )
+ if( lex.lookahead(0) == TOK_COLON || lex.lookahead(0) == TOK_DOUBLE_COLON )
{
GET_TOK(tok, lex);
+ if( tok.type() == TOK_DOUBLE_COLON ) {
+ lex.putback(Token(TOK_COLON));
+ }
while( lex.lookahead(0) == TOK_STRING )
{
@@ -126,6 +138,28 @@ class CAsmExpander:
}
}
+ if( lex.lookahead(0) == TOK_COLON || lex.lookahead(0) == TOK_DOUBLE_COLON )
+ {
+ GET_TOK(tok, lex);
+ if( tok.type() == TOK_DOUBLE_COLON ) {
+ lex.putback(Token(TOK_COLON));
+ }
+
+ if( GET_TOK(tok, lex) == TOK_IDENT && tok.str() == "volatile" )
+ {
+ flags.push_back( "volatile" );
+ }
+ else
+ {
+ PUTBACK(tok, lex);
+ }
+ }
+
+ if( lex.lookahead(0) != TOK_EOF )
+ {
+ ERROR(sp, E0000, "Unexpected token in asm! - " << lex.getToken());
+ }
+
::AST::ExprNodeP rv = ::AST::ExprNodeP( new ::AST::ExprNode_Asm { mv$(template_text), mv$(outputs), mv$(inputs), mv$(clobbers), mv$(flags) } );
// TODO: Convert this into an AST node
return box$( TTStreamO(TokenTree(Token( InterpolatedFragment(InterpolatedFragment::EXPR, rv.release()) ))));
diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp
index 7773a81c..8ea22cef 100644
--- a/src/mir/from_hir.cpp
+++ b/src/mir/from_hir.cpp
@@ -506,6 +506,7 @@ namespace {
TRACE_FUNCTION_F("_Asm");
::std::vector< ::std::pair< ::std::string, ::MIR::LValue> > inputs;
+ // Inputs just need to be in lvalues
for(auto& v : node.m_inputs) {
this->visit_node_ptr(v.value);
auto lv = m_builder.get_result_in_lvalue(v.value->span(), v.value->m_res_type);
@@ -513,9 +514,16 @@ namespace {
}
::std::vector< ::std::pair< ::std::string, ::MIR::LValue> > outputs;
+ // Outputs can also (sometimes) be rvalues (only for `*m`?)
for(auto& v : node.m_outputs) {
this->visit_node_ptr(v.value);
- auto lv = m_builder.get_result_unwrap_lvalue(v.value->span());
+ if( v.spec[0] != '=' )
+ ERROR(node.span(), E0000, "Assembly output specifiers must start with =");
+ ::MIR::LValue lv;
+ if(v.spec[1] == '*')
+ lv = m_builder.get_result_in_lvalue(v.value->span(), v.value->m_res_type);
+ else
+ lv = m_builder.get_result_unwrap_lvalue(v.value->span());
outputs.push_back( ::std::make_pair(v.spec, mv$(lv)) );
}
diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp
index b0767ef7..5e600c90 100644
--- a/src/trans/codegen_c.cpp
+++ b/src/trans/codegen_c.cpp
@@ -1488,25 +1488,25 @@ namespace {
}
else
{
- m_of << "{" << e.idx << ", { ";
+ m_of << "{" << e.idx;
if( e.vals.empty() )
{
if( m_options.disallow_empty_structs && !enm.m_variants.at(e.idx).second.is_Unit() )
{
- m_of << ".var_" << e.idx << " = {0} ";
+ m_of << ", { .var_" << e.idx << " = {0} }";
}
}
else
{
- m_of << ".var_" << e.idx << " = {";
+ m_of << ", { .var_" << e.idx << " = {";
for(unsigned int i = 0; i < e.vals.size(); i ++) {
if(i != 0) m_of << ",";
m_of << " ";
emit_literal(get_inner_type(e.idx, i), e.vals[i], params);
}
- m_of << "} ";
+ m_of << "} }";
}
- m_of << "}}";
+ m_of << "}";
}
),
(Integer,
@@ -2970,9 +2970,37 @@ namespace {
{
auto indent = RepeatLitStr{ "\t", static_cast<int>(indent_level) };
+ if( e.tpl == "fnstcw $0" )
+ {
+ // HARD CODE: `fnstcw` -> _control87
+ if( !(e.inputs.size() == 0 && e.outputs.size() == 1 && e.outputs[0].first == "=*m") )
+ MIR_BUG(mir_res, "Hard-coded asm translation doesn't apply - `" << e.tpl << "` inputs=" << e.inputs << " outputs=" << e.outputs);
+ m_of << indent << "*("; emit_lvalue(e.outputs[0].second); m_of << ") = _control87(0,0);\n";
+ return ;
+ }
+ else if( e.tpl == "fldcw $0" )
+ {
+ // HARD CODE: `fldcw` -> _control87
+ if( !(e.inputs.size() == 1 && e.inputs[0].first == "m" && e.outputs.size() == 0) )
+ MIR_BUG(mir_res, "Hard-coded asm translation doesn't apply - `" << e.tpl << "` inputs=" << e.inputs << " outputs=" << e.outputs);
+ m_of << indent << "_control87("; emit_lvalue(e.inputs[0].second); m_of << ", 0xFFFF);\n";
+ return ;
+ }
+ else if( e.tpl == "int $$0x29" )
+ {
+ if( !(e.inputs.size() == 1 && e.inputs[0].first == "{ecx}" && e.outputs.size() == 0) )
+ MIR_BUG(mir_res, "Hard-coded asm translation doesn't apply - `" << e.tpl << "` inputs=" << e.inputs << " outputs=" << e.outputs);
+ m_of << indent << "__fastfail("; emit_lvalue(e.inputs[0].second); m_of << ");\n";
+ return ;
+ }
+ else
+ {
+ // No hard-coded translations.
+ }
+
if( !e.inputs.empty() || !e.outputs.empty() )
{
- MIR_TODO(mir_res, "Inputs/outputs in msvc inline assembly");
+ MIR_TODO(mir_res, "Inputs/outputs in msvc inline assembly - `" << e.tpl << "` inputs=" << e.inputs << " outputs=" << e.outputs);
#if 0
m_of << indent << "{\n";
for(size_t i = 0; i < e.inputs.size(); i ++)