ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
CodePart.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
15#include "etiss/CodePart.h"
16
17using namespace etiss;
18
19void CodeSet::writeCodeParts(std::string &code, const std::list<CodePart> &parts, bool required, RegisterSet &ignored,
20 bool intersect)
21{
22
23 std::list<CodePart>::const_iterator iter = parts.begin();
24 while (iter != parts.end())
25 {
26 if (required)
27 { // no optional code; skip
28
29 if (iter->fullRegistersDependency())
30 {
31 ignored.clear();
32 }
33 else
34 {
35 if (intersect)
36 {
37 ignored.intersect(iter->getAffectedRegisters());
38 }
39 else
40 {
41 ignored.merge(iter->getAffectedRegisters());
42 }
43 ignored.applyShadow(iter->getRegisterDependencies());
44 }
45 code = iter->getCode() + "\n" + code;
46 // std::cout << "used code: {"<< std::endl << iter->getCode() << std::endl << "}"<<std::endl;
47 }
48 else
49 {
50 if (!iter->getAffectedRegisters().maskedBy(ignored))
51 { // check if affected registers will be overwritten anyway
52
53 if (iter->fullRegistersDependency())
54 { // requires all registers to be valid
55 ignored.clear();
56 }
57 else
58 {
59 if (intersect)
60 { // in case of return statements only intersection produces an alwasy valid state
61 ignored.intersect(iter->getAffectedRegisters());
62 }
63 else
64 {
65 ignored.merge(iter->getAffectedRegisters());
66 }
67 ignored.applyShadow(iter->getRegisterDependencies());
68 }
69
70 code = iter->getCode() + "\n" + code;
71 // std::cout << "used code: {"<< std::endl << iter->getCode() << std::endl << "}"<<std::endl;
72 }
73 else
74 {
75 // std::cout << "removed code: {"<< std::endl << iter->getCode() << std::endl << "}"<<std::endl;
76 }
77 }
78 iter++;
79 }
80}
81
82std::string CodeSet::toString(RegisterSet &ignored, bool &ok) const
83{
84 ok = true;
85 std::string code;
86
87 writeCodeParts(code, appretreq_parts_, true, ignored,
88 true); // std::cout << "Ignored 1: " << ignored._dbg_print() << std::endl;
89
90 writeCodeParts(code, appopt_parts_, false, ignored,
91 false); // std::cout << "Ignored 2: " << ignored._dbg_print() << std::endl;
92
93 writeCodeParts(code, appreq_parts_, true, ignored,
94 false); // std::cout << "Ignored 3: " << ignored._dbg_print() << std::endl;
95
96 writeCodeParts(code, midopt_parts_, false, ignored,
97 false); // std::cout << "Ignored 4: " << ignored._dbg_print() << std::endl;
98
99 writeCodeParts(code, inireq_parts_, true, ignored,
100 false); // std::cout << "Ignored 5: " << ignored._dbg_print() << std::endl;
101
102 writeCodeParts(code, pindbgretreq_parts_, true, ignored,
103 true); // std::cout << "Ignored 6: " << ignored._dbg_print() << std::endl;
104
105 return code;
106}
107
108void CodeBlock::toCode(std::stringstream &out, const std::string &funcname, std::set<std::string> *fileglobalcode)
109{
110
111 if (fileglobalcode)
112 {
113 for (auto iter = fileglobal_code.begin(); iter != fileglobal_code.end(); iter++)
114 {
115 if (fileglobalcode->find(*iter) == fileglobalcode->end())
116 {
117 out << *iter;
118 fileglobalcode->insert(*iter);
119 }
120 }
121 }
122 else
123 {
124 for (auto iter = fileglobal_code.begin(); iter != fileglobal_code.end(); iter++)
125 {
126 out << *iter;
127 }
128 }
129
130 out << "etiss_uint32 " << funcname
131 << "(ETISS_CPU * const cpu, ETISS_System * const system, void * const * const plugin_pointers) {\n"
132 " const etiss_uint64 blockglobal_startaddr = 0x"
133 << std::hex << startindex_ << std::dec
134 << "ULL;\n"
135 " const etiss_uint64 blockglobal_jumpaddr = cpu->instructionPointer - blockglobal_startaddr;\n";
136
137 for (auto iter = functionglobal_code.begin(); iter != functionglobal_code.end(); iter++)
138 {
139 out << *iter;
140 }
141
142 out << "\n\tswitch(blockglobal_jumpaddr){\n";
143 std::list<std::string> cases;
144 {
145 // write cases
146 RegisterSet ignored;
147#if DEBUG
148 etiss::uint64 last = 0;
149#endif
150 for (auto iter = lines_.rbegin(); iter != lines_.rend(); iter++)
151 {
152#if DEBUG
153 if (last > iter->getAddress())
154 {
155 etiss::log(etiss::FATALERROR, "error in code block: the line addresses are not in ascending order");
156 }
157#endif
158 bool ok = true; // TODO use result
159 std::stringstream lss;
160 lss << " case " << (iter->getAddress() - startindex_) << ":\n";
161 lss << " {\n";
162 lss << iter->getCodeSet().toString(ignored, ok);
163 lss << " }\n";
164 cases.push_front(lss.str());
165 }
166 }
167 for (auto iter = cases.begin(); iter != cases.end(); iter++)
168 out << *iter;
169 out << "\n\t break;\n"
170 "\tdefault:\n"
171 "\t\treturn ETISS_RETURNCODE_ILLEGALJUMP;\n"
172 "\t}"
173 "\treturn ETISS_RETURNCODE_NOERROR;\n"
174 "}\n\n";
175}
classes to hold code and additional information used for optimization of instruction translations
void toCode(std::stringstream &out, const std::string &funcname, std::set< std::string > *fileglobalcode)
Definition CodePart.cpp:108
std::set< std::string > functionglobal_code
Definition CodePart.h:575
std::vector< Line > lines_
Definition CodePart.h:571
etiss::uint64 startindex_
Definition CodePart.h:572
std::set< std::string > fileglobal_code
Definition CodePart.h:574
std::string toString(RegisterSet &ignored, bool &ok) const
writes the contained CodeParts as needed with respect to the given RegisterSet of bits that are not r...
Definition CodePart.cpp:82
std::list< CodePart > appretreq_parts_
Definition CodePart.h:512
std::list< CodePart > midopt_parts_
Definition CodePart.h:509
std::list< CodePart > pindbgretreq_parts_
Definition CodePart.h:507
std::list< CodePart > appreq_parts_
Definition CodePart.h:510
std::list< CodePart > inireq_parts_
Definition CodePart.h:508
static void writeCodeParts(std::string &code, const std::list< CodePart > &parts, bool required, RegisterSet &ignored, bool intersect)
Definition CodePart.cpp:19
std::list< CodePart > appopt_parts_
Definition CodePart.h:511
set of register parts.
Definition CodePart.h:163
void applyShadow(const RegisterSet &rs)
any register bits set in the passed RegisterSet won't be set in this RegisterSet
Definition CodePart.h:212
void merge(const RegisterSet &rs)
any register bits set in the passed RegisterSet will be set in this RegisterSet (plus previously set ...
Definition CodePart.h:241
void intersect(const RegisterSet &rs)
only register bits set in this AND the passed RegisterSet remain set in this RegisterSet
Definition CodePart.h:253
forwards: include/jit/*
Definition Benchmark.h:17
@ FATALERROR
Definition Misc.h:84
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition Misc.cpp:94