File: ucfunc.h

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (371 lines) | stat: -rw-r--r-- 11,063 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 *  Copyright (C) 2001-2022  The Exult Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef UCFUNC_H
#define UCFUNC_H

#include "ucc.h"
#include "usecode/ucsymtbl.h"

#include <cstdio>
#include <fstream>
#include <map>
#include <string>
#include <vector>

class UCFuncSet {
public:
	UCFuncSet(
			unsigned int new_funcid, unsigned int new_num_args,
			bool new_return_var, bool new_aborts, bool new_class_fun,
			const std::string&                   new_funcname,
			Usecode_symbol::Symbol_kind          new_kind,
			std::map<unsigned int, std::string>& new_varmap)
			: funcid(new_funcid), num_args(new_num_args),
			  return_var(new_return_var), aborts(new_aborts),
			  class_fun(new_class_fun), funcname(new_funcname), kind(new_kind),
			  varmap(new_varmap) {}

	unsigned int funcid;        // the id of the function
	unsigned int num_args;      // the number of arguments
	bool         return_var;    // if true, the function returns a variable
	bool         aborts;        // true if the function may not return
	bool         class_fun;     // true if this is a class member function
	std::string  funcname;      // the name of the function, if it has one
	Usecode_symbol::Symbol_kind          kind;      // Type of function.
	std::map<unsigned int, std::string>& varmap;    // Variable names
};

using FuncMap     = std::map<unsigned int, UCFuncSet>;
using FuncMapPair = std::pair<unsigned int, UCFuncSet>;

// #define DEBUG_GOTOSET
const unsigned int SIZEOF_USHORT = 2;

class FlagData {
public:
	enum {
		SETFLAG = false,
		GETFLAG = true
	};

	/* SETFLAG=UCC_POPF, GETFLAG=UCC_PUSHF */
	enum {
		POP  = false,
		PUSH = true
	};

	FlagData(
			const long func, const unsigned int offset, const unsigned int flag,
			const bool access)
			: _func(func), _offset(offset), _flag(flag), _access(access) {}

	long func() const {
		return _func;
	}

	unsigned int offset() const {
		return _offset;
	}

	unsigned int flag() const {
		return _flag;
	}

	bool access() const {
		return _access;
	}

private:
	long         _func;
	unsigned int _offset;
	unsigned int _flag;
	bool         _access;
};

class SortFlagDataLessFlag {
public:
	bool operator()(const FlagData& fd1, const FlagData& fd2) const {
		return (fd1.flag() < fd2.flag())   ? true
			   : (fd1.flag() > fd2.flag()) ? false
			   : (fd1.func() < fd2.func()) ? true
			   : (fd1.func() > fd2.func()) ? false
										   : (fd1.offset() < fd2.offset());
	}

	bool operator()(const FlagData* fd1, const FlagData* fd2) const {
		return (fd1->flag() < fd2->flag())   ? true
			   : (fd1->flag() > fd2->flag()) ? false
			   : (fd1->func() < fd2->func()) ? true
			   : (fd1->func() > fd2->func()) ? false
											 : (fd1->offset() < fd2->offset());
	}
};

class SortFlagDataLessFunc {
public:
	bool operator()(const FlagData& fd1, const FlagData& fd2) const {
		return (fd1.func() < fd2.func())   ? true
			   : (fd1.func() > fd2.func()) ? false
			   : (fd1.flag() < fd2.flag()) ? true
			   : (fd1.flag() > fd2.flag()) ? false
										   : (fd1.offset() < fd2.offset());
	}

	bool operator()(const FlagData* fd1, const FlagData* fd2) const {
		return (fd1->func() < fd2->func())   ? true
			   : (fd1->func() > fd2->func()) ? false
			   : (fd1->flag() < fd2->flag()) ? true
			   : (fd1->flag() > fd2->flag()) ? false
											 : (fd1->offset() < fd2->offset());
	}
};

