swamp_code_gen/
ctx.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use crate::alloc::FrameMemoryRegion;
7use swamp_vm_types::{FrameMemoryAddress, MemoryOffset, MemorySize};
8
9pub struct Context {
10    target_info: FrameMemoryRegion,
11    comment: String,
12}
13
14impl Context {
15    pub(crate) fn comment(&self) -> &str {
16        &self.comment
17    }
18}
19
20impl Context {
21    pub(crate) const fn target(&self) -> FrameMemoryRegion {
22        self.target_info
23    }
24}
25
26impl Context {
27    pub(crate) fn with_offset(&self, offset: MemoryOffset, memory_size: MemorySize) -> Self {
28        Self {
29            target_info: FrameMemoryRegion {
30                addr: self.addr().add_offset(offset),
31                size: memory_size,
32            },
33            comment: "".to_string(),
34        }
35    }
36}
37
38impl Context {
39    #[must_use]
40    pub fn new(target_info: FrameMemoryRegion) -> Self {
41        Self {
42            target_info,
43            comment: String::new(),
44        }
45    }
46
47    pub const fn addr(&self) -> FrameMemoryAddress {
48        self.target_info.addr
49    }
50    pub const fn target_size(&self) -> MemorySize {
51        self.target_info.size
52    }
53
54    #[must_use]
55    pub fn with_target(&self, target_info: FrameMemoryRegion, comment: &str) -> Self {
56        Self {
57            target_info,
58            comment: comment.to_string(),
59        }
60    }
61
62    #[must_use]
63    pub fn create_scope(&self) -> Self {
64        Self {
65            target_info: self.target_info,
66            comment: self.comment.clone(),
67        }
68    }
69
70    #[must_use]
71    pub fn create_function_scope(&self, return_target: FrameMemoryRegion, comment: &str) -> Self {
72        Self {
73            target_info: self.target_info,
74            comment: comment.to_string(),
75        }
76    }
77}