Skip to content

Commit

Permalink
fix some bigint/bits bugs and clean up includes.
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinafyi committed Mar 21, 2024
1 parent f5f878e commit ae935e8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.
24 changes: 14 additions & 10 deletions libASL/cpp_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let name_of_ident v =
let prefixed_name_of_ident st v =
let name = name_of_ident v in
match v with
| FIdent (f,_) when not (List.mem f st.fns) -> "super::" ^ name
| FIdent (f,_) when not (List.mem f st.fns) -> "iface." ^ name
| _ -> name

let rec name_of_lexpr l =
Expand Down Expand Up @@ -119,7 +119,8 @@ let rec prints_expr e st =
Printf.sprintf "List.nth (%s) (%s)" (prints_expr a st) (prints_expr i st)

(* Int Expressions *)
| Expr_LitInt i -> i ^ "LL"
| Expr_LitInt i ->
Printf.sprintf "iface.bigint_lit(\"%s\")" i
| Expr_TApply(FIdent("add_int", 0), [], [a;b]) ->
Printf.sprintf "(%s) + (%s)" (prints_expr a st) (prints_expr b st)
| Expr_TApply(FIdent("sub_int", 0), [], [a;b]) ->
Expand All @@ -132,12 +133,12 @@ let rec prints_expr e st =
(* Other operations *)
| Expr_LitBits b ->
let len = String.length b in
Printf.sprintf "super::bigint_lit(%dU, \"%s\")" len b
Printf.sprintf "iface.bits_lit(%dU, \"%s\")" len b
| Expr_Slices(e,[Slice_LoWd(i,w)]) ->
let e = prints_expr e st in
let i = prints_expr i st in
let w = prints_expr w st in
Printf.sprintf "super::extract_bits(%s, /*lo*/ %s, /*wd*/ %s)" e i w
Printf.sprintf "iface.extract_bits(%s, /*lo*/ %s, /*wd*/ %s)" e i w
| Expr_TApply(f, targs, args) ->
let f = prefixed_name_of_ident st f in
(* let args = List.map (fun e -> prints_expr e st) (targs @ args) in *)
Expand All @@ -153,9 +154,9 @@ let rec prints_expr e st =
and default_value t st =
match t with
| Type_Bits w ->
Printf.sprintf "super::bigint_zero(%s)" (prints_expr w st)
Printf.sprintf "iface.bits_zero(%s)" (prints_expr w st)
| Type_Constructor (Ident "boolean") -> "true"
| Type_Constructor (Ident "integer") -> "0LL"
| Type_Constructor (Ident "integer") -> "iface.bigint_zero()"
| Type_Constructor (Ident "rt_label") -> "rt_label{}"
| Type_Constructor (Ident "rt_expr") -> "rt_expr{}"
| Type_Array(Index_Range(lo, hi),ty) ->
Expand Down Expand Up @@ -350,7 +351,7 @@ let write_fn name (ret_tyo,_,targs,args,_,body) st =

let init_st oc = { depth = 0; skip_seq = false; oc ; ref_vars = IdentSet.empty ; fns = []; }
let stdlib_deps = ["cassert"; "tuple"; "variant"; "vector"; "stdexcept"; "interface.hpp"]
let global_deps = stdlib_deps @ ["aslp_lifter_base.hpp"]
let global_deps = stdlib_deps @ ["aslp_lifter_decl.hpp"]

(* Write an instruction file, containing just the behaviour of one instructions *)
let write_instr_file fn fnsig dir =
Expand Down Expand Up @@ -396,7 +397,7 @@ let write_decoder_file fn fnsig deps otherfns dir =
m

let write_header_file fn fnsig deps tests dir =
let m = "aslp_lifter_base" in
let m = "aslp_lifter_decl" in
let path = dir ^ "/" ^ m ^ ".hpp" in
let oc = open_out path in
let st = init_st oc in
Expand All @@ -410,9 +411,12 @@ let write_header_file fn fnsig deps tests dir =
write_line " {\n" st;

inc_depth st;
write_line ("using super = lifter_interface" ^ template_args ^ ";\n") st;
write_line ("public: using interface = lifter_interface" ^ template_args ^ ";\n") st;
write_line "private: interface& iface;\n" st;
write_line "public:\n" st;
write_line "aslp_lifter(interface& iface) : iface{iface} { }\n" st;
List.iter
(fun t -> write_line ("using typename super::" ^ t ^ ";\n") st)
(fun t -> write_line ("using typename interface::" ^ t ^ ";\n") st)
typenames;
List.iter
(fun f -> write_line (void_str ^ " " ^ name_of_ident f ^ "(bits);\n") st)
Expand Down
15 changes: 0 additions & 15 deletions offlineASL-cpp/aslp_lifter_base.hpp

This file was deleted.

23 changes: 5 additions & 18 deletions offlineASL-cpp/interface.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <llvm/IR/Instruction.h>
#include <llvm/IR/Instructions.h>
#include <llvm/MC/MCExpr.h>
#include <string_view>

namespace aslp {

Expand All @@ -22,8 +20,10 @@ class lifter_interface {
using rt_label = RT_LABEL;

protected:
virtual bigint bigint_lit(unsigned width, std::string_view str) = 0;
virtual bigint bigint_zero(unsigned width) = 0;
virtual bigint bits_lit(unsigned width, std::string_view str) = 0;
virtual bigint bits_zero(unsigned width) = 0;
virtual bits bigint_lit(std::string_view str) = 0;
virtual bits bigint_zero() = 0;
virtual bits extract_bits(const bits &val, bigint lo, bigint wd) = 0;

virtual bool f_eq_bits(const bits &x, const bits &y) = 0;
Expand Down Expand Up @@ -141,17 +141,4 @@ class lifter_interface {
rt_expr is64) = 0; // from override.asl
};

namespace llvm_detail {
using bits = llvm::APInt;
using bigint = long long;
using rt_expr = llvm::Value *;
using rt_lexpr = llvm::AllocaInst *;
using rt_label = llvm::BasicBlock *;

using lifter_interface_llvm =
lifter_interface<bits, bigint, rt_expr, rt_lexpr, rt_label>;
} // namespace llvm_detail

using llvm_detail::lifter_interface_llvm;

} // namespace aslp
3 changes: 2 additions & 1 deletion offlineASL-cpp/llvm_interface.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <llvm/ADT/APInt.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/Instructions.h>

#include "aslp_lifter_base.hpp"
#include "interface.hpp"

namespace aslp {
Expand Down

0 comments on commit ae935e8

Please sign in to comment.