/* * Generic thunking code to convert data between host and target CPU * * Copyright (c) 2003 Fabrice Bellard * * 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef THUNK_H #define THUNK_H #include #include "cpu.h" /* types enums definitions */ typedef enum argtype { TYPE_NULL, TYPE_CHAR, TYPE_SHORT, TYPE_INT, TYPE_LONG, TYPE_ULONG, TYPE_PTRVOID, /* pointer on unknown data */ TYPE_LONGLONG, TYPE_ULONGLONG, TYPE_PTR, TYPE_ARRAY, TYPE_STRUCT, } argtype; #define MK_PTR(type) TYPE_PTR, type #define MK_ARRAY(type, size) TYPE_ARRAY, size, type #define MK_STRUCT(id) TYPE_STRUCT, id #define THUNK_TARGET 0 #define THUNK_HOST 1 typedef struct { /* standard struct handling */ const argtype *field_types; int nb_fields; int *field_offsets[2]; /* special handling */ void (*convert[2])(void *dst, const void *src); int size[2]; int align[2]; const char *name; } StructEntry; /* Translation table for bitmasks... */ typedef struct bitmask_transtbl { unsigned int x86_mask; unsigned int x86_bits; unsigned int alpha_mask; unsigned int alpha_bits; } bitmask_transtbl; void thunk_register_struct(int id, const char *name, const argtype *types); void thunk_register_struct_direct(int id, const char *name, StructEntry *se1); const argtype *thunk_convert(void *dst, const void *src, const argtype *type_ptr, int to_host); #ifndef NO_THUNK_TYPE_SIZE extern StructEntry struct_entries[]; static inline int thunk_type_size(const argtype *type_ptr, int is_host) { int type, size; const StructEntry *se; type = *type_ptr; switch(type) { case TYPE_CHAR: return 1; case TYPE_SHORT: return 2; case TYPE_INT: return 4; case TYPE_LONGLONG: case TYPE_ULONGLONG: return 8; case TYPE_LONG: case TYPE_ULONG: case TYPE_PTRVOID: case TYPE_PTR: if (is_host) { return HOST_LONG_SIZE; } else { return TARGET_LONG_SIZE; } break; case TYPE_ARRAY: size = type_ptr[1]; return size * thunk_type_size(type_ptr + 2, is_host); case TYPE_STRUCT: se = struct_entries + type_ptr[1]; return se->size[is_host]; default: return -1; } } static inline int thunk_type_align(const argtype *type_ptr, int is_host) { int type; const StructEntry *se; type = *type_ptr; switch(type) { case TYPE_CHAR: return 1; case TYPE_SHORT: return 2; case TYPE_INT: return 4; case TYPE_LONGLONG: case TYPE_ULONGLONG: return 8; case TYPE_LONG: case TYPE_ULONG: case TYPE_PTRVOID: case TYPE_PTR: if (is_host) { return HOST_LONG_SIZE; } else { return TARGET_LONG_SIZE; } break; case TYPE_ARRAY: return thunk_type_align(type_ptr + 2, is_host); case TYPE_STRUCT: se = struct_entries + type_ptr[1]; return se->align[is_host]; default: return -1; } } #endif /* NO_THUNK_TYPE_SIZE */ unsigned int target_to_host_bitmask(unsigned int x86_mask, bitmask_transtbl * trans_tbl); unsigned int host_to_target_bitmask(unsigned int alpha_mask, bitmask_transtbl * trans_tbl); #endif 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
\chapter{Introduction}
\label{chapter:intro}

This document presents the Free and Open Source (FOSS) Verilog HDL synthesis tool ``Yosys''.
Its design and implementation as well as its performance on real-world designs
is discussed in this document.

\section{History of Yosys}

A Hardware Description Language (HDL) is a computer language used to describe
circuits. A HDL synthesis tool is a computer program that takes a formal
description of a circuit written in an HDL as input and generates a netlist
that implements the given circuit as output.

