Skip to content

Update backend.md #15062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/backend/jvm/GenBCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class GenBCode extends Phase {

private var myPrimitives: DottyPrimitives = null

def run(using Context): Unit =
override def run(using Context): Unit =
if myPrimitives == null then myPrimitives = new DottyPrimitives(ctx)
new GenBCodePipeline(
DottyBackendInterface(outputDir, superCallsMap),
Expand Down
47 changes: 25 additions & 22 deletions docs/_docs/internals/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ layout: doc-page
title: "Backend Internals"
---

The code for the backend is split up by functionality and assembled in the
object `GenBCode`.
The code for the JVM backend is split up by functionality and assembled in
`GenBCode.scala`. This file defines class `GenBCode`, the compiler phase.

```
object GenBCode --- [defines] --> PlainClassBuilder GenBCode also defines class BCodePhase, the compiler phase
class GenBCodePipeline -[defines]--> PlainClassBuilder
| |
[extends] [extends]
| |
Expand All @@ -18,34 +18,37 @@ BCodeBodyBuilder ----------------> PlainBodyBuilder
BCodeSkelBuilder ----------------> PlainSkelBuilder
| / | \
BCodeHelpers ----------------> BCClassGen BCAnnotGen ... (more components)
| \ \
| \ \-------------> helper methods
| \ \------------> JMirrorBuilder, JBeanInfoBuilder (uses some components, e.g. BCInnerClassGen)
| \
| | \
| | \-------------> helper methods
| | \------------> JMirrorBuilder, JBeanInfoBuilder (uses some components, e.g. BCInnerClassGen)
| |
| BytecodeWriters ---------> methods and classes to write byte code files
|
BCodeTypes ----------------> maps and fields for common BTypes, class Tracked, methods to collect information on classes, tests for BTypes (conforms), ...
|
BCodeIdiomatic ----------------> utilities for code generation, e.g. genPrimitiveArithmetic
|
BCodeGlue ----------------> BType class, predefined BTypes
\--------------> `bTypes`: maps and fields for common BTypes
```

The `BTypes.scala` class contains the `BType` class and predefined BTypes

### Data Flow ###
Compiler creates a `BCodePhase`, calls `runOn(compilationUnits)`.
Compiler creates a `GenBCode` `Phase`, calls `runOn(compilationUnits)`,
which calls `run(context)`. This:

* initializes fields of `GenBCode` defined in `BCodeTypes` (BType maps,
common BTypes like `StringReference`)
* initialize `primitives` map defined in `scalaPrimitives` (maps primitive
* initializes `myPrimitives` defined in `DottyPrimitives` (maps primitive
members, like `int.+`, to bytecode instructions)
* creates `BytecodeWriter`, `JMirrorBuilder` and `JBeanInfoBuilder` instances
(on each compiler run)
* creates a `GenBCodePipeline` and calls `run(tree)`

`GenBCodePipeline` now:

* initializes the `bTypes` field of `GenBCodePipeline` defined in `BCodeIdiomatic`
(BType maps, common BTypes like `StringRef`)
* creates `BytecodeWriter` and `JMirrorBuilder` instances (on each compiler run)
* `buildAndSendToDisk(units)`: uses work queues, see below.
- `BCodePhase.addToQ1` adds class trees to `q1`
- `Worker1.visit` creates ASM `ClassNodes`, adds to `q2`. It creates one
- `GenBCodePipeline.feedPipeline1` adds ClassDefs to `q1`
- `Worker1.run` creates ASM `ClassNodes`, adds to `q2`. It creates one
`PlainClassBuilder` for each compilation unit.
- `Worker2.addToQ3` adds byte arrays (one for each class) to `q3`
- `BCodePhase.drainQ3` writes byte arrays to disk
- `Worker2.run` adds byte arrays (one for each class) to `q3`
- `GenBCodePipeline.drainQ3` writes byte arrays to disk


### Architecture ###
Expand Down Expand Up @@ -110,7 +113,7 @@ To understand how it's built, see:
final def exemplar(csym0: Symbol): Tracked = { ... }
```

Details in `BCodeTypes.scala`
Details in `BTypes.scala`

#### (e) More "high-level" utilities for bytecode emission ####
In the spirit of `BCodeIdiomatic`, utilities are added in `BCodeHelpers` for
Expand Down