Skip to content

Commit 4fd5116

Browse files
committed
Added util source print.
1 parent c0bc4b1 commit 4fd5116

File tree

4 files changed

+242
-1
lines changed

4 files changed

+242
-1
lines changed

src/codegen/c-util-generator.cpp

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CiUtilGenerator::CiUtilGenerator()
1414
{
1515
Clear();
1616
tof = new FileWriter;
17+
condtree = new ConditionalTree;
1718
}
1819

1920
void CiUtilGenerator::Clear()
@@ -84,10 +85,11 @@ void CiUtilGenerator::Generate(std::vector<MessageDescriptor_t*>& msgs, const Fs
8485

8586
fdesc = &fsd;
8687

88+
// print header for util code
8789
PrintHeader();
8890

91+
// print main source for util code
8992
PrintSource();
90-
9193
}
9294

9395
void CiUtilGenerator::PrintHeader()
@@ -169,4 +171,105 @@ void CiUtilGenerator::PrintHeader()
169171

170172
void CiUtilGenerator::PrintSource()
171173
{
174+
tof->AppendLine(StrPrint("#include \"%s\"", fdesc->util_h.fname.c_str()), 2);
175+
176+
// optional RX and TX struct allocations
177+
if (rx.size() > 0 || tx.size() > 0)
178+
{
179+
tof->AppendLine(StrPrint("#ifdef __DEF_%s__", fdesc->DRVNAME.c_str()), 2);
180+
181+
if (rx.size() > 0)
182+
{
183+
tof->AppendLine(StrPrint("%s_rx_t %s_rx;", fdesc->drvname.c_str(), fdesc->drvname.c_str()), 2);
184+
}
185+
186+
if (tx.size() > 0)
187+
{
188+
tof->AppendLine(StrPrint("%s_tx_t %s_tx;", fdesc->drvname.c_str(), fdesc->drvname.c_str()), 2);
189+
}
190+
191+
tof->AppendLine(StrPrint("#endif // __DEF_%s__", fdesc->DRVNAME.c_str()), 2);
192+
}
193+
194+
if (rx.size() > 0)
195+
{
196+
// tree will be created inside (in dynamic memory) so this
197+
// scope is responsible for deletion these resources
198+
// tree is the struct tree-view which is used to execute
199+
// binary search on FrameID for selecting unpacking function
200+
auto tree = FillTreeLevel(rx, 0, rx.size());
201+
202+
tof->AppendLine(StrPrint("uint32_t %s_Receive(%s_rx_t* _m, const uint8_t* _d, uint32_t _id, uint8_t dlc_)",
203+
fdesc->drvname.c_str(), fdesc->drvname.c_str()));
204+
205+
tof->AppendLine("{");
206+
tof->AppendLine(" uint32_t recid = 0;");
207+
208+
// put tree-view struct on code (in treestr variable)
209+
std::string treestr;
210+
condtree->Clear();
211+
tof->AppendLine(condtree->WriteCode(tree, treestr, 1));
212+
213+
tof->AppendLine(" return recid;");
214+
tof->AppendLine("}", 2);
215+
216+
// clear tree after using
217+
condtree->DeleteTree(tree);
218+
}
219+
220+
tof->Flush(fdesc->util_c.fpath);
221+
}
222+
223+
ConditionalTree_t* CiUtilGenerator::FillTreeLevel(std::vector<MessageDescriptor_t*>& list,
224+
int32_t l,
225+
int32_t h,
226+
bool started)
227+
{
228+
int32_t span = h - l;
229+
int32_t lowhalf = span / 2;
230+
int32_t highhalf = span - lowhalf;
231+
232+
treestarted = started;
233+
234+
if (h < 1)
235+
{
236+
return nullptr;
237+
}
238+
239+
ConditionalTree_t* ret = new ConditionalTree_t;
240+
241+
if (!treestarted && h == 1)
242+
{
243+
ret->Type = ConditionalType::Cond;
244+
auto msg = list[l];
245+
ret->ConditionExpresion = StrPrint("_id == 0x%XU", msg->MsgID);
246+
ret->High = new ConditionalTree_t;
247+
ret->Type = ConditionalType::Express;
248+
ret->UtilCodeBody = StrPrint("recid = Unpack_%s_%s(&(_m->%s), _d, dlc_);",
249+
msg->Name.c_str(), code_drvname.c_str(), msg->Name.c_str());
250+
return ret;
251+
}
252+
253+
if (span > 1)
254+
{
255+
ret->Type = ConditionalType::Cond;
256+
257+
if (lowhalf > 1)
258+
ret->ConditionExpresion = StrPrint("(_id >= 0x%XU) && (_id < 0x%XU)", list[l]->MsgID, list[(l + lowhalf)]->MsgID);
259+
else
260+
ret->ConditionExpresion = StrPrint("_id == 0x%XU", list[l]->MsgID);
261+
262+
ret->High = FillTreeLevel(list, l, l + lowhalf, true);
263+
ret->Low = FillTreeLevel(list, l + lowhalf, h, true);
264+
}
265+
else
266+
{
267+
ret->Type = ConditionalType::Express;
268+
auto msg = list[l];
269+
ret->ConditionExpresion = StrPrint("_id == 0x%XU", msg->MsgID);
270+
ret->UtilCodeBody = StrPrint("recid = Unpack_%s_%s(&(_m->%s), _d, dlc_);",
271+
msg->Name.c_str(), code_drvname.c_str(), msg->Name.c_str());
272+
}
273+
274+
return ret;
172275
}

