File: ucfunc.h

package info (click to toggle)
exult 1.2-15.3
  • links: PTS, VCS
  • area: contrib
  • in suites: jessie, jessie-kfreebsd
  • size: 8,656 kB
  • ctags: 10,524
  • sloc: cpp: 99,373; sh: 7,324; ansic: 4,659; makefile: 991; yacc: 769; lex: 313; xml: 19
file content (271 lines) | stat: -rw-r--r-- 9,612 bytes parent folder | download | duplicates (8)
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
/*
 *  Copyright (C) 2001-2002  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 <map>
#include <vector>
#include <cstdio>
#include <string>
#include <fstream>
#include "ucc.h"

class UCFuncSet
{
	public:
		UCFuncSet(unsigned int new_funcid, unsigned int new_num_args, bool new_return_var, const std::string &new_funcname)
		         : funcid(new_funcid), num_args(new_num_args), return_var(new_return_var), funcname(new_funcname) {};
		~UCFuncSet() {};
		
		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
		std::string  funcname;    // the name of the function, if it has one
};

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

//#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=0) : ucc(newucc) { };
		~UCNode() { };
	
		UCc *ucc;
		std::vector<UCNode *> nodelist;
};

#include "ops.h"

class GotoSet
{
	public:
		GotoSet() : _offset(0) {};
		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 _uccs.size(); };

		void add(UCc *ucc, bool gc=false)
		{
			_uccs.push_back(std::pair<UCc *, bool>(ucc, gc));
		};

		/* Just a quick function to remove all the Uccs in _uccs marked for
		   garbage collection. */
		void gc()
		{
			for(GotoSet::iterator 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==true)
				{
					/* 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
					
					GotoSet::iterator rem(j);
					
					if(begin==false) j--;
					
					_uccs.erase(rem);
					
					if(begin==true) 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; };

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

class UCOpcodeData;

class UCFunc
{
	public:
		UCFunc() : _offset(0), _funcid(0), _funcsize(0), _bodyoffset(0), _datasize(0),
		           _codeoffset(0), _num_args(0), _num_locals(0), _num_externs(0),
		           return_var(false), debugging_info(false), debugging_offset(0), ext32(false) {};

		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);
		std::ostream &UCFunc::output_ucs_funcname(std::ostream &o, const FuncMap &funcmap);
		std::ostream &output_ucs_funcname(std::ostream &o, const FuncMap &funcmap,
                                    unsigned int funcid,
                                    unsigned int numargs, bool return_var);
		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);
		void output_ucs_data(std::ostream &o, const FuncMap &funcmap, const std::map<unsigned int, std::string> &intrinsics, const UCOptions &options, unsigned int indent);
		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);
		
		void parse_ucs(const FuncMap &funcmap, const std::map<unsigned int, std::string> &intrinsics, const UCOptions &options);
		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);
		std::vector<UCc *> parse_ucs_pass2a(std::vector<std::pair<UCc *, bool> >::reverse_iterator current,
		                               std::vector<std::pair<UCc *, bool> > &vec, unsigned int opsneeded,
		                               const FuncMap &funcmap, const std::map<unsigned int, std::string> &intrinsics);
		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);
		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);
		void output_raw_opcodes(std::ostream &o, const UCc &op);
		
		bool output_tt(std::ostream &o);

//	private:
	
		std::vector<GotoSet> gotoset;
		
		std::streampos _offset;      // offset to start of function
		unsigned int   _funcid;      // the id of the function
		unsigned int   _funcsize;    // the size of the function (bytes)
		std::streampos _bodyoffset;  // the file position after the header is read
		
		unsigned int   _datasize;    // the size of the data block
		
		std::map<unsigned int, std::string, std::less<unsigned int> > _data;
			// contains the entire data segment in offset from start of segment, and string data pairs
		
		std::streampos _codeoffset; // the offset to the start of the code segment
		
		unsigned int   _num_args;    // the number of arguments
		unsigned int   _num_locals;  // the number of local variables
		unsigned int   _num_externs; // the number of external function id's
		std::vector<unsigned int> _externs; // the external function id's
		
		std::vector<UCc> _opcodes;
		
		bool           return_var; // does the function return a variable?
		bool           debugging_info;
		unsigned int   debugging_offset;
		std::string    funcname;
		
		bool           ext32; // is this function an extended function?
		
		unsigned int codesize() const { return _funcsize - _datasize; };
		
		UCNode node;
};

void readbin_U7UCFunc(std::ifstream &f, UCFunc &ucf, const UCOptions &options);
void readbin_U8UCFunc(std::ifstream &f, UCFunc &ucf);

#endif