class UCNode;

class UCNode {
public:
	UCNode(UCc* newucc = nullptr) : ucc(newucc) {}

	UCc*                 ucc;
	std::vector<UCNode*> nodelist;
};

#include "ops.h"

class GotoSet {
public:
	GotoSet() = default;

	GotoSet(const unsigned int offset, UCc* ucc) : _offset(offset) {
		add(ucc);
	}

	GotoSet(UCc* ucc) : _offset(ucc->_offset) {
		add(ucc);
	}

	std::vector<std::pair<UCc*, bool>>& operator()() {
		return _uccs;
	}

	UCc& operator[](const unsigned int i) {
		return *(_uccs[i].first);
	}

	unsigned int size() const {
		return static_cast<unsigned int>(_uccs.size());
	}

	void add(UCc* ucc, bool gc = false) {
		_uccs.emplace_back(ucc, gc);
	}

	/* Just a quick function to remove all the Uccs in _uccs marked for
	   garbage collection. */
	void gc() {
		for (auto j = _uccs.begin(); j != _uccs.end();) {
#ifdef DEBUG_GOTOSET
			std::cout << "OP: " << opcode_table_data[j->first->_id].ucs_nmo
					  << '\t' << j->second;
#endif
			if (j->second) {
				/* ok, we need to take into consideration that the
				   iterator we're removing might ==_uccs.begin() */
				bool begin = false;
				if (j == _uccs.begin()) {
					begin = true;
				}

#ifdef DEBUG_GOTOSET
				std::cout << "\tremoved";
#endif

				auto rem(j);

				if (!begin) {
					--j;
				}

				_uccs.erase(rem);

				if (begin) {
					j = _uccs.begin();
				} else {
					++j;
				}

				if (j == _uccs.end()) {
					std::cout << "POTENTIAL PROBLEM" << std::endl;
				}
			} else {
				++j;
			}

#ifdef DEBUG_GOTOSET
			std::cout << std::endl;
#endif
		}
	}

	unsigned int offset() const {
		return _offset;
	}

private:
	unsigned int                       _offset = 0;
	std::vector<std::pair<UCc*, bool>> _uccs;
};

class UCOpcodeData;

class UCFunc {
public:
	UCFunc() = default;

	bool output_list(
			std::ostream& o, unsigned int funcno, const UCOptions& options);

	bool output_ucs(
			std::ostream& o, const FuncMap& funcmap,
			const std::map<unsigned int, std::string>& intrinsics,
			const UCOptions& options, int new_indent,
			Usecode_symbol_table* symtbl);
	std::ostream& output_ucs_funcname(
			std::ostream& o, const FuncMap& funcmap,
			Usecode_symbol_table* symtbl);
	std::ostream& output_ucs_funcname(
			std::ostream& o, const FuncMap& funcmap, unsigned int funcid,
			unsigned int numargs, Usecode_symbol_table* symtbl);
	void output_ucs_node(
			std::ostream& o, const FuncMap& funcmap, UCNode* ucn,
			const std::map<unsigned int, std::string>& intrinsics,
			unsigned int indent, const UCOptions& options,
			Usecode_symbol_table* symtbl);
	void output_ucs_data(
			std::ostream& o, const FuncMap& funcmap,
			const std::map<unsigned int, std::string>& intrinsics,
			const UCOptions& options, unsigned int indent,
			Usecode_symbol_table* symtbl);
	void output_ucs_opcode(
			std::ostream& o, const FuncMap& funcmap,
			const std::vector<UCOpcodeData>& optab, const UCc& op,
			const std::map<unsigned int, std::string>& intrinsics,
			unsigned int indent, Usecode_symbol_table* symtbl);

