-
Notifications
You must be signed in to change notification settings - Fork 30
/
ObxCGen.cpp
301 lines (279 loc) · 8.57 KB
/
ObxCGen.cpp
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
/*
* Copyright 2021 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Oberon+ parser/compiler library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "ObxCGen.h"
#include "ObxAst.h"
#include "ObxEvaluator.h"
#include "ObErrors.h"
#include <QTextStream>
#include <QtDebug>
using namespace Obx;
using namespace Ob;
static inline Type* derefed(Type* t )
{
if( t )
return t->derefed();
else
return t;
}
static inline Record* toRecord(Type* t)
{
// no deref!
if( t && t->getTag() == Thing::T_Pointer )
t = cast<Pointer*>(t)->d_to.data();
if( t && t->getTag() == Thing::T_Record )
return cast<Record*>(t);
else
return 0;
}
bool CGen::generateLjFfiBinding(Module* m, QIODevice* d, Ob::Errors* err)
{
if( !m->d_externC )
return false;
QByteArray dll, prefix;
SysAttr* a = m->d_sysAttrs.value("dll").data();
if( a && a->d_values.size() == 1 )
dll = a->d_values.first().toByteArray();
a = m->d_sysAttrs.value("prefix").data();
if( a && a->d_values.size() == 1 )
prefix = a->d_values.first().toByteArray();
QTextStream hout(d);
QByteArray str;
QTextStream bout(&str);
bout << "local module = {}" << endl;
hout << "local ffi = require 'ffi'" << endl;
hout << "ffi.cdef[[" << endl;
foreach( const Ref<Named>& n, m->d_order )
{
switch( n->getTag() )
{
case Thing::T_NamedType:
if( Record* r = toRecord(n->d_type.data()) ) // all others are aliasses or can be rendered inline
{
hout << "typedef ";
QByteArray nameType = prefix + n->d_name;
renderNameType( n->d_type.data(), nameType, prefix );
hout << nameType << ";" << endl;
bout << "module[" << r->d_slot << "] = ffi.typeof(\"" << (prefix+n->d_name) << "\")" << endl;
}
break;
}
}
foreach( const Ref<Named>& n, m->d_order )
{
switch( n->getTag() )
{
case Thing::T_Procedure:
{
Procedure* p = cast<Procedure*>(n.data());
QByteArray pfx = prefix;
a = p->d_sysAttrs.value("prefix").data();
if( a && a->d_values.size() == 1 )
pfx = a->d_values.first().toByteArray(); // can override prefix for function name
ProcType* pt = p->getProcType();
QByteArray function = pfx + n->d_name + renderFormals(pt,prefix);
if( pt->d_return )
renderNameType( pt->d_return.data(), function, prefix );
else
function = "void " + function;
hout << function << ";" << endl;
bout << "module[" << n->d_slot << "] = C." << (pfx+n->d_name) << endl;
}
break;
}
}
hout << "]]" << endl;
if( dll.isEmpty() )
hout << "local C = ffi.C" << endl;
else
hout << "local C = ffi.load('" << dll << "')" << endl;
bout << "return module" << endl;
bout.flush();
hout.flush();
d->write(str);
return true;
}
bool CGen::generateHeader(Module* m, QIODevice* out, Ob::Errors* err)
{
return false; // pending
}
bool CGen::renderNameType(Type* t, QByteArray& name, const QByteArray& pfx, bool vla)
{
if( t == 0 )
return false;
switch( t->getTag() )
{
case Thing::T_QualiType:
{
Type* td = derefed(t);
const int tag = td->getTag();
switch( tag )
{
case Thing::T_Array:
case Thing::T_BaseType:
case Thing::T_Enumeration:
case Thing::T_Pointer:
case Thing::T_ProcType:
renderNameType(td,name, pfx);
break;
default:
{
Q_ASSERT( tag == Thing::T_Record ); // because of derefed it cannot be QualiType
Record* r = cast<Record*>(td);
QualiType* q = cast<QualiType*>(t);
QualiType::ModItem names = q->getQuali();
// TODO: what about qualified names?
if( names.second )
name = ( r->d_union ? "union " : "struct " ) + pfx + names.second->d_name + " " + name;
else
name = "???";
}
break;
}
}
break;
case Thing::T_BaseType:
name = renderBasicType(t) + " " + name;
break;
case Thing::T_Array:
{
Array* a = cast<Array*>(t);
if( a->d_lenExpr.isNull() )
{
if( vla )
name += "[?]";
else
name = "*" + name;
}else
{
name += "[";
name += QByteArray::number(a->d_len);
name += "]";
}
renderNameType( a->d_type.data(), name, pfx );
}
break;
case Thing::T_Pointer:
{
name = "*" + name;
Pointer* p = cast<Pointer*>(t);
Type* td = derefed(p->d_to.data());
if( td && td->getTag() == Thing::T_Array )
renderNameType( cast<Array*>(td)->d_type.data(), name, pfx ); // ignore the first array the pointer points to
else
renderNameType( p->d_to.data(), name, pfx );
}
break;
case Thing::T_Enumeration:
name = "int " + name;
break;
case Thing::T_Record:
{
QByteArray rec;
Record* r = cast<Record*>(t);
if( r->d_union )
rec = "union ";
else
rec = "struct ";
Named* n = r->findDecl();
if( n )
rec += pfx + n->d_name;
if( !r->d_fields.isEmpty() )
rec += " {";
rec += "\n";
for( int i = 0; i < r->d_fields.size(); i++ )
{
QByteArray nameType = r->d_fields[i]->d_name;
renderNameType( r->d_fields[i]->d_type.data(), nameType, pfx );
rec += " " + nameType;
rec += ";\n";
}
if( !r->d_fields.isEmpty() )
rec += "} ";
name = rec + name;
}
break;
case Thing::T_ProcType:
{
ProcType* pt = cast<ProcType*>(t);
QByteArray returnType;
if( pt->d_return )
renderNameType( pt->d_return.data(), returnType, pfx );
else
returnType = "void";
name = returnType + "(*" + name + ")" + renderFormals(pt,pfx);
}
break;
default:
name = "???" + name;
break;
}
return true;
}
QByteArray CGen::renderBasicType(Type* t)
{
switch( t->getBaseType() )
{
case Type::BOOLEAN:
return "_Bool";
case Type::CHAR:
return "char";
case Type::WCHAR:
return "uint16_t";
case Type::BYTE:
return "uint8_t";
case Type::INT16:
return "int16_t";
case Type::INT32:
return "int32_t";
case Type::INT64:
return "int64_t";
case Type::REAL:
return "float";
case Type::LONGREAL:
return "double";
case Type::SET:
return "uint32_t";
case Type::CVOID:
return "void";
default:
return "???";
}
}
QByteArray CGen::renderFormals(ProcType* pt, const QByteArray& pfx)
{
QByteArray res = "(";
if( pt->d_formals.isEmpty() )
res += "void";
for( int i = 0; i < pt->d_formals.size(); i++ )
{
if( i != 0 )
res += ", ";
QByteArray nameType = pt->d_formals[i]->d_name;
renderNameType(pt->d_formals[i]->d_type.data(), nameType, pfx );
res += nameType;
}
if( pt->d_varargs )
{
if( !pt->d_formals.isEmpty() )
res += ", ";
res += "...";
}
res += ")";
return res;
}