123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330(******************************************************************************)(* ASLRef *)(******************************************************************************)(*
* SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
* SPDX-License-Identifier: BSD-3-Clause
*)(******************************************************************************)(* Disclaimer: *)(* This material covers both ASLv0 (viz, the existing ASL pseudocode language *)(* which appears in the Arm Architecture Reference Manual) and ASLv1, a new, *)(* experimental, and as yet unreleased version of ASL. *)(* This material is work in progress, more precisely at pre-Alpha quality as *)(* per Arm’s quality standards. *)(* In particular, this means that it would be premature to base any *)(* production tool development on this material. *)(* However, any feedback, question, query and feature request would be most *)(* welcome; those can be sent to Arm’s Architecture Formal Team Lead *)(* Jade Alglave <jade.alglave@arm.com>, or by raising issues or PRs to the *)(* herdtools7 github repository. *)(******************************************************************************)openASTopenPrintftypebuffer=Buffer.ttype'aprinter=buffer->'a->unitletaddbbufs=Buffer.add_stringbufsletwith_buff=(* Same default value as Stdlib.Printf *)letb=Buffer.create64inlet()=fbinBuffer.contentsbletpp_listpp_eltbuf=letpp_elt_with_sepelt=addbbuf"; ";pp_eltbufeltinfunction|[]->addbbuf"[]"|h::t->addbbuf"[";pp_eltbufh;List.iterpp_elt_with_sept;addbbuf"]"letpp_optionpp_somebuf=function|None->addbbuf"None"|Someelt->bprintfbuf"Some (%a)"pp_someeltletpp_pairpp_leftpp_rightf(left,right)=bprintff"(%a, %a)"pp_leftleftpp_rightrightletpp_pair_listpp_leftpp_right=pp_list(pp_pairpp_leftpp_right)letpp_stringf=bprintff"%S"letpp_id_assocpp_elt=pp_pair_listpp_stringpp_eltletpp_annotatedfbuf{desc;_}=bprintfbuf"annot (%a)"fdescletpp_binop:binop->string=function|AND->"AND"|BAND->"BAND"|BEQ->"BEQ"|BOR->"BOR"|DIV->"DIV"|DIVRM->"DIVRM"|EOR->"EOR"|EQ_OP->"EQ_OP"|GT->"GT"|GEQ->"GEQ"|IMPL->"IMPL"|LT->"LT"|LEQ->"LEQ"|MOD->"MOD"|MINUS->"MINUS"|MUL->"MUL"|NEQ->"NEQ"|OR->"OR"|PLUS->"PLUS"|RDIV->"RDIV"|SHL->"SHL"|SHR->"SHR"|POW->"POW"letpp_unop=functionBNOT->"BNOT"|NOT->"NOT"|NEG->"NEG"letpp_literalf=function|L_Inti->bprintff"V_Int (Z.of_string \"%a\")"Z.bprinti|L_Boolb->bprintff"V_Bool %B"b|L_Realr->bprintff"V_Real (Q.of_string \"%a\")"Q.bprintr|L_BitVectorbv->bprintff"V_BitVector (Bitvector.of_string %S)"(Bitvector.to_stringbv)|L_Strings->bprintff"V_String %S"sletrecpp_expr=letpp_descf=function|E_Literalv->bprintff"E_Literal (%a)"pp_literalv|E_Varx->bprintff"E_Var %S"x|E_CTC(e,t)->bprintff"E_CTC (%a, %a)"pp_exprepp_tyt|E_Binop(op,e1,e2)->bprintff"E_Binop (%s, %a, %a)"(pp_binopop)pp_expre1pp_expre2|E_Unop(op,e)->bprintff"E_Unop (%s, %a)"(pp_unopop)pp_expre|E_Call(name,args,named_args)->bprintff"E_Call (%S, %a, %a)"namepp_expr_listargs(pp_id_assocpp_expr)named_args|E_Slice(e,args)->bprintff"E_Slice (%a, %a)"pp_exprepp_slice_listargs|E_Cond(e1,e2,e3)->bprintff"E_Cond (%a, %a, %a)"pp_expre1pp_expre2pp_expre3|E_GetArray(e1,e2)->bprintff"E_GetArray (%a, %a)"pp_expre1pp_expre2|E_GetField(e,x)->bprintff"E_GetField (%a, %S)"pp_exprex|E_GetFields(e,x)->bprintff"E_GetFields (%a, %a)"pp_expre(pp_listpp_string)x|E_Record(ty,li)->bprintff"E_Record (%a, %a)"pp_tyty(pp_id_assocpp_expr)li|E_Concates->addbf"E_Concat ";pp_listpp_exprfes|E_Tuplees->addbf"E_Tuple ";pp_expr_listfes|E_Unknownty->bprintff"E_Unknown (%a)"pp_tyty|E_Pattern(e,p)->bprintff"E_Pattern (%a, %a)"pp_exprepp_patternpinfunfe->pp_annotatedpp_descfeandpp_expr_listf=pp_listpp_exprfandpp_slice_listf=pp_listpp_slicefandpp_slicef=function|Slice_Singlee->bprintff"Slice_Single (%a)"pp_expre|Slice_Range(e1,e2)->bprintff"Slice_Range (%a, %a)"pp_expre1pp_expre2|Slice_Length(e1,e2)->bprintff"Slice_Length (%a, %a)"pp_expre1pp_expre2|Slice_Star(e1,e2)->bprintff"Slice_Star (%a, %a)"pp_expre1pp_expre2andpp_patternf=function|Pattern_All->addbf"Pattern_All"|Pattern_Anyli->addbf"Pattern_Any ";pp_listpp_patternfli|Pattern_Geqe->bprintff"Pattern_Geq (%a)"pp_expre|Pattern_Leqe->bprintff"Pattern_Leq (%a)"pp_expre|Pattern_Maskm->bprintff"Pattern_Mask (Bitvector.mask_of_string \"%S\")"(Bitvector.mask_to_canonical_stringm)|Pattern_Notp->bprintff"Pattern_Not (%a)"pp_patternp|Pattern_Range(e1,e2)->bprintff"Pattern_Range (%a, %a)"pp_expre1pp_expre2|Pattern_Singlee->bprintff"Pattern_Single (%a)"pp_expre|Pattern_Tupleli->addbf"Pattern_Tuple ";pp_listpp_patternfliandpp_ty=letpp_descf=function|T_Intcs->bprintff"T_Int (%a)"pp_int_constraintscs|T_Real->addbf"T_Real"|T_String->addbf"T_String"|T_Bool->addbf"T_Bool"|T_Bits(bits_constraint,fields)->bprintff"T_Bits (%a, %a)"pp_exprbits_constraintpp_bitfieldsfields|T_Enumenum_type_desc->addbf"T_Enum ";pp_listpp_stringfenum_type_desc|T_Tupleli->addbf"T_Tuple ";pp_listpp_tyfli|T_Array(length,elt_type)->bprintff"T_Array (%a, %a)"pp_array_lengthlengthpp_tyelt_type|T_Recordli->addbf"T_Record ";pp_id_assocpp_tyfli|T_Exceptionli->addbf"T_Exception ";pp_id_assocpp_tyfli|T_Namedidentifier->bprintff"T_Named %S"identifierinfunfs->pp_annotatedpp_descfsandpp_array_lengthf=function|ArrayLength_Expre->bprintff"ArrayLength_Expr (%a)"pp_expre|ArrayLength_Enum(s,i)->bprintff"ArrayLength_Enum (%S, %i)"siandpp_bitfieldf=function|BitField_Simple(name,slices)->bprintff"BitField_Simple (%S, %a)"namepp_slice_listslices|BitField_Nested(name,slices,bitfields)->bprintff"BitField_Nested (%S, %a, %a)"namepp_slice_listslicespp_bitfieldsbitfields|BitField_Type(name,slices,ty)->bprintff"BitField_Type (%S, %a, %a)"namepp_slice_listslicespp_tytyandpp_bitfieldsfbitfields=pp_listpp_bitfieldfbitfieldsandpp_int_constraintf=function|Constraint_Exacte->bprintff"Constraint_Exact (%a)"pp_expre|Constraint_Range(bot,top)->bprintff"Constraint_Range (%a, %a)"pp_exprbotpp_exprtopandpp_int_constraintsf=function|UnConstrained->addbf"UnConstrained"|WellConstrainedcs->addbf"WellConstrained ";pp_listpp_int_constraintfcs|UnderConstrained(i,x)->bprintff"UnderConstrained (%d, %S)"ixletrecpp_lexpr=letpp_descf=function|LE_Varx->bprintff"LE_Var %S"x|LE_Slice(le,args)->bprintff"LE_Slice (%a, %a)"pp_lexprlepp_slice_listargs|LE_SetArray(le,e)->bprintff"LE_SetArray (%a, %a)"pp_lexprlepp_expre|LE_SetField(le,x)->bprintff"LE_SetField (%a, %S)"pp_lexprlex|LE_SetFields(le,x)->bprintff"LE_SetFields (%a, %a)"pp_lexprle(pp_listpp_string)x|LE_Discard->addbf"LE_Discard"|LE_Destructuringles->addbf"LE_Destructuring ";pp_listpp_lexprfles|LE_Concat(les,_)->bprintff"LE_Concat (%a, None)"(pp_listpp_lexpr)lesinfunfle->pp_annotatedpp_descfleletpp_local_decl_keyboardfk=pp_stringf(matchkwith|LDK_Var->"LDK_Var"|LDK_Constant->"LDK_Constant"|LDK_Let->"LDK_Let")letrecpp_local_decl_itemf=function|LDI_Discard->addbf"LDI_Discard"|LDI_Vars->bprintff"LDI_Var %S"s|LDI_Typed(ldi,t)->bprintff"LDI_Typed (%a, %a)"pp_local_decl_itemldipp_tyt|LDI_Tupleldis->bprintff"LDI_Tuple %a"(pp_listpp_local_decl_item)ldisletrecpp_stmt=letpp_descf=function|S_Pass->addbf"SPass"|S_Seq(s1,s2)->bprintff"S_Seq (%a, %a)"pp_stmts1pp_stmts2|S_Assign(le,e,_v)->bprintff"S_Assign (%a, %a)"pp_lexprlepp_expre|S_Call(name,args,named_args)->bprintff"S_Call (%S, %a, %a)"namepp_expr_listargs(pp_id_assocpp_expr)named_args|S_Cond(e,s1,s2)->bprintff"S_Cond (%a, %a, %a)"pp_exprepp_stmts1pp_stmts2|S_Returne->bprintff"S_Return (%a)"(pp_optionpp_expr)e|S_Case(e,cases)->bprintff"S_Case (%a, %a)"pp_expre(pp_list(pp_annotated(pp_pairpp_patternpp_stmt)))cases|S_Asserte->bprintff"S_Assert (%a)"pp_expre|S_While(e,s)->bprintff"S_While(%a, %a)"pp_exprepp_stmts|S_Repeat(s,e)->bprintff"S_Repeat(%a, %a)"pp_stmtspp_expre|S_For(id,e1,dir,e2,s)->bprintff"S_For (%S, %a, %s, %a, %a)"idpp_expre1(matchdirwithUp->"Up"|Down->"Down")pp_expre2pp_stmts|S_Decl(ldk,ldi,e_opt)->bprintff"S_Decl (%a, %a, %a)"pp_local_decl_keyboardldkpp_local_decl_itemldi(pp_optionpp_expr)e_opt|S_Throwopt->bprintff"S_Throw (%a)"(pp_option(pp_pairpp_expr(pp_optionpp_ty)))opt|S_Try(s,catchers,otherwise)->bprintff"S_Try (%a, %a, %a)"pp_stmts(pp_listpp_catcher)catchers(pp_optionpp_stmt)otherwise|S_Print{args;debug}->bprintff"S_Print { args = %a; debug = %B }"(pp_listpp_expr)argsdebuginfunfs->pp_annotatedpp_descfsandpp_catcherf(name,ty,s)=bprintff"(%a, %a, %a)"(pp_optionpp_string)namepp_tytypp_stmtsletpp_gdkfgdk=addbf@@matchgdkwith|GDK_Config->"GDK_Config"|GDK_Constant->"GDK_Constant"|GDK_Let->"GDK_Let"|GDK_Var->"GDK_Var"letpp_subprogram_typefst=addbf(matchstwith|ST_Function->"ST_Function"|ST_Procedure->"ST_Procedure"|ST_Setter->"ST_Setter"|ST_Getter->"ST_Getter")letpp_bodyf=function|SB_ASLs->bprintff"SB_ASL (%a)"pp_stmts|SB_Primitive->addbf"SB_Primitive"letpp_declfd=matchd.descwith|D_Func{name;args;body;return_type;parameters;subprogram_type}->bprintff"D_Func { name=%S; args=%a; body=%a; return_type=%a; parameters=%a; \
subprogram_type=%a }"name(pp_id_assocpp_ty)argspp_bodybody(pp_optionpp_ty)return_type(pp_list(pp_pairpp_string(pp_optionpp_ty)))parameterspp_subprogram_typesubprogram_type|D_GlobalStorage{name;keyword;ty;initial_value}->bprintff"D_GlobalConst { name=%S; keyword=%a; ty=%a; initial_value=%a}"namepp_gdkkeyword(pp_optionpp_ty)ty(pp_optionpp_expr)initial_value|D_TypeDecl(name,type_desc,subty_opt)->bprintff"D_TypeDecl (%S, %a, %a)"namepp_tytype_desc(pp_option(pp_pairpp_string(pp_id_assocpp_ty)))subty_optletpp_tfast=addbf"let open AST in let annot = ASTUtils.add_dummy_pos in ";pp_listpp_declfastlett_to_stringast=with_buf@@funb->pp_tbast