Skip to content

Commit

Permalink
Update backend.md
Browse files Browse the repository at this point in the history
Some of the information in backend.md seems to have grown outdated,
I updated it based on what I found in the code.
  • Loading branch information
raboof authored and bishabosha committed Oct 18, 2022
1 parent 5da757f commit 3f44f5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
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

0 comments on commit 3f44f5c

Please sign in to comment.