Skip to content

Compiler

Aman Priyadarshi edited this page Mar 12, 2017 · 7 revisions

Atom Compiler works on top of other available C# compiler like .NET or Mono. Core work like parsing language and converting it to corresponding CIL is done by these 3rd party compilers. Customer compiler built by us work on emitted CIL and generate target platform specific native assembly code. This assembly file is assembled by NAsm and Object code is generated which then linked to other libraries to produce final ELF image of kernel.

Compiler Diagram

Atom compiler handles a small subset of the C#/IL functionality and the rest of the functions are plugged. That means, that they are implemented using that subset of the library that works with the compiler, and the original function calls are then replaced with the plugs.

The original .NET framework is not available, only the plugged subset of the framework.

How plugs are implemented?

Plugs are resolved at compile time and should be present in input assemblies. Plugs are in itself divided into two parts.

For more details on plug attribute check this

How to Invoke Atomix Compiler?

You need to build Atomixilc project first. After this compiler's executable binary will be available in /Bin directory.

Command Line

   Atomixilc.exe -cpu x86 -i Atomix.Kernel_H.dll -o Kernel.asm -v -optimize
  • -cpu : Only x86 supported. Other values x64, ARM
  • -i : Comma Separated Input Assemblies
  • -o : Output Assembly file name
  • -v : Verbose Output
  • -optimize : Invoke Optimizer check Build.sh

Compiler Design

Fields

Static Fields

  • Compiler add these type of field in BSS Segment.
  • Compiler.cs function: ProcessFieldInfo.

Non-Static Fields

  • Memory is allocated in object's memory buffer.
  • First non-static field entry starts just after object's metadata.
  • Object Allocation and Initialization is handled by Newobj.cs

Objects

Value type

  • Not Supported

Reference type

Object Model

  • Type: Unique ID for object type
  • Flag: (0x1 | FieldIsClassType << 2)
    • FieldIsClassType: Number of subfields which are ClassType.
  • Memory Size: Total size of object buffer in bytes.
  • Object Buffer: Used for storing sub-fields value.

Arrays

Array is created by Newarr.cs

Array Model

  • Type: Unique ID for array element object type.
  • Flag: 0x2
  • Length: Number of elements in the array.
  • Size: Storage size of type (array element).
  • Object Buffer: Used for storing array elements.

Virtual Table

  /* VTables are implemented based on the idea of lookup tables
   * Structure:
   *      |next_block_offset|             : points to next block offset
   *      |method_uid|                    : MethodBase UID
   *          |method_address||type_id|   : MethodInfo Address and DeclaringType ID
   *          |method_address||type_id|
   *          |method_address||type_id|
   *          |"0"|                       : End of this block
   *      |next_block_offset|
   *      |method_uid|
   *          |method_address||type_id|
   *          |method_address||type_id|
   *          |method_address||type_id|
   *          |"0"|
   *      |"0"|                           : End of VTable
   */

Delegates

  /* method signature: void .ctor(memory, System.Object, IntPtr)
   * memory points to just allocated memory for this delegate
   * 0xC byte size metadata -- added by compiler
   * [memory + 0xC] := IntPtr
   * [memory + 0x10] := System.Object
   */
  • Invoke:
  /* method signature : void Invoke(memory, params ...)
   * check if System.Object [memory + 0x10] is null or not
   * if null, simply call Intptr [memory + 0xC] after pushing params on to the stack
   * if not null, first push System.Object [memory + 0x10]
   *    then push push params and then call Intptr [memory + 0xC]
   */
Clone this wiki locally