| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971 |
- // SPDX-License-Identifier: Apache-2.0
- use crate::codegen::cfg::{HashTy, ReturnCode};
- use crate::codegen::{Builtin, Expression};
- use crate::emit::binary::Binary;
- use crate::emit::math::{build_binary_op_with_overflow_check, multiply, power};
- use crate::emit::strings::{format_string, string_location};
- use crate::emit::{BinaryOp, TargetRuntime, Variable};
- use crate::sema::ast::{Namespace, RetrieveType, StructType, Type};
- use crate::Target;
- use inkwell::module::Linkage;
- use inkwell::types::{BasicType, StringRadix};
- use inkwell::values::{ArrayValue, BasicValueEnum, FunctionValue, IntValue};
- use inkwell::{AddressSpace, IntPredicate};
- use num_bigint::Sign;
- use std::collections::HashMap;
- /// The expression function recursively emits code for expressions. The BasicEnumValue it
- /// returns depends on the context; if it is simple integer, bool or bytes32 expression, the value
- /// is an Intvalue. For references to arrays, it is a PointerValue to the array. For references
- /// to storage, it is the storage slot. The references types are dereferenced by the Expression::Load()
- /// and Expression::StorageLoad() expression types.
- pub(super) fn expression<'a, T: TargetRuntime<'a> + ?Sized>(
- target: &T,
- bin: &Binary<'a>,
- e: &Expression,
- vartab: &HashMap<usize, Variable<'a>>,
- function: FunctionValue<'a>,
- ns: &Namespace,
- ) -> BasicValueEnum<'a> {
- match e {
- Expression::FunctionArg(_, _, pos) => function.get_nth_param(*pos as u32).unwrap(),
- Expression::BoolLiteral(_, val) => {
- bin.context.bool_type().const_int(*val as u64, false).into()
- }
- Expression::NumberLiteral(_, Type::Address(_), val) => {
- // address can be negative; "address(-1)" is 0xffff...
- let mut bs = val.to_signed_bytes_be();
- // make sure it's no more than 32
- if bs.len() > ns.address_length {
- // remove leading bytes
- for _ in 0..bs.len() - ns.address_length {
- bs.remove(0);
- }
- } else {
- // insert leading bytes
- let val = if val.sign() == Sign::Minus { 0xff } else { 0 };
- for _ in 0..ns.address_length - bs.len() {
- bs.insert(0, val);
- }
- }
- let address = bs
- .iter()
- .map(|b| bin.context.i8_type().const_int(*b as u64, false))
- .collect::<Vec<IntValue>>();
- bin.context.i8_type().const_array(&address).into()
- }
- Expression::NumberLiteral(_, ty, n) => bin.number_literal(ty.bits(ns) as u32, n, ns).into(),
- Expression::StructLiteral(_, ty, exprs) => {
- let struct_ty = bin.llvm_type(ty, ns);
- let s = bin
- .builder
- .build_call(
- bin.module.get_function("__malloc").unwrap(),
- &[struct_ty
- .size_of()
- .unwrap()
- .const_cast(bin.context.i32_type(), false)
- .into()],
- "",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- .into_pointer_value();
- let s = bin.builder.build_pointer_cast(
- s,
- struct_ty.ptr_type(AddressSpace::default()),
- "struct_literal",
- );
- for (i, expr) in exprs.iter().enumerate() {
- let elemptr = unsafe {
- bin.builder.build_gep(
- s,
- &[
- bin.context.i32_type().const_zero(),
- bin.context.i32_type().const_int(i as u64, false),
- ],
- "struct member",
- )
- };
- let elem = expression(target, bin, expr, vartab, function, ns);
- let elem = if expr.ty().is_fixed_reference_type() {
- bin.builder.build_load(elem.into_pointer_value(), "elem")
- } else {
- elem
- };
- bin.builder.build_store(elemptr, elem);
- }
- s.into()
- }
- Expression::BytesLiteral(_, _, bs) => {
- let ty = bin.context.custom_width_int_type((bs.len() * 8) as u32);
- // hex"11223344" should become i32 0x11223344
- let s = hex::encode(bs);
- ty.const_int_from_string(&s, StringRadix::Hexadecimal)
- .unwrap()
- .into()
- }
- Expression::Add(loc, _, unchecked, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- if bin.options.math_overflow_check && !*unchecked {
- let signed = l.ty().is_signed_int();
- build_binary_op_with_overflow_check(
- target,
- bin,
- function,
- left,
- right,
- BinaryOp::Add,
- signed,
- ns,
- *loc,
- )
- .into()
- } else {
- bin.builder.build_int_add(left, right, "").into()
- }
- }
- Expression::Subtract(loc, _, unchecked, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- if bin.options.math_overflow_check && !*unchecked {
- let signed = l.ty().is_signed_int();
- build_binary_op_with_overflow_check(
- target,
- bin,
- function,
- left,
- right,
- BinaryOp::Subtract,
- signed,
- ns,
- *loc,
- )
- .into()
- } else {
- bin.builder.build_int_sub(left, right, "").into()
- }
- }
- Expression::Multiply(loc, res_ty, unchecked, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- multiply(
- target,
- bin,
- function,
- *unchecked,
- left,
- right,
- res_ty.is_signed_int(),
- ns,
- *loc,
- )
- .into()
- }
- Expression::UnsignedDivide(loc, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- let bits = left.get_type().get_bit_width();
- if bits > 64 {
- let div_bits = if bits <= 128 { 128 } else { 256 };
- let name = format!("udivmod{div_bits}");
- let f = bin
- .module
- .get_function(&name)
- .expect("div function missing");
- let ty = bin.context.custom_width_int_type(div_bits);
- let dividend = bin.build_alloca(function, ty, "dividend");
- let divisor = bin.build_alloca(function, ty, "divisor");
- let rem = bin.build_alloca(function, ty, "remainder");
- let quotient = bin.build_alloca(function, ty, "quotient");
- bin.builder.build_store(
- dividend,
- if bits < div_bits {
- bin.builder.build_int_z_extend(left, ty, "")
- } else {
- left
- },
- );
- bin.builder.build_store(
- divisor,
- if bits < div_bits {
- bin.builder.build_int_z_extend(right, ty, "")
- } else {
- right
- },
- );
- let ret = bin
- .builder
- .build_call(
- f,
- &[dividend.into(), divisor.into(), rem.into(), quotient.into()],
- "udiv",
- )
- .try_as_basic_value()
- .left()
- .unwrap();
- let success = bin.builder.build_int_compare(
- IntPredicate::EQ,
- ret.into_int_value(),
- bin.context.i32_type().const_zero(),
- "success",
- );
- let success_block = bin.context.append_basic_block(function, "success");
- let bail_block = bin.context.append_basic_block(function, "bail");
- bin.builder
- .build_conditional_branch(success, success_block, bail_block);
- bin.builder.position_at_end(bail_block);
- // throw division by zero error should be an assert
- target.log_runtime_error(bin, "division by zero".to_string(), Some(*loc), ns);
- target.assert_failure(
- bin,
- bin.context
- .i8_type()
- .ptr_type(AddressSpace::default())
- .const_null(),
- bin.context.i32_type().const_zero(),
- );
- bin.builder.position_at_end(success_block);
- let quotient = bin
- .builder
- .build_load(quotient, "quotient")
- .into_int_value();
- if bits < div_bits {
- bin.builder
- .build_int_truncate(quotient, left.get_type(), "")
- } else {
- quotient
- }
- .into()
- } else {
- bin.builder.build_int_unsigned_div(left, right, "").into()
- }
- }
- Expression::SignedDivide(loc, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- let bits = left.get_type().get_bit_width();
- if bits > 64 {
- let div_bits = if bits <= 128 { 128 } else { 256 };
- let name = format!("sdivmod{div_bits}");
- let f = bin
- .module
- .get_function(&name)
- .expect("div function missing");
- let ty = bin.context.custom_width_int_type(div_bits);
- let dividend = bin.build_alloca(function, ty, "dividend");
- let divisor = bin.build_alloca(function, ty, "divisor");
- let rem = bin.build_alloca(function, ty, "remainder");
- let quotient = bin.build_alloca(function, ty, "quotient");
- bin.builder.build_store(
- dividend,
- if bits < div_bits {
- bin.builder.build_int_s_extend(left, ty, "")
- } else {
- left
- },
- );
- bin.builder.build_store(
- divisor,
- if bits < div_bits {
- bin.builder.build_int_s_extend(right, ty, "")
- } else {
- right
- },
- );
- let ret = bin
- .builder
- .build_call(
- f,
- &[dividend.into(), divisor.into(), rem.into(), quotient.into()],
- "udiv",
- )
- .try_as_basic_value()
- .left()
- .unwrap();
- let success = bin.builder.build_int_compare(
- IntPredicate::EQ,
- ret.into_int_value(),
- bin.context.i32_type().const_zero(),
- "success",
- );
- let success_block = bin.context.append_basic_block(function, "success");
- let bail_block = bin.context.append_basic_block(function, "bail");
- bin.builder
- .build_conditional_branch(success, success_block, bail_block);
- bin.builder.position_at_end(bail_block);
- // throw division by zero error should be an assert
- target.log_runtime_error(bin, "division by zero".to_string(), Some(*loc), ns);
- target.assert_failure(
- bin,
- bin.context
- .i8_type()
- .ptr_type(AddressSpace::default())
- .const_null(),
- bin.context.i32_type().const_zero(),
- );
- bin.builder.position_at_end(success_block);
- let quotient = bin
- .builder
- .build_load(quotient, "quotient")
- .into_int_value();
- if bits < div_bits {
- bin.builder
- .build_int_truncate(quotient, left.get_type(), "")
- } else {
- quotient
- }
- .into()
- } else if ns.target == Target::Solana {
- // no signed div on BPF; do abs udev and then negate if needed
- let left_negative = bin.builder.build_int_compare(
- IntPredicate::SLT,
- left,
- left.get_type().const_zero(),
- "left_negative",
- );
- let left = bin
- .builder
- .build_select(
- left_negative,
- bin.builder.build_int_neg(left, "signed_left"),
- left,
- "left_abs",
- )
- .into_int_value();
- let right_negative = bin.builder.build_int_compare(
- IntPredicate::SLT,
- right,
- right.get_type().const_zero(),
- "right_negative",
- );
- let right = bin
- .builder
- .build_select(
- right_negative,
- bin.builder.build_int_neg(right, "signed_right"),
- right,
- "right_abs",
- )
- .into_int_value();
- let res = bin.builder.build_int_unsigned_div(left, right, "");
- let negate_result =
- bin.builder
- .build_xor(left_negative, right_negative, "negate_result");
- bin.builder.build_select(
- negate_result,
- bin.builder.build_int_neg(res, "unsigned_res"),
- res,
- "res",
- )
- } else {
- bin.builder.build_int_signed_div(left, right, "").into()
- }
- }
- Expression::UnsignedModulo(loc, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- let bits = left.get_type().get_bit_width();
- if bits > 64 {
- let div_bits = if bits <= 128 { 128 } else { 256 };
- let name = format!("udivmod{div_bits}");
- let f = bin
- .module
- .get_function(&name)
- .expect("div function missing");
- let ty = bin.context.custom_width_int_type(div_bits);
- let dividend = bin.build_alloca(function, ty, "dividend");
- let divisor = bin.build_alloca(function, ty, "divisor");
- let rem = bin.build_alloca(function, ty, "remainder");
- let quotient = bin.build_alloca(function, ty, "quotient");
- bin.builder.build_store(
- dividend,
- if bits < div_bits {
- bin.builder.build_int_z_extend(left, ty, "")
- } else {
- left
- },
- );
- bin.builder.build_store(
- divisor,
- if bits < div_bits {
- bin.builder.build_int_z_extend(right, ty, "")
- } else {
- right
- },
- );
- let ret = bin
- .builder
- .build_call(
- f,
- &[dividend.into(), divisor.into(), rem.into(), quotient.into()],
- "udiv",
- )
- .try_as_basic_value()
- .left()
- .unwrap();
- let success = bin.builder.build_int_compare(
- IntPredicate::EQ,
- ret.into_int_value(),
- bin.context.i32_type().const_zero(),
- "success",
- );
- let success_block = bin.context.append_basic_block(function, "success");
- let bail_block = bin.context.append_basic_block(function, "bail");
- bin.builder
- .build_conditional_branch(success, success_block, bail_block);
- bin.builder.position_at_end(bail_block);
- // throw division by zero error should be an assert
- target.log_runtime_error(bin, "division by zero".to_string(), Some(*loc), ns);
- target.assert_failure(
- bin,
- bin.context
- .i8_type()
- .ptr_type(AddressSpace::default())
- .const_null(),
- bin.context.i32_type().const_zero(),
- );
- bin.builder.position_at_end(success_block);
- let rem = bin.builder.build_load(rem, "urem").into_int_value();
- if bits < div_bits {
- bin.builder
- .build_int_truncate(rem, bin.context.custom_width_int_type(bits), "")
- } else {
- rem
- }
- .into()
- } else {
- bin.builder.build_int_unsigned_rem(left, right, "").into()
- }
- }
- Expression::SignedModulo(loc, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- let bits = left.get_type().get_bit_width();
- if bits > 64 {
- let div_bits = if bits <= 128 { 128 } else { 256 };
- let name = format!("sdivmod{div_bits}");
- let f = bin
- .module
- .get_function(&name)
- .expect("div function missing");
- let ty = bin.context.custom_width_int_type(div_bits);
- let dividend = bin.build_alloca(function, ty, "dividend");
- let divisor = bin.build_alloca(function, ty, "divisor");
- let rem = bin.build_alloca(function, ty, "remainder");
- let quotient = bin.build_alloca(function, ty, "quotient");
- bin.builder.build_store(
- dividend,
- if bits < div_bits {
- bin.builder.build_int_s_extend(left, ty, "")
- } else {
- left
- },
- );
- bin.builder.build_store(
- divisor,
- if bits < div_bits {
- bin.builder.build_int_s_extend(right, ty, "")
- } else {
- right
- },
- );
- let ret = bin
- .builder
- .build_call(
- f,
- &[dividend.into(), divisor.into(), rem.into(), quotient.into()],
- "sdiv",
- )
- .try_as_basic_value()
- .left()
- .unwrap();
- let success = bin.builder.build_int_compare(
- IntPredicate::EQ,
- ret.into_int_value(),
- bin.context.i32_type().const_zero(),
- "success",
- );
- let success_block = bin.context.append_basic_block(function, "success");
- let bail_block = bin.context.append_basic_block(function, "bail");
- bin.builder
- .build_conditional_branch(success, success_block, bail_block);
- bin.builder.position_at_end(bail_block);
- // throw division by zero error should be an assert
- target.log_runtime_error(bin, "division by zero".to_string(), Some(*loc), ns);
- target.assert_failure(
- bin,
- bin.context
- .i8_type()
- .ptr_type(AddressSpace::default())
- .const_null(),
- bin.context.i32_type().const_zero(),
- );
- bin.builder.position_at_end(success_block);
- let rem = bin.builder.build_load(rem, "srem").into_int_value();
- if bits < div_bits {
- bin.builder
- .build_int_truncate(rem, bin.context.custom_width_int_type(bits), "")
- } else {
- rem
- }
- .into()
- } else if ns.target == Target::Solana {
- // no signed rem on BPF; do abs udev and then negate if needed
- let left_negative = bin.builder.build_int_compare(
- IntPredicate::SLT,
- left,
- left.get_type().const_zero(),
- "left_negative",
- );
- let left = bin.builder.build_select(
- left_negative,
- bin.builder.build_int_neg(left, "signed_left"),
- left,
- "left_abs",
- );
- let right_negative = bin.builder.build_int_compare(
- IntPredicate::SLT,
- right,
- right.get_type().const_zero(),
- "right_negative",
- );
- let right = bin.builder.build_select(
- right_negative,
- bin.builder.build_int_neg(right, "signed_right"),
- right,
- "right_abs",
- );
- let res = bin.builder.build_int_unsigned_rem(
- left.into_int_value(),
- right.into_int_value(),
- "",
- );
- bin.builder.build_select(
- left_negative,
- bin.builder.build_int_neg(res, "unsigned_res"),
- res,
- "res",
- )
- } else {
- bin.builder.build_int_signed_rem(left, right, "").into()
- }
- }
- Expression::Power(loc, res_ty, unchecked, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns);
- let right = expression(target, bin, r, vartab, function, ns);
- let bits = left.into_int_value().get_type().get_bit_width();
- let o = bin.build_alloca(function, left.get_type(), "");
- let f = power(
- target,
- bin,
- *unchecked,
- bits,
- res_ty.is_signed_int(),
- o,
- ns,
- *loc,
- );
- // If the function returns zero, then the operation was successful.
- let error_return = bin
- .builder
- .build_call(f, &[left.into(), right.into(), o.into()], "power")
- .try_as_basic_value()
- .left()
- .unwrap();
- // Load the result pointer
- let res = bin.builder.build_load(o, "");
- if !bin.options.math_overflow_check || *unchecked || ns.target != Target::Solana {
- // In Substrate, overflow case will hit an unreachable expression, so no additional checks are needed.
- res
- } else {
- // In Solana, a return other than zero will abort execution. We need to check if power() returned a zero or not.
- let error_block = bin.context.append_basic_block(function, "error");
- let return_block = bin.context.append_basic_block(function, "return_block");
- let error_ret = bin.builder.build_int_compare(
- IntPredicate::NE,
- error_return.into_int_value(),
- error_return.get_type().const_zero().into_int_value(),
- "",
- );
- bin.builder
- .build_conditional_branch(error_ret, error_block, return_block);
- bin.builder.position_at_end(error_block);
- target.log_runtime_error(bin, "math overflow".to_string(), Some(*loc), ns);
- target.assert_failure(
- bin,
- bin.context
- .i8_type()
- .ptr_type(AddressSpace::default())
- .const_null(),
- bin.context.i32_type().const_zero(),
- );
- bin.builder.position_at_end(return_block);
- res
- }
- }
- Expression::Equal(_, l, r) => {
- if l.ty().is_address() {
- let mut res = bin.context.bool_type().const_int(1, false);
- let left = expression(target, bin, l, vartab, function, ns).into_array_value();
- let right = expression(target, bin, r, vartab, function, ns).into_array_value();
- // TODO: Address should be passed around as pointer. Once this is done, we can replace
- // this with a call to address_equal()
- for index in 0..ns.address_length {
- let l = bin
- .builder
- .build_extract_value(left, index as u32, "left")
- .unwrap()
- .into_int_value();
- let r = bin
- .builder
- .build_extract_value(right, index as u32, "right")
- .unwrap()
- .into_int_value();
- res = bin.builder.build_and(
- res,
- bin.builder.build_int_compare(IntPredicate::EQ, l, r, ""),
- "cmp",
- );
- }
- res.into()
- } else {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder
- .build_int_compare(IntPredicate::EQ, left, right, "")
- .into()
- }
- }
- Expression::NotEqual(_, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder
- .build_int_compare(IntPredicate::NE, left, right, "")
- .into()
- }
- Expression::SignedMore(_, l, r) | Expression::UnsignedMore(_, l, r) => {
- if l.ty().is_address() {
- compare_address(target, bin, l, r, IntPredicate::SGT, vartab, function, ns).into()
- } else {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder
- .build_int_compare(
- if matches!(e, Expression::SignedMore(..)) {
- IntPredicate::SGT
- } else {
- IntPredicate::UGT
- },
- left,
- right,
- "",
- )
- .into()
- }
- }
- Expression::MoreEqual(_, l, r) => {
- if l.ty().is_address() {
- compare_address(target, bin, l, r, IntPredicate::SGE, vartab, function, ns).into()
- } else {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder
- .build_int_compare(
- if l.ty().is_signed_int() {
- IntPredicate::SGE
- } else {
- IntPredicate::UGE
- },
- left,
- right,
- "",
- )
- .into()
- }
- }
- Expression::SignedLess(_, l, r) | Expression::UnsignedLess(_, l, r) => {
- if l.ty().is_address() {
- compare_address(target, bin, l, r, IntPredicate::SLT, vartab, function, ns).into()
- } else {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder
- .build_int_compare(
- if matches!(e, Expression::SignedLess(..)) {
- IntPredicate::SLT
- } else {
- IntPredicate::ULT
- },
- left,
- right,
- "",
- )
- .into()
- }
- }
- Expression::LessEqual(_, l, r) => {
- if l.ty().is_address() {
- compare_address(target, bin, l, r, IntPredicate::SLE, vartab, function, ns).into()
- } else {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder
- .build_int_compare(
- if l.ty().is_signed_int() {
- IntPredicate::SLE
- } else {
- IntPredicate::ULE
- },
- left,
- right,
- "",
- )
- .into()
- }
- }
- Expression::Variable(_, _, s) => vartab[s].value,
- Expression::GetRef(_, _, expr) => {
- let address = expression(target, bin, expr, vartab, function, ns).into_array_value();
- let stack = bin.build_alloca(function, address.get_type(), "address");
- bin.builder.build_store(stack, address);
- stack.into()
- }
- Expression::Load(_, ty, e) => {
- let ptr = expression(target, bin, e, vartab, function, ns).into_pointer_value();
- let value = bin.builder.build_load(ptr, "");
- if ty.is_reference_type(ns) && !ty.is_fixed_reference_type() {
- // if the pointer is null, it needs to be allocated
- let allocation_needed = bin
- .builder
- .build_is_null(value.into_pointer_value(), "allocation_needed");
- let allocate = bin.context.append_basic_block(function, "allocate");
- let already_allocated = bin
- .context
- .append_basic_block(function, "already_allocated");
- bin.builder.build_conditional_branch(
- allocation_needed,
- allocate,
- already_allocated,
- );
- let entry = bin.builder.get_insert_block().unwrap();
- bin.builder.position_at_end(allocate);
- // allocate a new struct
- let ty = e.ty();
- let llvm_ty = bin.llvm_type(ty.deref_memory(), ns);
- let new_struct = bin
- .builder
- .build_call(
- bin.module.get_function("__malloc").unwrap(),
- &[llvm_ty
- .size_of()
- .unwrap()
- .const_cast(bin.context.i32_type(), false)
- .into()],
- "",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- .into_pointer_value();
- let new_struct = bin.builder.build_pointer_cast(
- new_struct,
- llvm_ty.ptr_type(AddressSpace::default()),
- &format!("new_{}", ty.to_string(ns)),
- );
- bin.builder.build_store(ptr, new_struct);
- bin.builder.build_unconditional_branch(already_allocated);
- bin.builder.position_at_end(already_allocated);
- // insert phi node
- let combined_struct_ptr = bin.builder.build_phi(
- llvm_ty.ptr_type(AddressSpace::default()),
- &format!("ptr_{}", ty.to_string(ns)),
- );
- combined_struct_ptr.add_incoming(&[(&value, entry), (&new_struct, allocate)]);
- combined_struct_ptr.as_basic_value()
- } else {
- value
- }
- }
- Expression::ZeroExt(_, t, e) => {
- let e = expression(target, bin, e, vartab, function, ns).into_int_value();
- let ty = bin.llvm_type(t, ns);
- bin.builder
- .build_int_z_extend(e, ty.into_int_type(), "")
- .into()
- }
- Expression::UnaryMinus(_, _, e) => {
- let e = expression(target, bin, e, vartab, function, ns).into_int_value();
- bin.builder.build_int_neg(e, "").into()
- }
- Expression::SignExt(_, t, e) => {
- let e = expression(target, bin, e, vartab, function, ns).into_int_value();
- let ty = bin.llvm_type(t, ns);
- bin.builder
- .build_int_s_extend(e, ty.into_int_type(), "")
- .into()
- }
- Expression::Trunc(_, t, e) => {
- let e = expression(target, bin, e, vartab, function, ns).into_int_value();
- let ty = bin.llvm_type(t, ns);
- bin.builder
- .build_int_truncate(e, ty.into_int_type(), "")
- .into()
- }
- Expression::Cast(_, to, e) => {
- let from = e.ty();
- let e = expression(target, bin, e, vartab, function, ns);
- runtime_cast(bin, function, &from, to, e, ns)
- }
- Expression::BytesCast(_, Type::DynamicBytes, Type::Bytes(_), e) => {
- let e = expression(target, bin, e, vartab, function, ns).into_int_value();
- let size = e.get_type().get_bit_width() / 8;
- let size = bin.context.i32_type().const_int(size as u64, false);
- let elem_size = bin.context.i32_type().const_int(1, false);
- // Swap the byte order
- let bytes_ptr = bin.build_alloca(function, e.get_type(), "bytes_ptr");
- bin.builder.build_store(bytes_ptr, e);
- let bytes_ptr = bin.builder.build_pointer_cast(
- bytes_ptr,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "bytes_ptr",
- );
- let init = bin.builder.build_pointer_cast(
- bin.build_alloca(function, e.get_type(), "init"),
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "init",
- );
- bin.builder.build_call(
- bin.module.get_function("__leNtobeN").unwrap(),
- &[bytes_ptr.into(), init.into(), size.into()],
- "",
- );
- bin.builder
- .build_call(
- bin.module.get_function("vector_new").unwrap(),
- &[size.into(), elem_size.into(), init.into()],
- "",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- }
- Expression::BytesCast(loc, Type::Bytes(n), Type::DynamicBytes, e) => {
- let array = expression(target, bin, e, vartab, function, ns);
- let len = bin.vector_len(array);
- // Check if equal to n
- let is_equal_to_n = bin.builder.build_int_compare(
- IntPredicate::EQ,
- len,
- bin.context.i32_type().const_int(*n as u64, false),
- "is_equal_to_n",
- );
- let cast = bin.context.append_basic_block(function, "cast");
- let error = bin.context.append_basic_block(function, "error");
- bin.builder
- .build_conditional_branch(is_equal_to_n, cast, error);
- bin.builder.position_at_end(error);
- target.log_runtime_error(bin, "bytes cast error".to_string(), Some(*loc), ns);
- target.assert_failure(
- bin,
- bin.context
- .i8_type()
- .ptr_type(AddressSpace::default())
- .const_null(),
- bin.context.i32_type().const_zero(),
- );
- bin.builder.position_at_end(cast);
- let bytes_ptr = bin.vector_bytes(array);
- // Switch byte order
- let ty = bin.context.custom_width_int_type(*n as u32 * 8);
- let le_bytes_ptr = bin.build_alloca(function, ty, "le_bytes");
- bin.builder.build_call(
- bin.module.get_function("__beNtoleN").unwrap(),
- &[
- bytes_ptr.into(),
- bin.builder
- .build_pointer_cast(
- le_bytes_ptr,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "le_bytes_ptr",
- )
- .into(),
- len.into(),
- ],
- "",
- );
- bin.builder.build_load(le_bytes_ptr, "bytes")
- }
- Expression::Not(_, e) => {
- let e = expression(target, bin, e, vartab, function, ns).into_int_value();
- bin.builder
- .build_int_compare(IntPredicate::EQ, e, e.get_type().const_zero(), "")
- .into()
- }
- Expression::Complement(_, _, e) => {
- let e = expression(target, bin, e, vartab, function, ns).into_int_value();
- bin.builder.build_not(e, "").into()
- }
- Expression::BitwiseOr(_, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder.build_or(left, right, "").into()
- }
- Expression::BitwiseAnd(_, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder.build_and(left, right, "").into()
- }
- Expression::BitwiseXor(_, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder.build_xor(left, right, "").into()
- }
- Expression::ShiftLeft(_, _, l, r) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder.build_left_shift(left, right, "").into()
- }
- Expression::ShiftRight(_, _, l, r, signed) => {
- let left = expression(target, bin, l, vartab, function, ns).into_int_value();
- let right = expression(target, bin, r, vartab, function, ns).into_int_value();
- bin.builder
- .build_right_shift(left, right, *signed, "")
- .into()
- }
- Expression::Subscript(loc, elem_ty, ty, a, i) => {
- if ty.is_storage_bytes() {
- let index = expression(target, bin, i, vartab, function, ns).into_int_value();
- let slot = expression(target, bin, a, vartab, function, ns).into_int_value();
- target
- .get_storage_bytes_subscript(bin, function, slot, index, *loc, ns)
- .into()
- } else if ty.is_contract_storage() {
- let array = expression(target, bin, a, vartab, function, ns).into_int_value();
- let index = expression(target, bin, i, vartab, function, ns);
- target
- .storage_subscript(bin, function, ty, array, index, ns)
- .into()
- } else if elem_ty.is_builtin_struct() == Some(StructType::AccountInfo) {
- let array = expression(target, bin, a, vartab, function, ns).into_pointer_value();
- let index = expression(target, bin, i, vartab, function, ns).into_int_value();
- unsafe {
- bin.builder
- .build_gep(array, &[index], "account_info")
- .into()
- }
- } else if ty.is_dynamic_memory() {
- let array = expression(target, bin, a, vartab, function, ns);
- let ty = bin.llvm_field_ty(elem_ty, ns);
- let mut array_index =
- expression(target, bin, i, vartab, function, ns).into_int_value();
- // bounds checking already done; we can down-cast if necessary
- if array_index.get_type().get_bit_width() > 32 {
- array_index = bin.builder.build_int_truncate(
- array_index,
- bin.context.i32_type(),
- "index",
- );
- }
- let index = bin.builder.build_int_mul(
- array_index,
- ty.into_pointer_type()
- .get_element_type()
- .size_of()
- .unwrap()
- .const_cast(bin.context.i32_type(), false),
- "",
- );
- let elem = unsafe {
- bin.builder
- .build_gep(bin.vector_bytes(array), &[index], "index_access")
- };
- bin.builder
- .build_pointer_cast(elem, ty.into_pointer_type(), "elem")
- .into()
- } else {
- let array = expression(target, bin, a, vartab, function, ns).into_pointer_value();
- let index = expression(target, bin, i, vartab, function, ns).into_int_value();
- unsafe {
- bin.builder
- .build_gep(
- array,
- &[bin.context.i32_type().const_zero(), index],
- "index_access",
- )
- .into()
- }
- }
- }
- Expression::StructMember(_, _, a, _)
- if a.ty().is_builtin_struct() == Some(StructType::AccountInfo) =>
- {
- target.builtin(bin, e, vartab, function, ns)
- }
- Expression::StructMember(_, _, a, i) => {
- let struct_ptr = expression(target, bin, a, vartab, function, ns).into_pointer_value();
- bin.builder
- .build_struct_gep(struct_ptr, *i as u32, "struct member")
- .unwrap()
- .into()
- }
- Expression::ConstArrayLiteral(_, _, dims, exprs) => {
- // For const arrays (declared with "constant" keyword, we should create a global constant
- let mut dims = dims.iter();
- let exprs = exprs
- .iter()
- .map(|e| expression(target, bin, e, vartab, function, ns).into_int_value())
- .collect::<Vec<IntValue>>();
- let ty = exprs[0].get_type();
- let top_size = *dims.next().unwrap();
- // Create a vector of ArrayValues
- let mut arrays = exprs
- .chunks(top_size as usize)
- .map(|a| ty.const_array(a))
- .collect::<Vec<ArrayValue>>();
- let mut ty = ty.array_type(top_size);
- // for each dimension, split the array into futher arrays
- for d in dims {
- ty = ty.array_type(*d);
- arrays = arrays
- .chunks(*d as usize)
- .map(|a| ty.const_array(a))
- .collect::<Vec<ArrayValue>>();
- }
- // We actually end up with an array with a single entry
- // now we've created the type, and the const array. Put it into a global
- let gv =
- bin.module
- .add_global(ty, Some(AddressSpace::default()), "const_array_literal");
- gv.set_linkage(Linkage::Internal);
- gv.set_initializer(&arrays[0]);
- gv.set_constant(true);
- gv.as_pointer_value().into()
- }
- Expression::ArrayLiteral(_, ty, dims, exprs) => {
- // non-const array literals should alloca'ed and each element assigned
- let ty = bin.llvm_type(ty, ns);
- let p = bin
- .builder
- .build_call(
- bin.module.get_function("__malloc").unwrap(),
- &[ty.size_of()
- .unwrap()
- .const_cast(bin.context.i32_type(), false)
- .into()],
- "array_literal",
- )
- .try_as_basic_value()
- .left()
- .unwrap();
- let array = bin.builder.build_pointer_cast(
- p.into_pointer_value(),
- ty.ptr_type(AddressSpace::default()),
- "array_literal",
- );
- for (i, expr) in exprs.iter().enumerate() {
- let mut ind = vec![bin.context.i32_type().const_zero()];
- let mut e = i as u32;
- for d in dims {
- ind.insert(1, bin.context.i32_type().const_int((e % *d).into(), false));
- e /= *d;
- }
- let elemptr = unsafe { bin.builder.build_gep(array, &ind, &format!("elemptr{i}")) };
- let elem = expression(target, bin, expr, vartab, function, ns);
- let elem = if expr.ty().is_fixed_reference_type() {
- bin.builder.build_load(elem.into_pointer_value(), "elem")
- } else {
- elem
- };
- bin.builder.build_store(elemptr, elem);
- }
- array.into()
- }
- Expression::AllocDynamicBytes(_, ty, size, init) => {
- if matches!(ty, Type::Slice(_)) {
- let init = init.as_ref().unwrap();
- let data = bin.emit_global_string("const_string", init, true);
- bin.llvm_type(ty, ns)
- .into_struct_type()
- .const_named_struct(&[
- data.into(),
- bin.context
- .custom_width_int_type(ns.target.ptr_size().into())
- .const_int(init.len() as u64, false)
- .into(),
- ])
- .into()
- } else {
- let elem = match ty {
- Type::Slice(_) | Type::String | Type::DynamicBytes => Type::Bytes(1),
- _ => ty.array_elem(),
- };
- let size = expression(target, bin, size, vartab, function, ns).into_int_value();
- let elem_size = bin
- .llvm_type(&elem, ns)
- .size_of()
- .unwrap()
- .const_cast(bin.context.i32_type(), false);
- bin.vector_new(size, elem_size, init.as_ref()).into()
- }
- }
- Expression::Builtin(_, _, Builtin::ArrayLength, args)
- if args[0].ty().array_deref().is_builtin_struct().is_none() =>
- {
- let array = expression(target, bin, &args[0], vartab, function, ns);
- bin.vector_len(array).into()
- }
- Expression::Builtin(_, returns, Builtin::ReadFromBuffer, args) => {
- let v = expression(target, bin, &args[0], vartab, function, ns);
- let offset = expression(target, bin, &args[1], vartab, function, ns).into_int_value();
- let data = if args[0].ty().is_dynamic_memory() {
- bin.vector_bytes(v)
- } else {
- v.into_pointer_value()
- };
- let start = unsafe { bin.builder.build_gep(data, &[offset], "start") };
- if matches!(returns[0], Type::Bytes(_) | Type::FunctionSelector) {
- let n = returns[0].bytes(ns);
- let store = bin.build_alloca(
- function,
- bin.context.custom_width_int_type(n as u32 * 8),
- "stack",
- );
- bin.builder.build_call(
- bin.module.get_function("__beNtoleN").unwrap(),
- &[
- bin.builder
- .build_pointer_cast(
- start,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "",
- )
- .into(),
- bin.builder
- .build_pointer_cast(
- store,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "",
- )
- .into(),
- bin.context.i32_type().const_int(n as u64, false).into(),
- ],
- "",
- );
- bin.builder.build_load(store, &format!("bytes{n}"))
- } else {
- let start = bin.builder.build_pointer_cast(
- start,
- bin.llvm_type(&returns[0], ns)
- .ptr_type(AddressSpace::default()),
- "start",
- );
- bin.builder.build_load(start, "value")
- }
- }
- Expression::Keccak256(_, _, exprs) => {
- let mut length = bin.context.i32_type().const_zero();
- let mut values: Vec<(BasicValueEnum, IntValue, Type)> = Vec::new();
- // first we need to calculate the length of the buffer and get the types/lengths
- for e in exprs {
- let v = expression(target, bin, e, vartab, function, ns);
- let len = match e.ty() {
- Type::DynamicBytes | Type::String => bin.vector_len(v),
- _ => v
- .get_type()
- .size_of()
- .unwrap()
- .const_cast(bin.context.i32_type(), false),
- };
- length = bin.builder.build_int_add(length, len, "");
- values.push((v, len, e.ty()));
- }
- // now allocate a buffer
- let src = bin
- .builder
- .build_array_alloca(bin.context.i8_type(), length, "keccak_src");
- // fill in all the fields
- let mut offset = bin.context.i32_type().const_zero();
- for (v, len, ty) in values {
- let elem = unsafe { bin.builder.build_gep(src, &[offset], "elem") };
- offset = bin.builder.build_int_add(offset, len, "");
- match ty {
- Type::DynamicBytes | Type::String => {
- let data = bin.vector_bytes(v);
- bin.builder.build_call(
- bin.module.get_function("__memcpy").unwrap(),
- &[
- elem.into(),
- bin.builder
- .build_pointer_cast(
- data,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "data",
- )
- .into(),
- len.into(),
- ],
- "",
- );
- }
- _ => {
- let elem = bin.builder.build_pointer_cast(
- elem,
- v.get_type().ptr_type(AddressSpace::default()),
- "",
- );
- bin.builder.build_store(elem, v);
- }
- }
- }
- let dst = bin
- .builder
- .build_alloca(bin.context.custom_width_int_type(256), "keccak_dst");
- target.keccak256_hash(bin, src, length, dst, ns);
- bin.builder.build_load(dst, "keccak256_hash")
- }
- Expression::StringCompare(_, l, r) => {
- let (left, left_len) = string_location(target, bin, l, vartab, function, ns);
- let (right, right_len) = string_location(target, bin, r, vartab, function, ns);
- bin.builder
- .build_call(
- bin.module.get_function("__memcmp").unwrap(),
- &[left.into(), left_len.into(), right.into(), right_len.into()],
- "",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- }
- Expression::StringConcat(_, _, l, r) => {
- let (left, left_len) = string_location(target, bin, l, vartab, function, ns);
- let (right, right_len) = string_location(target, bin, r, vartab, function, ns);
- bin.builder
- .build_call(
- bin.module.get_function("concat").unwrap(),
- &[left.into(), left_len.into(), right.into(), right_len.into()],
- "",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- }
- Expression::ReturnData(_) => target.return_data(bin, function).into(),
- Expression::StorageArrayLength { array, elem_ty, .. } => {
- let slot = expression(target, bin, array, vartab, function, ns).into_int_value();
- target
- .storage_array_length(bin, function, slot, elem_ty, ns)
- .into()
- }
- Expression::AbiEncode {
- tys, packed, args, ..
- } => target
- .abi_encode_to_vector(
- bin,
- function,
- &packed
- .iter()
- .map(|a| expression(target, bin, a, vartab, function, ns))
- .collect::<Vec<BasicValueEnum>>(),
- &args
- .iter()
- .map(|a| expression(target, bin, a, vartab, function, ns))
- .collect::<Vec<BasicValueEnum>>(),
- tys,
- ns,
- )
- .into(),
- Expression::Builtin(_, _, Builtin::Signature, _) if ns.target != Target::Solana => {
- // need to byte-reverse selector
- let selector = bin.build_alloca(function, bin.context.i32_type(), "selector");
- // byte order needs to be reversed. e.g. hex"11223344" should be 0x10 0x11 0x22 0x33 0x44
- bin.builder.build_call(
- bin.module.get_function("__beNtoleN").unwrap(),
- &[
- bin.builder
- .build_pointer_cast(
- bin.selector.as_pointer_value(),
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "",
- )
- .into(),
- bin.builder
- .build_pointer_cast(
- selector,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "",
- )
- .into(),
- bin.context.i32_type().const_int(4, false).into(),
- ],
- "",
- );
- bin.builder.build_load(selector, "selector")
- }
- Expression::Builtin(_, _, Builtin::AddMod, args) => {
- let arith_ty = bin.context.custom_width_int_type(512);
- let res_ty = bin.context.custom_width_int_type(256);
- let x = expression(target, bin, &args[0], vartab, function, ns).into_int_value();
- let y = expression(target, bin, &args[1], vartab, function, ns).into_int_value();
- let k = expression(target, bin, &args[2], vartab, function, ns).into_int_value();
- let dividend = bin.builder.build_int_add(
- bin.builder.build_int_z_extend(x, arith_ty, "wide_x"),
- bin.builder.build_int_z_extend(y, arith_ty, "wide_y"),
- "x_plus_y",
- );
- let divisor = bin.builder.build_int_z_extend(k, arith_ty, "wide_k");
- let pdividend = bin.build_alloca(function, arith_ty, "dividend");
- let pdivisor = bin.build_alloca(function, arith_ty, "divisor");
- let rem = bin.build_alloca(function, arith_ty, "remainder");
- let quotient = bin.build_alloca(function, arith_ty, "quotient");
- bin.builder.build_store(pdividend, dividend);
- bin.builder.build_store(pdivisor, divisor);
- let ret = bin
- .builder
- .build_call(
- bin.module.get_function("udivmod512").unwrap(),
- &[
- pdividend.into(),
- pdivisor.into(),
- rem.into(),
- quotient.into(),
- ],
- "quotient",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- .into_int_value();
- let success = bin.builder.build_int_compare(
- IntPredicate::EQ,
- ret,
- bin.context.i32_type().const_zero(),
- "success",
- );
- let success_block = bin.context.append_basic_block(function, "success");
- let bail_block = bin.context.append_basic_block(function, "bail");
- bin.builder
- .build_conditional_branch(success, success_block, bail_block);
- bin.builder.position_at_end(bail_block);
- // On Solana the return type is 64 bit
- let ret: BasicValueEnum = bin
- .builder
- .build_int_z_extend(
- ret,
- bin.return_values[&ReturnCode::Success].get_type(),
- "ret",
- )
- .into();
- bin.builder.build_return(Some(&ret));
- bin.builder.position_at_end(success_block);
- let remainder = bin.builder.build_load(rem, "remainder").into_int_value();
- bin.builder
- .build_int_truncate(remainder, res_ty, "quotient")
- .into()
- }
- Expression::Builtin(_, _, Builtin::MulMod, args) => {
- let arith_ty = bin.context.custom_width_int_type(512);
- let res_ty = bin.context.custom_width_int_type(256);
- let x = expression(target, bin, &args[0], vartab, function, ns).into_int_value();
- let y = expression(target, bin, &args[1], vartab, function, ns).into_int_value();
- let x_m = bin.build_alloca(function, arith_ty, "x_m");
- let y_m = bin.build_alloca(function, arith_ty, "x_y");
- let x_times_y_m = bin.build_alloca(function, arith_ty, "x_times_y_m");
- bin.builder
- .build_store(x_m, bin.builder.build_int_z_extend(x, arith_ty, "wide_x"));
- bin.builder
- .build_store(y_m, bin.builder.build_int_z_extend(y, arith_ty, "wide_y"));
- bin.builder.build_call(
- bin.module.get_function("__mul32").unwrap(),
- &[
- bin.builder
- .build_pointer_cast(
- x_m,
- bin.context.i32_type().ptr_type(AddressSpace::default()),
- "left",
- )
- .into(),
- bin.builder
- .build_pointer_cast(
- y_m,
- bin.context.i32_type().ptr_type(AddressSpace::default()),
- "right",
- )
- .into(),
- bin.builder
- .build_pointer_cast(
- x_times_y_m,
- bin.context.i32_type().ptr_type(AddressSpace::default()),
- "output",
- )
- .into(),
- bin.context.i32_type().const_int(512 / 32, false).into(),
- ],
- "",
- );
- let k = expression(target, bin, &args[2], vartab, function, ns).into_int_value();
- let dividend = bin.builder.build_load(x_times_y_m, "x_t_y");
- let divisor = bin.builder.build_int_z_extend(k, arith_ty, "wide_k");
- let pdividend = bin.build_alloca(function, arith_ty, "dividend");
- let pdivisor = bin.build_alloca(function, arith_ty, "divisor");
- let rem = bin.build_alloca(function, arith_ty, "remainder");
- let quotient = bin.build_alloca(function, arith_ty, "quotient");
- bin.builder.build_store(pdividend, dividend);
- bin.builder.build_store(pdivisor, divisor);
- let ret = bin
- .builder
- .build_call(
- bin.module.get_function("udivmod512").unwrap(),
- &[
- pdividend.into(),
- pdivisor.into(),
- rem.into(),
- quotient.into(),
- ],
- "quotient",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- .into_int_value();
- let success = bin.builder.build_int_compare(
- IntPredicate::EQ,
- ret,
- bin.context.i32_type().const_zero(),
- "success",
- );
- let success_block = bin.context.append_basic_block(function, "success");
- let bail_block = bin.context.append_basic_block(function, "bail");
- bin.builder
- .build_conditional_branch(success, success_block, bail_block);
- bin.builder.position_at_end(bail_block);
- // On Solana the return type is 64 bit
- let ret: BasicValueEnum = bin
- .builder
- .build_int_z_extend(
- ret,
- bin.return_values[&ReturnCode::Success].get_type(),
- "ret",
- )
- .into();
- bin.builder.build_return(Some(&ret));
- bin.builder.position_at_end(success_block);
- let remainder = bin.builder.build_load(rem, "quotient").into_int_value();
- bin.builder
- .build_int_truncate(remainder, res_ty, "quotient")
- .into()
- }
- Expression::Builtin(_, _, hash @ Builtin::Ripemd160, args)
- | Expression::Builtin(_, _, hash @ Builtin::Keccak256, args)
- | Expression::Builtin(_, _, hash @ Builtin::Blake2_128, args)
- | Expression::Builtin(_, _, hash @ Builtin::Blake2_256, args)
- | Expression::Builtin(_, _, hash @ Builtin::Sha256, args) => {
- let v = expression(target, bin, &args[0], vartab, function, ns);
- let hash = match hash {
- Builtin::Ripemd160 => HashTy::Ripemd160,
- Builtin::Sha256 => HashTy::Sha256,
- Builtin::Keccak256 => HashTy::Keccak256,
- Builtin::Blake2_128 => HashTy::Blake2_128,
- Builtin::Blake2_256 => HashTy::Blake2_256,
- _ => unreachable!(),
- };
- target
- .hash(
- bin,
- function,
- hash,
- bin.vector_bytes(v),
- bin.vector_len(v),
- ns,
- )
- .into()
- }
- Expression::Builtin(..) => target.builtin(bin, e, vartab, function, ns),
- Expression::InternalFunctionCfg(cfg_no) => bin.functions[cfg_no]
- .as_global_value()
- .as_pointer_value()
- .into(),
- Expression::FormatString(_, args) => format_string(target, bin, args, vartab, function, ns),
- Expression::AdvancePointer {
- pointer,
- bytes_offset,
- } => {
- let pointer = if pointer.ty().is_dynamic_memory() {
- bin.vector_bytes(expression(target, bin, pointer, vartab, function, ns))
- } else {
- expression(target, bin, pointer, vartab, function, ns).into_pointer_value()
- };
- let offset =
- expression(target, bin, bytes_offset, vartab, function, ns).into_int_value();
- let advanced = unsafe { bin.builder.build_gep(pointer, &[offset], "adv_pointer") };
- advanced.into()
- }
- Expression::RationalNumberLiteral(..)
- | Expression::List(..)
- | Expression::Undefined(..)
- | Expression::Poison
- | Expression::BytesCast(..) => {
- unreachable!("should not exist in cfg")
- }
- }
- }
- pub(super) fn compare_address<'a, T: TargetRuntime<'a> + ?Sized>(
- target: &T,
- binary: &Binary<'a>,
- left: &Expression,
- right: &Expression,
- op: inkwell::IntPredicate,
- vartab: &HashMap<usize, Variable<'a>>,
- function: FunctionValue<'a>,
- ns: &Namespace,
- ) -> IntValue<'a> {
- let l = expression(target, binary, left, vartab, function, ns).into_array_value();
- let r = expression(target, binary, right, vartab, function, ns).into_array_value();
- let left = binary.build_alloca(function, binary.address_type(ns), "left");
- let right = binary.build_alloca(function, binary.address_type(ns), "right");
- binary.builder.build_store(left, l);
- binary.builder.build_store(right, r);
- let res = binary
- .builder
- .build_call(
- binary.module.get_function("__memcmp_ord").unwrap(),
- &[
- binary
- .builder
- .build_pointer_cast(
- left,
- binary.context.i8_type().ptr_type(AddressSpace::default()),
- "left",
- )
- .into(),
- binary
- .builder
- .build_pointer_cast(
- right,
- binary.context.i8_type().ptr_type(AddressSpace::default()),
- "right",
- )
- .into(),
- binary
- .context
- .i32_type()
- .const_int(ns.address_length as u64, false)
- .into(),
- ],
- "",
- )
- .try_as_basic_value()
- .left()
- .unwrap()
- .into_int_value();
- binary
- .builder
- .build_int_compare(op, res, binary.context.i32_type().const_zero(), "")
- }
- fn runtime_cast<'a>(
- bin: &Binary<'a>,
- function: FunctionValue<'a>,
- from: &Type,
- to: &Type,
- val: BasicValueEnum<'a>,
- ns: &Namespace,
- ) -> BasicValueEnum<'a> {
- if matches!(from, Type::Address(_) | Type::Contract(_))
- && matches!(to, Type::Address(_) | Type::Contract(_))
- {
- // no conversion needed
- val
- } else if let Type::Address(_) = to {
- let llvm_ty = bin.llvm_type(from, ns);
- let src = bin.build_alloca(function, llvm_ty, "dest");
- bin.builder.build_store(src, val.into_int_value());
- let dest = bin.build_alloca(function, bin.address_type(ns), "address");
- let len = bin
- .context
- .i32_type()
- .const_int(ns.address_length as u64, false);
- bin.builder.build_call(
- bin.module.get_function("__leNtobeN").unwrap(),
- &[
- bin.builder
- .build_pointer_cast(
- src,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "address_ptr",
- )
- .into(),
- bin.builder
- .build_pointer_cast(
- dest,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "dest_ptr",
- )
- .into(),
- len.into(),
- ],
- "",
- );
- bin.builder.build_load(dest, "val")
- } else if let Type::Address(_) = from {
- let llvm_ty = bin.llvm_type(to, ns);
- let src = bin.build_alloca(function, bin.address_type(ns), "address");
- bin.builder.build_store(src, val.into_array_value());
- let dest = bin.build_alloca(function, llvm_ty, "dest");
- let len = bin
- .context
- .i32_type()
- .const_int(ns.address_length as u64, false);
- bin.builder.build_call(
- bin.module.get_function("__beNtoleN").unwrap(),
- &[
- bin.builder
- .build_pointer_cast(
- src,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "address_ptr",
- )
- .into(),
- bin.builder
- .build_pointer_cast(
- dest,
- bin.context.i8_type().ptr_type(AddressSpace::default()),
- "dest_ptr",
- )
- .into(),
- len.into(),
- ],
- "",
- );
- bin.builder.build_load(dest, "val")
- } else if matches!(from, Type::Bool) && matches!(to, Type::Int(_) | Type::Uint(_)) {
- bin.builder
- .build_int_cast(
- val.into_int_value(),
- bin.llvm_type(to, ns).into_int_type(),
- "bool_to_int_cast",
- )
- .into()
- } else if !from.is_contract_storage()
- && from.is_reference_type(ns)
- && matches!(to, Type::Uint(_))
- {
- bin.builder
- .build_ptr_to_int(
- val.into_pointer_value(),
- bin.llvm_type(to, ns).into_int_type(),
- "ptr_to_int",
- )
- .into()
- } else if to.is_reference_type(ns) && matches!(from, Type::Uint(_)) {
- bin.builder
- .build_int_to_ptr(
- val.into_int_value(),
- bin.llvm_type(to, ns).ptr_type(AddressSpace::default()),
- "int_to_ptr",
- )
- .into()
- } else if matches!((from, to), (Type::DynamicBytes, Type::Slice(_))) {
- let slice = bin.build_alloca(function, bin.llvm_type(to, ns), "slice");
- let data = bin.vector_bytes(val);
- let data_ptr = bin.builder.build_struct_gep(slice, 0, "data").unwrap();
- bin.builder.build_store(data_ptr, data);
- let len =
- bin.builder
- .build_int_z_extend(bin.vector_len(val), bin.context.i64_type(), "len");
- let len_ptr = bin.builder.build_struct_gep(slice, 1, "len").unwrap();
- bin.builder.build_store(len_ptr, len);
- bin.builder.build_load(slice, "slice")
- } else {
- val
- }
- }
- pub(crate) fn string_to_basic_value<'a>(
- bin: &Binary<'a>,
- ns: &Namespace,
- input: String,
- ) -> BasicValueEnum<'a> {
- let elem = Type::Bytes(1);
- let size = bin.context.i32_type().const_int(input.len() as u64, false);
- let elem_size = bin
- .llvm_type(&elem, ns)
- .size_of()
- .unwrap()
- .const_cast(bin.context.i32_type(), false);
- let init = Option::Some(input.as_bytes().to_vec());
- bin.vector_new(size, elem_size, init.as_ref()).into()
- }
|