1- use cranelift_codegen:: ir;
21use serde:: { Deserialize , Serialize } ;
3- use std:: convert:: TryFrom ;
42use std:: fmt:: { Display , Formatter } ;
53
64#[ derive( Clone , Copy , Debug , PartialEq , Serialize , Deserialize ) ]
@@ -22,46 +20,6 @@ impl Display for ValueType {
2220 }
2321}
2422
25- #[ derive( Debug ) ]
26- pub enum ValueError {
27- Unrepresentable ,
28- InvalidVMContext ,
29- }
30-
31- impl TryFrom < & ir:: AbiParam > for ValueType {
32- type Error = ValueError ;
33-
34- fn try_from ( value : & ir:: AbiParam ) -> Result < Self , Self :: Error > {
35- match value {
36- ir:: AbiParam {
37- value_type : cranelift_ty,
38- purpose : ir:: ArgumentPurpose :: Normal ,
39- extension : ir:: ArgumentExtension :: None ,
40- location : ir:: ArgumentLoc :: Unassigned ,
41- } => {
42- let size = cranelift_ty. bits ( ) ;
43-
44- if cranelift_ty. is_int ( ) {
45- match size {
46- 32 => Ok ( ValueType :: I32 ) ,
47- 64 => Ok ( ValueType :: I64 ) ,
48- _ => Err ( ValueError :: Unrepresentable ) ,
49- }
50- } else if cranelift_ty. is_float ( ) {
51- match size {
52- 32 => Ok ( ValueType :: F32 ) ,
53- 64 => Ok ( ValueType :: F64 ) ,
54- _ => Err ( ValueError :: Unrepresentable ) ,
55- }
56- } else {
57- Err ( ValueError :: Unrepresentable )
58- }
59- }
60- _ => Err ( ValueError :: Unrepresentable ) ,
61- }
62- }
63- }
64-
6523/// A signature for a function in a wasm module.
6624///
6725/// Note that this does not explicitly name VMContext as a parameter! It is assumed that all wasm
@@ -112,71 +70,3 @@ macro_rules! lucet_signature {
11270 }
11371 } ;
11472}
115-
116- #[ derive( Debug ) ]
117- pub enum SignatureError {
118- BadElement ( ir:: AbiParam , ValueError ) ,
119- BadSignature ,
120- }
121-
122- impl TryFrom < & ir:: Signature > for Signature {
123- type Error = SignatureError ;
124-
125- fn try_from ( value : & ir:: Signature ) -> Result < Self , Self :: Error > {
126- let mut params: Vec < ValueType > = Vec :: new ( ) ;
127-
128- let mut param_iter = value. params . iter ( ) ;
129-
130- // Enforce that the first parameter is VMContext, as Signature assumes.
131- // Even functions declared no-arg take VMContext in reality.
132- if let Some ( param) = param_iter. next ( ) {
133- match & param {
134- ir:: AbiParam {
135- value_type : value,
136- purpose : ir:: ArgumentPurpose :: VMContext ,
137- extension : ir:: ArgumentExtension :: None ,
138- location : ir:: ArgumentLoc :: Unassigned ,
139- } => {
140- if value. is_int ( ) && value. bits ( ) == 64 {
141- // this is VMContext, so we can move on.
142- } else {
143- return Err ( SignatureError :: BadElement (
144- param. to_owned ( ) ,
145- ValueError :: InvalidVMContext ,
146- ) ) ;
147- }
148- }
149- _ => {
150- return Err ( SignatureError :: BadElement (
151- param. to_owned ( ) ,
152- ValueError :: InvalidVMContext ,
153- ) ) ;
154- }
155- }
156- } else {
157- return Err ( SignatureError :: BadSignature ) ;
158- }
159-
160- for param in param_iter {
161- let value_ty = ValueType :: try_from ( param)
162- . map_err ( |e| SignatureError :: BadElement ( param. clone ( ) , e) ) ?;
163-
164- params. push ( value_ty) ;
165- }
166-
167- let ret_ty: Option < ValueType > = match value. returns . as_slice ( ) {
168- & [ ] => None ,
169- & [ ref ret_ty] => {
170- let value_ty = ValueType :: try_from ( ret_ty)
171- . map_err ( |e| SignatureError :: BadElement ( ret_ty. clone ( ) , e) ) ?;
172-
173- Some ( value_ty)
174- }
175- _ => {
176- return Err ( SignatureError :: BadSignature ) ;
177- }
178- } ;
179-
180- Ok ( Signature { params, ret_ty } )
181- }
182- }
0 commit comments