AGENTS.md — how to modify this module
Invariants, conventions and change recipes for contributors — and for AI coding assistants, which load this file automatically when editing the module. Largely derived from the source and verified against it.
Mirrored from subprojects/frontends/c-frontend/AGENTS.md — edit it there, not here.
frontends/c-frontend — notes for editing this module
Gradle module: :theta-c-frontend. Parses preprocessed C with ANTLR into the IM (a CStatement tree plus a C type model), which c2xcfa turns into an XCFA. Pipeline overview: README.md and transformation/Readme.md.
Build and test
./gradlew :theta-c-frontend:testcovers the type model, object layouts and the typedef-aware parse (src/test/kotlin/hu/bme/mit/theta/frontend/). Most frontend behaviour is tested from./gradlew :theta-c2xcfa:test(e.g.CLiteralTypingTest,ShortCircuitTest), whosegetXcfaFromCparses, builds and runs the C pass pipeline. Run both.- A frontend unit test parses a source string with
parseTypeAware(...).accept(FunctionVisitor(ParseContext(), NullLogger.getInstance()))and inspects theCProgramor theParseContext. - Grammar and visitor changes must also pass
:theta-xcfa-cli:fixtureTestandcanaryTest, with a new fixture that fails before the change and passes after (canaries/README.md).
Layout
Paths below are relative to src/main/java/hu/bme/mit/theta/frontend/.
- C.g4 — the grammar, generated into
hu.bme.mit.theta.c.frontend.dsl.gen(ANTLR runs with-Werror). transformation/grammar/— the visitors:function/FunctionVisitor(statements; entry pointvisitCompilationUnit),expression/ExpressionVisitor,type/TypeVisitorandDeclarationVisitor,preprocess/(typedefs, globals reachable frommain, arithmetic traits), andCParseUtils.kt(parseTypeAware).transformation/model/— the IM:statements/(CStatementVisitorplus one class per statement),types/simple/(declared specifiers such asStruct,Enum,NamedType;getActualType()resolves them),types/complex/(the resolvedCComplexTypes: integers, reals,CPointer,CArray,CStruct, object layouts).- ParseContext.java — per-build settings (architecture, arithmetic, memory model) and the
FrontendMetadata.stdlib/holds the built-in contents of the supported#include <...>headers.
Invariants / gotchas
- Parsing is two-pass.
parseTypeAwarefirst harvests typedef names with a permissive, error-tolerant parse, then parses withBailErrorStrategy, accepting only those names as types. There is deliberately no permissive fallback. Never run the frontend's visitors in the first pass: they register struct tags and writecTypemetadata. - An expression's C type is metadata, not part of the
Expr. It is stored under"cType"inParseContext.getMetadata(), keyed by object identity.CComplexType.getType(expr, ctx)falls back to a type derived from the SMT sort when it is missing, so stamp every expression you build from a typed one (metadata.create(newExpr, "cType", type)). - Static registries are per build.
Struct(tag registry),Enum(constants) andFunctionIdsare static and are reset inFunctionVisitor.visitCompilationUnit, because one JVM can build the same input several times (the memory-model and arithmetic fallbacks, portfolio re-parses). Reset any new static per-program state there too. - Side effects inside an expression are lowered to
preStatements(run before it) andpostStatements(run at the end of the full expression). Statements emitted by an operand of&&/||must run only when that operand is evaluated (guardShortCircuitedinExpressionVisitor). - A local declared without an initializer inside a loop or outside
mainyields aCAssumecarrying the variable (getHavocked()). c2xcfa lowers it to a havoc plus the range assume, so every re-execution gets a fresh value; a declaration that runs once keeps the backend's initial value, which explicit-state backends such as MDD rely on. CStatementVisitorhas two implementors:StatisticsCollectorVisitor(CStatistics.kt) and c2xcfa'sFrontendXcfaBuilder(throughCStatementVisitorBase, whose defaults throw). A new statement class needs both.- Unsupported constructs throw
UnsupportedFrontendElementException. Throw the subclassRequiresByteAddressedMemoryExceptiononly when--memory-model bytescan express the construct: when no memory model was requested, xcfa-cli retries underbyteson exactly that type. - Two arithmetic backends.
types/complex/visitors/{integer,bitvector}each implement the cast, limit, null/unit value and SMT-type visitors. A newCComplexType, or a change to one's semantics, has to be made in both. - Headers are bundled, not read from disk.
stdlib/HeaderFile.ktmaps a header name to a Kotlin string of declarations. Add a new header there and to the enum; an unknown#include <x>expands to nothing. - Stack objects go through
alloca. Local aggregates and VLAs becomeallocacalls, and leaving a block, also bybreak,continueorgoto, emitsFunctionVisitor.SCOPE_END(__theta_scope_end). Both are lowered byAllocaFunctionPassin:theta-xcfa. Entering a block emitsSCOPE_STARTfor its address-taken scalars, lowered to a fresh base byReferenceElimination; a jump past a block's entry that would skip one is refused.returnis handled at procedure exit by those two passes. A VLA size is in cells, not bytes. A program's ownalloca(n)and__builtin_alloca(n)become__builtin_alloca, sized in bytes.