Index: head/usr.bin/dtc/HACKING =================================================================== --- head/usr.bin/dtc/HACKING (revision 338231) +++ head/usr.bin/dtc/HACKING (revision 338232) @@ -1,63 +1,63 @@ $FreeBSD$ Notes for people hacking on dtc =============================== This file contains some notes for people wishing to hack on dtc. Upstreaming ----------- -This code is developed in the FreeBSD svn repository: +This code is developed in the git repository: -https://svn.freebsd.org/base/head/usr.bin/dtc +https://github.com/davidchisnall/dtc If you got the source from anywhere else and wish to make changes, please ensure that you are working against the latest version, or you may end up fixing bugs that are already fixed upstream. Although the license makes no requirement that you share any improvements that you make, patches are very welcome. C++11 ----- This project uses C++11, as the goal for FreeBSD 11 is to require C/C++11 as a minimum, either from clang or an external toolchain. In particular, it uses `std::unique_ptr` extensively for memory management within the tree. Unique pointers are also used in several other places to track ownership. Most iterator loops use the new loop syntax and the `auto` type for type deduction. Range-based `for` loops generally improve the readability of the code, though `auto` should only be used in places where the type can be deduced as easily by the reader as by the compiler. The code also makes use of `static_assert()` to track compile-time invariants. Adding New Checks ----------------- Currently, the biggest weakness of this version of the tool is that it lacks most of the semantic checkers that can be implemented by simply reading the ePAPR spec. The `checker` class provides a simple superclass for implementing these quite easily. There are also helper methods on `device_tree` for finding specific nodes, for checks that require some understanding of the structure of the tree. We should probably add a parent pointer to the `node` class for easily walking up the tree. Adding Direct C Output ---------------------- The FreeBSD build system currently uses dtc to generate a blob and then converts this to C source code. A new `output_writer` subclass could easily generate the C directly. Parser Improvements ------------------- There are a few FIXME lines in the parser for some corner cases that are not currently used by FreeBSD. These are mainly related to labels in the middle of values. These can be fixed by creating a new `property_value` with the specified label, starting at the location of the label. Don't forget to remove the associated comments from the BUGS section of the man page if you fix this. Index: head/usr.bin/dtc/string.cc =================================================================== --- head/usr.bin/dtc/string.cc (revision 338231) +++ head/usr.bin/dtc/string.cc (revision 338232) @@ -1,151 +1,152 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2013 David Chisnall * All rights reserved. * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include +#include #include #include #include "util.hh" using std::string; namespace dtc { void push_string(byte_buffer &buffer, const string &s, bool escapes) { size_t length = s.size(); for (size_t i=0 ; i= '0') { v <<= 3; v |= digittoint(s[i+1]); i++; if (i+1 < length && s[i+1] <= '7' && s[i+1] >= '0') { v <<= 3; v |= digittoint(s[i+1]); } } c = (uint8_t)v; break; } case 'x': { ++i; if (i >= length) { break; } int v = digittoint(s[i]); if (i+1 < length && ishexdigit(s[i+1])) { v <<= 4; v |= digittoint(s[++i]); } c = (uint8_t)v; break; } } } buffer.push_back(c); } } namespace { string dirbasename(std::function fn, const string &s) { if (s == string()) { return string(); } std::unique_ptr str = {strdup(s.c_str()), free}; string dn(fn(str.get())); return dn; } } string dirname(const string &s) { return dirbasename(::dirname, s); } string basename(const string &s) { return dirbasename(::basename, s); } } // namespace dtc Index: head/usr.bin/dtc/util.hh =================================================================== --- head/usr.bin/dtc/util.hh (revision 338231) +++ head/usr.bin/dtc/util.hh (revision 338232) @@ -1,117 +1,120 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2013 David Chisnall * All rights reserved. * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _UTIL_HH_ #define _UTIL_HH_ +#include +#include +#include #include // If we aren't using C++11, then just ignore static asserts. #if __cplusplus < 201103L #ifndef static_assert #define static_assert(x, y) ((void)0) #endif #endif namespace dtc { /** * Type for a buffer of bytes. This is used for a lot of short-lived temporary * variables, so may eventually be changed to something like LLVM's * SmallVector, but currently the program runs in a tiny fraction of a second, * so this is not an issue. */ typedef std::vector byte_buffer; /** * Helper function to push a big endian value into a byte buffer. We use * native-endian values for all of the in-memory data structures and only * transform them into big endian form for output. */ template inline void push_big_endian(byte_buffer &v, T val) { static_assert(sizeof(T) > 1, "Big endian doesn't make sense for single-byte values"); for (int bit=(sizeof(T) - 1)*8 ; bit>=0 ; bit-= 8) { v.push_back((val >> bit) & 0xff); } } void push_string(byte_buffer &v, const std::string &s, bool escapes=false); /** * Simple inline non-locale-aware check that this is a valid ASCII * digit. */ inline bool isdigit(char c) { return (c >= '0') && (c <= '9'); } /** * Simple inline non-locale-aware check that this is a valid ASCII * hex digit. */ inline bool ishexdigit(char c) { return ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F')); } /** * Simple inline non-locale-aware check that this is a valid ASCII * letter. */ inline bool isalpha(char c) { return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); } /** * A wrapper around dirname(3) that handles inconsistencies relating to memory * management between platforms and provides a std::string interface. */ std::string dirname(const std::string&); /** * A wrapper around basename(3) that handles inconsistencies relating to memory * management between platforms and provides a std::string interface. */ std::string basename(const std::string&); }// namespace dtc #endif // !_UTIL_HH_