Skip to content

Commit

Permalink
Merge pull request #4697 from georgerennie/george/zero_width_string
Browse files Browse the repository at this point in the history
frontends/ast.cc: special-case zero width strings as "\0"
  • Loading branch information
Ravenslofty authored Nov 4, 2024
2 parents 3250f2b + dbfca1b commit 52c231d
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions frontends/ast/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -877,16 +877,25 @@ AstNode *AstNode::mkconst_str(const std::vector<RTLIL::State> &v)
// create an AST node for a constant (using a string as value)
AstNode *AstNode::mkconst_str(const std::string &str)
{
std::vector<RTLIL::State> data;
data.reserve(str.size() * 8);
for (size_t i = 0; i < str.size(); i++) {
unsigned char ch = str[str.size() - i - 1];
for (int j = 0; j < 8; j++) {
data.push_back((ch & 1) ? State::S1 : State::S0);
ch = ch >> 1;
AstNode *node;

// LRM 1364-2005 5.2.3.3 The empty string literal ("") shall be considered
// equivalent to the ASCII NUL ("\0")
if (str.empty()) {
node = AstNode::mkconst_int(0, false, 8);
} else {
std::vector<RTLIL::State> data;
data.reserve(str.size() * 8);
for (size_t i = 0; i < str.size(); i++) {
unsigned char ch = str[str.size() - i - 1];
for (int j = 0; j < 8; j++) {
data.push_back((ch & 1) ? State::S1 : State::S0);
ch = ch >> 1;
}
}
node = AstNode::mkconst_bits(data, false);
}
AstNode *node = AstNode::mkconst_bits(data, false);

node->is_string = true;
node->str = str;
return node;
Expand Down

0 comments on commit 52c231d

Please sign in to comment.