src/codegen/c-util-generator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "types/message.h"
44
#include "fs-creator.h"
55
#include "filewriter.h"
6+
#include "conditional-tree.h"
67

78
class CiUtilGenerator {
89
public:
@@ -25,6 +26,8 @@ class CiUtilGenerator {
2526
private:
2627
void PrintHeader();
2728
void PrintSource();
29+
ConditionalTree_t* FillTreeLevel(std::vector<MessageDescriptor_t*>& msgs,
30+
int32_t l, int32_t h, bool started = false);
2831

2932
private:
3033

@@ -39,4 +42,7 @@ class CiUtilGenerator {
3942
std::string file_drvname;
4043

4144
const FsDescriptor_t* fdesc;
45+
ConditionalTree* condtree;
46+
47+
bool treestarted;
4248
};

src/codegen/conditional-tree.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "conditional-tree.h"
2+
#include "helpers/formatter.h"
3+
4+
ConditionalTree::ConditionalTree()
5+
{
6+
}
7+
8+
// this method performs printing tree-viewed frames collection
9+
// to proper source code
10+
std::string ConditionalTree::WriteCode(const ConditionalTree_t* tree, std::string& outstr, uint8_t intend)
11+
{
12+
if (tree != nullptr)
13+
{
14+
std::string temp;
15+
16+
if (tree->Type == ConditionalType::Cond)
17+
{
18+
temp = StrPrint("if (%s) {", tree->ConditionExpresion.c_str());
19+
PrintCode(temp, intend);
20+
21+
WriteCode(tree->High, outstr, intend + 1);
22+
23+
if (tree->Low != nullptr)
24+
{
25+
if (tree->Low->Type == ConditionalType::Express)
26+
{
27+
temp = StrPrint("} else if (%s) {", tree->Low->ConditionExpresion.c_str());
28+
PrintCode(temp, intend);
29+
}
30+
else
31+
{
32+
temp = "} else {";
33+
PrintCode(temp, intend);
34+
}
35+
36+
WriteCode(tree->Low, outstr, intend + 1);
37+
}
38+
else
39+
{
40+
temp = "} else {";
41+
PrintCode(temp, intend);
42+
}
43+
44+
if (tree->Low != nullptr)
45+
{
46+
temp = "}";
47+
PrintCode(temp, intend);
48+
}
49+
}
50+
else
51+
{
52+
temp = StrPrint("%s", tree->UtilCodeBody.c_str());
53+
PrintCode(temp, intend);
54+
}
55+
}
56+
57+
return codestr;
58+
}
59+
60+
void ConditionalTree::PrintCode(std::string& str, uint8_t indent)
61+
{
62+
while (indent--)
63+
{
64+
codestr += " ";
65+
}
66+
67+
codestr += str;
68+
codestr += "\n";
69+
}
70+
71+
72+
void ConditionalTree::DeleteTree(ConditionalTree_t* tree)
73+
{
74+
if (tree != nullptr)
75+
{
76+
if (tree->Low != nullptr)
77+
{
78+
DeleteTree(tree->Low);
79+
}
80+
81+
if (tree->High != nullptr)
82+
{
83+
DeleteTree(tree->High);
84+
}
85+
86+
delete tree;
87+
}
88+
}

src/codegen/conditional-tree.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
enum class ConditionalType { Cond, Express, Single };
6+
7+
struct ConditionalTree_t
8+
{
9+
ConditionalTree_t* High;
10+
11+
ConditionalTree_t* Low;
12+
13+
ConditionalType Type;
14+
15+
std::string ConditionExpresion;
16+
17+
std::string UtilCodeBody;
18+
19+
ConditionalTree_t() {
20+
High = nullptr;
21+
Low = nullptr;
22+
Type = ConditionalType::Single;
23+
}
24+
};
25+
26+
class ConditionalTree {
27+
public:
28+
ConditionalTree();
29+
30+
void Clear() {
31+
codestr.clear();
32+
}
33+
34+
std::string WriteCode(const ConditionalTree_t* tree, std::string& outstr, uint8_t intend);
35+
36+
// deletes all memory allocated to tree members
37+
void DeleteTree(ConditionalTree_t* tree);
38+
39+
private:
40+
41+
void PrintCode(std::string& str, uint8_t indent);
42+
43+
std::string codestr;
44+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy