dtc: Add support for named integer constants

You may define constants as follows:

/define/ CHROME_OS_BOOT_DEVICES "emmc", "spi";
/define/ GBB_BASE <0x00e08000>;
/define/ UART_BAUD_OPTIONS <115200 57600 19200>;

And properties may use these values as follows:

test-node {
    boot-devices = <CHROME_OS_BOOT_DEVICES>;
    gbb = <GBB_BASE 0x20000>;
    baud-rates = <UART_BAUD_OPTIONS>;
};

BUG=chromium-os:29014
TEST=compile the above code and see with fdtdump that we get correct results.

Change-Id: I891fb9446c76adc90b78c3021cc33acce13d12e7
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/dtc-lexer.l b/dtc-lexer.l
index 4715f31..18a76eb 100644
--- a/dtc-lexer.l
+++ b/dtc-lexer.l
@@ -28,6 +28,7 @@
 PROPNODECHAR	[a-zA-Z0-9,._+*#?@-]
 PATHCHAR	({PROPNODECHAR}|[/])
 LABEL		[a-zA-Z_][a-zA-Z0-9_]*
+IDENTIFIER	[a-zA-Z_][a-zA-Z0-9_]*
 STRING		\"([^\\"]|\\.)*\"
 CHAR_LITERAL	'([^']|\\')*'
 WS		[[:space:]]
@@ -103,6 +104,12 @@
 			return DT_BITS;
 		}
 
+<*>"/define/"	{
+			DPRINT("Keyword: /define/\n");
+			BEGIN_DEFAULT();
+			return DT_DEFINE;
+		}
+
 <*>{LABEL}:	{
 			DPRINT("Label: %s\n", yytext);
 			yylval.labelref = xstrdup(yytext);
@@ -110,6 +117,12 @@
 			return DT_LABEL;
 		}
 
+<V1>{IDENTIFIER} {
+			DPRINT("identifier: %s\n", yytext);
+			yylval.identifier = xstrdup(yytext + 1);
+			return DT_IDENTIFIER;
+		}
+
 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
 			yylval.literal = xstrdup(yytext);
 			DPRINT("Literal: '%s'\n", yylval.literal);
diff --git a/dtc-parser.y b/dtc-parser.y
index 6d5c2c2..a80e19b 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -33,6 +33,9 @@
 extern struct boot_info *the_boot_info;
 extern int treesource_error;
 
+static struct identifier *get_identifier(const char *s);
+static void set_identifier(const char *name, struct data data);
+static void eval_identifier(struct data *data, const char *name);
 static unsigned long long eval_literal(const char *s, int base, int bits);
 static unsigned char eval_char_literal(const char *s);
 %}
@@ -41,6 +44,7 @@
 	char *propnodename;
 	char *literal;
 	char *labelref;
+	char *identifier;
 	unsigned int cbase;
 	uint8_t byte;
 	struct data data;
@@ -61,8 +65,10 @@
 %token DT_V1
 %token DT_MEMRESERVE
 %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
+%token DT_DEFINE
 %token DT_BITS
 %token <propnodename> DT_PROPNODENAME
+%token <identifier> DT_IDENTIFIER
 %token <literal> DT_LITERAL
 %token <literal> DT_CHAR_LITERAL
 %token <cbase> DT_BASE
@@ -72,6 +78,7 @@
 %token <labelref> DT_REF
 %token DT_INCBIN
 
+%type <re> define
 %type <data> propdata
 %type <data> propdataprefix
 %type <re> memreserve
@@ -134,11 +141,22 @@
 		}
 	;
 
+define:
+	  DT_DEFINE DT_IDENTIFIER propdata ';'
+		{
+			set_identifier($2, $3);
+		}
+	;
+
 devicetree:
 	  '/' nodedef
 		{
 			$$ = name_node($2, "");
 		}
+	| define
+		{
+			$$ = name_node(build_node(NULL, NULL), "");
+		}
 	| devicetree '/' nodedef
 		{
 			$$ = merge_nodes($1, $3);
@@ -153,6 +171,10 @@
 				print_error("label or path, '%s', not found", $2);
 			$$ = $1;
 		}
