@@ -117,6 +117,63 @@ impl Index<FieldIdx> for Assembly {
117117 }
118118}
119119impl Assembly {
120+ /// Offsets `addr` by `index` * sizeof(`tpe`)
121+ pub fn offset (
122+ & mut self ,
123+ addr : impl IntoAsmIndex < NodeIdx > ,
124+ index : impl IntoAsmIndex < NodeIdx > ,
125+ tpe : impl IntoAsmIndex < TypeIdx > ,
126+ ) -> NodeIdx {
127+ let index = index. into_idx ( self ) ;
128+ let stride = self . size_of ( tpe) ;
129+ let stride = self . int_cast ( stride, Int :: USize , ExtendKind :: ZeroExtend ) ;
130+ let offset = self . biop ( index, stride, BinOp :: Mul ) ;
131+ self . biop ( addr, offset, BinOp :: Add )
132+ }
133+ /// Dereferences `addr`, loading data of type `tpe`
134+ pub fn load (
135+ & mut self ,
136+ addr : impl IntoAsmIndex < NodeIdx > ,
137+ tpe : impl IntoAsmIndex < TypeIdx > ,
138+ ) -> NodeIdx {
139+ let addr = addr. into_idx ( self ) ;
140+ let tpe = tpe. into_idx ( self ) ;
141+ self . alloc_node ( CILNode :: LdInd {
142+ addr,
143+ tpe,
144+ volatile : false ,
145+ } )
146+ }
147+ /// Gets the field of a valuetype / pointer `addr`.
148+ pub fn ld_field (
149+ & mut self ,
150+ addr : impl IntoAsmIndex < NodeIdx > ,
151+ field : impl IntoAsmIndex < FieldIdx > ,
152+ ) -> NodeIdx {
153+ let addr = addr. into_idx ( self ) ;
154+ let field = field. into_idx ( self ) ;
155+ self . alloc_node ( CILNode :: LdField { addr, field } )
156+ }
157+ /// Casts a pointer / usize / isize (`addr`) to a pointer to `tpe`.
158+ pub fn cast_ptr (
159+ & mut self ,
160+ addr : impl IntoAsmIndex < NodeIdx > ,
161+ tpe : impl IntoAsmIndex < TypeIdx > ,
162+ ) -> NodeIdx {
163+ let addr = addr. into_idx ( self ) ;
164+ let tpe = tpe. into_idx ( self ) ;
165+ self . alloc_node ( CILNode :: PtrCast ( addr, Box :: new ( PtrCastRes :: Ptr ( tpe) ) ) )
166+ }
167+ /// Gets the addres of a field of a pointer to valuetype `addr`.
168+ pub fn ld_field_addr (
169+ & mut self ,
170+ addr : impl IntoAsmIndex < NodeIdx > ,
171+ field : impl IntoAsmIndex < FieldIdx > ,
172+ ) -> NodeIdx {
173+ let addr = addr. into_idx ( self ) ;
174+ let field = field. into_idx ( self ) ;
175+ self . alloc_node ( CILNode :: LdFieldAdress { addr, field } )
176+ }
120177 pub fn typecheck ( & mut self ) {
121178 let method_def_idxs: Box < [ _ ] > = self . method_defs . keys ( ) . copied ( ) . collect ( ) ;
122179 for method in method_def_idxs {
@@ -276,20 +333,20 @@ impl Assembly {
276333 pub fn get_root ( & self , root : RootIdx ) -> & CILRoot {
277334 self . roots . get ( root)
278335 }
279- pub fn size_of ( & mut self , tpe : impl IntoAsmIndex < TypeIdx > ) -> CILNode {
336+ pub fn size_of ( & mut self , tpe : impl IntoAsmIndex < TypeIdx > ) -> NodeIdx {
280337 let idx = tpe. into_idx ( self ) ;
281338 assert_ne ! ( self [ idx] , Type :: Void ) ;
282- CILNode :: SizeOf ( idx)
339+ self . alloc_node ( CILNode :: SizeOf ( idx) )
283340 }
284341 pub fn biop (
285342 & mut self ,
286343 lhs : impl IntoAsmIndex < NodeIdx > ,
287344 rhs : impl IntoAsmIndex < NodeIdx > ,
288345 op : BinOp ,
289- ) -> CILNode {
346+ ) -> NodeIdx {
290347 let lhs = lhs. into_idx ( self ) ;
291348 let rhs = rhs. into_idx ( self ) ;
292- CILNode :: BinOp ( lhs, rhs, op)
349+ self . alloc_node ( CILNode :: BinOp ( lhs, rhs, op) )
293350 }
294351 pub fn unop ( & mut self , val : impl Into < CILNode > , op : UnOp ) -> CILNode {
295352 let val = self . nodes . alloc ( val. into ( ) ) ;
@@ -967,7 +1024,7 @@ impl Assembly {
9671024 self . new_method ( mref. into_def ( implementation, Access :: Private , self ) ) ;
9681025 continue ;
9691026 }
970-
1027+
9711028 // Check if this method is in the extern list
9721029 if let Some ( lib) = externs. get ( & mref. name ( ) ) {
9731030 let arg_names = ( 0 ..( self [ mref. sig ( ) ] . inputs ( ) . len ( ) ) )
@@ -995,7 +1052,7 @@ impl Assembly {
9951052 continue ;
9961053 }
9971054 // Create a replacement method.
998-
1055+
9991056 let arg_names = ( 0 ..( self [ mref. sig ( ) ] . inputs ( ) . len ( ) ) )
10001057 . map ( |_| None )
10011058 . collect ( ) ;
0 commit comments