it_lilo/lifter/
macros.rs

1/*
2 * Copyright 2021 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#[macro_export]
18macro_rules! value_der {
19    ($self:expr, $store:expr, $offset:expr, @seq_start $($ids:tt),* @seq_end) => {
20        [$($self.reader.view.read_byte($store, $offset + $ids)),+]
21    };
22
23    ($self:expr, $store:expr, $offset:expr, 1) => {
24        crate::value_der!($self, $store, $offset, @seq_start 0 @seq_end)
25    };
26
27    ($self:expr, $store:expr, $offset:expr, 2) => {
28        crate::value_der!($self, $store, $offset, @seq_start 0, 1 @seq_end)
29    };
30
31    ($self:expr, $store:expr, $offset:expr, 4) => {
32        crate::value_der!($self, $store, $offset, @seq_start 0, 1, 2, 3 @seq_end)
33    };
34
35    ($self:expr, $store:expr, $offset:expr, 8) => {
36        crate::value_der!($self, $store, $offset, @seq_start 0, 1, 2, 3, 4, 5, 6, 7 @seq_end)
37    };
38
39    ($self:expr, $store:expr, $offset:expr, 16) => {
40        crate::value_der!($self, $store, $offset, @seq_start 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 @seq_end)
41    };
42}
43
44#[macro_export]
45macro_rules! read_ty {
46    ($func_name:ident, $ty:ty, 1) => {
47        pub fn $func_name(
48            &self,
49            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
50        ) -> $ty {
51            let offset = self.offset.get();
52            let result = <$ty>::from_le_bytes(crate::value_der!(self, store, offset, 1));
53
54            self.offset.set(offset + 1);
55            result
56        }
57    };
58
59    ($func_name:ident, $ty:ty, 2) => {
60        pub fn $func_name(
61            &self,
62            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
63        ) -> $ty {
64            let offset = self.offset.get();
65            let result = <$ty>::from_le_bytes(crate::value_der!(self, store, offset, 2));
66
67            self.offset.set(offset + 2);
68            result
69        }
70    };
71
72    ($func_name:ident, $ty:ty, 4) => {
73        pub fn $func_name(
74            &self,
75            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
76        ) -> $ty {
77            let offset = self.offset.get();
78            let result = <$ty>::from_le_bytes(crate::value_der!(self, store, offset, 4));
79
80            self.offset.set(offset + 4);
81            result
82        }
83    };
84
85    ($func_name:ident, $ty:ty, 8) => {
86        pub fn $func_name(
87            &self,
88            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
89        ) -> $ty {
90            let offset = self.offset.get();
91            let result = <$ty>::from_le_bytes(crate::value_der!(self, store, offset, 8));
92
93            self.offset.set(offset + 8);
94            result
95        }
96    };
97
98    ($func_name:ident, $ty:ty, 16) => {
99        pub fn $func_name(
100            &self,
101            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
102        ) -> $ty {
103            let offset = self.offset.get();
104            let result = <$ty>::from_le_bytes(crate::value_der!(self, store, offset, 16));
105
106            self.offset.set(offset + 16);
107            result
108        }
109    };
110}
111
112#[macro_export]
113macro_rules! read_ty_decl {
114    ($func_name:ident, $ty:ty, 1) => {
115        fn $func_name(
116            &self,
117            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
118        ) -> $ty;
119    };
120
121    ($func_name:ident, $ty:ty, 2) => {
122        fn $func_name(
123            &self,
124            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
125        ) -> $ty;
126    };
127
128    ($func_name:ident, $ty:ty, 4) => {
129        fn $func_name(
130            &self,
131            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
132        ) -> $ty;
133    };
134
135    ($func_name:ident, $ty:ty, 8) => {
136        fn $func_name(
137            &self,
138            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
139        ) -> $ty;
140    };
141
142    ($func_name:ident, $ty:ty, 16) => {
143        fn $func_name(
144            &self,
145            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
146        ) -> $ty;
147    };
148}
149
150#[macro_export]
151macro_rules! read_array_ty {
152    ($func_name:ident, $ty:ident, $ity:ident) => {
153        pub fn $func_name(
154            &self,
155            store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
156            offset: u32,
157            elements_count: u32,
158        ) -> super::LiResult<Vec<crate::IValue>> {
159            let reader = self.sequential_reader(
160                store,
161                offset,
162                (std::mem::size_of::<$ty>() as u32) * elements_count,
163            )?;
164            let mut result = Vec::with_capacity(elements_count as usize);
165
166            for _ in 0..elements_count {
167                let value = paste::paste! { reader.[<read_ $ty>](store)};
168                result.push(IValue::$ity(value));
169            }
170
171            Ok(result)
172        }
173    };
174}