abc9: -reintegrate recover type from existing cell, check against boxid
[yosys.git] / frontends / verilog / const2ast.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * ---
19 *
20 * The Verilog frontend.
21 *
22 * This frontend is using the AST frontend library (see frontends/ast/).
23 * Thus this frontend does not generate RTLIL code directly but creates an
24 * AST directly from the Verilog parse tree and then passes this AST to
25 * the AST frontend library.
26 *
27 * ---
28 *
29 * This file contains an ad-hoc parser for Verilog constants. The Verilog
30 * lexer does only recognize a constant but does not actually split it to its
31 * components. I.e. it just passes the Verilog code for the constant to the
32 * bison parser. The parser then uses the function const2ast() from this file
33 * to create an AST node for the constant.
34 *
35 */
36
37 #include "verilog_frontend.h"
38 #include "kernel/log.h"
39 #include <string.h>
40 #include <math.h>
41
42 YOSYS_NAMESPACE_BEGIN
43
44 using namespace AST;
45
46 // divide an arbitrary length decimal number by two and return the rest
47 static int my_decimal_div_by_two(std::vector<uint8_t> &digits)
48 {
49 int carry = 0;
50 for (size_t i = 0; i < digits.size(); i++) {
51 if (digits[i] >= 10)
52 log_file_error(current_filename, get_line_num(), "Invalid use of [a-fxz?] in decimal constant.\n");
53 digits[i] += carry * 10;
54 carry = digits[i] % 2;
55 digits[i] /= 2;
56 }
57 while (!digits.empty() && !digits.front())
58 digits.erase(digits.begin());
59 return carry;
60 }
61
62 // find the number of significant bits in a binary number (not including the sign bit)
63 static int my_ilog2(int x)
64 {
65 int ret = 0;
66 while (x != 0 && x != -1) {
67 x = x >> 1;
68 ret++;
69 }
70 return ret;
71 }
72
73 // parse a binary, decimal, hexadecimal or octal number with support for special bits ('x', 'z' and '?')
74 static void my_strtobin(std::vector<RTLIL::State> &data, const char *str, int len_in_bits, int base, char case_type, bool is_unsized)
75 {
76 // all digits in string (MSB at index 0)
77 std::vector<uint8_t> digits;
78
79 while (*str) {
80 if ('0' <= *str && *str <= '9')
81 digits.push_back(*str - '0');
82 else if ('a' <= *str && *str <= 'f')
83 digits.push_back(10 + *str - 'a');
84 else if ('A' <= *str && *str <= 'F')
85 digits.push_back(10 + *str - 'A');
86 else if (*str == 'x' || *str == 'X')
87 digits.push_back(0xf0);
88 else if (*str == 'z' || *str == 'Z' || *str == '?')
89 digits.push_back(0xf1);
90 str++;
91 }
92
93 if (base == 10 && GetSize(digits) == 1 && digits.front() >= 0xf0)
94 base = 2;
95
96 data.clear();
97
98 if (base == 10) {
99 while (!digits.empty())
100 data.push_back(my_decimal_div_by_two(digits) ? State::S1 : State::S0);
101 } else {
102 int bits_per_digit = my_ilog2(base-1);
103 for (auto it = digits.rbegin(), e = digits.rend(); it != e; it++) {
104 if (*it > (base-1) && *it < 0xf0)
105 log_file_error(current_filename, get_line_num(), "Digit larger than %d used in in base-%d constant.\n",
106 base-1, base);
107 for (int i = 0; i < bits_per_digit; i++) {
108 int bitmask = 1 << i;
109 if (*it == 0xf0)
110 data.push_back(case_type == 'x' ? RTLIL::Sa : RTLIL::Sx);
111 else if (*it == 0xf1)
112 data.push_back(case_type == 'x' || case_type == 'z' ? RTLIL::Sa : RTLIL::Sz);
113 else
114 data.push_back((*it & bitmask) ? State::S1 : State::S0);
115 }
116 }
117 }
118
119 int len = GetSize(data);
120 RTLIL::State msb = data.empty() ? State::S0 : data.back();
121
122 if (len_in_bits < 0) {
123 if (len < 32)
124 data.resize(32, msb == State::S0 || msb == State::S1 ? RTLIL::S0 : msb);
125 return;
126 }
127
128 if (is_unsized && (len > len_in_bits))
129 log_file_error(current_filename, get_line_num(), "Unsized constant must have width of 1 bit, but have %d bits!\n", len);
130
131 for (len = len - 1; len >= 0; len--)
132 if (data[len] == State::S1)
133 break;
134 if (msb == State::S0 || msb == State::S1) {
135 len += 1;
136 data.resize(len_in_bits, State::S0);
137 } else {
138 len += 2;
139 data.resize(len_in_bits, msb);
140 }
141
142 if (len > len_in_bits)
143 log_warning("Literal has a width of %d bit, but value requires %d bit. (%s:%d)\n",
144 len_in_bits, len, current_filename.c_str(), get_line_num());
145 }
146
147 // convert the Verilog code for a constant to an AST node
148 AstNode *VERILOG_FRONTEND::const2ast(std::string code, char case_type, bool warn_z)
149 {
150 if (warn_z) {
151 AstNode *ret = const2ast(code, case_type);
152 if (ret != nullptr && std::find(ret->bits.begin(), ret->bits.end(), RTLIL::State::Sz) != ret->bits.end())
153 log_warning("Yosys has only limited support for tri-state logic at the moment. (%s:%d)\n",
154 current_filename.c_str(), get_line_num());
155 return ret;
156 }
157
158 const char *str = code.c_str();
159
160 // Strings
161 if (*str == '"') {
162 int len = strlen(str) - 2;
163 std::vector<RTLIL::State> data;
164 data.reserve(len * 8);
165 for (int i = 0; i < len; i++) {
166 unsigned char ch = str[len - i];
167 for (int j = 0; j < 8; j++) {
168 data.push_back((ch & 1) ? State::S1 : State::S0);
169 ch = ch >> 1;
170 }
171 }
172 AstNode *ast = AstNode::mkconst_bits(data, false);
173 ast->str = code;
174 return ast;
175 }
176
177 for (size_t i = 0; i < code.size(); i++)
178 if (code[i] == '_' || code[i] == ' ' || code[i] == '\t' || code[i] == '\r' || code[i] == '\n')
179 code.erase(code.begin()+(i--));
180 str = code.c_str();
181
182 char *endptr;
183 long len_in_bits = strtol(str, &endptr, 10);
184
185 // Simple base-10 integer
186 if (*endptr == 0) {
187 std::vector<RTLIL::State> data;
188 my_strtobin(data, str, -1, 10, case_type, false);
189 if (data.back() == State::S1)
190 data.push_back(State::S0);
191 return AstNode::mkconst_bits(data, true);
192 }
193
194 // unsized constant
195 if (str == endptr)
196 len_in_bits = -1;
197
198 // The "<bits>'[sS]?[bodhBODH]<digits>" syntax
199 if (*endptr == '\'')
200 {
201 std::vector<RTLIL::State> data;
202 bool is_signed = false;
203 bool is_unsized = len_in_bits < 0;
204 if (*(endptr+1) == 's' || *(endptr+1) == 'S') {
205 is_signed = true;
206 endptr++;
207 }
208 switch (*(endptr+1))
209 {
210 case 'b':
211 case 'B':
212 my_strtobin(data, endptr+2, len_in_bits, 2, case_type, is_unsized);
213 break;
214 case 'o':
215 case 'O':
216 my_strtobin(data, endptr+2, len_in_bits, 8, case_type, is_unsized);
217 break;
218 case 'd':
219 case 'D':
220 my_strtobin(data, endptr+2, len_in_bits, 10, case_type, is_unsized);
221 break;
222 case 'h':
223 case 'H':
224 my_strtobin(data, endptr+2, len_in_bits, 16, case_type, is_unsized);
225 break;
226 default:
227 char next_char = char(tolower(*(endptr+1)));
228 if (next_char == '0' || next_char == '1' || next_char == 'x' || next_char == 'z') {
229 is_unsized = true;
230 my_strtobin(data, endptr+1, 1, 2, case_type, is_unsized);
231 } else {
232 return NULL;
233 }
234 }
235 if (len_in_bits < 0) {
236 if (is_signed && data.back() == State::S1)
237 data.push_back(State::S0);
238 }
239 return AstNode::mkconst_bits(data, is_signed, is_unsized);
240 }
241
242 return NULL;
243 }
244
245 YOSYS_NAMESPACE_END