Bascom Precompiler

Aus ProjectWiki
Wechseln zu:Navigation, Suche



The precompiler processes the Bascom source files (also taking includes into account), outputs the generated source files besides the original files and invokes bascomp.exe. Original files are not touched. It supports the following enhancements:

  • Macros with arguments
  • RAM and ROM address data type
  • Enumerations
  • Conditional Compilation Checks: #if Subexist(""), #if Labelexist("")
  • Composed data types (structures) and OOP concepts
  • Automatic generated constants: _sourceline, _sourcefileline, _filename, _subroutinename, _precompiler, _currentdate, _currenttime, _buildnumber, _freeramstart, _isrstackcount
  • Export disassembly (optionally annotated with original source code)

Some precompiler steps involve parsing the compiler output, in that case, a second compile step is then performed using the gathered data.

Functional description

Macros with arguments


Macro [MacroName] ([ArgumentList])
End Macro

These macros work the same as the built-in ones, but accept additional arguments, which work as a textual replacement. Therefore arguments are type-agnostic. Also supports argument concatenation using "." (argument concatenation has priority over the type-member-/enum-access qualifier "."). Nested macros are allowed.

Example 1:

Macro Mymacro(printtext)
   Print "This is my text: "; printtext
End Macro

Invoking with a string argument:

Mymacro "some characters"

Will yield in output:

Print "This is my text: "; "some characters"

Invoking with a variable as argument:

Dim Myvariable As Byte
Mymacro Myvariable


Print "This is my text: "; Myvariable


Macro Concat (Arg1, Arg2, Arg3)
   Arg1.Arg2 Arg3
End Macro


Concat($reg, file, "m32def.dat")


$regfile "m32def.dat"

RAM/ROM address data type

Some devices have more than 64K program space and use 24 bit wide addresses (instead of 16 bit) for flash access, which makes porting code between the addressing schemes more tedious. The same happens with RAM addresses for devices using external memory. To support this, the precompiler introduces two new variable types: Rampointer and Rompointer. They will be assigned a Word or a Dword variable type on compilation, according to the currently used addressing schemes.


Dim Labeladdress As Rompointer
Dim Myramspace As Rampointer

Sub Testthis(position As Rampointer)
   Local Flashdata As Rompointer
End Sub


Defines a list of key-value pairs. If no value is specified, the value is either 0 for the first key entry or the value of the previous key + 1. One key/value pair per line. Usage is like constants, members are accessed using [EnumName].[MemberName].


Enum Myenum
   Value2 = 5,
End Enum

Print Myenum.Value1; Myenum.Value2; Myenum.Value3          ' Output: 056

Conditional Compilation Checks

The precompiler adds additional symbol-checks to Bascom's conditional compilation switches (they act in the same way the as built-in Varexist("") directive).


#if Subexist("[SubroutineName]")
#if Labelexist("[LabelName]")

Composed Data Types

A composed data type consists of several data types (intrinsic or other composed types) grouped together in memory. It supports every available data type (bit, byte, word, integer, dword, long, single, double, string, rampointer, rompointer and composed types) and optionally supports inheritance. Composed types as member field or dimensioned using Dim, Local or as a subroutine parameter are internally referenced by the Rampointer data type.

To create an instance of the type definition, a memory block of sufficient size has to be reserved and referenced through a variable pointing to the first memory address of that block. Memberfields are accessed by the "." qualifier. Only assignments of a variable from the memberfield or assignments of the memberfield from a variable or constant are allowed (by now). Internally, these are translated to Settype/Gettype functions as found in Bit member fields internally use a byte.


Typedef [TypedefName]
   [Extends TypedefName]
   [MemberFieldName] As [Bit/Byte/Word/Integer/Dword/Long/Single/Double/String*N/TypedefName]
End Typedef

Sub Memberfunction(object As [TypedefName])
   Local Anotherobject As [TypedefName]
End Sub

Dim Myinstance As [TypedefName]

Myinstance = Malloc(SizeOf([TypedefName]))

Myinstance.[MemberfieldName] = [Constant/Variable]
Variable = Myinstance.[MemberfieldName]

Automatic generated constants

These keywords are replaced by their value as number or string literal, usage is like constants.


(Number) Returns the current line number, inclusive includes.


(Number) Returns the current line number only counting the current file


(String) Returns the current source file name


(String) Returns the current sub routine name (valid inside Subs/Functions and Label-Return-pairs, empty string if not inside subroutine).


(Number) Actually creates a constant containing the precompiler version (current: 1), which could be checked for existence by #if Varexist("_precompiler") to check if the code is beeing processed using the precompiler.


(String) Contains the build time.


(String) Contains the build date.


(Number) Returns the first unused memory position. Needs a second compile pass.


(Number) Returns the used stack size (PUSH statements) inside an ISR. Needs a second compile pass.

Const _buildnumber = 0

This constant is treated specially, in the output it will contain the current build number, incremented by 1 on each compilation. Current build count is maintained in a file besides the original source file with the extension ".bld".

Precompiler directives


Instructs the precompiler to output a disassembly of the compilation result. Needs a second compile pass.


Instructs the precompiler to output a disassembly of the compilation result, mapped to the corresponding source lines. Needs a second compile pass.

Command Line


prebascomp.exe [FileName] [Options]



Instructs the precompiler to output a disassembly of the compilation result. Needs a second compile pass.


Instructs the precompiler to output a disassembly of the compilation result, mapped to the corresponding source lines. Needs a second compile pass.


Generate a log file of the precompilation process.


Bascom OOP & Precompiler Download