The C++ YANC accepts¶
cppcomp, YANC’s C++ compiler, takes a large part of C and C++: classes,
inheritance and virtual functions, templates, references, new and delete,
recursion, function pointers, a few standard headers. It is for code written
elsewhere (a filter, an estimator, a control loop) that should run on a
SAPHO with few changes. What it costs is size and speed: the same algorithm
written in C± (see The C± language) usually takes fewer
instructions and cycles.
#pragma yanc nuioin 1
#pragma yanc nuioou 1
int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
void main(void)
{
int n = in(0);
out(0, fact(n));
}
yanc fact.cpp makes the processor fact (the file’s name, which has to be
a valid Verilog module name: see The C± language). The
program is one source file, which can #include others.
The program¶
mainis required.void main(void)andint main()both work; the valuemainreturns is discarded.One source file per processor. Other files come in with
#include, searched in the including file’s folder, then in the folders given withyanc -I <dir>, then in the headers that come with YANC.The processor is configured with
#pragma yanclines, the C++ form of the C± directives (see Configuration).
Input and output¶
call |
does |
|---|---|
|
reads input port |
|
writes the integer |
|
pass a word over link |
A port carries an integer word. There is no fin/fout in C++: to read a
float, read an int and scale it; to write one, cast it, out(0, (int)x).
out with a float writes the float’s raw bits, not its value, and the
compiler warns.
With a keyboard and a screen beside the processor (myKeyPS2 and
myVGAText, see Peripherals), a program reads and writes
text as on a PC:
#include <iostream>
using namespace std;
int main()
{
char nome[20];
int a, b;
cout << "Nome? ";
cin.getline(nome, 20);
cout << "Oi, " << nome << '!' << endl;
cout << "a b? ";
cin >> a >> b;
cout << a << " + " << b << " = " << a + b << endl;
return 0;
}
Types¶
type |
in YANC |
|---|---|
|
one word, 32 bits; |
|
stored in a word, wrapping at their width as on a PC |
|
one 32-bit word: the compiler warns that the 64 bits are ignored |
|
a word holding 0 or 1 |
|
the SAPHO float, 23 mantissa and 8 exponent bits by default: the sizes of IEEE 754 single, not its encoding, no infinity, no NaN (see Floating point) |
|
the same 32-bit float, with a warning |
pointers, references |
one word; a pointer counts in words, and |
arrays |
up to 8 dimensions; |
|
the values are integer literals ( |
|
as in C++ |
|
for a local with an initializer, and in a range-for |
Constant data in the data memory. A string literal, and a global
const array whose initializer is made of constants (const int t[4] = {1, 2, 3};, const float k[] = {0.5f, -1.0f};, const char msg[] = "Oi";),
go straight into the data memory: no instruction stores them at the start
of the program. Write a table that never changes as const: without it, a
global array with an initializer is stored by code when main starts (3
instructions a word), because the data memory is loaded once, when the FPGA
is configured, and a value the program changed would stay changed after a
reset. An array of classes or of pointers is always stored by code.
sizeof counts words, not bytes: sizeof(int) == sizeof(char) == 1.
const objects cannot be assigned; constexpr is read as const, and a
const int with a constant value can size an array or be a template
argument. Every float operation rounds to the nearest (#FROUND 2): C++
always compiles at that level.
Statements and expressions¶
Everything C has: if, switch (with fall-through), while, do, for,
break, continue, goto, return; compound assignment (+=, <<=,
…), ++/--, ?:, the comma, casts (C casts and static_cast,
reinterpret_cast, const_cast, dynamic_cast, the last with no runtime
check), sizeof, static_assert, designated initializers, compound
literals. A range-for (for (auto x : c)) walks an array, or an object with
size() and operator[] such as std::vector and std::array. A number
starting with 0 is octal. A float used as a condition (if (x), !x,
x && y, x ? a : b) is true when it is not zero, as in C: the compiler
tests its mantissa, because SAPHO’s zero float is not an all-zero word.
Functions¶
Overloading by the number and type of the parameters, and default arguments, for functions and methods. The overload is chosen as C++ ranks it: the same type first, then a promotion (
charorbooltoint), then a conversion (between integer types, integer and float, one pointer type to another).char,bool,unsignedandintare told apart (f('A')callsf(char)),'A'is achar,trueabool, and a reference parameter (int&) matches by the type it refers to.Recursion works: a function that can reach itself gets a stack frame on a 1024-word call stack, built only when the program has one. Inside a recursive function a local cannot be an array or a struct; the compiler says so. Functions that do not recurse keep their locals at fixed addresses, as in C±.
Function pointers, including arrays of them, as parameters and as struct fields.
No variadic functions (
f(int n, ...)).
Classes¶
Data members, methods (inline or defined outside the class), access labels, static data members and static methods.
Constructors, overloaded, with member-init lists and default member initializers; copy constructors; destructors, run at the end of the scope and on
return,breakandcontinue. Every object is constructed: globals, locals, array elements, members,newandnew[].Single inheritance (one base class), virtual functions, pure virtual functions, virtual destructors.
Operator overloading, as member functions only, of `+ - * / == != < > <=
= << >> = [] += -= *= /=
and unary-and+. Not%,<<=,>>=, the bitwise operators,!,++,–,->,(), conversion operators, and not as free functions. An operator that returns a reference to its object chains:s << a << bcallsoperator<<` on what the first returned.Templates of functions and of classes, with type parameters (
typename/class) andintparameters, explicitf<T>(...). No specialization and no default template argument.Namespaces, nested; a qualified name and
using namespacework. No anonymousnamespace {. A namespace only groups names: every name in it is also global, sostd::coutandcoutare one object, and a program that includes<iostream>cannot name its own variablehexordec.A lambda only as an immediately-invoked global initializer with no captures and no parameters (
int k = []() { ... }();).
Dynamic memory¶
new, new[], delete, delete[], and malloc(nwords) / free(p)
(sizes in words). They share a heap of 2048 words, which the processor
gets only when the program allocates; when it is full, the allocation
returns 0. The heap’s size is fixed.
The standard headers¶
header |
gives |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
the integer types, |
|
|
|
|
There is no <string>, <algorithm>, <memory> or <utility>. A header
the program includes but does not use costs nothing: the code nobody calls
leaves the processor.
Text on the keyboard and the screen¶
<iostream> and <cstdio> read the keyboard block myKeyPS2 and write
the screen block myVGAText (Peripherals), over two
links. By default the keyboard is on link 0 and the screen on link 1, both
blocks on the processor’s clock. A program changes that with #define
before its first #include:
define |
default |
means |
|---|---|---|
|
0 |
the keyboard’s link |
|
1 |
the screen’s link |
|
1 |
1: the blocks on the processor’s clock ( |
|
1 |
1: what is typed shows on the screen |
|
80 |
the longest line read |
Writing. cout << takes const char*, char, bool (written as 1 or
0, as C++ does), int, unsigned and float, and std::endl,
std::flush, std::hex, std::dec, std::oct. A float is written as C++
writes it by default, with 6 significant digits: 7.5, 3.14159, 1e+10,
0.00015. The SAPHO float carries about 7 decimal digits, so the sixth can
be off by one. cout.put(c) and cout.write(s, n) also work. In C:
putchar(c), puts(s) (adds the new line), fputs(s, stdout).
Reading. As on a PC, nothing reaches the program before Enter: the
characters typed show on the screen and Backspace takes the last one back.
On a PC the terminal does that; here std::cin and getchar do. Then:
call |
reads |
|---|---|
|
|
|
the rest of the line, up to |
|
the next character, taken or left |
|
drops up to |
|
as C’s ( |
A number that is not there (a letter where cin >> n wants digits) sets
cin.fail(), gives 0 and takes nothing; cin.clear() resets it. As on a
PC, cin >> n leaves the Enter in the line, so a getline right after it
reads an empty line: cin.ignore(100, '\n') between them.
What it costs. Only what the program calls: a program that writes but
never reads gets no keyboard link. Printing a number divides nothing (the
processor gets no divider for it); printing or reading a float uses the
float adder and multiplier. The line typed takes YANC_LINE_MAX + 2 words
of data memory.
Not yet (GitHub issue #5): printf and scanf (they need variadic
functions), std::string and std::getline(cin, s), setw,
setprecision, and if (cin >> x) or !cin (use cin.fail()).
The preprocessor¶
#include "..." and <...>, #define with or without arguments (variadic
macros, # and ##), #undef, #if/#elif/#else/#endif with
defined, #ifdef/#ifndef, #pragma once, #error (stops) and
#warning. A line cannot continue onto the next with a backslash, and there
are no predefined macros (__FILE__, __cplusplus). yanc -S keeps the
assembly; cppcomp -E file.cpp prints the preprocessed source.
Configuration¶
#pragma yanc <setting> <value> sets what the C± directives set (see
Directives):
setting |
sets |
default |
|---|---|---|
|
the word width |
32 |
|
the float’s mantissa and exponent bits |
23 and 8 at 32 bits |
|
the number of input and output ports |
1 and 1 |
|
C±’s |
128 |
|
the data and call stack depths |
worked out from the program |
An unknown setting gives a warning. #pragma yanc prname (the name is the
file’s), fftsiz and itradd (bit-reversed addressing and interrupts
exist in C± only) are errors.
What does not exist¶
Exceptions (try, catch, throw), typeid and RTTI, friend,
decltype, multiple inheritance, variadic functions, lambdas inside a
function or with captures, template specialization and default template
arguments, std::string, file streams, printf, enum values that are not plain
literals, and more than one source file (use #include). Most of these
stop the compiler with “syntax error” at the line. GitHub issue #43 keeps
the list of the forms not accepted yet, and issue #31 the functions of
C±’s library that C++ lacks (fin, fout, sin and the rest).