1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2017 Fabian Schuiki
#![allow(unused_variables)]

//! Modules in LLHD encapsulate a design hierarchy and its data dependency and
//! control flow graphs.

use std;
use std::collections::HashMap;
use function::Function;
use process::Process;
use entity::Entity;
use value::{Context, Value, ValueRef, FunctionRef, ProcessRef, EntityRef};


pub struct Module {
	funcs: HashMap<FunctionRef, Function>,
	procs: HashMap<ProcessRef, Process>,
	entities: HashMap<EntityRef, Entity>,
	values: Vec<ValueRef>,
}

impl Module {
	/// Create a new empty module.
	pub fn new() -> Module {
		Module {
			funcs: HashMap::new(),
			procs: HashMap::new(),
			entities: HashMap::new(),
			values: Vec::new(),
		}
	}

	/// Add a function to the module.
	pub fn add_function(&mut self, func: Function) -> FunctionRef {
		let r = func.as_ref();
		self.funcs.insert(r, func);
		self.values.push(r.into());
		r
	}

	/// Add a process to the module.
	pub fn add_process(&mut self, prok: Process) -> ProcessRef {
		let r = prok.as_ref();
		self.procs.insert(r, prok);
		self.values.push(r.into());
		r
	}

	/// Add a entity to the module.
	pub fn add_entity(&mut self, entity: Entity) -> EntityRef {
		let r = entity.as_ref();
		self.entities.insert(r, entity);
		self.values.push(r.into());
		r
	}

	/// Get a reference to a function in the module.
	pub fn function(&self, func: FunctionRef) -> &Function {
		self.funcs.get(&func).unwrap()
	}

	/// Get a mutable reference to a function in the module.
	pub fn function_mut(&mut self, func: FunctionRef) -> &mut Function {
		self.funcs.get_mut(&func).unwrap()
	}

	/// Get a reference to a process in the module.
	pub fn process(&self, prok: ProcessRef) -> &Process {
		self.procs.get(&prok).unwrap()
	}

	/// Get a mutable reference to a process in the module.
	pub fn process_mut(&mut self, prok: ProcessRef) -> &mut Process {
		self.procs.get_mut(&prok).unwrap()
	}

	/// Get a reference to an entity in the module.
	pub fn entity(&self, entity: EntityRef) -> &Entity {
		self.entities.get(&entity).unwrap()
	}

	/// Get a mutable reference to an entity in the module.
	pub fn entity_mut(&mut self, entity: EntityRef) -> &mut Entity {
		self.entities.get_mut(&entity).unwrap()
	}

	/// Obtain an iterator over the values in the module. This includes globals,
	/// functions, processes, and entities.
	pub fn values(&self) -> std::slice::Iter<ValueRef> {
		self.values.iter()
	}
}



pub struct ModuleContext<'tctx> {
	module: &'tctx Module,
}

impl<'tctx> ModuleContext<'tctx> {
	pub fn new(module: &Module) -> ModuleContext {
		ModuleContext {
			module: module,
		}
	}

	pub fn function(&self, func: FunctionRef) -> &Function {
		self.module.function(func)
	}

	pub fn process(&self, prok: ProcessRef) -> &Process {
		self.module.process(prok)
	}

	pub fn entity(&self, entity: EntityRef) -> &Entity {
		self.module.entity(entity)
	}
}

impl<'tctx> Context for ModuleContext<'tctx> {
	fn try_value(&self, value: &ValueRef) -> Option<&Value> {
		match *value {
			ValueRef::Function(id) => Some(self.function(id)),
			ValueRef::Process(id) => Some(self.process(id)),
			ValueRef::Entity(id) => Some(self.entity(id)),
			_ => None,
		}
	}
}