VBCC Compiler System: Volker Barthelmann
VBCC Compiler System: Volker Barthelmann
VBCC Compiler System: Volker Barthelmann
Volker Barthelmann
i
Table of Contents
1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Legal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 Installing for Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.2 Installing for DOS/Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.3 Installing for AmigaOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 The Frontend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 The Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1 General Compiler Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.4 Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4.1 Register Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4.2 Flow Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4.3 Common Subexpression Elimination . . . . . . . . . . . . . . . . . . . . . . 19
3.4.4 Copy Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.4.5 Constant Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.4.6 Dead Code Elimination. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4.7 Loop-Invariant Code Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4.8 Strength Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4.9 Induction Variable Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4.10 Loop Unrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4.11 Function Inlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4.12 Intrinsic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.4.13 Unused Object Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.4.14 Alias Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.4.15 Inter-Procedural Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.4.16 Cross-Module Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4.17 Instruction Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4.18 Target-Specific Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.4.19 Debugging Optimized Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.5 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.5.1 Pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.5.2 Register Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.5.3 Inline-Assembly Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.5.4 Variable Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
ii vbcc manual
4 M68k/Coldfire Backend . . . . . . . . . . . . . . . . . . . . . . . 39
4.1 Additional options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.2 ABI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.3 Small data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.4 Small code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.5 CPUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.6 FPUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.7 Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.8 Target-Specific Variable Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.9 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.10 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.11 Stdarg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.12 Known problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5 PowerPC Backend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.1 Additional options for this version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.2 ABI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.3 Target-specific variable-attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.4 Target-specific pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.5 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.6 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.7 Stdarg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.8 Known problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
7 i386 Backend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.1 Additional options for this version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.2 ABI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.3 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
7.4 Stdarg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
7.5 Known Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
iii
8 c16x Backend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.1 Additional options for this version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.2 ABI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.3 Target-specific variable-attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
8.4 Target-specific type-attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.5 Target-specific types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.6 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.7 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.8 Stdarg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.9 Known Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
9 68hc12 Backend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.1 Additional options for this version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.2 ABI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.3 Target-specific variable-attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.4 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.5 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.6 Stdarg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.7 Known Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
10 VideoCore IV Backend . . . . . . . . . . . . . . . . . . . . . . 63
10.1 Additional options for this version . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
10.2 ABI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
10.3 Target-specific variable-attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.4 Target-specific pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.5 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.6 Stdarg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.7 Known problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
11 Instruction Scheduler . . . . . . . . . . . . . . . . . . . . . . . . 67
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
11.2 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
11.3 Known problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
12 C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
12.2 Legal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
12.3 Embedded Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
12.3.1 Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
12.3.2 Heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
12.3.3 Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
12.3.4 Floating Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
12.3.5 Useless Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
12.3.6 Linking/Locating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
12.4 AmigaOS/68k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
12.4.1 Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
iv vbcc manual
12.9.6 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
12.9.7 libamiga.a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
12.9.8 libauto.a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
12.9.9 newlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
12.9.9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
12.9.9.2 Known Newlib Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
12.9.9.3 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
12.10 Atari TOS/MiNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
12.10.1 Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
12.10.2 Floating point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
12.10.3 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
12.10.4 16-bit integer model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
12.10.5 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.11 VideoCore/Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.11.1 Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.11.2 Floating point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.11.3 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.11.4 Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.11.5 System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.11.6 Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.11.6.1 Object Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.11.6.2 Command line arguments . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.11.6.3 Debug Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.11.7 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.12 ATARI Jaguar/68k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.12.1 Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.12.2 Floating point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.12.3 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.12.4 Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.12.5 stdio support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.12.6 The jaglib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13 List of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
1 General
1.1 Introduction
vbcc is a highly optimizing portable and retargetable ISO C compiler. It supports ISO
C according to ISO/IEC 9899:1989 and a subset of the new standard ISO/IEC 9899:1999
(C99).
It is split into a target-independent and a target-dependent part, and provides complete
abstraction of host- and target-arithmetic. Therefore, it fully supports cross-compiling for
8, 16, 32 and 64bit architectures.
Embedded systems are supported by features like different pointer-sizes (e.g. differently
sized function- and object-pointers or near- and far-pointers), ROM-able code, inline-
assembly, bit-types, interrupt-handlers, section-attributes, stack-calculation and many oth-
ers (depending on the backend).
vbcc provides a large set of aggressive high-level optimizations (see Section 3.4 [Optimiza-
tions], page 16) as well as target-specific optimizations to produce faster or smaller code.
Rather than restricting analysis and optimization to single functions or files, vbcc is able
to optimize across functions and even modules. Target-independent optimizations include:
− cross-module function-inlining
− partial inlining of recursive functions
− inter-procedural data-flow analysis
− inter-procedural register-allocation
− register-allocation for global variables
− global common-subexpression-elimination
− global constant-propagation
− global copy-propagation
− dead-code-elimination
− alias-analysis
− loop-unrolling
− induction-variable elimination
− loop-invariant code-motion
− loop-reversal
1.2 Legal
vbcc is copyright in 1995-2017 by Volker Barthelmann.
This archive may be redistributed without modifications and used for non-commercial pur-
poses.
An exception for commercial usage is granted, provided that the target CPU is M68k and
the target OS is AmigaOS. Resulting binaries may be distributed commercially without
further licensing.
In all other cases you need my written consent.
2 vbcc manual
1.3 Installation
The vbcc directory tree looks as follows:
vbcc/config
Config files for the frontend.
vbcc/targets/<target>
Subdirectory containing all files specific to a certain target (e.g. m68k-amigaos
or ppc-eabi).
Chapter 1: General 3
or
setenv VBCC <prefix>/vbcc
or
setenv PATH <prefix>/vbcc/bin:"$PATH"
vbcc_target_ppc-amigaos
Header files and libraries for AmigaOS 4.x.
vbcc_target_ppc-morphos
Header files and libraries for MorphOS.
vbcc_target_ppc-powerup
Header files and libraries for PowerUp.
vbcc_target_ppc-warpos
Header files and libraries for WarpOS.
Usually you will install the binary archive for your host architecture of choice, then add as
many target archives you need.
When installing manually it is recommended to add the following assigns to your
s:User-Startup file (only do the assignments required for the installed targets):
assign >NIL: vbcc: <path to vbcc directory>
assign >NIL: C: vbcc:bin add
Also, the stack-size has to be increased from the default, for those binaries which don’t
do that automatically (e.g. AmigaOS4). 64KB is a sensible value, for very large projects
higher values might be necessary.
For writing AmigaOS/MorphOS programs you will need the appropriate system header
files. Please use NDK 3.9 or later for AmigaOS/68k, as the proto/inline headers have been
created with it.
There are different configuration files provided in the config-subdirectory to choose differ-
ent targets (i.e. the system you want to generate programs for) and hosts (i.e. the system
Chapter 1: General 5
you want the compiler an tools to run on). The general naming-scheme for these files ist
<target> <host>.
Available config files, when all targets are installed, are aos68k (AmigaOS on M68k), kick13
(Kickstart 1.x on M68k), aosppc (AmigaOS4 on PPC using vclib), newlib (AmigaOS4 on
PPC using newlib), morphos (PPC systems running MorphOS), powerup (PPC boards
using the PowerUp system), warpos (PPC boards using the WarpOS system).
You can choose one of these systems using the +-option of vc, e.g.
vc +aos68k_powerup ...
will compile for AmigaOS/68k using the compiler running on PowerUp.
You may choose to create copies of some of these configuration files with simpler names.
E.g. if you usually want the compiler to run on WarpOS you could copy aos68k_warpos
to aos68k, warpos_warpos to warpos and so on. Then you can just specify the target and
your preferred host system will be chosen automatically.
Additionally, you may copy the configuration file for your preferred host/target-combination
to vc.config. This configuration will be chosen by default if you do not specify anything.
By default, the target-only-specifications use 68k-native tools on AmigaOS 2.x/3.x, e.g.
+warpos will create code for WarpOS, but the compiler and tools will run on the 68k. The
default vc.config will then create code for 68k using tools running on 68k. Having installed
the MorphOS-native binary archive instead, the default vc.config will create PPC code
for MorphOS using tools running on MorphOS.
1.4 Tutorial
Now you should be able to use vbcc. To compile and link the program hello.c, type
vc hello.c
The file hello.c will be compiled and linked to create the executable a.out in the current
directory.
vc hello.c -o hello
will do the same, but the created executable will be called hello.
vc -c t1.c t2.c
will compile t1.c and t2.c without linking, creating the object files t1.o and t2.o.
vc t1.o t2.o -o tt
will link them together and create the executable tt.
If your program uses floating point, you may have to link with a math-library. The details
are dependent on the target, but usually -lm will be suitable (for AmigaOS on m68k choose
one of -lmieee, -lm881, -lm040 or -lm060).
vc calc.c -o calc -lmieee
Chapter 2: The Frontend 7
2 The Frontend
This chapter describes vc, the frontend for vbcc. It knows how to deal with different file
types and optimization settings and will call the compiler, assembler and linker. It is not
recommended to call the different translation-phases directly. vc provides an easy-to-use
interface which is mostly compatible to Unix cc.
2.1 Usage
The general syntax for calling vc
vc [options] file1 file2 ...
processes all files according to their suffix and links all objects together (unless any of -E,
-S, -c is specified). The following file types are recognized:
.c C source
.i already preprocessed C source
.scs assembly source to be fed to the scheduler
.asm
.s assembly source
.obj
.o object file
Usually pattern matching is supported - however this depends on the port and the host
system.
The options recognized by vc are:
-v Verbose mode. Prints all commands before executing them.
-vv Very verbose. Displays some internals as well.
-Ox Sets the optimization level.
-O0 is equivalent to -O=0.
-O will activate some optimizations (at the moment -O=991).
-O2 will activate most optimizations (at the moment -O=1023 -schedule).
-O3 will activate all optimizations (at the moment -O=~0 -schedule).
-O4 will activate full cross-module-optimization.
can not be linked directly. Also, make sure to pass all relevant compiler options
also to the linker-command.
Higher values may or may not activate even more optimizations. The default
is -O=1. It is also possible to specify an exact value with -O=n. However, I do
not recommend this unless you know exactly what you are doing.
-o file Save the target as file (default for executables is a.out).
-E Save the preprocessed C sources with .i suffix.
-S Do not assemble. Save the compiled files with .asm suffix.
-SCS Do not schedule. Save the compiled files with .scs suffix.
-c Do not link. Save the compiled files with .o suffix.
-k Keep all intermediate files. By default all generated files except the source files
and the targets are deleted.
-Dstr #define a preprocessor symbol, e.g. -DAMIGA or -DCPU=68000. The former
syntax is equivalent to:
#define AMIGA 1
The latter form is equivalent to:
#define CPU 68000
-Ipath Add path to the include-search-path.
-lulib Link with library ulib.
-Lpath Add path to the library-search-path. This is passed through to the linker.
-static Instruct the linker to link against static libraries. This may override the default
to link against dynamic libraries first.
-nostdlib
Do not link with standard-startup/libraries. Useful only for people who know
what they are doing.
-notmpfile
Do not use names from tmpnam() for temporary files.
-schedule
Invoke the instruction-scheduler, if available.
+file Use file as config-file.
All other options are passed through to vbcc.
2.2 Configuration
vc needs a config file to know how to call all the translation phases (compiler, assembler,
linker). Unless a different file is specified using the +-option, it will look for a file vc.config
(vc.cfg for DOS/Windows).
On AmigaOS vc will search in the current directory, in ENV: and VBCC:.
On Unix vc will search in the current directory followed by /etc/.
Chapter 2: The Frontend 9
-pp=string
The preprocessor will be called like in printf(string,opts,infile,outfile),
e.g. the default for vcpp searching the includes in vinclude: and defining __
STDC__) is -pp=vcpp -Ivinclude: -D__STDC__=1 %s %s %s. Note that there is
an internal preprocessor, called ucpp, since V0.8, you usually don’t need this
option any more.
-cc=string
For the compiler. Note that you cannot use vc to call another compiler than
vbcc. But you can call different versions of vbcc this way, e.g.: -cc=vbccm68k
-quiet or -cc=vbcci386 -quiet
-isc=string
The same for the scheduler, e.g.: -isc=vscppc -quiet %s %s Omit, if there is
no scheduler for the architecture.
-as=string
The same for the assembler, e.g.: -as=vasmm68k_mot -quiet -Fhunk -phxass
-opt-pea -opt-clr %s -o %s or -as=as %s -o %s
-rm=string
This is the string for the delete command and takes only one argument, e.g.
-rm=delete quiet %s or -rm=rm %s
-ld=string
This is for the linker and takes three arguments. The first one are the object files
(separated by spaces), the second one the user specified libraries and the last one
the name of the resulting executable. This has to link with proper startup-code
and c-libraries, e.g.: -ld=vlink -x -Bstatic -Cvbcc -nostdlib -Lvlibos3:
vlibos3:startup.o %s %s -lvc -o %s or -ld=ld /usr/lib/crt0.o %s %s -lc
-o %s
-l2=string
The same like -ld, but standard-startup and -libraries should not be linked;
used when -nostdlib is specified.
-ldnodb=string
This option string is inserted in the linker command before specifying the li-
braries, whenever an executable without debugging information and symbols
should be created (AKA as a ’stripped’ executable).
10 vbcc manual
-ldstatic=string
This option string is inserted in the linker command before specifying the li-
braries when static linking was requested with option -static.
All those strings should tell the command to omit any output apart from error messages
if possible. However for every of those options there exists one with an additional ‘v’, i.e.
-ppv=, -asv=, etc. which should produce some output, if possible. If vc is invoked with the
-vv option the verbose commands will be called, if not the quiet ones will be used.
‘-ul=string’
Format for additional libraries specified with -l<lib>. The result of
printf(string,lib) will be added to the command invoking the linker.
Examples are: -ul=vlib:%s.lib or -ul=-l%s
Chapter 3: The Compiler 11
3 The Compiler
This chapter describes the target-independent part of the compiler. It documents the op-
tions and extensions which are not specific to a certain target. Be sure to also read the
chapter on the backend you are using. It will likely contain important additional information
like data-representation or additional options.
-cmd=<file>
A file containing additional command line options can be specified using this
command. This may be useful for very long command lines.
-c99 Switch to the 1999 ISO standard for C /ISO/IEC9899:1999). Currently the
following changes of C99 are handled:
− long long int (not supported by all backends)
− flexible array members as last element of a struct
− mixed statements and declarations
− declarations within for-loops
− inline function-specifier
− restrict-qualifier
− new reserved keywords
− //-comments
− vararg-macros
− _Pragma
− implicit int deprecated
− implicit function-declarations deprecated
− increased translation-limits
− designated initializers
− non-constant initializers for automatic aggregates
− compound literals
− variable-length arrays (incomplete)
-unsigned-char
Make the unqualified type of char unsigned.
-maxoptpasses=n
Set maximum number of optimizer passes to n. See Section 3.4 [Optimizations],
page 16.
-inline-size=n
Set the maximum ’size’ of functions to be inlined. See Section 3.4.11 [Function
Inlining], page 25.
-inline-depth=n
Inline functions up to n nesting-levels (including recursive calls). The default
value is 1. Be careful with values greater than 2. See Section 3.4.11 [Function
Inlining], page 25.
-unroll-size=n
Set the maximum ’size’ of unrolled loops. See Section 3.4.10 [Loop Unrolling],
page 23.
-unroll-all
Unroll loops with a non-constant number of iterations if the number can be cal-
culated at runtime before entering the loop. See Section 3.4.10 [Loop Unrolling],
page 23.
Chapter 3: The Compiler 13
-no-inline-peephole
Some backends provide peephole-optimizers which perform simple optimiza-
tions on the assembly code output by vbcc. By default, these optimizations
will also be performed on inline-assembly code of the application. This switch
turns off this behaviour. See Section 3.5.3 [Inline-Assembly Functions], page 33.
-fp-associative
Floating point operations do not obey the law of associativity, e.g.
(a+b)+c==a+(b+c) is not true for all floating point numbers a,b,c. Therefore
certain optimizations depending on this property cannot be performed on
floating point numbers.
This option tells vbcc to treat floating point operations as associative and
perform those optimizations even if that may change the results in some cases
(not ISO conforming).
-no-alias-opt
Do not perform type-based alias analysis. See Section 3.4.14 [Alias Analysis],
page 28.
-no-multiple-ccs
If the backend supports multiple condition code registers, vbcc will try to use
them when optimizing. This flag prevents vbcc from using them.
-double-push
On targets where function-arguments are passed in registers but also stack-slots
are left empty for such arguments, pass those arguments both in registers and
on the stack.
This generates less efficient code but some broken code (e.g. code which calls
varargs functions without correct prototypes in scope) may work.
-short-push
In the presence of a prototype, no promotion will be done on function argu-
ments. For example, <char> will be passed as <char> rather than <int> and
<float> will not be promoted to <double>. This may be more efficient on small
targets.
However, please note that this feature may not be supported by all backends
and that using this option breaks ANSI/ISO conformance. For example, a
function with a <char> parameter must never be called without a prototype in
scope.
-soft-float
On targets supporting this flag, software floating point emulation will be used
rather than a hardware FPU. Please consult the corresponding backend docu-
mentation when using this flag.
-stack-check
Insert code for dynamic stack checking/extending if the backend and the envi-
ronment support this feature.
-ansi
-iso Switch to ANSI/ISO mode.
14 vbcc manual
long double
The default signedness for integer types is signed.
Depending on the backend, some of these types can have identical representation. The
representation (size, alignment etc.) of these types usually varies between different backends.
vbcc is able to support arbitrary implementations.
Backends may be restricted and omit some types (e.g. floating point on small embedded
architectures) or offer additional types. E.g. some backends may provide special bit types
or different pointer types.
3.4 Optimizations
vbcc offers different levels of optimization, ranging from fast compilation with straight-
forward code suitable for easy debugging to highly aggressive cross-module optimizations
delivering very fast and/or tight code.
This section describes the general phases of compilation and gives a short overview on the
available optimizations.
In the first compilation phase every function is parsed into a tree structure one expression
after the other. Type-checking and some minor optimizations like constant-folding or some
algebraic simplifications are done on the trees. This phase of the translation is identical in
optimizing and non-optimizing compilation.
Then intermediate code is generated from the trees. In non-optimizing compilation tem-
poraries needed to evaluate the expression are immediately assigned to registers while in
optimizing compilation, a new variable is generated for each temporary. Slightly different
intermediate code is produced in optimizing compilation. Some minor optimizations are
performed while generating the intermediate code (simple elimination of unreachable code,
some optimizations on branches etc.).
After intermediate code for the whole function has been generated, simple register allocation
may be done in non-optimizing compilation if bit 1 has been set in the -O option. Afterwards,
the intermediate code is passed to the code generator and then all memory for the function,
its variables etc. is freed.
In optimizing compilation flowgraphs are constructed, data flow analysis is performed and
many passes are made over the function’s intermediate code. Code may be moved around,
new variables may be added, other variables removed etc. etc. (for more detailed informa-
tion on the optimizations look at the description for the -O option below).
Many of the optimization routines depend on each other. If one routine finds an optimiza-
tion, this often enables other routines to find further ones. Also, some routines only do a
first step and let other routines ’clean up’ afterwards. Therefore vbcc usually makes many
passes until no further optimizations are found. To avoid possible extremely long optimiza-
tion times, the number of those passes can be limited with -maxoptpasses (the default is
max. 10 passes). vbcc will display a warning if more passes might be useful.
Depending on the optimization level, a whole translation-unit or even several translation-
units will be read at once. Also, the intermediate code for all functions may be kept in
memory during the entire compilation. Be aware that higher optimization levels can take
much more time and memory to complete.
Chapter 3: The Compiler 17
The following table lists the optimizations which are activated by bits in the argument of
the -O option. Note that not all combinations are valid. It is heavily recommended not
to fiddle with this option but just use one of the settings provided by vc (e.g. -O0 - -O4).
These options also automatically handle actions like invoking the scheduler or cross-module
optimizer.
Bit 0 (1) Perform Register allocation. See Section 3.4.1 [Register Allocation], page 18.
Bit 1 (2) This flag turns on the optimizer. If it is set to zero, no global optimizations will
be performed, no matter what the other flags are set to. Slightly different inter-
mediate code will be generated by the first translation phases and a flowgraph
will be constructed. See Section 3.4.2 [Flow Optimizations], page 18.
Bit 2 (4) Perform common subexpression elimination (see Section 3.4.3 [Common Subex-
pression Elimination], page 19) and copy propagation (see Section 3.4.4 [Copy
Propagation], page 20). This can be done globally or only within basic blocks
depending on bit 5.
Bit 3 (8) Perform constant propagation (see Section 3.4.5 [Constant Propagation],
page 20). This can be done globally or only within basic blocks depending on
bit 5.
Bit 4 (16) Perform dead code elimination (see Section 3.4.6 [Dead Code Elimination],
page 21).
Bit 5 (32) Some optimizations are available in local and global versions. This flag turns
on the global versions. Several major optimizations will not be performed and
only one optimization pass is done unless this flag is set.
Bit 6 (64) Reserved.
Bit 7 (128)
vbcc will try to identify loops and perform some loop optimizations. See
Section 3.4.8 [Strength Reduction], page 22 and Section 3.4.7 [Loop-Invariant
Code Motion], page 21. These only work if bit 5 (32) is set.
Bit 8 (256)
vbcc tries to place variables at the same memory addresses if possible (see
Section 3.4.13 [Unused Object Elimination], page 27).
Bit 9 (512)
Reserved.
Bit 10 (1024)
Pointers are analyzed and more precise alias-information is generated (see
Section 3.4.14 [Alias Analysis], page 28). Using this information, better
data-flow analysis is possible.
Also, vbcc tries to place global/static variables and variables which have their
address taken in registers, if possible (see Section 3.4.1 [Register Allocation],
page 18).
Bit 11 (2048)
More aggressive loop optimizations are performed (see Section 3.4.10 [Loop
Unrolling], page 23 and Section 3.4.9 [Induction Variable Elimination], page 22).
Only works if bit 5 (32) and bit 7 (128) are set.
18 vbcc manual
Bit 12 (4096)
Perform function inlining (see Section 3.4.11 [Function Inlining], page 25).
Bit 13 (8192)
Reserved.
Bit 14 (16384)
Perform inter-procedural analysis (see Section 3.4.15 [Inter-Procedural Analy-
sis], page 29) and cross-module optimizations (see Section 3.4.16 [Cross-Module
Optimizations], page 30).
Also look at the documentation for the target-dependent part of vbcc. There may be
additional machine specific optimization options.
Also, unused labels will be removed and basic blocks united to allow further optimizations.
For example, the following code
void f(int x, int y)
{
if(x > y)
goto label1;
q();
label1:
goto label2;
r();
label2:
}
will be optimized like:
void f(int x, int y)
{
if(x <= y)
q();
}
Identical code at the beginning or end of basic blocks will be moved to the
successors/predecessors under certain conditions.
tmp = x * y;
q(tmp, tmp);
}
Depending on the optimization level, vbcc will perform this optimization only locally within
basic blocks or globally across an entire function.
As this optimization requires detecting whether operand of an expression may have changed,
it will be affected by other optimizations. See Section 3.4.14 [Alias Analysis], page 28,
Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Opti-
mizations], page 30.
20 vbcc manual
int f()
{
int x;
x = y;
return x;
}
will be optimized like:
int y;
int f()
{
return y;
}
Depending on the optimization level, vbcc will perform this optimization only locally within
basic blocks or globally across an entire function.
As this optimization requires detecting whether a variable may have changed, it will be
affected by other optimizations. See Section 3.4.14 [Alias Analysis], page 28, Section 3.4.15
[Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations],
page 30.
As this optimization requires detecting whether a variable may have changed, it will be
affected by other optimizations. See Section 3.4.14 [Alias Analysis], page 28, Section 3.4.15
[Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations],
page 30.
void f()
{
int y;
x = 1;
y = 2;
x = 3;
}
will be optimized like:
int x;
void f()
{
x = 3;
}
As this optimization requires detecting whether a variable may be read, it will be affected
by other optimizations. See Section 3.4.14 [Alias Analysis], page 28, Section 3.4.15 [Inter-
Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations], page 30.
void f(int n)
{
int i;
}
As this optimization requires detecting whether operands of an expression may
have changed, it will be affected by other optimizations. See Section 3.4.14 [Alias
Analysis], page 28, Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16
[Cross-Module Optimizations], page 30.
q(1);
q(2);
q(3);
}
If the number of iteration is constant the loop will be unrolled as many times as permitted
by the size of the loop and -unroll-size. If the number of iterations is not a multiple of
the number of replications, the remaining iterations will be unrolled separately.
For example, the following code
void f()
{
int i;
i = 0;
Chapter 3: The Compiler 25
tmp = n & 3;
switch(tmp){
case 3:
q(i++);
case 2:
q(i++);
case 1:
q(i++);
}
while(i < n){
q(i++);
q(i++);
q(i++);
q(i++);
}
}
As this optimization requires detecting whether operands of an expression may
have changed, it will be affected by other optimizations. See Section 3.4.14 [Alias
Analysis], page 28, Section 3.4.15 [Inter-Procedural Analysis], page 29 and Section 3.4.16
[Cross-Module Optimizations], page 30.
int f(int n)
{
return n + 1;
}
At lower optimization levels a function must be defined in the same translation-unit as the
caller to be inlined. With cross-module optimizations, vbcc will also inline functions which
are defined in other files. See Section 3.4.16 [Cross-Module Optimizations], page 30.
See also Section 3.5.3 [Inline-Assembly Functions], page 33.
function or from anywhere outside the program, interrupt-handlers or other data which
should be preserved. See Section 3.4.16 [Cross-Module Optimizations], page 30.
if(a < b)
p1 = &x[a];
else
p1 = &y[b];
p2 = &z[c];
...
}
As pointers itself may be aliased and function calls might modify pointers, this
analysis sometimes benefits from a larger scope of optimization. See Section 3.4.15
[Inter-Procedural Analysis], page 29 and Section 3.4.16 [Cross-Module Optimizations],
page 30.
This optimization will alter the behaviour of broken code which uses pointer arithmetic
to step from one object into another.
Chapter 3: The Compiler 29
− The 1999 C standard provides the restrict-qualifier to help alias analysis. If a pointer
is declared with this qualifier, the compiler may assume that the object pointed to by
this pointer is only aliased by pointers which are derived from this pointer. For a formal
definition of the rules for restrict please consult ISO/IEC9899:1999.
vbcc will make use of this information at higher optimization levels (-c99 must be used
to use this new keyword).
A very useful application for restrict are function parameters. Consider the following
example:
void cross_prod(float *restrict res,
float *restrict x,
float *restrict y)
{
res[0] = x[1] * y[2] - x[2] * y[1];
res[1] = x[2] * y[0] - x[0] * y[2];
res[2] = x[0] * y[1] - x[1] * y[0];
}
Without restrict, a compiler has to assume that writing the results through res can
modify the object pointed to by x and y. Therefore, the compiler has to reload all the
values on the right side twice. With restrict vbcc will optimize this code like:
void cross_prod(float *restrict res,
float *restrict x,
float *restrict y)
{
float x0 = x[0], x1 = x[1], x2 = x[2];
float y0 = y[0], y1 = x[1], y2 = y[2];
res[0] = x1 * y2 - x2 * y1;
res[1] = x2 * y0 - x0 * y2;
res[2] = x0 * y1 - x1 * y0;
}
close to each other. Also, instructions which depend on the result of another instruction
will be moved further apart to avoid pipeline-stalls.
Please note that it may be crucial to specify the correct derivate of a CPU family in order to
get best results from the sceduler. Different variants of an architecture may have a different
number and behaviour of pipelines requiring different scheduling decisions.
Consult the backend documentation for details.
− Missed break-points.
Setting break-points (especially on source-lines) needs some care when optimized code
is debugged. E.g. code may have been moved or even replicated at different parts.
A break-point set in a debugger will usually only be set on one instance of the code.
Therefore, a different instance of the code may have been executed although the break-
point was not hit.
3.5 Extensions
This section lists and describes all extensions to the C language provided by vbcc. Most
of them are implemented in a way which does not break correct C code and still allows all
diagnostics required by the C standard by using reserved identifiers.
The only exception (see Section 3.5.3 [Inline-Assembly Functions], page 33) can be turned
off using -iso or -ansi.
3.5.1 Pragmas
vbcc accepts the following #pragma-directives:
#pragma printflike <function>
#pragma scanflike <function>
vbcc will handle <function> specially. <function> has to be an already de-
clared function, with external linkage, that takes a variable number of argu-
ments and a const char * as the last fixed parameter.
If such a function is called with a string-constant as format-string, vbcc will
check if the arguments seem to match the format-specifiers in the format-string,
according to the rules of printf or scanf. Also, vbcc will replace the call by a
call to a simplified version according to the following rules, if such a function
has been declared with external linkage:
− If no format-specifiers are used at all, __v0<function> will be called.
− If no qualifiers are used and only d,i,x,X,o,s,c are used,
__v1<function> will be called.
− If no floating-point arguments are used, __v2<function> will be called.
#pragma dontwarn <n>
Disables warning number n. Must be followed by #pragma popwarn.
#pragma warn <n>
Enables warning number n. Must be followed by #pragma popwarn.
#pragma popwarn
Undoes the last modification done by #pragma warn or #pragma dontwarn.
#pragma only-inline on
The following functions will be parsed and are available for inlining (see
Section 3.4.11 [Function Inlining], page 25), but no out-of-line code will be
generated, even if some calls could not be inlined.
Do not use this with functions that have local static variables!
#pragma only-inline off
The following functions are translated as usual again.
Chapter 3: The Compiler 33
Example:
double sin(__reg("fp0") double) = "\tfsin.x\tfp0\n";
There are several issues to take care of when writing inline-assembly.
− As inline-assembly is subject to loop unrolling or function inlining it may be replicated
at different locations. Unless it is absolutely known that this will not happen, the code
should not define any labels (e.g. for branches). Use offsets instead.
− If a backend provides an instruction scheduler, inline-assembly code will also be sched-
uled. Some schedulers make assumptions about their input (usually compiler-generated
code) to improve the code. Have a look at the backend documentation to see if there
are any issues to consider.
− If a backend provides a peephole optimizer which optimizes the assembly output, inline-
assembly code will also be optimized unless -no-inline-peephole is specified. Have
a look at the backend documentation to see if there are any issues to consider.
− vbcc assumes that inline-assembly does not introduce any new control-flow edges. I.e.
control will only enter inline-assembly if the function call is reached and if control leaves
inline-assembly it will continue after the call.
Inline-assembly-functions are not recognized when ANSI/ISO mode is turned on.
char * __far p;
is a far-qualified pointer to an unqualified char.
Please note that some common extensions like __far are type attributes on some architec-
tures, but actually variable attributes (see Section 3.5.4 [Variable Attributes], page 34) on
others. This is due to significantly different meanings on different architectures.
3.5.6 __typeof
__typeof is syntactically equivalent to sizeof, but its result is of type int and is a number
representing the type of its argument. This may be necessary for implementing stdarg.h.
3.5.7 __alignof
__alignof is syntactically equivalent to sizeof, but its result is of type int and is the
alignment in bytes of the type of the argument. This may be necessary for implementing
stdarg.h.
3.5.8 __offsetof
__offsetof is a builtin version of the offsetof-macro as defined in the C language. The
first argument is a structure type and the second a member of the structure type. The
result will be a constant expression representing the offset of the specified member in the
structure.
read by the function. If a variable is read and written, both attributes have to be specified.
The variable-list ist a list of identifiers, separated by slashes and enclosed in double quotes.
The attribute __writesmem(<type>) is used to specify that the function accesses memory
using a certain type. This is necessary if the function modifies memory accessible to the
calling function which cannot be specified using __varsmodified (e.g. because it is accessed
via pointers). __readsmem is similar, but specifies memory which is read.
If one of __varsused, varsmodified, __readsmem and __writesmem is specified, all relevant
side-effects must be specified. If, for example, only __varsused("my_global") is specified,
this implies that the function only reads my_global and does not modify any variable
accessible to the caller.
All of these attributes may be specified multiple times.
3.7 Credits
All those who wrote parts of the vbcc distribution, made suggestions, answered my ques-
tions, tested vbcc, reported errors or were otherwise involved in the development of vbcc
(in descending alphabetical order, under work, not complete):
• Frank Wille
• Gary Watson
• Andrea Vallinotto
• Johnny Tevessen
• Eero Tamminen
• Gabriele Svelto
• Dirk Stoecker
• Ralph Schmidt
• Markus Schmidinger
• Thorsten Schaaps
• Anton Rolls
• Michaela Pruess
• Thomas Pornin
• Joerg Plate
• Gilles Pirio
• Bartlomiej Pater
• Elena Novaretti
• Gunther Nikl
• Constantinos Nicolakakis
• Timm S. Mueller
• Robert Claus Mueller
• Joern Maass
• Aki M Laukkanen
• Kai Kohlmorgen
• Uwe Klinger
• Andreas Kleinert
• Julian Kinraid
• Acereda Macia Jorge
• Dirk Holtwick
• Matthew Hey
• Tim Hanson
• Kasper Graversen
38 vbcc manual
• Jens Granseuer
• Volker Graf
• Marcus Geelnard
• Matthias Fleischer
• Alexander Fichtner
• Olivier Fabre
• Robert Ennals
• Thomas Dorn
• Walter Doerwald
• Aaron Digulla
• Lars Dannenberg
• Sam Crow
• Michael Bode
• Michael Bauer
• Juergen Barthelmann
• Thomas Arnhold
• Alkinoos Alexandros Argiropoulos
• Thomas Aglassinger
Chapter 4: M68k/Coldfire Backend 39
4 M68k/Coldfire Backend
This chapter documents the backend for the M68k and Coldfire processor families.
-no-mreg-return
Do not use multiple registers to return types that do not fit into a single register.
This is mainly for backwards compatibility with certain libraries.
-no-peephole
Do not perform peephole-optimizations.
-prof Insert code for profiling.
-sc Use small code model (see below).
-sd Use small data model (see below).
-use-commons
Use real common symbols instead of bss symbols for non-initialized external
variables.
-use-framepointer
By default automatic variables are addressed through a7 instead of a5. This
generates slightly better code, because the function entry and exit overhead is
reduced and a5 can be used as register variable etc.
However this may be a bit confusing when debugging and you can force vbcc
to use a5 as a fixed framepointer.
4.2 ABI
The current version generates assembler output for use with the vasmm68k_mot. Most
peephole optimizations are done by the assembler so vbcc only does some that the assembler
cannot make. The generated executables will probably only work with OS2.0 or higher.
With -gas assembler output suitable for the GNU assembler is generated (the version must
understand the Motorola syntax - some old ones do not). The output is only slightly
modified from the vasm-output and will therefore result in worse code on gas.
The register names provided by this backend are:
a0, a1, a2, a3, a4, a5, a6, a7
d0, d1, d2, d3, d4, d5, d6, d7
fp0, fp1, fp2, fp3, fp4, fp5, fp6, fp7
The registers a0 - a7 are supported to hold pointer types. d0 - d7 can be used for integers
types excluding long long, pointers and float if no FPU code is generated. fp0 - fp7 can
be used for all floating point types if FPU code is generated.
Additionally the following register pairs can be used for long long:
d0/d1, d2/d3, d4/d5, d6/d7
The registers d0, d1, a0, a1, fp0 and fp1 are used as scratch registers (i.e. they can be
destroyed in function calls), all other registers are preserved.
By default, all function arguments are passed on the stack.
All scalar types up to 4 bytes are returned in register d0, long long is returned in d0/d1. If
compiled for FPU, floating point values are returned in fp0 unless -no-fpreturn is specified.
Types which are 8, 12 or 16 bytes large will be returned in several registers (d0/d1/a0/a1)
unless -no-mreg-return is specified. All other types are returned by passing the function
Chapter 4: M68k/Coldfire Backend 41
the address of the result as a hidden argument - such a function must not be called without
a proper declaration in scope.
Objects which have been compiled with different settings must not be linked together.
a7 is used as stack pointer. If -sd is used, a4 will be used as small data pointer. If -use-
framepointer is used, a5 will be used as frame pointer. All other registers will be used by
the register allocator and can be used for register parameters.
The size of the stack frame is limited to 32KB for early members of the 68000 family prior
to 68020.
The basic data types are represented like:
type size in bits alignment in bytes
char 8 1
short 16 2
int 32 2
long 32 2
long long 64 2
all pointers 32 2
float(fpu) 32 2 see below
double(fpu) 64 2 see below
long double(fpu) 64 2 see below
The advantage is slightly smaller and faster code. The disadvantages are that all the code
(including library functions) must be small enough. Objects/libraries can be linked together
if they have been compiled with different code models.
4.5 CPUs
The values of -cpu=n have those effects:
n<68000 Code for the Coldfire family is generated.
n>=68000 Code for the 68k family is generated.
n>=68020
− 32bit multiplication/division/modulo is done with the mul?.l, div?.l and
div?l.l instructions.
− tst.l ax is used.
− extb.l dx is used.
− 16/32bit offsets are used in certain addressing modes.
− link.l is used.
− Addressing modes with scaling are used.
n==68040
− 8bit constants are not copied in data registers.
− Static memory is not subject to common subexpression elimination.
4.6 FPUs
At the moment the values of -fpu=n have those effects:
n>68000 Floating point calculations are done using the FPU.
n=68040
n=68060 Instructions that have to be emulated on these FPUs will not be used; at the
moment this only includes the fintrz instruction in case of the 040.
4.7 Math
Long multiply on CPUs <68020 uses inline routines. This may increase code size a bit,
but it should be significantly faster, because function call overhead is not necessary. Long
division and modulo is handled by calls to library functions. (Some operations involving
constants (e.g. powers of two) are always implemented by more efficient inline code.)
If no FPU is specified floating point math is done using math libraries. 32bit IEEE format
is used for float and 64bit IEEE for double and long double.
If floating point math is done with the FPU floating point values are kept in registers and
therefore may have extended precision sometimes. This is not ANSI compliant but will
usually cause no harm. When floating point values are stored in memory they use the same
IEEE formats as without FPU. Return values are passed in fp0.
Note that you must not link object files together if they were not compiled with the same
-fpu settings and that a proper math library must be linked.
Chapter 4: M68k/Coldfire Backend 43
4.10 Stack
If the -stack-check option is used, every function-prologue will call the function __stack_
check with the stacksize needed by the current function on the stack. This function has to
consider its own stacksize and must restore all registers.
If the compiler is able to calculate the maximum stack-size of a function including all callees,
it will add a comment in the generated assembly-output (subject to change to labels).
44 vbcc manual
4.11 Stdarg
A possible <stdarg.h> could look like this:
#endif
5 PowerPC Backend
This chapter documents the Backend for the PowerPC processor family.
5.2 ABI
This backend supports the following registers:
− r0 through r31 for the general purpose registers,
− f0 through f31 for the floating point registers and
− cr0 through cr7 for the condition-code registers.
Additionally, the register pairs r3/r4, r5/r6, r7/r8, r9/r10, r14/r15, r16/r17,
r18/r19, r20/r21, r22/r23, r24/r25, r26/r27, r28/r29 and r30/r31 are available.
r0, r11, r12, f0, f12 and f13 are reserved by the backend.
The current version generates assembly output for use with vasmppc or the GNU assembler.
The generated code should work on 32bit systems based on a PowerPC CPU using the V.4
ABI or the PowerPC Embedded ABI (eabi).
Chapter 5: PowerPC Backend 47
The registers r0, r3-r12, f0-f13 and cr0-cr1 are used as scratch registers (i.e. they can be
destroyed in function calls), all other registers are preserved. r1 is the stack-pointer and r13
is the small-data-pointer if small-data-mode is used.
The first 8 function arguments which have integer or pointer types are passed in registers
r3 through r10 and the first 8 floating-point arguments are passed in registers f1 through
f8. All other arguments are passed on the stack.
Integers and pointers are returned in r3 (and r4 for long long), floats and doubles in f1.
All other types are returned by passing the function the address of the result as a hidden
argument - so when you call such a function without a proper declaration in scope you can
expect a crash.
The elementary data types are represented like:
type size in bits alignment in bytes (-amiga-align)
char 8 1 (1)
short 16 2 (2)
int 32 4 (2)
long 32 4 (2)
long long 64 8 (2)
all pointers 32 4 (2)
float 32 4 (2)
double 64 8 (2)
5.6 Stack
If the -stack-check option is used, every function-prologue will call the function __stack_
check with the stacksize needed by this function in register r12. This function has to
consider its own stacksize and must restore all registers.
5.7 Stdarg
A possible <stdarg.h> for V.4 ABI could look like this:
typedef struct {
int gpr;
int fpr;
char *regbase;
char *membase;
} va_list;
char *__va_start(void);
char *__va_regbase(void);
int __va_fixedgpr(void);
int __va_fixedfpr(void);
#define va_start(vl,dummy) \
( \
vl.gpr=__va_fixedgpr(), \
vl.fpr=__va_fixedfpr(), \
vl.regbase=__va_regbase(), \
vl.membase=__va_start() \
)
#define va_arg(vl,type) \
( \
(__typeof(type)&127)>10? \
__va_mem((vl),type) \
: \
( \
(((__typeof(type)&127)>=6&&(__typeof(type)&127)<=8)) ? \
( \
++(vl).fpr<=8 ? \
((type*)((vl).regbase+32))[(vl).fpr-1] \
: \
__va_mem((vl),type) \
) \
: \
( \
++(vl).gpr<=8 ? \
((type*)((vl).regbase+0))[(vl).gpr-1] \
: \
__va_mem((vl),type) \
) \
) \
)
6.2 ABI
This backend supports the following registers:
− $0 through $31 for the general purpose registers and
− $f0 through $f31 for the floating point registers.
The current version generates assembly output for use with the GNU assembler. The
generated code should work on systems with 21064, 21066, 21164 and higher Alpha CPUs.
The registers $0-$8, $16-$28, $f0, $f1 and $f10-$f30 are used as scratch registers (i.e.
they can be destroyed in function calls), all other registers are preserved. Of course $31
and $f31 cannot be used.
The first 6 function arguments which have arithmetic or pointer types are passed in registers
$16/$f16 through $21/$f21.
Integers and pointers are returned in $0, floats and doubles in $f0. All other types are
returned by passing the function the address of the result as a hidden argument - so when
you call such a function without a proper declaration in scope you can expect a crash.
The elementary data types are represented like:
type size in bits alignment in bytes
char 8 1
short 16 2
int 32 4
long 64 8
52 vbcc manual
long long 64 8
all pointers 64 8
float 32 4
double 64 8
6.4 Stdarg
A possible <stdarg.h> could look like this:
typedef struct {
char *regbase;
char *membase;
int arg;
} va_list;
char *__va_start(void);
int __va_fixargs(void);
#define va_start(vl,dummy) \
(vl.arg=__va_fixargs(),vl.regbase=__va_start(),vl.membase=vl.regbase+(6-vl.arg)*16)
7 i386 Backend
This chapter documents the Backend for the Intel i386 processor family.
7.2 ABI
This backend supports the following registers:
− %eax, %ebx, %ecx, %edx
− %esi, %edi, %ebp, %esp
(And %st(0)-%st(7) for the floating point stack but these must not bes used for register
variables because they cannot be handled like normal registers.)
The current version generates assembly output for use with the GNU assembler. The
generated code should work on systems with Intel 80386 or higher CPUs with FPU and
compatible chips.
The registers %eax, %ecx and %edx (as well as the floating point stack) are used as scratch
registers (i.e. they can be destroyed in function calls), all other registers are preserved.
All elementary types up to 4 bytes are returned in register %eax Floating point values are
returned in %st(0). All other types are returned by passing the function the address of the
result as a hidden argument - so when you call such a function without a proper declaration
in scope you can expect a crash.
56 vbcc manual
vbcc uses %eax, %ebx, %ecx, %edx, %esi, %edi, %ebp and the floating point stack for
temporary results and register variables. Local variables are created on the stack and
addressed via %esp.
The elementary data types are represented like:
type size in bits alignment in bytes (-longalign)
char 8 1 (1)
short 16 2 (4)
int 32 2 (4)
long 32 2 (4)
long long n/a n/a
all pointers 32 2 (4)
float 32 2 (4)
double 64 2 (4)
7.4 Stdarg
A possible <stdarg.h> could look like this:
typedef unsigned char *va_list;
8 c16x Backend
This chapter documents the Backend for the c16x/st10 microcontroller family.
8.2 ABI
This backend supports the following registers:
− R0 through R15 for the general purpose registers
Additionally, the register pairs R2/R3, R3/R4, R4/R5, R6/R7, R7/R8, R8/R9, R12/R13,
R13/R14, and R15/R15 are available.
R1, R11 and R12 are reserved by the backend.
The current version generates assembly output for use with the vasm assembler. Option-
ally, assembly code for the Tasking assembler can be generated. The default memory
58 vbcc manual
model corresponds to the Tasking small-memory model with 16bit data-pointers and 32bit
function-pointers. However, the DPPx registers have to be set up in a way to create a linear
16bit address space (i.e. DPPx=x). The generated code should work on systems with c161,
c163, c164, c165 and c167 microcontrollers as well as ST10 derivates. Old versions like the
c166 are not supported
The registers R1-R5 and R10-R15 are used as scratch registers (i.e. they can be destroyed
in function calls), all other registers are preserved.
R0 is used as user stack pointer. Automatic variables and temporaries are put on the user
stack. Return addresses are pushed on the system stack.
The first 4 function arguments which have integer or pointer types are passed in registers
R12 through R15.
Integers and pointers are returned in R4/R5. All other types are returned by passing the
function the address of the result as a hidden argument - so when you call such a function
without a proper declaration in scope you can expect a crash.
The elementary data types are represented like:
type size in bits alignment in bytes
char 8 1
short 16 2
int 16 2
long 32 2
long long n/a n/a
near pointers 16 2
far pointers 32 2
huge pointers 32 2
float n/a n/a
double n/a n/a
__esfr(<addr>)
The same for extended special function registers.
__sfrbit(<addr>,<bit>)
Declare a single bit in the bit-addressable area.
Example:
__sfr(0xff10,11) volatile __bit IEN;
__esfrbit(<addr>,<bit>)
The same for the extended bit-addressable area.
8.7 Stack
If the -stack-check option is used, every function-prologue will call the function __stack_
check with the stacksize needed by this function in register R1. This function has to consider
its own stacksize and must restore all registers.
Only stack-checking of the user-stack is supported. Checking the system-stack is supported
by hardware.
8.8 Stdarg
A possible <stdarg.h> could look like this:
typedef char *va_list;
va_list __va_start(void);
#define __va_rounded_size(__TYPE) \
(((sizeof (__TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
9 68hc12 Backend
This chapter documents the Backend for the 68hc12 microcontroller family.
Note that this backend is not done!
9.2 ABI
The current version generates assembly output for use with the GNU assembler using the
non-banked model.
This backend supports the following registers:
− d for the accumulator (also used for byte, i.e. a)
− x for index register x
− y for index register y
− sp for the stack-pointer
All registers are scratch (i.e. caller-save).
The first function arguments which has integer or pointer types is passed in the accumulator
d or the register pair d/x. The remaining arguments are passed on the stack.
Integers and pointers are returned in d or d/x. All other types are returned by passing the
function the address of the result as a hidden argument - so when you call such a function
without a proper declaration in scope you can expect a crash.
The elementary data types are represented like:
62 vbcc manual
char 8 1
short 16 2
int 16 2
long currently not supported
near pointers 16 2
far pointers currently not supported
huge pointers currently not supported
float currently not supported
double currently not supported
9.5 Stack
If the -stack-check option is used, every function-prologue will call the function __stack_
check with the stacksize needed by this function in register y. This function has to consider
its own stacksize and must restore all registers.
9.6 Stdarg
To be written...
10 VideoCore IV Backend
This chapter documents the Backend for the VideoCore IV processor family.
The backend is in a very early stage, it is not complete, and it can not yet be considered
useful!
Also note that it is based on freely available, inofficial, and possibly incorrect information
on the target processor.
10.2 ABI
This backend supports the following registers:
− r0 through r31 for the general purpose registers
Additionally, the register pairs r0/r1 r2/r3, r4/r5, r6/r7, r8/r9, r10/r11, r12/r13,
r14/r15, r16/r17, r18/r19, r20/r21, r22/r23 are available.
r14, r15, r24-r31 are currently reserved by the backend.
The current version generates assembly output for use with vasm.
The registers r0-r5 and r14-r15 are used as scratch registers (i.e. they can be destroyed in
function calls), all other registers are preserved. r25 is the stack-pointer.
The first 6 function arguments which have integer, float32 or pointer types are passed in
registers r0 through r5. All other arguments are passed on the stack.
64 vbcc manual
Integers, float32 and pointers are returned in r0. All other types are returned by passing the
function the address of the result as a hidden argument - so when you call such a function
without a proper declaration in scope you can expect a crash.
The elementary data types are represented like:
type size in bits alignment in bytes
char 8 1
short 16 2
int 32 4
long 32 4
long long 64 8 not yet supported
all pointers 32 4
float 32 4
double 64 (32) 4
long double 64 (32) 4
10.6 Stdarg
stdarg-implementation is not yet fully working. One restriction is that when calling a
varargs function, the prototype must be in scope (this is ISO C conforming). Another one
is that the stdarg-macros only work as long as all fixed arguments are passed in registers.
This will be fixed in the future.
11 Instruction Scheduler
vsc - scheduler for vbcc (c) in 1997-99 by Volker Barthelmann
11.1 Introduction
vsc is an instruction-scheduler which reorders the assembly output of vbcc and tries to
improve performance of the generated code by avoiding pipeline stalls etc.
Like the compiler vbcc it is split into a target independent and a target dependent part.
However there may be code-generators for vbcc which do not have a corresponding scheduler.
This document only deals with the target independent parts of vsc. Be sure to read all the
documents for your machine.
11.2 Usage
Usually vsc will be called by a frontend. However if you call it directly, it has to be done
like this:
vsc [options] input-file output-file
The following options are supported:
-quiet Do not print the copyright notice.
-debug=<n>
Set debug-level to <n>.
Note that depending on the target vbcc may insert hints into the generated code to tell
vsc what CPU to schedule for. Code scheduled for a certain CPU may run much slower on
slightly different CPUs. Therefore it is especially important to specify the correct target-
CPU when compiling.
12 C Library
This chapter describes the C library usually provided with vbcc.
12.1 Introduction
To execute code compiled by vbcc, a library is needed. It provides basic interfaces to the
underlying operating system or hardware as well as a set of often used functions.
A big part of the library is portable across all architectures. However, some functions (e.g.
for input/output or memory allocation) are naturally dependent on the operating system
or hardware. There are several sections in this chapter dealing with different versions of the
library.
The library itself often is split into several parts. A startup-code will do useful initializations,
like setting up IO, parsing the command line or initializing variables and hardware.
The biggest part of the functions will usually be stored in one library file. The name and
format of this file depends on the conventions of the underlying system (e.g. vc.lib or
libvc.a).
Often, floating point code (if available) is stored in a different file (e.g. m.lib or libm.a).
If floating point is used in an application, it might be necessary to explicitly link with this
library (e.g. by specifying -lm).
In many cases, the include files provide special inline-code or similar optimizations. There-
fore, it is recommended to always include the corresponding include file when using a library
function. Even if it is not necessary in all cases, it may affect the quality of the generated
code.
The library implements the functions specified by ISO9899:1989 as well as a small number
of the new functions from ISO9899:1999.
12.2 Legal
Most parts of this library are public domain. However, for some systems, parts may be
under a different license. Please consult the system specific documentation. Usually, linking
against this library will not put any restrictions on the created executable unless otherwise
mentioned.
Parts of the math library (e.g. transcendental functions) are derived from Sun’s free math
library:
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
The softfloat functions, used by some targets, are derived from John Hauser’s IEC/IEEE
Floating-point Artithmetic Package:
70 vbcc manual
Written by John R. Hauser. This work was made possible in part by the
International Computer Science Institute, located at Suite 600, 1947 Center
Street, Berkeley, California 94704. Funding was partially provided by the
National Science Foundation under grant MIP-9311980. The original version
of this code was written as part of a project to build a fixed-point vector
processor in collaboration with the University of California at Berkeley,
overseen by Profs. Nelson Morgan and John Wawrzynek. More information
is available through the web page ‘http://HTTP.CS.Berkeley.EDU/~jhauser/
arithmetic/softfloat.html’.
12.3.1 Startup
The startup is usually split into two parts. The first part is done by assembly code that
produces the object file lib/startup.o. This assembly code is usually provided with vbcc
and may have to be adapted to the hardware you are using. The key actions that have to
be performed by this code are:
−hardware initialization
It may be necessary to perform some hardware initialization right at the be-
ginning, e.g. to configure the memory system. This has to be modified by the
user.
−variable initializations
When running code from ROM, some memory sections have to be initialized.
Usually, the init-values of initialized variables have to be copied from ROM to
the data segment and the values of un-initialized variables have to be cleared
in the bss segment. This code is usually provided in the startup code.
−stack pointer
The stack pointer has to be set to a suitable memory area. The startup code
will set the stack pointer to the value of the pointer __stack. There is a default
stack provided in the C library which will be used unless the application defines
Chapter 12: C Library 71
its own stack using, for example, the following code (assuming that the stack
grows downwards):
#define STACKSIZE <whatever>
−calling __main
After all the above initializations have been performed, the function __main()
has to be called. This function is provided by the library and performs high-
level initializations, if necessary (mainly it calls constructors created by the
linker) and will then call the user main() function. Note that the library may
not work correctly if the user main() function is called directly from the startup
code.
12.3.2 Heap
When dynamic memory management is used (e.g. by using the malloc() function), a heap
memory area is needed to allocate memory from. The malloc() function assumes that __
heapptr is a variable pointing to the beginning of the heap memory and that __heapsize
specifies the size of the heap area in bytes. The library will provide a default heap memory
area that can be replaced by adding, for example, the following file to the application:
#define HEAPSIZE <whatever>
char __heap[HEAPSIZE],*__heapptr=__heap;
size_t __heapsize=HEAPSIZE;
12.3.3 Input/Output
The standard C input/output functions are provided also for embedded systems. Read-
ing/writing to a stream will be directed to void unless the following low-level I/O-functions
are provided by the application:
int __open(const char *name,const char *mode);
void __close(int h);
size_t __read(int h,char *p,size_t l);
size_t __write(int h,const char *p,size_t l);
The __open() function receives a name and a mode string (as in the C fopen() function)
as arguments and has to return a file-descriptor if it is possible to open this file. The other
functions are equivalent to the corresponding POSIX functions.
Also, stdin, stdout and stderr can be used with the standard descriptors.
12.3.6 Linking/Locating
To produce ROM images (e.g. in the form of absolute ELF executables, Intel Hex files
or Motorola S-Records), the linker is called with a linker script. This script can be used
to join together different sections of the input files and locate them to suitable absolute
memory areas. Also, this linker script can be used to set symbols that may be used by the
application or the startup code, e.g. addresses of data sections, initialization values or small
data pointers.
Code or data that has to reside at special locations can be put into a special section using
the __section attribute. This section can then be placed at the desired location using the
linker script.
Usually, an example linker script will be provided. While this is often not suitable for
different chips, it may serve as a starting point.
12.4 AmigaOS/68k
This section describes specifics of the C library for AmigaOS/68k provided by the
target m68k-amigaos. The relevant files are startup.o, minstart.o, minres.o,
vc.lib, vcs.lib, mieee.lib, mieees.lib, m881.lib, m881s.lib, m040.lib, m040s.lib,
m060.lib, m060s.lib, msoft.lib, msofts.lib, amiga.lib, amigas.lib, auto.lib and
autos.lib.
Note that extra.lib is no longer part of the vbcc distribution. It was replaced by
’PosixLib’, available on Aminet dev/c/vbcc_PosixLib.lha, which has a much more com-
prehensive support for POSIX and Unix functions.
The following config files are available:
aos68k Standard startup code (startup.o) with command line parsing and optional
Workbench startup (See Section 12.4.1 [Standard Startup], page 73).
aos68km Minimal startup code (minstart.o) without command line parsing. You have
to open all libraries yourself (See Section 12.4.6 [Minimal Startup], page 74).
aos68kr Minimal startup code (minres.o) for resident programs. Always compiles in
small data mode and links with vcs.lib (See Section 12.4.7 [Minimal Resident],
page 75).
Chapter 12: C Library 73
12.4.1 Startup
The startup code currently consists of a slightly modified standard Amiga startup
(startup.o). The startup code sets up some global variables and initializes stdin, stdout
and stderr. The exit code closes all open files and frees all memory. If you link with a
math library the startup/exit code will be taken from there if necessary.
12.4.3 Stack
An application can specify the stack-size needed by defining a variable __stack (of type
size_t) with external linkage, e.g.
size_t __stack=65536; /* 64KB stack-size */
The startup code will check whether the stack-size specified is larger than the default stack-
size (as set in the shell) and switch to a new stack of appropriate size, if necessary.
74 vbcc manual
If the -stack-check option is specified when compiling, the library will check for a stack
overflow and abort the program, if the stack overflows. Note, however, that only code
compiled with this option will be checked. Calls to libraries which have not been compiled
with -stack-check or calls to OS function may cause a stack overflow which is not noticed.
Additionally, if -stack-check is used, the maximum stack-size used can be read by querying
the external variable __stack_usage.
#include <stdio.h>
main()
{
do_program();
printf("stack used: %lu\n",(unsigned long)__stack_usage);
}
Like above, the stack used by functions not compiled using -stack-check or OS functions
is ignored.
12.4.5 Restrictions
The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock() The clock() function always returns -1. This is correct, according to the C
standard, because on AmigaOS it is not possible to obtain the time used by the
calling process.
The command line is not parsed, but passed to main() as a single string, so you can declare
main as int main(char *command) or int main(void).
Also no Amiga libraries are opened (but SysBase ist set up), so you have to define and
open DOSBase yourself if you need it. If you want to use floating point with the IEEE
libraries you have to define and open MathIeeeSingBas.library, MathIeeeDoubBas.library
and MathIeeeDoubTrans.library (in this order!) and link with mieee.lib (if compiled for
FPU this is not needed).
A hello world using minstart could look like this:
#include <proto/exec.h>
#include <proto/dos.h>
main()
{
if(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",0)){
Write(Output(),"Hello, world!\n",14);
CloseLibrary((struct Library *)DOSBase);
}
return 0;
}
This can yield an executable of under 300 bytes when compiled with -sc -sd -O2 and linked
with minstart.o and amigas.lib (using vlink - may not work with other linkers).
12.4.8 amiga.lib
To write programs accessing AmigaOS (rather than standard C functions only), a replace-
ment for the original (copyrighted) amiga.lib is provided with vbcc. This replacement is
adapted to vbcc, does not cause collisions with some functions (e.g. sprintf) provided by
the original amiga.lib and is available in small data. It is recommended to always use this
library rather than the original version.
Additionally, there are header files (in the proto- and inline-subdirectories) which cause
inlined calls to Amiga library functions.
Note that the OS-call inlines have been generated using the NDK3.9 clib header files, so it
is advised to use the same NDK for development. Otherwise you will get warnings about
missing CONST typedefs and similar.
Specify -lamiga to link with amiga.lib.
12.4.9 auto.lib
To link with auto.lib (or the small data version autos.lib) specify the -lauto or -lautos
option to vc.
When you are calling a standard Amiga library function and do not have defined the
corresponding library base then the library base as well as code to open/close it will be
taken from auto.lib.
By default, auto.lib will try to open any library version. If you need at least a certain
version you can define and set a variable <library-base>Ver with external linkage, e.g. (on
file-scope):
int _IntuitionBaseVer = 39;
Note that your program will abort before reaching main() if one of the libraries cannot be
opened. Also note that the dos.library will be openened by the standard startup code, not by
auto.lib. This means you have to open dos.library yourself, when linking with minstart.o.
12.5 Kickstart1.x/68k
This section describes specifics of the C library for Amiga Kickstart 1.2 and 1.3 provided by
the target m68k-kick13. The relevant files are startup.o, minstart.o, minres.o vc.lib,
vcs.lib, m13.lib, m13s.lib, m881.lib, m881s.lib, amiga.lib, amigas.lib, auto.lib
and autos.lib.
It makes it possible to develop programs targeted for these older versions of the Amiga
operating system, using the original Commodore Kickstart 1.3 header files.
The following config files are available:
kick13 Standard startup code (startup.o) with command line parsing and optional
Workbench startup (See Section 12.5.1 [Startup13], page 77).
kick13m Minimal startup code (minstart.o) without command line parsing. You have
to open all libraries yourself (See Section 12.4.6 [Minimal Startup], page 74).
kick13r Minimal startup code (minres.o) for resident programs. Always compiles in
small data mode and links with vcs.lib (See Section 12.4.7 [Minimal Resident],
page 75).
Chapter 12: C Library 77
12.5.1 Startup
The startup code currently consists of a slightly modified standard Amiga 1.3 startup
(startup.o). The startup code sets up some global variables and initializes stdin, std-
out and stderr. The exit code closes all open files and frees all memory. If you link with a
math library the startup/exit code will be taken from there if necessary.
12.5.3 Stack
Stack-checking is available similar to AmigaOS/68k (See Section 12.4.3 [amiga-stack],
page 73). But there is no automatic stack-extension under Kickstart 1.3 and a __stack
variable will be ignored.
12.5.5 Restrictions
The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock() The clock() function always returns -1. This is correct, according to the C
standard, because on AmigaOS it is not possible to obtain the time used by the
calling process.
12.5.6 amiga.lib
See Section 12.4.8 [amigalib], page 76.
This version of amiga.lib only supports the functionality present in Kickstart 1.2/1.3.
78 vbcc manual
12.5.7 auto.lib
This library corresponds to the AmigaOS/68k version (See Section 12.4.9 [auto.lib],
page 76), but only supports libraries of Kickstart 1.3.
12.6 PowerUp/PPC
This section describes specifics of the C library for PowerUp/PPC provided by the target
ppc-powerup. The relevant files are startup.o, minstart.o, libvc.a, libvcs.a, libm.a,
libms.a libamiga.a, libamigas.a, libauto.a and libautos.a.
Note that libextra.a is no longer part of the vbcc distribution. It was replaced by
’PosixLib’, available on Aminet dev/c/vbcc_PosixLib.lha, which has a much more com-
prehensive support for POSIX and Unix functions.
12.6.1 Startup
The startup code startup.o sets up some global variables and initializes stdin, stdout and
stderr. The exit code closes all open files and frees all memory. If you link with a math
library the startup/exit code will be taken from there if necessary.
12.6.3 Stack
Stack-handling is similar to AmigaOS/68k (See Section 12.4.3 [amiga-stack], page 73). The
only difference is that stack-swapping cannot be done. If the default stack-size is less than
the stack-size specified with __stack the program will abort.
12.6.5 Restrictions
The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock() The clock() function always returns -1. This is correct, according to the C
standard, because on AmigaOS it is not possible to obtain the time used by the
calling process.
12.6.7 libamiga.a
To write programs accessing AmigaOS (rather than standard C functions only), a replace-
ment for the original (copyrighted) amiga.lib is provided with vbcc. This replacement
(libamiga.a) automatically performs a necessary context switch to the 68k to execute
the system call. Furthermore, it is adapted to vbcc, does not cause collisions with some
functions (e.g. sprintf) provided by the original amiga.lib and is available in small data.
Specify -lamiga to link with libamiga.a.
12.6.8 libauto.a
This library corresponds to the AmigaOS/68k version (See Section 12.4.9 [auto.lib],
page 76).
12.7 WarpOS/PPC
This section describes specifics of the C library for WarpOS/PPC provided by the target
ppc-warpos. The relevant files are startup.o, vc.lib, m.lib, amiga.lib and auto.lib.
Note that extra.lib is no longer part of the vbcc distribution. It was replaced by
’PosixLib’, available on Aminet dev/c/vbcc_PosixLib.lha, which has a much more com-
prehensive support for POSIX and Unix functions.
12.7.1 Startup
The startup code startup.o sets up some global variables and initializes stdin, stdout and
stderr. The exit code closes all open files and frees all memory. If you link with a math
library the startup/exit code will be taken from there if necessary.
To make use of this library, link with ppcmath.lib before m.lib, e.g.
vc test.c -lppcmath -lm
Depending on the CPU/FPU selected, #including math.h will cause inline-code generated
for certain math functions.
12.7.3 Stack
Stack-handling is similar to AmigaOS/68k (See Section 12.4.3 [amiga-stack], page 73).
12.7.4 Restrictions
The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock() The clock() function always returns -1. This is correct, according to the C
standard, because on AmigaOS it is not possible to obtain the time used by the
calling process.
12.7.5 amiga.lib
To write programs accessing AmigaOS (rather than standard C functions only), a replace-
ment for the original (copyrighted) amiga.lib is provided with vbcc. This replacement
automatically performs a necessary context switch to the 68k to execute the system call.
Furthermore, it is adapted to vbcc, does not cause collisions with some functions (e.g.
sprintf) provided by the original amiga.lib and is available in small data.
Specify -lamiga to link with amiga.lib.
12.7.6 auto.lib
This library corresponds to the AmigaOS/68k version (See Section 12.4.9 [auto.lib],
page 76).
12.8 MorphOS/PPC
This section describes specifics of the C library for MorphOS/PPC provided by the target
ppc-morphos. The relevant files are startup.o, minstart.o, libvc.a, libvcs.a, libm.a,
libms.a libamiga.a, libamigas.a, libauto.a and libautos.a.
Note that libextra.a is no longer part of the vbcc distribution. It was replaced by
’PosixLib’, available on Aminet dev/c/vbcc_PosixLib.lha, which has a much more com-
prehensive support for POSIX and Unix functions.
12.8.1 Startup
The startup code startup.o sets up some global variables and initializes stdin, stdout and
stderr. The exit code closes all open files and frees all memory. If you link with a math
library the startup/exit code will be taken from there if necessary.
The math library (libm.a) is linked against the floating point library libmoto by Motorola.
Depending on the CPU/FPU selected, #including math.h will cause inline-code generated
for certain math functions.
12.8.3 Stack
Stack-handling is similar to AmigaOS/68k (See Section 12.4.3 [amiga-stack], page 73).
12.8.5 Restrictions
The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock() The clock() function always returns -1. This is correct, according to the C
standard, because on MorphOS it is not possible to obtain the time used by
the calling process.
12.8.6 libamiga.a
To write programs using AmigaOS compatible functions, a replacement for the original
(copyrighted) amiga.lib is provided with vbcc. This replacement (libamiga.a) will invoke
the MorphOS 68k emulator to execute the system function. Furthermore, it is adapted to
vbcc and does not cause collisions with some functions (e.g. sprintf) and is available in
small data.
Specify -lamiga to link with libamiga.a.
12.8.7 libauto.a
This library corresponds to the AmigaOS/68k version (See Section 12.4.9 [auto.lib],
page 76).
12.9 AmigaOS4/PPC
This section describes specifics of the C library for AmigaOS4/PPC provided by the target
ppc-amigaos. The relevant files are startup.o, minstart.o, libvc.a, libvcs.a, libm.a,
libms.a libamiga.a, libamigas.a, libauto.a and libautos.a.
Note that libextra.a is no longer part of the vbcc distribution. It was replaced by
’PosixLib’, available on Aminet dev/c/vbcc_PosixLib.lha, which has a much more com-
prehensive support for POSIX and Unix functions.
82 vbcc manual
12.9.1 Startup
The startup code startup.o sets up some global variables and initializes stdin, stdout and
stderr. Then it runs all constructors of dynamically linked libraries, before entering the
main program. The exit code runs all destructors of dynamically linked libraries, closes all
open files and frees all memory. If you link with a math library the startup/exit code will
be taken from there if necessary.
12.9.3 Stack
There is no automatic stack extension for AmigaOS 4! This should be done automatically
by the operating system.
12.9.6 Restrictions
The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock() The clock() function always returns -1. This is correct, according to the C
standard, because on AmigaOS it is not possible to obtain the time used by the
calling process.
Small data in dynamically linked executables
There is a bug in elf.library V52.4 (and earlier), which doesn’t load .sdata
and .sbss as a contiguous block into memory, when the executable requires
Chapter 12: C Library 83
12.9.7 libamiga.a
In contrast to other amigalibs the OS4 libamiga.a doesn’t contain any stubs for calling
system functions. AmigaOS 4 system calls are done through special macros in the SDK’s
interface header files.
The library only includes some remaining amigalib functions, not already integrated into
the OS, like CreateIO(), but its use is discouraged.
Specify -lamiga to link with libamiga.a.
12.9.8 libauto.a
Auto-open -close functions for the following libraries are included:
Asl, CyberGfx, DataTypes, Dos, GadTools, Graphics, Icon, IFFParse, Intuition,
Locale, LowLevel, Picasso96, BSDSocket, Utility, Workbench
Note that gcc’s libauto.a doesn’t include CyberGfx.
12.9.9 newlib
12.9.9.1 Introduction
newlib.library is a shared AmigaOS4 library, which is covered by several BSD like licenses,
and includes standard ANSI and POSIX functions as well as some functions common in
Unix, BSD and similar operating systems. It is part of the OS4 SDK.
The config file newlib will be created on installation to use the paths for header files and
libraries pointing to the newlib from the SDK.
What are the main differences between vclib and newlib?
− vclib contains (almost) only standard ANSI functions. If you want to port Unix pro-
grams you will probably miss a lot of functions. Also newlib supports things like
mapping Unix directory paths to Amiga paths or expanding wildcards in command
lines automatically.
− Programs compiled for newlib will be shorter because the code for all functions is not
contained in the executable itself.
− Programs compiled for newlib will need the shared object libc.so present when
started.
− Programs compiled for newlib will probably need more memory because the entire
(rather large) libc.so will be loaded into memory. With vclib only the functions your
program uses will be in RAM. However if you have several programs using newlib at
the same time only one copy of libc.so should be loaded.
Things you should note:
− With newlib you do not need extra math-libraries.
− You must link with a vbcc-specific startup.o from the newlib lib/ directory as startup
code. The config-file newlib will usually take care of this.
− You must use the newlib-includes from the SDK rather than the ones which are for
vc.lib. The config-file newlib will usually take care of this.
84 vbcc manual
− There may be vbcc-related bugs in the SDK-newlib. Patches are automatically installed
when using the Amiga Installer. When installing the target manually, you also have
to fix the SDK manually. For a list of known SDK bugs at this point of time, See
Section 12.9.9.2 [Known Newlib Bugs], page 84.
12.9.9.3 Usage
To compile a program to use newlib for OS4 you must make sure the proper config-file
(newlib) is used, e.g.
vc +newlib hello.c
With a new SDK this will usually generate a dynamically linked executable, which requires
libc.so. To force a statically linked executable:
vc +newlib -static hello.c
target cf-atari. Both share the same startup-code and are based on common library
sources and header files. Executables linked with this C library run on plain TOS as well
as on MiNT, without modifications.
The relevant files are startup.o, minstart.o, libvc.a, libm.a, libgem.a. For the M68k
target there are also math libs with FPU support (libm881.a, libm040.a and libm060.a)
and 16-bit integer versions of all libraries (lib*16.a).
The following config files are available:
tos M68k 32-bit int for classic TOS machines.
tos16 M68k 16-bit int for classic TOS machines.
mint M68k 32-bit int for MiNT. Also works on classic machines, but uses an em-
bedded a.out header for MiNT, includes a changeable __stksize and sets the
FastLoad, FastRAM and FastAlloc flags in the header.
mintcf ColdFire 32-bit int. Otherwise same as mint.
12.10.1 Startup
The startup code startup.o sets up some global variables and initializes stdin, stdout and
stderr and returns the unneeded memory to the system. The exit code closes all open files
and frees all memory.
12.10.3 Stack
The default stack size is 64k. There is a MiNT tool called stack which can adjust the stack
size of an executable to any value, by looking for a symbol named __stksize (defined by
vclib’s startup code).
Additionally the required stack size can be specified by defining a variable __stack (of type
size_t) with external linkage, as in other vbcc targets.
12.10.5 Restrictions
The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock() The clock() function always returns -1. This is correct, according to the C
standard, because neither under TOS nor under MiNT it is possible to obtain
the time used by the calling process.
12.11 VideoCore/Linux
This section describes specifics of the C library for VideoCore under Linux provided by the
target vidcore-linux.
The relevant files are vcload, startup.o, libvc.a, libm.a, libms.a.
The config file vc4-linux is part of the library.
12.11.1 Startup
The startup code startup.o sets up stack and heap and provides a function __armcall()
to transfer control to the loader on the ARM side. The startup process calls constructors
to set up some global variables and initialize stdin, stdout and stderr if needed.
12.11.3 Stack
The library contains a default stack of 32KB. If another size is needed, you can add the
following to your project:
.align 4
.space <desired-size, suitably aligned>
___stackend:
.global ___stackend
12.11.4 Heap
Currently, a global variable of 16KB is used to get memory for malloc() etc. If another size
is needed, you can add the following to your project:
#define HEAPSIZE <desired size>
char __heap[HEAPSIZE],*__heapptr=__heap;
size_t __heapsize=HEAPSIZE;
Note that this mechanism will likely be changed in the future!
Chapter 12: C Library 87
12.11.6 Loader
A loader is required to execute VideoCore code from the ARM side. For standalone Video-
Core code, the provided loader can be used. Usually, it will be necessary to adapt the loader
to communicate between ARM and VideoCore side during runtime.
h <addr> [<num>]
Display <num> 16bit halfwords starting at <addr>. <addr> must be the offset
into the image. If <num> is omitted, one unit is displayed.
b <addr> [<num>]
Display <num> 8bit bytes starting at <addr>. <addr> must be the offset into
the image. If <num> is omitted, one unit is displayed.
c Start/continue execution.
q Quit.
bp <addr> Set a breakpoint at <addr>.
This is currently a very crude implementation. It will just write a branch to
__armcall() to <addr>. If everything works well, you will end in the debugger
if <addr> is reached. However, the arguments passed are random (and might be
dangerous syscalls by accident). Also, the old code at this address is currently
not restored.
As a result, you must not continue execution after hitting a breakpoint!
12.11.7 Restrictions
The following list contains some restrictions of this version of the library:
− no real floating point support yet
− lots, lots, lots...
12.12.1 Startup
The startup code startup.o sets up stack and heap. The startup process calls constructors
to set up some global variables and initialize stdin, stdout and stderr.
The ATARI Jaguar has no OS, so it is impossible to define how input, output and files can
be handled. There are a few set of function you have to define if you want to use stdio.
Alternatively you can use the libjag.a. This library initializes a console window with
stdout support and uses optionally a SkunkBoard to redirect stderr and file I/O.
12.12.3 Stack
The library contains a default stack of 32KB. If another size is needed, you can add a global
variable named stack to your code:
12.12.4 Heap
Currently the free RAM is used as global heapsize for malloc() etc.
It is necessary to place a symbol named BSS END at the end of the BSS segment. The
heap allocates the free RAM between BSS END and the bottom of the stack.
If less size is needed feel free to manipulate the value of BSS END.
All allocated heap objects can be used as internal JAGUAR objects, because they are qhrase
aligned.
/**
* param handle: handle from jagopen
*
* No need to handle stdin, stdout and stderr here
*/
void jagclose(int handle)
/**
* param handle: handle from jagopen
* param p: points to the char buffer to fill.
* param l: buffer size of p
* returns: >=0 number of read bytes
<0 indicate an error
*
90 vbcc manual
/**
* param handle: handle from jagopen
* param p: points to the char buffer to write.
* param l: number of bytes of p
* returns: >=0 number of bytes written
<0 indicate an error
*
* Handle stdin, stdout and stderr here
*/
size_t jagwrite(int handle,const char *p, size_t l)
/**
* param handle: handle from jagopen
* param offset: number of bytes to seek.
* param direction: see fseek direction
* returns: =0 successfull seek
<>0 indicate an error
-1: seek not supported
*
* Handle stdin, stdout and stderr here
*/
long jagseek(int handle,long offset,int direction)
#include <stdio.h>
int main()
{
printf("Hello, world\n");
}
Keep in mind: Your JAGUAR will get a red background color to indicate main() has exited.
The jaglib API documentation is available in a separate document. There is more demo
code available in the jaglib-demo (https://github.com/toarnold/jaglib-demo) gibhub
repository.
Chapter 13: List of Errors 91
13 List of Errors
0. "declaration expected" (Fatal, Error, ANSI-violation)
Something is pretty wrong with the source.
1. "only one input file allowed" (Fatal)
vbcc accepts only a single filename to compile. You can use a frontend to compile
multiple files or perhaps you mistyped an option.
2. "Flag <%s> specified more than once" ()
You specified a command line option that should be specified only once more than
once. Maybe you have this option in your config-file and used it in the command line,
too? The first occurrence will override the latter ones.
3. "Flag <%s> needs string" (Fatal)
This option hast to be specified with a string parameter, e.g. -flag=foobar
4. "Flag <%s> needs value" (Fatal)
This option hast to be specified with an integer parameter, e.g. -flag=1234
5. "Unknown Flag <%s>" (Fatal)
This option is not recognized by vbcc. Perhaps you mistyped it, used the wrong case
or specified an option of the frontend to vbcc?
6. "No input file" (Fatal)
You did not specify an input file. Your source file should not start with a ’-’ and if you
use a frontend make sure it has the proper suffix.
7. "Could not open <%s> for input" (Fatal)
A file could not be opened.
8. "need a struct or union to get a member" (Error, ANSI-violation)
The source contains something like a.b where a is not a structure or union.
9. "too many (%d) nested blocks" (Fatal, Error)
vbcc only allows a maximum number of nested blocks (compund-statements). You can
increase this number by changing the line #define MAXN <something> in vbc.h and
recompiling vbcc.
10. "left block 0" (Error, ANSI-violation)
This error should not occur.
11. "incomplete struct <%s>" (Error, ANSI-violation)
You tried to get a member of an incomplete structure/union. You defined struct x y;
somewhere without defining struct x{...}.
12. "out of memory" (Fatal, Error)
Guess what.
13. "redeclaration of struct <%s>" (Error, ANSI-violation)
You may not redeclare a struct/union in the same block.
14. "incomplete type (%s) in struct" (Error, ANSI-violation)
Every member in a struct/union declaration must be complete. Perhaps you only
wanted a pointer to that type and forgot the ’*’ ?
92 vbcc manual
89. "operands for || and && have to be arithmetic or pointer" (Error, ANSI-violation)
90. "bitwise operations need integer operands" (Error, ANSI-violation)
91. "assignment discards const" (Warning, ANSI-violation)
You assigned something like (const type *) to (type *).
92. "relational expression needs arithmetic or pointer type" (Error, ANSI-violation)
93. "both operands of comparison must be pointers" (Error, ANSI-violation)
You wrote an expression like a == b where one operand was a pointer while the other
was not. Perhaps a function is not declared correctly or you used NULL instead of 0?
94. "operand needs arithmetic type" (Error, ANSI-violation)
95. "pointer arithmetic with void * is not possible" (Error, ANSI-violation)
Adding/subtracting from a pointer to void is not possible.
96. "pointers can only be subtracted" (Error, ANSI-violation)
You cannot add, multiply etc. two pointers.
97. "invalid types for operation <%s>" (Error, ANSI-violation)
98. "invalid operand type" (Error, ANSI-violation)
99. "integer-pointer is not allowed" (Error, ANSI-violation)
You may not subtract a pointer from an integer. Adding an integer or subtracting it
from a pointer is ok.
100. "assignment discards volatile" (Warning, ANSI-violation)
You assigned something like (volatile type *) to (type *).
101. "<<, >> and % need integer operands" (Error, ANSI-violation)
102. "casting from void is not allowed" (Error, ANSI-violation)
Casting something of type void to anything makes no sense.
103. "integer too large to fit into pointer" (Error, ANSI-violation)
You tried to assign an integer to a pointer that is too small to hold the integer. Note
that assignment of pointers<->integers is never portable.
104. "only integers can be cast to pointers" (Error, ANSI-violation)
105. "invalid cast" (Error, ANSI-violation)
106. "pointer too large to fit into integer" (Error, ANSI-violation)
You tried to assign a pointer to an integer that is too small to hold the pointer. Note
that assignment of pointers<->integers is never portable.
107. "unary operator needs arithmetic type" (Error, ANSI-violation)
108. "negation type must be arithmetic or pointer" (Error, ANSI-violation)
109. "complement operator needs integer type" (Error, ANSI-violation)
110. "pointer assignment with different qualifiers" (Warning, ANSI-violation)
You tried to assign a pointer to a pointer that points to a type with different qualifiers
(e.g. signed<->unsigned).
111. "dereferenced object is no pointer" (Error, ANSI-violation)
112. "dereferenced object is incomplete" (Error, ANSI-violation)
You tried to dereference a pointer to an incomplete object. Either you had a pointer to
an array of unknown size or a pointer to a struct or union that was not (yet) defined.
96 vbcc manual
if(i=0)...
This could an error, if you wanted if(i==0). If you turned on this warning and want
it to shut up for a certain expression you can cast it to its type, e.g.
if((int)(i=0))...
Note that only assignments with ’=’ will be warned, not ’+=’ etc.
165. "comparison redundant because operand is unsigned" (Warning)
A comparison with an unsigned variable is redundant, because the result will always
be constant, e.g.
unsigned int i; if(i<0)...
This usually is a programming error and can be avoided in all cases.
166. "cast to narrow type may cause loss of precision" (Warning)
A variable is cast to a type smaller than its original type, so that some information
may get lost. However this warning will be displayed in lots of cases where no problem
can arise, e.g. (short)(a==b).
167. "pointer cast may cause alignment problems" (Warning)
A pointer is cast to a pointer to a type with stricter alignment requirements, i.e. the
new pointer might be invalid if you do not know what you are doing. Those casts
should be avoidable in all ’usual’ cases.
168. "no declaration of global variable <%s> before definition" (Warning)
It is usually good to declare all global variables (including functions) in header files.
169. "’extern’ inside function" (Warning)
Declaration of external variables in inner blocks is usually not a good idea.
170. "dead assignment to <%s> eliminated" (Warning)
A variable is assigned a value that is never used or gets overwritten before it is used.
If this occurs in real code then there is either an errror or an unnecessary assignment.
This is detected only in optimizing compilation.
171. "var <%s> is used before defined" (Warning)
The variable is used before it was assigned a value and therefore is undefined. It cannot
be detected if the code where it is used can be reached, but if it is reached it will cause
undefined behaviour. So it is most probably an error either way (see 170).
However not all unitialized usages can be found.
Also note that the compiler may choose convenient values for uninitialized variables.
Example:
int f(int a) { int x; if(a) x=0; return(x); }
Here the optimizer may choose that x==0 if it is uninitialized and then only generate
a return(0); It can also happen that you get different values if you read an uninitialized
variable twice although it was not assigned a value inbetween.
This is only detected in optimizing compilation.
172. "would need more than %ld optimizer passes for best results" (Warning)
The optimizer would probably be able to do some further optimizations if you increased
the number of allowed passes with the -optpasses=n option.
100 vbcc manual
14 Backend Interface
14.1 Introduction
This chapter is under construction!
This chapter describes some of the internals of vbcc and tries to explain what has to be
done to write a code generator for vbcc. However if someone wants to write one, I suggest
to contact me first, so that it can be integrated into the source tree.
You have to create a new directory for the new target named machines/<target-name>
and write the files machine.c, machine.h and machine.dt. The compiler for this target
will be called vbcc<target-name> and can be built doing a make TARGET=<target-name>
bin/vbcc<target-name>.
From now on integer means any of char, short, int, long, long long or their unsigned
couterparts. Arithmetic means integer or float or double or long double. Elementary
type means arithmetic or pointer.
Note that this documentation may mention explicit values when introducing symbolic con-
stants. This is due to copying and pasting from the source code. These values may not be
up to date and in some cases can be overridden. Therefore do never use the absolute values
but rather the symbolic representations.
Every available target has at least one subdirectory with its name in vbcc/machines and
contains at least the files machine.h, machine.c and machine.dt. Target-specific object-
files will also be stored in that directory.
The executables will be placed in vbcc/bin/. The main compiler will be called
vbcc<target>.
CC Here you have to insert a command that invokes an ANSI C compiler you want
to use to build vbcc. It must support -D, -I, -c and -o the same like e.g. vc or
gcc. Additional options should also be inserted here. E.g. if you are compiling
for the Amiga with vbcc you should add -DAMIGA.
LDFLAGS Here you have to add options which are necessary for linking. E.g. some
compilers need special libraries for floating-point.
NCC
NLDFLAGS These are similar to CC and LDFLAGS but they must always describe a native
compiler, i.e. Programs compiled with NCC/NLDFLAGS must be executable on
the host system. This is needed because during the build programs may have
to be executed on the host.
An example for the Amiga using vbcc would be:
CC = vc -DAMIGA -c99
LDFLAGS = -lmieee
NCC = $(CC)
NLDFLAGS = $(LDFLAGS)
An example for a typical Unix-installation would be:
CC = cc
LDFLAGS = -lm
NCC = $(CC)
NLDFLAGS = $(LDFLAGS)
The following settings are probably necessary for Open/Free/Any BSD i386 systems:
CC = gcc -D_ANSI_SOURCE
LDFLAGS = -lm
NCC = $(CC)
NLDFLAGS = $(LDFLAGS)
14.2.3 Building vc
Note to users of Open/Free/Any BSD i386 systems: You will probably have to use GNU
make instead of BSD make, i.e. in the following examples replace "make" with "gmake".
Type:
make bin/vc
Chapter 14: Backend Interface 109
14.2.6 Configuring
Consult the vbcc-documentation for information on how to create the necessary config-files.
Now create a second directory containing all the sources to vbcc and set CC/LDFLAGS to vc
using the config-file for vbccB and NCC/NLDFLAGS to the A->A compiler. Type:
make bin/vc
make TARGET=C bin/vscC # omit if there is no machines/C/schedule.c
make TARGET=C bin/vbccC
struct IC{
struct IC *prev;
struct IC *next;
int code;
int typf;
int typf2;
[...]
struct obj q1;
struct obj q2;
struct obj z;
[...]
struct ext_ic ext; /* optional */
};
The only members relevant to the code generator are prev, next, code, typf, typf2,
q1, q2, z and (optionally) ext_ic.
prev and next are pointers to the previous and next IC. The first IC has prev==0 and the
last one has next==0.
typf and typf2 are the type of the operands of this IC. In most ICs all operands have the
same type and therefore only typf is used. However, some ICs have operands of different
types (e.g. converting an operand to another type or adding an integer to a pointer). typf2
is used in these cases.
Macros are provided which yield the type of an operand. q1typ(), q2typ() and ztyp()
return the type of the first source operand, the second source operand and the destination,
respectively. They have to be passed a pointer to a valid IC as argument. The results are
Chapter 14: Backend Interface 111
undefined if the IC does not contain the specified operand (e.g. q2typ() for an IC with
only a single operand).
The standard types which are defined by default are:
#define CHAR
#define SHORT
#define INT
#define LONG
#define LLONG
#define FLOAT
#define DOUBLE
#define LDOUBLE
#define VOID
#define POINTER
#define ARRAY
#define STRUCT
#define UNION
#define ENUM /* not relevant for code generator */
#define FUNKT
and can be additionally or’ed by
#define UNSIGNED
#define CONST
#define VOLATILE
#define UNCOMPLETE
However, only UNSIGNED is of real importance for the code generator. typf&NQ yields the
type without any qualifiers, typf&NU yields the type without any qualifiers but UNSIGNED.
It is possible for backends to define additional types. See Section 14.9.8 [exttypes], page 145
for documentation on how to extend the type system.
14.3.2 Operands
q1, q2 and z are the source1 (quelle1 in German), source2 and target (ziel) operands,
respectively. If a result has to be computed, it always will be stored in the object z and
the objects q1 and q2 usually may not be destroyed during this operation (unless they are
aliased with the destination).
The objects are described by this structure:
struct obj{
int flags;
int reg;
int dtyp;
struct Var *v;
struct AddressingMode *am;
union atyps{
zchar vchar;
zchar vuchar;
zshort vshort;
112 vbcc manual
zushort vushort;
zint vint;
zuint vuint;
zlong vlong;
zulong vulong;
zllong vllong;
zullong vullong;
zmax vmax;
zumax vumax;
zfloat vfloat;
zdouble vdouble;
zldouble vldouble;
} val;
};
• VAR|REG|DREFOBJ
• VAR|VARADR
Also some other bits which are not relevant to the code generator may be set.
Constants will usually be in q2 if possible. One of the sources always is not constant and
the target is always an lvalue. The types of the operands can be queried using the macros
q1typ(), q2typ() and ztyp(). In most cases (i.e. when not explicitly stated) the type is
an elementary type (i.e. arithmetic or pointer).
am can be used to store information on special addressing modes. This has to be han-
dled by the by the code generator. However am has to be 0 or has to point to a struct
AddressingMode that was allocated using malloc() when the code generator returns.
struct AddressingMode has to be defined in machine.h.
val stores either the value of the object if it is a constant or an offset if it is a variable.
code specifies the operation. Fur further details see Section 14.3.5 [operations], page 116.
14.3.3 Variables
A struct Var looks like:
struct Var{
int storage_class;
[...]
char *identifier;
[...]
zmax offset;
struct Typ *vtyp;
[...]
char *vattr;
unsigned long tattr; /* optional */
};
The relevant entries are:
identifier
The name of the variable. Usually only of interest for variables with external-
linkage.
storage_class
One of:
#define AUTO 1
#define REGISTER 2
#define STATIC 3
#define EXTERN 4
#define TYPEDEF 5 /* not relevant */
The backend should use the macros isauto(), isstatic() and isextern()
to check which category a variable falls into.
offset Contains an offset relative to the beginning of the variable’s storage. Used, for
example, when accessing members of structures.
114 vbcc manual
vtyp The type of the variable (see Section 14.3.4 [compositetypes], page 114).
vattr A string with attributes used in the declaration of the variable. See
Section 14.9.6 [targetattributes], page 144 for further details.
tattr Flags used when declaring the variable. See Section 14.9.6 [targetattributes],
page 144 for further details.
If the variable is not assigned to a register (i.e. bit REG is not set in the flags of the
corresponding struct obj) then the variable can be addressed in the following ways (with
examples of 68k-code):
isauto(storage_class) != 0
offset contains the offset inside the local-variables section. The code generator
must decide how it’s going to handle the activation record. If offset < 0 then
the variable is a function argument on the stack. In this case the offset in the
parameter-area is - (offset + maxalign).
The code generator may have to calculate the actual offset to a stack- or frame-
pointer from the value in offset.
offset + val.vlong(sp)
Note that storage_class == REGISTER is equivalent to AUTO - whether the
variable is actually assigned a register is specified by the bit REG in the flags
of the struct obj.
isextern(storage_class) != 0
The variable can be addressed through its name in identifier.
val.vlong + ’_’identifier
isstatic(storage_class) != 0
The variable can be addressed through a numbered label. The label number is
stored in offset.
val.vlong+’l’offset
};
flags is the simple type as it is generally used in the backend. The meaning of the other
members depends on flags. attr is an attribute that can be added to the type using the
sytax __attr("...") (which is parsed like a type-qualifier, e.g. const). If several attributes
are specified for a type, the strings will be concatenated, separated by semi-colons.
If the type is a pointer (ISPOINTER(flags) != 0), then next will point to the type the
pointer points to.
If the type is an array (ISARRAY(flags) != 0), then size contains the number of elements
and next points to a type structure representing the type of each array element.
If the type is a structure (ISSTRUCT(flags) != 0), a union (ISUNION(flags) != 0) or a
function (ISFUNC(flags) != 0), then exact is a pointer to a struct_declaration (which
is also used to represent unions and function prototypes) that looks like this:
struct struct_declaration {
int count;
int label;
int typ;
...
struct struct_list (*sl)[];
char *identifier;
};
count is the number of members, label can be used to store a label when generating
debug information. typ is either STRUCT, UNION or FUNKT to denote whether it applies to a
structure, union or function-prototype.
identifier is only available for struct- and union-tags.
sl points to an array of struct struct_lists which contain information on each mem-
ber/parameter:
struct struct_list {
char *identifier;
struct Typ *styp;
zmax align;
int bfoffset;
int bfsize;
int storage_class;
int reg;
};
identifier is the identifier of the member/parameter, if available. styp denotes the full
type, align the alignment in bytes (only for struct/union), bfoffset and bfsize the size
and offset of bitfield-members, storage_class the storage class of function parameters
(may be AUTO or REGISTER) and reg denotes the register a parameter is passed in.
Example: If struct Typ *t points to a structure-type, then the type of the second structure
member can be accessed through (*t->exact->sl)[1].styp.
A prototyped function will have a last argument of type VOID unless it is a function accepting
a variable number of arguments. If a function was declared without a prototype it will have
116 vbcc manual
no parameters, a function declared with prototype accepting no arguments will have one
parameter of type VOID.
Also, in the case of a function type, the next-member of a struct Typ points to the return
type of the function.
14.3.5 Operations
This section lists all the different operations allowed in the intermediate code passed to the
backend. It lists the symbolic name of the code value (the value should not be used), a
template of the operands and a description. The description sometimes contains internals
(e.g. which types are stored in typf and which in typf2), but they should not be used.
Access them using the macros provided (e.g. q1typ,q2typ,ztyp) whenever possible.
#define ASSIGN 2
Copy q1 to z. q1->z.
q2.val.vmax contains the size of the objects (this is necessary if it is an array
or a struct). It should be accessed using the opsize()-macro. typf does not
have to be an elementary type!
The only case where typf == ARRAY should be in automatic initializations.
It is also possible that (typf&NQ) == CHAR but the size is != 1. This is created
for an inline memcpy/strcpy where the type is not known.
#define OR 16
#define XOR 17
#define AND 18
Bitwise boolean operations. q1,q2->z.
All operands are integers.
#define LSHIFT 25
#define RSHIFT 26
Bit shifting. q1,q2->z.
’q2’ is the number of shifts. All operands are integers.
#define ADD 27
#define SUB 28
#define MULT 29
#define DIV 30
Standard arithmetic operations. q1,q2->z.
All operands are of arithmetic types (integers or floating point).
#define MOD 31
Modulo (%). q1,q2->z.
All operands are integers.
#define KOMPLEMENT 33
Bitwise complement. q1->z.
All operands are integers.
#define MINUS 38
Unary minus. q1->z.
Chapter 14: Backend Interface 117
#define SUBIFP 82
Subtract an Integer from a pointer. q1,q2->z.
q1 and z are always pointers (of type typf2) and q2 is an integer of type typf.
z has to be q1 decreased by q2 bytes.
#define SUBPFP 83
Subtract a pointer from a pointer. q1,q2->z.
q1 and q2 are pointers (of type typf2) and z is an integer of type typf. z has
to be q1 - q2 in bytes.
#define GETRETURN 93
Get the return value of the last function call. ->z.
If the return value is in a register, its number will be q1.reg. Otherwise q1.reg
will be 0. GETRETURN immediately follows a CALL IC (except possible FREEREGs).
#define SETRETURN 94
Set the return value of the current function. q1.
If the return value is in a register, its number will be z.reg. Otherwise z.reg
will be 0. SETRETURN is immediately followed by a function exit (i.e. it is the
last IC or followed by an unconditional branch to a label which is the last IC -
always ignoring possible FREEREGs).
#define MOVEFROMREG 95
Move a register to memory. q1->z.
q1 is always a register and z an array of size regsize[q1.reg].
#define MOVETOREG 96
Load a register from memory. q1->z.
z is always a register and q1 an array of size regsize[z.reg].
#define NOP 97
Do nothing.
If the code generator can use several representations, these can be added on the same line
separated by spaces. E.g. the code generator for m68k does not care if the integers are
stored big-endian or little-endian on the host system because it only accesses them through
the provided arithmetic functions. It does, however, access floats and doubles through
byte-pointers and therefore requires them to be stored in big-endian-format.
14.5 machine.h
This section describes the contents of the file machine.h. Note that some optional
macros/declaration may be described someplace else in this manual.
#include "dt.h"
This should be the first statement in machine.h.
struct AddressingMode { ... };
If machine-specific addressing modes (see Section 14.8.2 [addressingmodes],
page 133) are used, an appropriate structure can be specified here. Otherwise,
just enter the following code:
struct AddressingMode {
int never_used;
};
#define MAXR <n>
Insert the number of available registers.
#define MAXGF <n>
Insert the number of command line flags that can be used to configure the
behaviour of the code generator. This must be at least one even if you do not
use any flags.
#define USEQ2ASZ <0/1>
If this is set to zero, vbcc will not generate ICs with the target operand being
the same as the 2nd source operand. This can sometimes simplify the code-
generator, but usually the code is better if the code-generator allows it.
122 vbcc manual
#define AVOID_FLOAT_TO_UNSIGNED 1
#define AVOID_UNSIGNED_TO_FLOAT 1
Insert these lines to tell the frontend not to generate CONVERT ICs that con-
vert between unsigned integers and floating point. In those cases, additional
intermediate code will be generated that implements the conversion using only
signed integers.
14.6 machine.c
This is the main part of the code generator. The first statement should be #include
"supp.h" which will include all necessary declarations.
The following variables and functions must be provided by machine.c.
14.6.5 Functions
The following functions have to be implemented by the code generator. There may be
optional additional functions described in other sections.
int init_cg(void);
This function is called after the commandline arguments are parsed. It can
set up certain internal data, etc. The arrays regarding the data types and
Chapter 14: Backend Interface 127
the register set can be set up at this point rather than with a static initializa-
tion, however the arrays regarding the commandline options have to be static
initialized. The results of the commandline options are available at this point.
If something goes wrong, 0 has to be returned, otherwise 1.
void cleanup_cg(FILE *f);
This function is called before the compiler exits. f is the output file which must
be checked against 0 before using.
int freturn(struct Typ *t);
This function has to return the number of the register return values of type
t are passed in. If the type is not passed in a register, 0 must be returned.
Usually the decision can be made only considering t->flags, ignoring the full
type (see Section 14.3.4 [compositetypes], page 114).
int regok(int r, int t, int mode);
Check whether the type t can be stored in register r and whether the usual
operations (for this type) can be generated. Return 0, if not.
If t is a pointer and mode==0 the register only has to be able to store the pointer
and do arithmetic, but if mode!=0 it has to be able to dereference the pointer.
mode==-1 is used with context-sensitive register-allocation (see Section 14.8.10
[targetralloc], page 139). If the backend does not support it, this case can be
handled equivalent to mode==0.
If t==0 return whether the register can be used to store condition codes. This
is only relevant if multiple_ccs is set to 1.
int dangerous_IC(struct IC *p);
Check if this IC can raise exceptions or is otherwise dangerous. Movement of
ICs which are dangerous is restricted to preserve the semantics of the program.
Typical dangerous ICs are divisions or pointer dereferencing. On certain targets
floating point or even signed integer arithmetic can raise exceptions, too.
int must_convert(int from,int to,int const_expr);
Check if code must be generated to convert from type from to type to. E.g.
on many machines certain types have identical representations (integers of the
same size or pointers and integers of the same size).
If const_expr != 0 return if a conversion was necessary in a constant expres-
sion.
For example, a machine may have identical pointers and integers, but different
sets of registers (one set supports integer operations and the other pointer
operations). Therefore, must_convert() would return 1 (we need a CONVERT
IC to move the value from one register set to the other).
This would imply that vbcc would not allow a cast from a pointer to an integer
or vice-versa in constant expressions (as it will not generate code for static
initializations). However, in this case, a static initialization would be ok as
the representation is identical and registers are not involved. Therefore, the
backend can return 1 if const_expr == 0 and 0 otherwise.
128 vbcc manual
isstatic(sc)
Tests whether the storage-class sc denotes a variable with static storage and
no external linkage.
isextern(sc)
Tests whether the storage-class sc denotes a variable with static storage and
external linkage.
isauto(sc)
Tests whether the storage-class sc denotes a variable with automatic storage-
duration.
t_min(t)
t_max(t) These macros yield the smallest and largest representable value of any target
integer type, e.g. t_min(INT) or t_max(UNSIGNED|LONG).
ISPOINTER(t)
ISINT(t)
ISFLOAT(t)
ISFUNC(t)
ISSTRUCT(t)
ISUNION(t)
ISARRAY(t)
ISSCALAR(t)
ISARITH(t)
These macros test whether the simple type t is a pointer type, an integral type,
a floating point type, a function, a structure type, a union type, an array type,
a scalar (integer, floating point or pointer) and an arithmetic type (integer or
floating point), respectively.
int label;
The number of the last label used so far. For a new label number, use ++label.
The function returns 0 if no modification took place and non-zero if the IC has
been modified.
eval_const() reads the member of the union corresponding to the type t and
converts it into all the global variables while insert_const() takes the global
variable according to t and puts it into the appropriate member of the union
atyps.
The global variable gval may be used as a temporary union atyps by the
backend.
132 vbcc manual
Sometimes, a simple backend is sufficient and the work required to produce high-quality
code is not worthwile. However, this section lists a series of common backend optimizations
which are often done in case that good code-quality is desired. Note that neither are all of
these optimizations applicable (without modifications or at all) to all architectures nor is
this an exhaustive list. It is just a list of recommendations to consider. You have to make
sure that the optimization is safe and beneficial for the architecture you are targetting.
it. The machine independant part of vbcc will not use these modes, so your code generator
has to find a way to combine several statements to make use of these modes.
A possible implementation of a structure to handle the addressing mode described above
as well as a register-indirect mode could be:
#define IMM_IND 1
#define REG_IND 2
struct AddressingMode {
int flags; /* either IMM_IND or REG_IND */
int base; /* base register */
zmax offset; /* offset in case of IMM_IND */
int idx; /* index register in case of REG_IND */
}
When the code generator is done that pointer in every struct obj must either be zero or
point to a mymalloced struct AddressingMode which will be free’d by vbcc.
Following is an example of a function which traverses a list of ICs and inserts addressing
modes with constant offsets where possible.
/* search for possible addressing-modes */
static void find_addr_modes(struct IC *p)
{
int c,c2,r;
struct IC *p2;
struct AddressingMode *am;
for(;p;p=p->next){
c=p->code;
if(IMM_IND&&(c==ADDI2P||c==SUBIFP)&&
isreg(z)&&(p->q2.flags&(KONST|DREFOBJ))==KONST){
/* we have found addi2p q1,#const->reg */
int base;zmax of;struct obj *o;
eval_const(&p->q2.val,p->typf);
/* handle sub instead of add */
if(c==SUBIFP)
of=zmsub(l2zm(0L),vmax);
else
of=vmax;
base=p->q1.reg;
else
base=r;
o=0;
/* Now search the following instructions. */
for(p2=p->next;p2;p2=p2->next){
c2=p2->code;
if(c2!=CALL&&(c2<LABEL||c2>BRA)&&c2!=ADDRESS){
/* See, if we find a valid use (dereference) of the
temporary register. */
if(!p->q1.am&&(p2->q1.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&
p2->q1.reg==r){
if(o) break;
o=&p2->q1;
}
if(!p->q1.am&&(p2->q2.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&
p2->q2.reg==r){
if(o) break;
o=&p2->q2;
}
if(!p->q1.am&&(p2->z.flags&(REG|DREFOBJ))==(REG|DREFOBJ)&&
p2->z.reg==r){
if(o) break;
o=&p2->z;
}
}
if(c2==FREEREG||(p2->z.flags&(REG|DREFOBJ))==REG){
int m;
if(c2==FREEREG)
m=p2->q1.reg;
else
m=p2->z.reg;
if(m==r){
/* The value of the temporary register is not used any more
136 vbcc manual
Care has to be taken to delete this information if either the condition codes may be modified
or the object they represent is modified. Also, this optimization is usually hard to do across
labels.
Some architectures provide versions of instructions which set condition codes as well as
versions which do not. This obviously enable more optimizations, but it is more difficult to
make use of this. One possibility is to search the list of ICs backwards starting from every
suitable TEST or COMPARE instruction. If an IC is found which computes the tested object,
the IC can be marked (extended ICs can be used for marking, see Section 14.8.13 [extic],
page 140).
Chapter 14: Backend Interface 137
number of cases that are used divided by the range of cases. If the density is high, vbcc
will use jump-tables only for sequences that have few unused cases inside. If the case tables
occupy multiple ranges, vbcc is able to split them up and create multiple jump-tables.
calc_case_table returns a pointer to a struct case_table with the following content:
num The number of cases.
typf The type of the case IDs.
next_ic The first IC after the list of ICs that can be replaced by the jump-table.
density The case density.
vals The values of the case IDs (array containing num entries).
labels The labels of the code corresponding to the case IDs (array containing num
entries).
min The lowest case ID.
max The highest case ID.
diff max-min.
If the backend decides to emit a jump-table, it has to generate code that will check that the
control expression lies between min and max. If not, the jump-table must not be executed.
Code for the computed jump must then be generated. The actual table can be emitted
using emit_jump_table(). Processing can then continue with next_ic.
int emit_peephole(void)
{
int entries,i,r1,r2;
long x,y;
/* pointer to the lines in order of output */
char *asmline[EMIT_BUF_DEPTH];
i=emit_l;
/* compute number of entries in ring buffer */
if(emit_f==0)
entries=i-emit_f+1;
else
entries=EMIT_BUF_DEPTH;
/* the first line */
asmline[0]=emit_buffer[i];
if(entries>=2){
/* we have at least two line sin the buffer */
/* calculate the next line (modulo EMIT_BUF_DEPTH) */
i--;
if(i<0) i=EMIT_BUF_DEPTH-1;
asmline[1]=emit_buffer[i];
if(sscanf(asmline[0],"\tadd\tR%d,#%ld",&r1,&x)==2&&
sscanf(asmline[1],"\tadd\tR%d,#%ld",&r2,&y)==2&&
r1==r2){
sprintf(asmline[1],"\tadd\tR%d,#%ld\n",r1,x+y);
remove_asm();
return 1;
}
}
return 0;
}
Be very careful when doing such optimizations. Only perform optimizations which are really
legal. Especially assembly code often has side effects like setting of flags.
Depending on command line flags inline assembly code may or may not be passed through
this peephole optimizer. By default, it will be done, enabling optimizations between gener-
ated code and inline assembly.
The flag should be set when the operation is in a context that suggests it will translate to
efficient machine code. The optimizer will transform this IC less aggressively.
As this is all happens before register allocation, the decision is of a very heuristic nature.
142 vbcc manual
− For very large blocks, calling a library function may be useful. While this creates some
overhead, the function can dynamically check the alignment or perhaps even use special
hardware, if available.
− Set INLINEMEMCOPY to reasonable values. Set it to a very high value if you implement
very good block copying.
14.9.2 -speed/-size
Often it is desired to generate code which runs as fast as possible but sometimes small code
is needed. The command line options -speed and -size are provided for the user to specify
his intention.
These options already may change the intermediate code produced by the frontend, but
the backend should also respect these switches, if possible. The variables optspeed and
optsize can be queried to see if these options were specified.
If e.g. optspeed was specified, the backend should choose faster code-sequences, even if
code-size is increased significantly. Vice-versa, if optsize is specified, it should always
choose the shorter code if there is a trade-off between size and speed.
Typical cases for such tradeoffs are for example, block-copy (ASSIGN and PUSH) ICs. Often
it is possible to call a library function or generate a simple short loop for small code, but
an unrolled inlined loop for fast code.
14.9.4 stdarg.h
FIXME: To be written.
the corresponding struct Var, separating it by a semi-colon. The backend can use this
information by parsing the string. The frontend will just build the string, it will not interpret
it. If a backend offers attributes using the __vattr() mechanism, it is recommended to
provide target-specific macros (see Section 14.9.3 [targetmacros], page 144) which expand
to the appropriate __vattr()-syntax. Only these macros should be documented.
A second way to specify attributes is enabled by adding
#define HAVE_TARGET_PRAGMAS 1
to machine.h and adding an array
char *g_attr_name[];
to machine.c. This array should point to the strings used for the attributes, terminated
by a null-pointer, e.g.:
char *g_attr_name[] = {
"__far",
"__near",
"__interrupt",
0
};
These attributes can be queried in the member
unsigned long tattr;
of a struct Var. The first attribute is represented by bit 1, the second by bit 2 and so
on. Using this mechanism, the frontend will check for redeclarations with different setting
of attributes or multiple specification of attributes. However, only boolean attributes are
possible. If parameters have to be specified, the __vattr()-mechanism has to be used.
Each struct Var contains the members char *dfilename and int dline which specify the
file and line number of the variable’s definition. Also, every IC contains the members char
*file and int line with the file name and line number this IC belongs to. Note however,
that there may be ICs with file == 0 - not all ICs can be assigned a certain code location.
Also, ICs do not always have increasing line numbers and line numbers may repeat. Not
all debuggers may be able to deal with this.
14.9.10.1 DWARF2
There is support for the DWARF2 debug standard which can be added to a backend rather
easily. The following additions are necessary:
1. Add the line
#include "dwarf2.c"
to machine.c.
2. Add the following lines to init_db():
dwarf2_setup(sizetab[POINTER],
".byte",
".2byte",
".4byte",
".4byte",
labprefix,
idprefix,
".section");
dwarf2_print_comp_unit_header(f);
The arguments to dwarf2_setup() have the following meanings:
1. The size of an address on the target.
2. An assembler directive to create one byte of initialized storage.
3. An assembler directive to create two bytes of initialized storage (without any
padding for alignment).
4. An assembler directive to create four bytes of initialized storage (without any
padding for alignment).
5. An assembler directive to create initialized storage representing a target address
(without any padding for alignment).
6. A prefix which is used for emitting numbered labels (or empty string).
7. A prefix which is used for emitting external identifiers (or empty string).
8. An assembler directive to switch to a new named section.
3. Add the line
dwarf2_cleanup(f);
to cleanup_db().
4. Write the function
static int dwarf2_regnumber(int r);
which returns the DWARF2 regnumber for a vbcc register number.
5. Write the function
Chapter 14: Backend Interface 147
14.9.13 Profiling
FIXME: To be written.
vbcc can be told to generate calls to library functions for certain ICs. When
defining HAVE_LIBCALLS, the backend must provide the function char *use_
libcall(<code>,<typf>,<typf2>). This function gets called with the elements code,
typf and typf2 of an IC. If use_libcall returns a name, this library function will be
called instead of the IC. Otherwise, 0 must be returned.
All library functions have to be declared in init_cg() with declare_builtin(), supporting
the following arguments:
name The name of the library function. Usually, a reserved identifier should be chosen
(e.g. starting with __).
ztyp The return type of the function (only integral and float types are supported).
q1typ The type of the first parameter (only integral and float types are supported).
q1reg The register to pass the first argument in (0 passes via stack).
q2typ The type of the second parameter (only integral and float types are supported).
For functions with a single parameter, use 0.
q2reg The register to pass the second argument in (0 passes via stack).
nosidefx If this is non-zero, the function will be declared to have no side-effects and allow
some more optimizations.
inline_asm
Inline assembly can be specified for the function.
Note that not all ICs can be converted to library calls.
− A new array reg_prio[] is needed and controls the order in which registers are allo-
cated.
− The parameters of must_convert() have changed.
− Static functions must not use identifiers, but have to use numbered labels.
Volker Barthelmann vb@compilers.de