ecp5: fix rebase mistake
[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_in_bits == 0)
143 log_file_error(current_filename, get_line_num(), "Illegal integer constant size of zero (IEEE 1800-2012, 5.7).\n");
144
145 if (len > len_in_bits)
146 log_warning("Literal has a width of %d bit, but value requires %d bit. (%s:%d)\n",
147 len_in_bits, len, current_filename.c_str(), get_line_num());
148 }
149
150 // convert the Verilog code for a constant to an AST node
151 AstNode *VERILOG_FRONTEND::const2ast(std::string code, char case_type, bool warn_z)
152 {
153 if (warn_z) {
154 AstNode *ret = const2ast(code, case_type);
155 if (ret != nullptr && std::find(ret->bits.begin(), ret->bits.end(), RTLIL::State::Sz) != ret->bits.end())
156 log_warning("Yosys has only limited support for tri-state logic at the moment. (%s:%d)\n",
157 current_filename.c_str(), get_line_num());
158 return ret;
159 }
160
161 const char *str = code.c_str();
162
163 // Strings
164 if (*str == '"') {
165 int len = strlen(str) - 2;
166 std::vector<RTLIL::State> data;
167 data.reserve(len * 8);
168 for (int i = 0; i < len; i++) {
169 unsigned char ch = str[len - i];
170 for (int j = 0; j < 8; j++) {
171 data.push_back((ch & 1) ? State::S1 : State::S0);
172 ch = ch >> 1;
173 }
174 }
175 AstNode *ast = AstNode::mkconst_bits(data, false);
176 ast->str = code;
177 return ast;
178 }
179
180 for (size_t i = 0; i < code.size(); i++)
181 if (code[i] == '_' || code[i] == ' ' || code[i] == '\t' || code[i] == '\r' || code[i] == '\n')
182 code.erase(code.begin()+(i--));
183 str = code.c_str();
184
185 char *endptr;
186 long len_in_bits = strtol(str, &endptr, 10);
187
188 // Simple base-10 integer
189 if (*endptr == 0) {
190 std::vector<RTLIL::State> data;
191 my_strtobin(data, str, -1, 10, case_type, false);
192 if (data.back() == State::S1)
193 data.push_back(State::S0);
194 return AstNode::mkconst_bits(data, true);
195 }
196
197 // unsized constant
198 if (str == endptr)
199 len_in_bits = -1;
200
201 // The "<bits>'[sS]?[bodhBODH]<digits>" syntax
202 if (*endptr == '\'')
203 {
204 std::vector<RTLIL::State> data;
205 bool is_signed = false;
206 bool is_unsized = len_in_bits < 0;
207 if (*(endptr+1) == 's' || *(endptr+1) == 'S') {
208 is_signed = true;
209 endptr++;
210 }
211 switch (*(endptr+1))
212 {
213 case 'b':
214 case 'B':
215 my_strtobin(data, endptr+2, len_in_bits, 2, case_type, is_unsized);
216 break;
217 case 'o':
218 case 'O':
219 my_strtobin(data, endptr+2, len_in_bits, 8, case_type, is_unsized);
220 break;
221 case 'd':
222 case 'D':
223 my_strtobin(data, endptr+2, len_in_bits, 10, case_type, is_unsized);
224 break;
225 case 'h':
226 case 'H':
227 my_strtobin(data, endptr+2, len_in_bits, 16, case_type, is_unsized);
228 break;
229 default:
230 char next_char = char(tolower(*(endptr+1)));
231 if (next_char == '0' || next_char == '1' || next_char == 'x' || next_char == 'z') {
232 is_unsized = true;
233 my_strtobin(data, endptr+1, 1, 2, case_type, is_unsized);
234 } else {
235 return NULL;
236 }
237 }
238 if (len_in_bits < 0) {
239 if (is_signed && data.back() == State::S1)
240 data.push_back(State::S0);
241 }
242 return AstNode::mkconst_bits(data, is_signed, is_unsized);
243 }
244
245 return NULL;
246 }
247
248 YOSYS_NAMESPACE_END