Currently the most widely used and supported HDLs for digital circuits are
Verilog \cite{Verilog2005}\cite{VerilogSynth} and
VHDL\footnote{VHDL is an acronym for ``VHSIC hardware description language''
and VHSIC is an acronym for ``Very-High-Speed Integrated
Circuits''.} \cite{VHDL}\cite{VHDLSynth}.
Both HDLs are used for test and verification purposes as well as logic
synthesis, resulting in a set of synthesizable and a set of non-synthesizable
language features. In this document we only look at the synthesizable subset
of the language features.

In recent work on heterogeneous coarse-grain reconfigurable
logic \cite{intersynth} the need for a custom application-specific HDL synthesis
tool emerged. It was soon realised that a synthesis tool that understood Verilog
or VHDL would be preferred over a synthesis tool for a custom HDL. Given an
existing Verilog or VHDL front end, the work for writing the necessary
additional features and integrating them in an existing tool can be estimated to be
about the same as writing a new tool with support for a minimalistic custom HDL.

The proposed custom HDL synthesis tool should be licensed under a Free
and Open Source Software (FOSS) licence. So an existing FOSS Verilog or VHDL
synthesis tool would have been needed as basis to build upon. The main advantages
of choosing Verilog or VHDL is the ability to synthesize existing HDL code and
to mitigate the requirement for circuit-designers to learn a new language. In order to take full advantage of any existing FOSS Verilog or VHDL tool,
such a tool would have to provide a feature-complete implementation of the
synthesizable HDL subset.

Basic RTL synthesis is a well understood field \cite{LogicSynthesis}. Lexing,
parsing and processing of computer languages \cite{Dragonbook} is a thoroughly
researched field. All the information required to write such tools has been openly
available for a long time, and it is therefore likely that a FOSS HDL synthesis tool
with a feature-complete Verilog or VHDL front end must exist which can be used as a basis for a custom RTL synthesis tool.

Due to the author's preference for Verilog over VHDL it was decided early
on to go for Verilog instead of VHDL\footnote{A quick investigation into FOSS
VHDL tools yielded similar grim results for FOSS VHDL synthesis tools.}.
So the existing FOSS Verilog synthesis tools were evaluated (see
App.~\ref{chapter:sota}).  The results of this evaluation are utterly
devastating. Therefore a completely new Verilog synthesis tool was implemented
and is recommended as basis for custom synthesis tools. This is the tool that
is discussed in this document.

\section{Structure of this Document}

The structure of this document is as follows:

Chapter~\ref{chapter:intro} is this introduction.

Chapter~\ref{chapter:basics} covers a short introduction to the world of HDL
synthesis. Basic principles and the terminology are outlined in this chapter.

Chapter~\ref{chapter:approach} gives the quickest possible outline to how the
problem of implementing a HDL synthesis tool is approached in the case of
Yosys.

Chapter~\ref{chapter:overview} contains a more detailed overview of the
implementation of Yosys. This chapter covers the data structures used in
Yosys to represent a design in detail and is therefore recommended reading
for everyone who is interested in understanding the Yosys internals.

Chapter~\ref{chapter:celllib} covers the internal cell library used by Yosys.
This is especially important knowledge for anyone who wants to understand the
intermediate netlists used internally by Yosys.

Chapter~ \ref{chapter:prog} gives a tour to the internal APIs of Yosys. This
is recommended reading for everyone who actually wants to read or write
Yosys source code. The chapter concludes with an example loadable module
for Yosys.

Chapters~\ref{chapter:verilog}, \ref{chapter:opt}, and \ref{chapter:techmap}
cover three important pieces of the synthesis pipeline: The Verilog frontend,
the optimization passes and the technology mapping to the target architecture,
respectively.

Chapter~\ref{chapter:eval} covers the evaluation of the performance
(correctness and quality) of Yosys on real-world input data.
The chapter concludes the main part of this document with conclusions and
outlook to future work.

Various appendices, including a command reference manual
(App.~\ref{commandref}) and an evaluation of pre-existing FOSS Verilog
synthesis tools (App.~\ref{chapter:sota}) complete this document.