summaryrefslogtreecommitdiff
path: root/passes/abc/vlparse.cc
blob: fe10f57b1a523de14c328f4d8e2696fc741961de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
 *  
 *  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.
 *  
 *  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.
 *
 */

#include "vlparse.h"
#include "kernel/log.h"
#include <stdio.h>
#include <string>

static int lex_line, lex_tok;
static std::string lex_str;

static int token(int tok)
{
	lex_tok = tok;
#if 0
	if (lex_tok == 256)
		fprintf(stderr, "STR in line %d: >>%s<<\n", lex_line, lex_str.c_str());
	else if (tok >= 32 && tok < 255)
		fprintf(stderr, "CHAR in line %d: >>%c<<\n", lex_line, lex_tok);
	else
		fprintf(stderr, "CHAR in line %d: %d\n", lex_line, lex_tok);
#endif
	return tok;
}

static int lex(FILE *f)
{
	int ch = getc(f);

	while (ch == ' ' || ch == '\t' || ch == '\n') {
		if (ch == '\n')
			lex_line++;
		ch = getc(f);
	}

	if (ch <= 0 || 255 < ch)
		return token(lex_tok);
	
	if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||
			('0' <= ch && ch <= '9') || ch == '_' || ch == '\'') {
		lex_str = char(ch);
		while (1) {
			ch = getc(f);
			if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||
					('0' <= ch && ch <= '9') || ch == '_' || ch == '\'') {
				lex_str += char(ch);
				continue;
			}
			break;
		}
		ungetc(ch, f);
		return token(256);
	}

	if (ch == '/') {
		ch = getc(f);
		if (ch == '/') {
			while (ch != '\n')
				ch = getc(f);
			ungetc(ch, f);
			return lex(f);
		}
		ungetc(ch, f);
		return token('/');
	}

	return token(ch);
}

RTLIL::Design *abc_parse_verilog(FILE *f)
{
	RTLIL::Design *design = new RTLIL::Design;
	RTLIL::Module *module;
	RTLIL::Wire *wire;
	RTLIL::Cell *cell;

	int port_count = 1;
	lex_line = 1;

	// parse module header
	if (lex(f) != 256 || lex_str != "module")
		goto error;
	if (lex(f) != 256)
		goto error;

	module = new RTLIL::Module;
	module->name = "\\" + lex_str;
	design->modules[module->name] = module;

	if (lex(f) != '(')
		goto error;
	while (lex(f) != ')') {
		if (lex_tok != 256 && lex_tok != ',')
			goto error;
	}
	if (lex(f) != ';')
		goto error;

	// parse module body
	while (1)
	{
		if (lex(f) != 256)
			goto error;

		if (lex_str == "endmodule")
			return design;

		if (lex_str == "input" || lex_str == "output" || lex_str == "wire")
		{
			std::string mode = lex_str;
			while (lex(f) != ';') {
				if (lex_tok != 256 && lex_tok != ',')
					goto error;
				if (lex_tok == 256) {
					// printf("%s [%s]\n", mode.c_str(), lex_str.c_str());
					wire = new RTLIL::Wire;
					wire->name = "\\" + lex_str;
					if (mode == "input") {
						wire->port_id = port_count++;
						wire->port_input = true;
					}
					if (mode == "output") {
						wire->port_id = port_count++;
						wire->port_output = true;
					}
					module->wires[wire->name] = wire;
				}
			}
		}
		else if (lex_str == "assign")
		{
			std::string lhs, rhs;

			if (lex(f) != 256)
				goto error;
			lhs = lex_str;

			if (lex(f) != '=')
				goto error;
			if (lex(f) != 256)
				goto error;
			rhs = lex_str;

			if (lex(f) != ';')
				goto error;

			if (module->wires.count(RTLIL::escape_id(lhs)) == 0)
				goto error;

			if (rhs == "1'b0")
				module->connections.push_back(RTLIL::SigSig(module->wires.at(RTLIL::escape_id(lhs)), RTLIL::SigSpec(0, 1)));
			else if (rhs == "1'b1")
				module->connections.push_back(RTLIL::SigSig(module->wires.at(RTLIL::escape_id(lhs)), RTLIL::SigSpec(1, 1)));
			else if (module->wires.count(RTLIL::escape_id(rhs)) > 0)
				module->connections.push_back(RTLIL::SigSig(module->wires.at(RTLIL::escape_id(lhs)), module->wires.at(RTLIL::escape_id(rhs))));
			else
				goto error;
		}
		else
		{
			std::string cell_type = lex_str;

			if (lex(f) != 256)
				goto error;

			std::string cell_name = lex_str;

			if (lex(f) != '(')
				goto error;

			// printf("cell [%s] [%s]\n", cell_type.c_str(), cell_name.c_str());
			cell = new RTLIL::Cell;
			cell->type = "\\" + cell_type;
			cell->name = "\\" + cell_name;
			module->cells[cell->name] = cell;

			lex(f);
			while (lex_tok != ')')
			{
				if (lex_tok != '.' || lex(f) != 256)
					goto error;

				std::string cell_port = lex_str;

				if (lex(f) != '(' || lex(f) != 256)
					goto error;

				std::string wire_name = lex_str;

				// printf("  [%s] <- [%s]\n", cell_port.c_str(), wire_name.c_str());
				if (module->wires.count("\\" + wire_name) == 0)
					goto error;
				cell->connections["\\" + cell_port] = RTLIL::SigSpec(module->wires["\\" + wire_name]);

				if (lex(f) != ')' || (lex(f) != ',' && lex_tok != ')'))
					goto error;
				while (lex_tok == ',')
					lex(f);
			}

			if (lex(f) != ';')
				goto error;
		}
	}

error:
	log_error("Syntax error in line %d!\n", lex_line);
	// delete design;
	// return NULL;
}