	void parse_ucs(
			const FuncMap&                             funcmap,
			const std::map<unsigned int, std::string>& intrinsics,
			const UCOptions& options, Usecode_symbol_table* symtbl);
	void parse_ucs_pass1(std::vector<UCNode*>& nodes);
	void parse_ucs_pass2(
			std::vector<GotoSet>& gotoset, const FuncMap& funcmap,
			const std::map<unsigned int, std::string>& intrinsics,
			const UCOptions& options, Usecode_symbol_table* symtbl);
	std::vector<UCc*> parse_ucs_pass2a(
			std::vector<std::pair<UCc*, bool>>::reverse_iterator current,
			std::vector<std::pair<UCc*, bool>>& vec, int opsneeded,
			const FuncMap&                             funcmap,
			const std::map<unsigned int, std::string>& intrinsics,
			const UCOptions& options, Usecode_symbol_table* symtbl);
	void parse_ucs_pass3(
			std::vector<GotoSet>&                      gotoset,
			const std::map<unsigned int, std::string>& intrinsics);

	bool output_asm(
			std::ostream& o, const FuncMap& funcmap,
			const std::map<unsigned int, std::string>& intrinsics,
			const UCOptions& options, Usecode_symbol_table* symtbl);
	void output_asm_data(std::ostream& o);
	void output_asm_opcode(
			std::ostream& o, const FuncMap& funcmap,
			const std::vector<UCOpcodeData>&           optab,
			const std::map<unsigned int, std::string>& intrinsics,
			const UCc& op, const UCOptions& options,
			Usecode_symbol_table* symtbl);
	void output_raw_opcodes(std::ostream& o, const UCc& op);

	bool output_tt(std::ostream& o);

	//	private:

	std::vector<GotoSet> gotoset;

	std::streampos _offset   = 0;    // offset to start of function
	unsigned int   _funcid   = 0;    // the id of the function
	unsigned int   _funcsize = 0;    // the size of the function (bytes)
	std::streampos _bodyoffset
			= 0;    // the file position after the header is read

	unsigned int _datasize = 0;    // the size of the data block

	std::map<unsigned int, std::string> _data;
	std::map<unsigned int, std::string> _varmap;
	// contains the entire data segment in offset from start of segment, and
	// string data pairs

	std::streampos _codeoffset
			= 0;    // the offset to the start of the code segment

	unsigned int _num_args    = 0;    // the number of arguments
	unsigned int _num_locals  = 0;    // the number of local variables
	unsigned int _num_externs = 0;    // the number of external function id's
	int          _num_statics
			= 0;    // the number of static variables defined in the function
	std::vector<unsigned int> _externs;    // the external function id's

	std::vector<UCc> _opcodes;

	bool         return_var = false;    // does the function return a variable?
	bool         aborts     = false;    // true if the function may not return.
	bool         debugging_info   = false;
	unsigned int debugging_offset = 0;
	std::string  funcname;
	Usecode_symbol*       _sym = nullptr;
	Usecode_class_symbol* _cls = nullptr;    // Class member functions

	bool ext32 = false;    // is this function an extended function?

	unsigned int codesize() const {
		return _funcsize - _datasize;
	}

	UCNode node;

	// A few static variables.
	static int num_global_statics;    // the number of global static variables
	static const std::string                   VARPREFIX;
	static const std::string                   CLASSVARPREFIX;
	static const std::string                   STATICPREFIX;
	static const std::string                   GLOBALSTATICPREFIX;
	static const std::string                   THISVAR;
	static const std::string                   VARNAME;
	static const std::string                   STATICNAME;
	static const std::string                   NORETURN;
	static const std::string                   SHAPENUM;
	static const std::string                   OBJECTNUM;
	static std::map<unsigned int, std::string> FlagMap;
};

void readbin_U7UCFunc(
		std::istream& f, UCFunc& ucf, const UCOptions& options,
		Usecode_symbol_table* symtbl);
void readbin_U8UCFunc(std::istream& f, UCFunc& ucf);

std::ostream& tab_indent(const unsigned int indent, std::ostream& o);
#endif