+	| devicetree define
+		{
+			$$ = $1;
+		}
 	;
 
 nodedef:
@@ -313,6 +335,13 @@
 		{
 			$$.data = data_add_marker($1.data, LABEL, $2);
 		}
+	| arrayprefix DT_IDENTIFIER
+		{
+			struct data data;
+
+			eval_identifier(&data, $2);
+			$$.data = data_merge($1.data, data);
+		}
 	;
 
 integer_prim:
@@ -464,6 +493,53 @@
 	print_error("%s", s);
 }
 
+struct identifier {
+	const char *name;
+	struct data data;
+	struct identifier *next;
+};
+static struct identifier *identifiers;
+
+static struct identifier *get_identifier(const char *name)
+{
+	struct identifier *identifier = identifiers;
+
+	while (identifier != NULL) {
+		if (streq(name, identifier->name))
+			return identifier;
+		identifier = identifier->next;
+	}
+
+	return NULL;
+}
+
+static void set_identifier(const char *name, struct data data)
+{
+	struct identifier *identifier;
+
+	if (get_identifier(name) != NULL) {
+		print_error("redefining %s", name);
+		return;
+	}
+
+	identifier = xmalloc(sizeof(*identifier));
+	identifier->name = name;
+	identifier->data = data;
+	identifier->next = identifiers;
+	identifiers = identifier;
+}
+
+void eval_identifier(struct data *data, const char *name)
+{
+	struct identifier *identifier = get_identifier(name);
+
+	if (identifier == NULL) {
+		print_error("identifier %s does not exist", name);
+		return;
+	}
+	*data = identifier->data;
+}
+
 static unsigned long long eval_literal(const char *s, int base, int bits)
 {
 	unsigned long long val;
diff --git a/tests/.gitignore b/tests/.gitignore
index 0b71bcf..604f6b7 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -19,6 +19,7 @@
 /get_path
 /get_phandle
 /getprop
+/identifiers
 /incbin
 /mangle-layout
 /move_and_save
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index 1795466..73a9da3 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -20,7 +20,8 @@
 	dtb_reverse dtbs_equal_unordered \
 	add_subnode_with_nops path_offset_aliases \
 	utilfdt_test \
-	integer-expressions
+	integer-expressions \
+	utilfdt_test identifiers
 LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%)
 
 LIBTREE_TESTS_L = truncated_property
diff --git a/tests/identifiers.c b/tests/identifiers.c
new file mode 100644
index 0000000..a013d00
--- /dev/null
+++ b/tests/identifiers.c
@@ -0,0 +1,44 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ *	Testcase for /define/
+ * Copyright (C) 2011 NVIDIA, Inc.
+ * Derived from code:
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "tests.h"
+#include "testdata.h"
+
+int main(int argc, char *argv[])
+{
+	void *fdt;
+
+	test_init(argc, argv);
+	fdt = load_blob_arg(argc, argv);
+
+	check_property_cell(fdt, 0, "var1", TEST_VALUE_1);
+	check_property_cell(fdt, 0, "var2", TEST_VALUE_1);
+
+	PASS();
+}
diff --git a/tests/identifiers.dts b/tests/identifiers.dts
new file mode 100644
index 0000000..098e8f5
--- /dev/null
+++ b/tests/identifiers.dts
@@ -0,0 +1,11 @@
+/dts-v1/;
+
+/define/ VAR1 <0xdeadbeef>;
+/define/ VAR2 <VAR1>;
+
+/ {
+	var1 = <VAR1>;
+	var2 = <VAR2>;
+};
+
+/define/ OTHER <0xdeadbeef>;
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index bf68f44..2a0e92e 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -410,6 +410,10 @@
 	integer-expressions.test.dts
     run_test integer-expressions integer-expressions.test.dtb
 
+    # Test identifiers
+    run_dtc_test -I dts -O dtb -o identifiers.dtb identifiers.dts
+    run_test identifiers identifiers.dtb
+
     # Check for graceful failure in some error conditions
     run_sh_test dtc-fatal.sh -I dts -O dtb nosuchfile.dts
     run_sh_test dtc-fatal.sh -I dtb -O dtb nosuchfile.dtb