Skip to content

Commit

Permalink
feat: implement inline assembly
Browse files Browse the repository at this point in the history
This commit fleshes out the implementation of inline assembly that we
had essentially stubbed out prior to this. The following are provided as
part of this implementation:

* A representation of the Miden Assembly instruction set, in crate,
  with the ops we intend to support in the near term. This is used to
  represent the contents of inline assembly, and is also used in the
  upcoming codegen backend as the target IR for Miden Assembly, as it
  is a flatter representation similar to Miden IR. The intent is to also
  implement a tiny evaluator for this subset of Miden Assembly for use
  in tests without having to emit MASM files, and run the Miden VM which
  is much more expensive. It also allows us to track the subset of Miden
  Assembly which we support in the compiler, as well as represent
  experimental changes that have not yet been implemented upstream.
* `MasmBuilder`, which is constructed from an `InstBuilder` via `inline_asm`,
  and plays a similar role to `FunctionBuilder`+`InstBuilder`, but in
  terms of Miden Assembly, in the context of constructing a single inline
  assembly block.
* Improved pretty printing of inline assembly. Now, an inline assembly
  block is printed like so (this block adds the two inputs, and squares
  the result, which is returned as the result of the inline assembly
  block):

  ```hir
  asm (v1, v2) {
      add
      mul.2
  } : felt
  ```
* A generic data structure for use in representing the operand stack,
  and the common operations which are performed on it. This is used to
  validate inline assembly, as well as keep track of values during code
  generation. It will also likely be used for whatever evaluator we
  throw together for use in testing.
  • Loading branch information
bitwalker committed Sep 7, 2023
1 parent 840eacd commit 0b907c5
Show file tree
Hide file tree
Showing 6 changed files with 2,486 additions and 34 deletions.
8 changes: 8 additions & 0 deletions hir-type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate alloc;
use alloc::{alloc::Layout, boxed::Box, vec::Vec};
use core::fmt;

const FELT_SIZE: usize = core::mem::size_of::<u64>();
const WORD_SIZE: usize = core::mem::size_of::<[u64; 4]>();

/// Represents the type of a value
Expand Down Expand Up @@ -179,6 +180,13 @@ impl Type {
self.layout().pad_to_align().size()
}

/// Returns the size in field elements of this type, including necessary alignment padding
pub fn size_in_felts(&self) -> usize {
let bytes = self.size_in_bytes();
let trailing = bytes % FELT_SIZE;
(bytes / FELT_SIZE) + ((trailing > 0) as usize)
}

/// Returns the size in words of this type, including necessary alignment padding
pub fn size_in_words(&self) -> usize {
let bytes = self.size_in_bytes();
Expand Down
Loading

0 comments on commit 0b907c5

Please sign in to comment.