2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* The Verilog frontend.
|
|
|
|
*
|
|
|
|
* This frontend is using the AST frontend library (see frontends/ast/).
|
|
|
|
* Thus this frontend does not generate RTLIL code directly but creates an
|
|
|
|
* AST directly from the Verilog parse tree and then passes this AST to
|
|
|
|
* the AST frontend library.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* This file contains an ad-hoc parser for Verilog constants. The Verilog
|
|
|
|
* lexer does only recognize a constant but does not actually split it to its
|
|
|
|
* components. I.e. it just passes the Verilog code for the constant to the
|
|
|
|
* bison parser. The parser then uses the function const2ast() from this file
|
|
|
|
* to create an AST node for the constant.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "verilog_frontend.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2014-07-31 06:19:47 -05:00
|
|
|
YOSYS_NAMESPACE_BEGIN
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
using namespace AST;
|
|
|
|
|
|
|
|
// divide an arbitrary length decimal number by two and return the rest
|
|
|
|
static int my_decimal_div_by_two(std::vector<uint8_t> &digits)
|
|
|
|
{
|
|
|
|
int carry = 0;
|
|
|
|
for (size_t i = 0; i < digits.size(); i++) {
|
2015-08-11 04:32:37 -05:00
|
|
|
if (digits[i] >= 10)
|
2018-07-20 11:37:44 -05:00
|
|
|
log_file_error(current_filename, get_line_num(), "Invalid use of [a-fxz?] in decimal constant.\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
digits[i] += carry * 10;
|
|
|
|
carry = digits[i] % 2;
|
|
|
|
digits[i] /= 2;
|
|
|
|
}
|
2014-06-15 01:48:17 -05:00
|
|
|
while (!digits.empty() && !digits.front())
|
|
|
|
digits.erase(digits.begin());
|
2013-01-05 04:13:26 -06:00
|
|
|
return carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the number of significant bits in a binary number (not including the sign bit)
|
|
|
|
static int my_ilog2(int x)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
while (x != 0 && x != -1) {
|
|
|
|
x = x >> 1;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse a binary, decimal, hexadecimal or octal number with support for special bits ('x', 'z' and '?')
|
2019-05-27 04:42:10 -05:00
|
|
|
static void my_strtobin(std::vector<RTLIL::State> &data, const char *str, int len_in_bits, int base, char case_type, bool is_unsized)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
|
|
|
// all digits in string (MSB at index 0)
|
|
|
|
std::vector<uint8_t> digits;
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
if ('0' <= *str && *str <= '9')
|
|
|
|
digits.push_back(*str - '0');
|
|
|
|
else if ('a' <= *str && *str <= 'f')
|
|
|
|
digits.push_back(10 + *str - 'a');
|
|
|
|
else if ('A' <= *str && *str <= 'F')
|
|
|
|
digits.push_back(10 + *str - 'A');
|
|
|
|
else if (*str == 'x' || *str == 'X')
|
|
|
|
digits.push_back(0xf0);
|
|
|
|
else if (*str == 'z' || *str == 'Z')
|
|
|
|
digits.push_back(0xf1);
|
|
|
|
else if (*str == '?')
|
|
|
|
digits.push_back(0xf2);
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
2015-08-11 04:32:37 -05:00
|
|
|
if (base == 10 && GetSize(digits) == 1 && digits.front() >= 0xf0)
|
|
|
|
base = 2;
|
|
|
|
|
2015-08-17 08:27:35 -05:00
|
|
|
data.clear();
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
if (base == 10) {
|
2015-08-17 08:27:35 -05:00
|
|
|
while (!digits.empty())
|
2019-08-07 13:12:38 -05:00
|
|
|
data.push_back(my_decimal_div_by_two(digits) ? State::S1 : State::S0);
|
2015-08-17 08:27:35 -05:00
|
|
|
} else {
|
|
|
|
int bits_per_digit = my_ilog2(base-1);
|
|
|
|
for (auto it = digits.rbegin(), e = digits.rend(); it != e; it++) {
|
2015-08-17 08:37:33 -05:00
|
|
|
if (*it > (base-1) && *it < 0xf0)
|
2018-07-20 11:37:44 -05:00
|
|
|
log_file_error(current_filename, get_line_num(), "Digit larger than %d used in in base-%d constant.\n",
|
|
|
|
base-1, base);
|
2015-08-17 08:27:35 -05:00
|
|
|
for (int i = 0; i < bits_per_digit; i++) {
|
|
|
|
int bitmask = 1 << i;
|
|
|
|
if (*it == 0xf0)
|
|
|
|
data.push_back(case_type == 'x' ? RTLIL::Sa : RTLIL::Sx);
|
|
|
|
else if (*it == 0xf1)
|
|
|
|
data.push_back(case_type == 'x' || case_type == 'z' ? RTLIL::Sa : RTLIL::Sz);
|
|
|
|
else if (*it == 0xf2)
|
|
|
|
data.push_back(RTLIL::Sa);
|
|
|
|
else
|
2019-08-07 13:12:38 -05:00
|
|
|
data.push_back((*it & bitmask) ? State::S1 : State::S0);
|
2015-08-17 08:27:35 -05:00
|
|
|
}
|
2014-06-15 01:48:17 -05:00
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2015-08-17 08:27:35 -05:00
|
|
|
int len = GetSize(data);
|
2019-08-07 13:12:38 -05:00
|
|
|
RTLIL::State msb = data.empty() ? State::S0 : data.back();
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2015-08-17 08:27:35 -05:00
|
|
|
if (len_in_bits < 0) {
|
|
|
|
if (len < 32)
|
2019-08-07 13:12:38 -05:00
|
|
|
data.resize(32, msb == State::S0 || msb == State::S1 ? RTLIL::S0 : msb);
|
2015-08-17 08:27:35 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:42:10 -05:00
|
|
|
if (is_unsized && (len > len_in_bits))
|
|
|
|
log_file_error(current_filename, get_line_num(), "Unsized constant must have width of 1 bit, but have %d bits!\n", len);
|
|
|
|
|
2015-08-17 08:27:35 -05:00
|
|
|
for (len = len - 1; len >= 0; len--)
|
2019-08-07 13:12:38 -05:00
|
|
|
if (data[len] == State::S1)
|
2015-08-17 08:27:35 -05:00
|
|
|
break;
|
2019-08-07 13:12:38 -05:00
|
|
|
if (msb == State::S0 || msb == State::S1) {
|
2015-08-17 08:27:35 -05:00
|
|
|
len += 1;
|
2019-08-07 13:12:38 -05:00
|
|
|
data.resize(len_in_bits, State::S0);
|
2015-08-17 08:27:35 -05:00
|
|
|
} else {
|
|
|
|
len += 2;
|
|
|
|
data.resize(len_in_bits, msb);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2015-08-17 08:27:35 -05:00
|
|
|
|
|
|
|
if (len > len_in_bits)
|
2015-08-17 09:22:53 -05:00
|
|
|
log_warning("Literal has a width of %d bit, but value requires %d bit. (%s:%d)\n",
|
2015-08-17 08:27:35 -05:00
|
|
|
len_in_bits, len, current_filename.c_str(), get_line_num());
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2015-08-14 15:23:01 -05:00
|
|
|
// convert the Verilog code for a constant to an AST node
|
2014-11-14 12:59:50 -06:00
|
|
|
AstNode *VERILOG_FRONTEND::const2ast(std::string code, char case_type, bool warn_z)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2014-11-14 12:59:50 -06:00
|
|
|
if (warn_z) {
|
|
|
|
AstNode *ret = const2ast(code, case_type);
|
2019-06-26 04:09:43 -05:00
|
|
|
if (ret != nullptr && std::find(ret->bits.begin(), ret->bits.end(), RTLIL::State::Sz) != ret->bits.end())
|
2015-08-17 09:22:53 -05:00
|
|
|
log_warning("Yosys has only limited support for tri-state logic at the moment. (%s:%d)\n",
|
2015-08-11 04:32:37 -05:00
|
|
|
current_filename.c_str(), get_line_num());
|
2014-11-14 12:59:50 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
const char *str = code.c_str();
|
|
|
|
|
|
|
|
// Strings
|
|
|
|
if (*str == '"') {
|
|
|
|
int len = strlen(str) - 2;
|
|
|
|
std::vector<RTLIL::State> data;
|
|
|
|
data.reserve(len * 8);
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
unsigned char ch = str[len - i];
|
|
|
|
for (int j = 0; j < 8; j++) {
|
2019-08-07 13:12:38 -05:00
|
|
|
data.push_back((ch & 1) ? State::S1 : State::S0);
|
2013-01-05 04:13:26 -06:00
|
|
|
ch = ch >> 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AstNode *ast = AstNode::mkconst_bits(data, false);
|
|
|
|
ast->str = code;
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < code.size(); i++)
|
|
|
|
if (code[i] == '_' || code[i] == ' ' || code[i] == '\t' || code[i] == '\r' || code[i] == '\n')
|
|
|
|
code.erase(code.begin()+(i--));
|
|
|
|
str = code.c_str();
|
|
|
|
|
|
|
|
char *endptr;
|
2014-06-15 01:48:17 -05:00
|
|
|
long len_in_bits = strtol(str, &endptr, 10);
|
|
|
|
|
|
|
|
// Simple base-10 integer
|
|
|
|
if (*endptr == 0) {
|
|
|
|
std::vector<RTLIL::State> data;
|
2019-05-27 04:42:10 -05:00
|
|
|
my_strtobin(data, str, -1, 10, case_type, false);
|
2019-08-07 13:12:38 -05:00
|
|
|
if (data.back() == State::S1)
|
|
|
|
data.push_back(State::S0);
|
2014-06-15 01:48:17 -05:00
|
|
|
return AstNode::mkconst_bits(data, true);
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-01-24 08:05:24 -06:00
|
|
|
// unsized constant
|
2013-01-05 04:13:26 -06:00
|
|
|
if (str == endptr)
|
2014-06-15 01:48:17 -05:00
|
|
|
len_in_bits = -1;
|
2013-01-05 04:13:26 -06:00
|
|
|
|
2014-11-24 07:39:24 -06:00
|
|
|
// The "<bits>'s?[bodhBODH]<digits>" syntax
|
2013-01-05 04:13:26 -06:00
|
|
|
if (*endptr == '\'')
|
|
|
|
{
|
|
|
|
std::vector<RTLIL::State> data;
|
|
|
|
bool is_signed = false;
|
2019-06-20 14:43:39 -05:00
|
|
|
bool is_unsized = len_in_bits < 0;
|
2013-01-05 04:13:26 -06:00
|
|
|
if (*(endptr+1) == 's') {
|
|
|
|
is_signed = true;
|
|
|
|
endptr++;
|
|
|
|
}
|
|
|
|
switch (*(endptr+1))
|
|
|
|
{
|
|
|
|
case 'b':
|
2014-11-24 07:39:24 -06:00
|
|
|
case 'B':
|
2019-06-20 14:43:39 -05:00
|
|
|
my_strtobin(data, endptr+2, len_in_bits, 2, case_type, is_unsized);
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
case 'o':
|
2014-11-24 07:39:24 -06:00
|
|
|
case 'O':
|
2019-06-20 14:43:39 -05:00
|
|
|
my_strtobin(data, endptr+2, len_in_bits, 8, case_type, is_unsized);
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
case 'd':
|
2014-11-24 07:39:24 -06:00
|
|
|
case 'D':
|
2019-06-20 14:43:39 -05:00
|
|
|
my_strtobin(data, endptr+2, len_in_bits, 10, case_type, is_unsized);
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
case 'h':
|
2014-11-24 07:39:24 -06:00
|
|
|
case 'H':
|
2019-06-20 14:43:39 -05:00
|
|
|
my_strtobin(data, endptr+2, len_in_bits, 16, case_type, is_unsized);
|
2013-01-05 04:13:26 -06:00
|
|
|
break;
|
|
|
|
default:
|
2019-05-27 04:42:10 -05:00
|
|
|
char next_char = char(tolower(*(endptr+1)));
|
|
|
|
if (next_char == '0' || next_char == '1' || next_char == 'x' || next_char == 'z') {
|
|
|
|
is_unsized = true;
|
2019-06-20 14:43:39 -05:00
|
|
|
my_strtobin(data, endptr+1, 1, 2, case_type, is_unsized);
|
2019-05-27 04:42:10 -05:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
2014-06-15 01:48:17 -05:00
|
|
|
if (len_in_bits < 0) {
|
2019-08-07 13:12:38 -05:00
|
|
|
if (is_signed && data.back() == State::S1)
|
|
|
|
data.push_back(State::S0);
|
2014-06-15 01:48:17 -05:00
|
|
|
}
|
2019-05-27 04:42:10 -05:00
|
|
|
return AstNode::mkconst_bits(data, is_signed, is_unsized);
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2013-06-07 06:59:13 -05:00
|
|
|
return NULL;
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2014-07-31 06:19:47 -05:00
|
|
|
YOSYS_NAMESPACE_END
|