Debian Codegraph @

gcc/rust/backend/rust-compile-asm.cc

raw
1 #include "rust-compile-asm.h"
2 #include "rust-compile-expr.h"
3 namespace Rust {
4 namespace Compile {
5
6 CompileAsm::CompileAsm (Context *ctx) : HIRCompileBase (ctx) {}
7
8 tree
9 CompileAsm::tree_codegen_asm (HIR::InlineAsm &expr)
10 {
11 auto asm_expr
12 = asm_build_stmt (expr.get_locus (), {asm_construct_string_tree (expr),
13 asm_construct_outputs (expr),
14 asm_construct_inputs (expr),
15 asm_construct_clobber_tree (expr),
16 asm_construct_label_tree (expr)});
17
18 ASM_BASIC_P (asm_expr) = expr.is_simple_asm ();
19 ASM_VOLATILE_P (asm_expr) = false;
20 ASM_INLINE_P (asm_expr) = expr.is_inline_asm ();
21 /*Backend::debug (asm_expr);*/
22 return asm_expr;
23 }
24
25 tree
26 CompileAsm::asm_build_stmt (
27 location_t loc,
28 const std::array<tree, CompileAsm::ASM_TREE_ARRAY_LENGTH> &trees)
29 {
30 // Prototype functiion for building an ASM_EXPR tree.
31 tree ret;
32 bool side_effects;
33
34 ret = make_node (ASM_EXPR);
35 TREE_TYPE (ret) = void_type_node;
36 SET_EXPR_LOCATION (ret, loc);
37
38 /* TREE_SIDE_EFFECTS will already be set for statements with
39 implicit side effects. Here we make sure it is set for other
40 expressions by checking whether the parameters have side
41 effects. */
42
43 // This is here because of c-typeck.cc's code
44 // I'm not sure what kind of effects it has
45 side_effects = false;
46 for (size_t i = 0; i < trees.size (); i++)
47 {
48 tree t = trees[i];
49 if (t && !TYPE_P (t))
50 side_effects |= TREE_SIDE_EFFECTS (t);
51 TREE_OPERAND (ret, i) = t;
52 }
53
54 TREE_SIDE_EFFECTS (ret) |= side_effects;
55
56 return ret;
57 }
58
59 tree
60 CompileAsm::asm_construct_string_tree (HIR::InlineAsm &expr)
61 {
62 // To construct an ASM_EXPR tree, we need to build a STRING_CST tree.
63 //
64 // We do this by concatenating all the template strings in the InlineAsm
65 // into one big std::string seperated by tabs and newlines. (For easier
66 // debugging and reading)
67 std::stringstream ss;
68 for (const auto &template_str : expr.template_strs)
69 ss << template_str.symbol << "\n";
70
71 std::string result = ss.str ();
72 return Backend::string_constant_expression (result);
73 }
74
75 tree
76 CompileAsm::asm_construct_outputs (HIR::InlineAsm &expr)
77 {
78 // TODO: Do i need to do this?
79
80 tree head = NULL_TREE;
81 for (auto &output : expr.get_operands ())
82 {
83 if (output.get_register_type ()
84 == AST::InlineAsmOperand::RegisterType::Out)
85 {
86 auto out = output.get_out ();
87
88 tree out_tree = CompileExpr::Compile (*out.expr, this->ctx);
89 // expects a tree list
90 // TODO: This assumes that the output is a register
91 std::string expr_name = "=r";
92 auto name = build_string (expr_name.size () + 1, expr_name.c_str ());
93 head
94 = chainon (head, build_tree_list (build_tree_list (NULL_TREE, name),
95 out_tree));
96
97 /*Backend::debug (head);*/
98 /*head = chainon (head, out_tree);*/
99 }
100 }
101 return head;
102 }
103
104 tree
105 CompileAsm::asm_construct_inputs (HIR::InlineAsm &expr)
106 {
107 // TODO: Do i need to do this?
108 tree head = NULL_TREE;
109 for (auto &input : expr.get_operands ())
110 {
111 if (input.get_register_type () == AST::InlineAsmOperand::RegisterType::In)
112 {
113 auto in = input.get_in ();
114
115 tree in_tree = CompileExpr::Compile (*in.expr, this->ctx);
116 // expects a tree list
117 // TODO: This assumes that the input is a register
118 std::string expr_name = "r";
119 auto name = build_string (expr_name.size () + 1, expr_name.c_str ());
120 head
121 = chainon (head, build_tree_list (build_tree_list (NULL_TREE, name),
122 in_tree));
123
124 /*head = chainon (head, out_tree);*/
125 }
126 }
127 return head;
128 }
129
130 tree
131 CompileAsm::asm_construct_clobber_tree (HIR::InlineAsm &expr)
132 {
133 // TODO: Do i need to do this?
134 return NULL_TREE;
135 }
136
137 tree
138 CompileAsm::asm_construct_label_tree (HIR::InlineAsm &expr)
139 {
140 // TODO: Do i need to do this?
141 return NULL_TREE;
142 }
143
144 } // namespace Compile
145 } // namespace Rust