Skip to content

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:test covers 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), whose getXcfaFromC parses, 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 the CProgram or the ParseContext.
  • Grammar and visitor changes must also pass :theta-xcfa-cli:fixtureTest and canaryTest, 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 point visitCompilationUnit), expression/ExpressionVisitor, type/TypeVisitor and DeclarationVisitor, preprocess/ (typedefs, globals reachable from main, arithmetic traits), and CParseUtils.kt (parseTypeAware).
  • transformation/model/ — the IM: statements/ (CStatementVisitor plus one class per statement), types/simple/ (declared specifiers such as Struct, Enum, NamedType; getActualType() resolves them), types/complex/ (the resolved CComplexTypes: 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

  1. Parsing is two-pass. parseTypeAware first harvests typedef names with a permissive, error-tolerant parse, then parses with BailErrorStrategy, 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 write cType metadata.
  2. An expression's C type is metadata, not part of the Expr. It is stored under "cType" in ParseContext.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)).
  3. Static registries are per build. Struct (tag registry), Enum (constants) and FunctionIds are static and are reset in FunctionVisitor.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.
  4. Side effects inside an expression are lowered to preStatements (run before it) and postStatements (run at the end of the full expression). Statements emitted by an operand of &&/|| must run only when that operand is evaluated (guardShortCircuited in ExpressionVisitor).
  5. A local declared without an initializer inside a loop or outside main yields a CAssume carrying 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.
  6. CStatementVisitor has two implementors: StatisticsCollectorVisitor (CStatistics.kt) and c2xcfa's FrontendXcfaBuilder (through CStatementVisitorBase, whose defaults throw). A new statement class needs both.
  7. Unsupported constructs throw UnsupportedFrontendElementException. Throw the subclass RequiresByteAddressedMemoryException only when --memory-model bytes can express the construct: when no memory model was requested, xcfa-cli retries under bytes on exactly that type.
  8. Two arithmetic backends. types/complex/visitors/{integer,bitvector} each implement the cast, limit, null/unit value and SMT-type visitors. A new CComplexType, or a change to one's semantics, has to be made in both.
  9. Headers are bundled, not read from disk. stdlib/HeaderFile.kt maps 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.
  10. Stack objects go through alloca. Local aggregates and VLAs become alloca calls, and leaving a block, also by break, continue or goto, emits FunctionVisitor.SCOPE_END (__theta_scope_end). Both are lowered by AllocaFunctionPass in :theta-xcfa. Entering a block emits SCOPE_START for its address-taken scalars, lowered to a fresh base by ReferenceElimination; a jump past a block's entry that would skip one is refused. return is handled at procedure exit by those two passes. A VLA size is in cells, not bytes. A program's own alloca(n) and __builtin_alloca(n) become __builtin_alloca, sized in bytes.