1 | # -*- coding: utf-8 -*-
|
---|
2 | #
|
---|
3 | # mruby => TECS bridge
|
---|
4 | #
|
---|
5 | # Copyright (C) 2008-2017 by TOPPERS Project
|
---|
6 | #
|
---|
7 | # 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
8 | # ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
9 | # 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
10 | # (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
11 | # 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
12 | # スコード中に含まれていること.
|
---|
13 | # (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
14 | # 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
15 | # 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
16 | # の無保証規定を掲載すること.
|
---|
17 | # (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
18 | # 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
19 | # と.
|
---|
20 | # (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
21 | # 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
22 | # (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
23 | # 報告すること.
|
---|
24 | # (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
25 | # 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
26 | # また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
27 | # 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
28 | # 免責すること.
|
---|
29 | #
|
---|
30 | # 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
31 | # よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
32 | # に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
33 | # アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
34 | # の責任を負わない.
|
---|
35 | #
|
---|
36 | # $Id$
|
---|
37 | #
|
---|
38 |
|
---|
39 | # Todo:
|
---|
40 | # 生成するもの
|
---|
41 | # gen_cdl_file
|
---|
42 | # ブリッジ初期化セルタイプ (シグニチャごと)
|
---|
43 | # @@init_celltypes に記録
|
---|
44 | # ブリッジセルタイプ (シグニチャごと)
|
---|
45 | # @@celltypes に記録
|
---|
46 | # 構造体セルタイプ
|
---|
47 | # @@struct_list に記録
|
---|
48 | # new_cell
|
---|
49 | # TECS 初期化セル(プロトタイプ宣言)
|
---|
50 | # @@VM_init_cells に記録
|
---|
51 | # @@VM_struct_list に記録
|
---|
52 | # @@VM_ptr_list に記録
|
---|
53 | # gen_post_code
|
---|
54 | # 構造体初期化セル
|
---|
55 | # ポインタ初期化セル
|
---|
56 | # TECS 初期化セル
|
---|
57 | #
|
---|
58 | # ep_func の作成
|
---|
59 | # signature, ポインタ、構造体 … 初期化受け口=mruby VM への登録
|
---|
60 | # ポインタ
|
---|
61 | # preamble の作成
|
---|
62 | # ポインタ、構造体 … アクセス用コードの生成
|
---|
63 | # signature … ブリッジ関数のプロトタイプ宣言
|
---|
64 | # postamble の作成
|
---|
65 | # signature … ブリッジ関数の定義
|
---|
66 |
|
---|
67 | class MrubyBridgeSignaturePlugin < SignaturePlugin
|
---|
68 |
|
---|
69 | # プラグイン引数名 => Proc
|
---|
70 | MrubyBridgePluginArgProc = {
|
---|
71 | "ignoreUnsigned" => Proc.new { |obj,rhs| obj.set_ignoreUnsigned rhs },
|
---|
72 | "include" => Proc.new { |obj,rhs| obj.set_include rhs },
|
---|
73 | "exclude" => Proc.new { |obj,rhs| obj.set_exclude rhs },
|
---|
74 | "auto_exclude" => Proc.new { |obj,rhs| obj.set_auto_exclude rhs },
|
---|
75 | }
|
---|
76 |
|
---|
77 | @@b_no_banner = false #
|
---|
78 | @@b_gen_post_code_by_dependent = false # true if gen_post_code is called by MrubyBridgeCellPlugin
|
---|
79 | @@celltypes = { } # {celltype_name => [ BridgePlugin のインスタンスの配列 }
|
---|
80 | @@init_celltypes = { } # {celltype_name => [ BridgePlugin のインスタンスの配列 }
|
---|
81 | @@struct_list = { } # {struct_name=>StructType}
|
---|
82 | @@ptr_list = { } # {ptr_celltype_name=> @@TYPE_MAP の対応するもの}
|
---|
83 | @@VM_list = { } # VM_name => true
|
---|
84 | @@VM_celltypes = { } # VM_name => { @celltype_name => セルの配列 }
|
---|
85 | @@VM_struct_list = { } # {name=>StructType}
|
---|
86 | @@VM_ptr_list = { } # { VM_name => {name=> @@TYPE_MAP の対応するもの} }
|
---|
87 | @@TYPE_MAP = { # type_str class GET_SET
|
---|
88 | :char_t => [:char_t, "Char", :Char, :INT ],
|
---|
89 | :uchar_t => [:uchar_t, "UChar", :Char, :INT ],
|
---|
90 | :schar_t => [:schar_t, "SChar", :Char, :INT ],
|
---|
91 |
|
---|
92 | :bool_t => [:bool_t, "Bool", :Bool, :BOOL ],
|
---|
93 | :int8_t => [:int8_t, "Int8", :Int, :INT ],
|
---|
94 | :int16_t => [:int16_t, "Int16", :Int, :INT ],
|
---|
95 | :int32_t => [:int32_t, "Int32", :Int, :INT ],
|
---|
96 | :int64_t => [:int64_t, "Int64", :Int, :INT ],
|
---|
97 | :uint8_t => [:uint8_t, "UInt8", :Int, :INT ],
|
---|
98 | :uint16_t => [:uint16_t, "UInt16", :Int, :INT ],
|
---|
99 | :uint32_t => [:uint32_t, "UInt32", :Int, :INT ],
|
---|
100 | :uint64_t => [:uint64_t, "UInt64", :Int, :INT ],
|
---|
101 |
|
---|
102 | :int => [:int, "Int", :Int, :INT ],
|
---|
103 | :char => [:char, "Char", :Char, :INT ], # char は char_t として扱う
|
---|
104 | :short => [:short, "Short", :Int, :INT ],
|
---|
105 | :long => [:long, "Long", :Int, :INT ],
|
---|
106 |
|
---|
107 | :"unsigned char" => [:uchar_t, "UChar", :Char, :INT ],
|
---|
108 | :"unsigned int" => [:"unsigned int", "UInt", :Int, :INT ],
|
---|
109 | :"unsigned short" => [:"unsigned short", "UShort", :Int, :INT ],
|
---|
110 | :"unsigned long" => [:"unsigned long", "ULong", :Int, :INT ],
|
---|
111 | :"signed char" => [:schar_t, "SChar", :Char, :INT ],
|
---|
112 | :"signed int" => [:int, "Int", :Int, :INT ],
|
---|
113 | :"signed short" => [:short, "Short", :Int, :INT ],
|
---|
114 | :"signed long" => [:long, "Long", :Int, :INT ],
|
---|
115 |
|
---|
116 | :float32_t => [:float32_t, "Float32", :Float, :FLOAT ],
|
---|
117 | :double64_t => [:double64_t,"Double64", :Float, :FLOAT ],
|
---|
118 |
|
---|
119 | :float => [:float, "Float32", :Float, :FLOAT ],
|
---|
120 | :double => [:double, "Double64", :Float, :FLOAT ]
|
---|
121 | }
|
---|
122 |
|
---|
123 | # included or excluded functions
|
---|
124 |
|
---|
125 | ### ロードされた時点で実行される ###
|
---|
126 |
|
---|
127 | # -I に $(TECSPATH)/mruby を追加
|
---|
128 | # TECSGEN::Makefile.add_obj "$(MRUBY_MAIN_OBJ)"
|
---|
129 | TECSGEN::Makefile.add_ldflag "-lmruby -L$(MRUBYPATH)/lib -lm"
|
---|
130 | TECSGEN::Makefile.add_search_path "$(MRUBYPATH)/include"
|
---|
131 | TECSGEN::Makefile.add_var "MRUBYPATH", "..", "CHANGE this to suitable path"
|
---|
132 | # TECSGEN::Makefile.add_var "MRUBY_MAIN_OBJ", "$(_TECS_OBJ_DIR)tecs_mruby.o", "CHANGE this if your have your main"
|
---|
133 |
|
---|
134 | #=== プラグインインスタンスの初期化
|
---|
135 | # 戻り値、引数の型が使用可能なものかチェックする
|
---|
136 | #
|
---|
137 | def initialize( signature, option )
|
---|
138 | super
|
---|
139 |
|
---|
140 | if ! @@b_no_banner
|
---|
141 | STDERR << "MrubyBridgePlugin: version 1.3.0 (Suitable for mruby ver 1.3.0 & ver 1.2.0). \n"
|
---|
142 | @@b_no_banner = true
|
---|
143 | end
|
---|
144 |
|
---|
145 | @b_ignoreUnsigned = false
|
---|
146 | @includes = []
|
---|
147 | @excludes = []
|
---|
148 | @struct_list = { }
|
---|
149 | @ptr_list = { }
|
---|
150 | @auto_exclude_list = {}
|
---|
151 | @b_auto_exclude = true # auto_exclude = true by default
|
---|
152 |
|
---|
153 | @plugin_arg_check_proc_tab = MrubyBridgePluginArgProc
|
---|
154 | parse_plugin_arg
|
---|
155 |
|
---|
156 | @celltype_name = :"t#{@signature.get_global_name}"
|
---|
157 | @init_celltype_name = :"#{@celltype_name}_Initializer"
|
---|
158 | # this variable is sometimes not used. rhs coded directry.
|
---|
159 | @class_name = :"T#{@signature.get_global_name}"
|
---|
160 |
|
---|
161 | @func_head_array = []
|
---|
162 | fh_array = []
|
---|
163 | if @includes.length > 0 && @excludes.length > 0 then
|
---|
164 | cdl_error( "MRB1011 both include && exclude are specified" )
|
---|
165 | end
|
---|
166 |
|
---|
167 | if signature.get_function_head_array == nil then
|
---|
168 | return # 以前に文法エラー発生
|
---|
169 | end
|
---|
170 |
|
---|
171 | signature.get_function_head_array.each{ |func_head|
|
---|
172 | if @includes.length > 0 then
|
---|
173 | if @includes.index func_head.get_name then
|
---|
174 | dbgPrint "MrubyBridgePlugin: #{func_head.get_name} INCLUDED\n"
|
---|
175 | fh_array << func_head
|
---|
176 | else
|
---|
177 | dbgPrint "MrubyBridgePlugin: #{func_head.get_name} NOT included\n"
|
---|
178 | end
|
---|
179 | elsif @excludes.length > 0 then
|
---|
180 | if @excludes.index( func_head.get_name ) == nil then
|
---|
181 | dbgPrint "MrubyBridgePlugin: #{func_head.get_name} NOT excluded\n"
|
---|
182 | fh_array << func_head
|
---|
183 | else
|
---|
184 | dbgPrint "MrubyBridgePlugin: #{func_head.get_name} EXCLUDED\n"
|
---|
185 | end
|
---|
186 | else
|
---|
187 | fh_array << func_head
|
---|
188 | end
|
---|
189 | }
|
---|
190 |
|
---|
191 | check_name_and_return_type fh_array
|
---|
192 | check_parameter_type fh_array
|
---|
193 |
|
---|
194 | fh_array.each{ |fh|
|
---|
195 | if @auto_exclude_list[ fh.get_name ] == nil then
|
---|
196 | @func_head_array << fh
|
---|
197 | else
|
---|
198 | dbgPrint "MrubyBridgePlugin: auto_exclude #{fh.get_name}\n"
|
---|
199 | end
|
---|
200 | }
|
---|
201 |
|
---|
202 | if @func_head_array.length == 0 then
|
---|
203 | cdl_error( "MRB1012 '$1' no function remained by exclude", @signature.get_name )
|
---|
204 | end
|
---|
205 | end
|
---|
206 |
|
---|
207 | #=== check function name & return type
|
---|
208 | def check_name_and_return_type func_head_array
|
---|
209 | b_init = false; b_init_cell = false
|
---|
210 | func_head_array.each{ |func_head|
|
---|
211 | if( func_head.get_name == :initialize )then
|
---|
212 | cdl_warning( "MRW2001 initialize: internally defined. change to initialize_cell in ruby" )
|
---|
213 | b_init = true
|
---|
214 | elsif( func_head.get_name == :initialize_cell )then
|
---|
215 | b_init_cell = true
|
---|
216 | end
|
---|
217 | rtype = func_head.get_return_type.get_original_type
|
---|
218 | case rtype
|
---|
219 | when BoolType, IntType, FloatType, VoidType
|
---|
220 | else
|
---|
221 | if @b_auto_exclude then
|
---|
222 | cdl_info( "MRI0001 cannot return type $1, $2 automatcally excluded", rtype.get_type_str, func_head.get_name )
|
---|
223 | @auto_exclude_list[ func_head.get_name ] = func_head
|
---|
224 | else
|
---|
225 | cdl_error( "MRB1001 cannot return type $1", rtype.get_type_str )
|
---|
226 | end
|
---|
227 | end
|
---|
228 | }
|
---|
229 | if( b_init && b_init_cell )then
|
---|
230 | cdl_warning( "MRB1002 initialize: internally defined. change to initialize_cell in ruby" )
|
---|
231 | end
|
---|
232 | end
|
---|
233 |
|
---|
234 | #=== check paramter type
|
---|
235 | def check_parameter_type func_head_array
|
---|
236 | # check type of parameters
|
---|
237 | func_head_array.each{ |fh|
|
---|
238 | fh.get_paramlist.get_items.each{ |param_decl|
|
---|
239 | case param_decl.get_direction
|
---|
240 | when :SEND, :RECEIVE
|
---|
241 | if @b_auto_exclude then
|
---|
242 | cdl_info( "MRI0002 $1: $2 parameter cannot be used in mruby Bridge, $3 automatcally excluded",
|
---|
243 | param_decl.get_name, param_decl.get_direction.to_s.downcase, fh.get_name )
|
---|
244 | @auto_exclude_list[ fh.get_name ] = fh
|
---|
245 | else
|
---|
246 | cdl_error( "MRB1003 $1: $2 parameter cannot be used in mruby Bridge", param_decl.get_name, param_decl.get_direction.to_s.downcase )
|
---|
247 | end
|
---|
248 | end
|
---|
249 | type = param_decl.get_type
|
---|
250 | type_org = type.get_original_type
|
---|
251 | type_str = type.get_type_str + type.get_type_str_post
|
---|
252 |
|
---|
253 | b_ng = false
|
---|
254 | case type_org
|
---|
255 | when IntType
|
---|
256 | case type_org.get_bit_size
|
---|
257 | when 8, 16, 32, 64
|
---|
258 | when -1, -2, -3, -4, -11
|
---|
259 | else
|
---|
260 | b_ng = true
|
---|
261 | end
|
---|
262 | when BoolType
|
---|
263 | when FloatType
|
---|
264 | when PtrType
|
---|
265 | ttype_org = type_org.get_type # ポインタの指している先の型
|
---|
266 | ttype = ttype_org.get_original_type # 上記の typedef されている場合、元の型
|
---|
267 | register_ptr_type ttype_org, fh
|
---|
268 |
|
---|
269 | if( type_org.get_string.to_s == "-1" ) then
|
---|
270 | case param_decl.get_direction
|
---|
271 | when :OUT, :INOUT
|
---|
272 | if @b_auto_exclude then
|
---|
273 | cdl_info( "MRB9999 string specifier without length cannot be used for out & inout parameter, $1 automatcally excluded",
|
---|
274 | fh.get_name )
|
---|
275 | @auto_exclude_list[ fh.get_name ] = fh
|
---|
276 | else
|
---|
277 | cdl_error( "MRB9999 string specifier without length cannot be used for out & inout parameter")
|
---|
278 | end
|
---|
279 | end
|
---|
280 | end
|
---|
281 |
|
---|
282 | case ttype
|
---|
283 | when IntType
|
---|
284 | bit_size = ttype.get_bit_size
|
---|
285 | # if bit_size < 0 && bit_size != -1 then
|
---|
286 | # b_ng = true
|
---|
287 | # end
|
---|
288 | when FloatType
|
---|
289 | when BoolType
|
---|
290 | when StructType
|
---|
291 | if( type_org.get_size || type_org.get_string || type_org.get_count ) then
|
---|
292 | if @b_auto_exclude then
|
---|
293 | cdl_info( "MRI9999 $1: size_is, count_is, string cannot be specified for struct pointer, $2 automatcally excluded",
|
---|
294 | param_decl.get_name, fh.get_name )
|
---|
295 | @auto_exclude_list[ fh.get_name ] = fh
|
---|
296 | else
|
---|
297 | cdl_error( "MRB1004 $1: size_is, count_is, string cannot be specified for struct pointer", param_decl.get_name )
|
---|
298 | end
|
---|
299 | end
|
---|
300 | check_struct_member ttype_org, fh
|
---|
301 | else
|
---|
302 | b_ng = true
|
---|
303 | end
|
---|
304 | when StructType
|
---|
305 | check_struct_member type_org, fh
|
---|
306 | else # ArrayType, FuncType, EnumType, VoidType
|
---|
307 | b_ng = true
|
---|
308 | end
|
---|
309 | if b_ng then
|
---|
310 | if @b_auto_exclude then
|
---|
311 | cdl_info( "MRI9999 $1: type $2 cannot be used in mruby Bridge, $3 automatcally excluded",
|
---|
312 | param_decl.get_name, type_str, fh.get_name )
|
---|
313 | @auto_exclude_list[ fh.get_name ] = fh
|
---|
314 | else
|
---|
315 | cdl_error( "MRB1005 $1: type $2 cannot be used in mruby Bridge", param_decl.get_name, type_str )
|
---|
316 | end
|
---|
317 | end
|
---|
318 | }
|
---|
319 | }
|
---|
320 | end
|
---|
321 |
|
---|
322 | #=== 構造体のメンバーの型のチェック
|
---|
323 | def check_struct_member struct_type, fh
|
---|
324 | #p "tag name:#{struct_type.get_name}"
|
---|
325 | # sttype = Namespace.find_tag( struct_type.get_name )
|
---|
326 | sttype = struct_type.get_original_type
|
---|
327 | if sttype.get_name == nil then
|
---|
328 | if @b_auto_exclude then
|
---|
329 | cdl_info( "MRI9999 tagless-struct cannot be handled, $1 automatcally excluded", fh.get_name )
|
---|
330 | @auto_exclude_list[ fh.get_name ] = fh
|
---|
331 | return # 登録しないように打ち切る
|
---|
332 | else
|
---|
333 | cdl_error( "MRB10007 tagless-struct cannot be handled")
|
---|
334 | end
|
---|
335 | end
|
---|
336 | sttype.get_members_decl.get_items.each { |d|
|
---|
337 | t = d.get_type.get_original_type
|
---|
338 | case t
|
---|
339 | when IntType, FloatType, BoolType
|
---|
340 | else
|
---|
341 | if @b_auto_exclude then
|
---|
342 | cdl_info( "MRI9999 $1: type $2 not allowed for struct member, $3 automatcally excluded",
|
---|
343 | d.get_name, d.get_type.get_type_str + d.get_type.get_type_str_post, fh.get_name )
|
---|
344 | @auto_exclude_list[ fh.get_name ] = fh
|
---|
345 | return # 登録しないように打ち切る
|
---|
346 | else
|
---|
347 | cdl_error( "MRB1006 $1: type $2 not allowed for struct member", d.get_name, d.get_type.get_type_str + d.get_type.get_type_str_post )
|
---|
348 | end
|
---|
349 | end
|
---|
350 | }
|
---|
351 | st_name = :"t{}"
|
---|
352 | if @struct_list[ sttype.get_name ] == nil then
|
---|
353 | # print_msg " MrubyBridgePlugin: [struct] #{struct_type.get_type_str} => class TECS::Struct#{sttype.get_name}\n"
|
---|
354 | print " MrubyBridgePlugin: [struct] #{struct_type.get_type_str} => class TECS::Struct#{sttype.get_name}\n"
|
---|
355 | @struct_list[ sttype.get_name ] = sttype
|
---|
356 | end
|
---|
357 | end
|
---|
358 |
|
---|
359 | def register_ptr_type ttype, fh
|
---|
360 | t_org = ttype.get_original_type
|
---|
361 | tment = get_type_map_ent t_org
|
---|
362 | if tment == nil then
|
---|
363 | return
|
---|
364 | # cdl_error( "MRB1008 unknown pointer type '$1'", ttype.get_type_str )
|
---|
365 | end
|
---|
366 | ptr_celltype_name = :"t#{tment[1]}Pointer"
|
---|
367 | if @@ptr_list[ ptr_celltype_name ] == nil then
|
---|
368 | # print_msg " MrubyBridgePlugin: [pointer] #{ttype.get_type_str}* => class TECS::#{tment[1]}Pointer\n"
|
---|
369 | print " MrubyBridgePlugin: [pointer] #{ttype.get_type_str}* => class TECS::#{tment[1]}Pointer\n"
|
---|
370 | @@ptr_list[ ptr_celltype_name ] = tment
|
---|
371 | end
|
---|
372 | if @ptr_list[ ptr_celltype_name ] == nil then
|
---|
373 | @ptr_list[ ptr_celltype_name ] = tment
|
---|
374 | end
|
---|
375 | end
|
---|
376 |
|
---|
377 | def get_type_map_ent ttype
|
---|
378 | # structure type is registerd in check_struct_member
|
---|
379 | if ttype.kind_of? StructType
|
---|
380 | return
|
---|
381 | end
|
---|
382 | tstr = ttype.get_type_str.sub( /const /, "" ) # const は無視
|
---|
383 | tstr = tstr.sub( /volatile /, "" ) # volatile も無視
|
---|
384 | if @b_ignoreUnsigned then
|
---|
385 | tstr = tstr.sub( /unsigned /, "" ) # volatile も無視
|
---|
386 | tstr = tstr.sub( /uint/, "int" ) # volatile も無視
|
---|
387 | tstr = tstr.sub( /[cs]char/, "char" ) # volatile も無視
|
---|
388 | end
|
---|
389 | return @@TYPE_MAP[ tstr.to_sym ]
|
---|
390 | end
|
---|
391 |
|
---|
392 | #=== CDL ファイルの生成
|
---|
393 | # typedef, signature, celltype, cell コードを生成
|
---|
394 | #file:: FILE 生成するファイル
|
---|
395 | def gen_cdl_file(file)
|
---|
396 |
|
---|
397 | # ブリッジセルタイプの生成
|
---|
398 | if @@celltypes[ @celltype_name ] == nil then
|
---|
399 | @@celltypes[ @celltype_name ] = [ self ]
|
---|
400 | @@init_celltypes[ @init_celltype_name ] = true
|
---|
401 | print_msg <<EOT
|
---|
402 | MrubyBridgePlugin: [signature] #{@signature.get_namespace_path} => [celltype] nMruby::#{@celltype_name} => [class] TECS::#{@class_name}
|
---|
403 | EOT
|
---|
404 |
|
---|
405 | file.print <<EOT
|
---|
406 | /*
|
---|
407 | * MrubyBridgeSignaturePlugin:
|
---|
408 | * signature=#{@signature.get_namespace_path}
|
---|
409 | *
|
---|
410 | * => celltype=nMruby::#{@celltype_name}
|
---|
411 | * (bridge cell's celltype; generated in this file)
|
---|
412 | * cell nMruby::#{@celltype_name} BridgeCellName { cTECS = CellName.eEntry; };
|
---|
413 | * where eEntry's signature must be #{@signature.get_namespace_path}.
|
---|
414 | * => class=TECS::#{@class_name}
|
---|
415 | * (mruby's class; accessible from your script)
|
---|
416 | * bridge = TECS::#{@class_name}.new("BridgeCellName")
|
---|
417 | */
|
---|
418 | import( <mruby.cdl> );
|
---|
419 |
|
---|
420 | /**** Ruby => TECS Bridge Celltype (MBP500) ****/
|
---|
421 | namespace nMruby{
|
---|
422 | // bridge celltype
|
---|
423 | [idx_is_id,active] // not actually active, to avoid warning W1002, W1007
|
---|
424 | celltype #{@celltype_name} {
|
---|
425 | call #{@signature.get_namespace_path.to_s} cTECS;
|
---|
426 | attr {
|
---|
427 | [omit]
|
---|
428 | char_t *VMname = "VM";
|
---|
429 | [omit]
|
---|
430 | char_t *bridgeName = C_EXP( "$cell$" );
|
---|
431 | };
|
---|
432 | };
|
---|
433 | // bridge initializer celltype
|
---|
434 | celltype #{@init_celltype_name} {
|
---|
435 | entry sInitializeTECSBridge eInitialize;
|
---|
436 | };
|
---|
437 | };
|
---|
438 | EOT
|
---|
439 |
|
---|
440 | # 構造体セルタイプの生成
|
---|
441 | @struct_list.each{ |name, sttype|
|
---|
442 | if @@struct_list[ name ] == nil then
|
---|
443 | file.print <<EOT
|
---|
444 | namespace nMruby{
|
---|
445 | [singleton]
|
---|
446 | celltype #{name} {
|
---|
447 | entry sInitializeTECSBridge eInitialize;
|
---|
448 | };
|
---|
449 | };
|
---|
450 | EOT
|
---|
451 | @@struct_list[ name ] = sttype
|
---|
452 | end
|
---|
453 | }
|
---|
454 |
|
---|
455 | else
|
---|
456 | cdl_info( "MRBW001 MrubyBridgePlugin: signature '$1' duplicate. ignored current one", @signature.get_namespace_path )
|
---|
457 | @@celltypes[ @celltype_name ] << self
|
---|
458 | end
|
---|
459 | end
|
---|
460 |
|
---|
461 | #=== gen_cdl_file で定義したセルタイプに 新しいセルが定義された
|
---|
462 | # cell のセルタイプの名前は @celltype_name
|
---|
463 | def new_cell cell
|
---|
464 | if cell.get_celltype.get_name != @celltype_name then
|
---|
465 | return
|
---|
466 | end
|
---|
467 |
|
---|
468 | join = cell.get_join_list.get_item :VMname
|
---|
469 | if join then
|
---|
470 | vm_name = CDLString.remove_dquote(join.get_rhs.to_s).to_sym
|
---|
471 | else
|
---|
472 | vm_name = :"VM"
|
---|
473 | end
|
---|
474 |
|
---|
475 | if @@VM_list[ vm_name ] == nil then
|
---|
476 | @@VM_list[ vm_name ] = true
|
---|
477 |
|
---|
478 | initializer_celltype_cdl = "#{$gen}/#{cell.get_name}Initializer.cdl"
|
---|
479 | file = CFile.open( initializer_celltype_cdl, "w" )
|
---|
480 |
|
---|
481 | # TECS 初期化セル(プロトタイプ宣言)
|
---|
482 | print_msg " MrubyBridgePlugin: join your VM's cInitialize to #{vm_name}_TECSInitializer.eInitialize\n"
|
---|
483 |
|
---|
484 | file.print <<EOT
|
---|
485 |
|
---|
486 | // prototype of TECSInitializer (MBP510)
|
---|
487 | cell nMruby::tTECSInitializer #{vm_name}_TECSInitializer;
|
---|
488 | EOT
|
---|
489 | file.close
|
---|
490 |
|
---|
491 | Generator.parse( initializer_celltype_cdl, self )
|
---|
492 | end
|
---|
493 |
|
---|
494 | if @@VM_celltypes[ vm_name ] then
|
---|
495 | vma = @@VM_celltypes[ vm_name ]
|
---|
496 |
|
---|
497 | if vma[ @celltype_name ] then
|
---|
498 | vma[ @celltype_name ] << cell
|
---|
499 | else
|
---|
500 | vma[ @celltype_name ] = [cell]
|
---|
501 | @@VM_celltypes[ vm_name ] = vma
|
---|
502 | end
|
---|
503 | else
|
---|
504 | vma = { }
|
---|
505 | vma[ @celltype_name ] = [cell]
|
---|
506 | @@VM_celltypes[ vm_name ] = vma
|
---|
507 | end
|
---|
508 |
|
---|
509 | @struct_list.each{ |stname, sttype|
|
---|
510 | if @@VM_struct_list[ vm_name ] then
|
---|
511 | @@VM_struct_list[ vm_name ][ sttype.get_name ] = sttype
|
---|
512 | else
|
---|
513 | @@VM_struct_list[ vm_name ] = { sttype.get_name => sttype }
|
---|
514 | end
|
---|
515 | }
|
---|
516 | @ptr_list.each{ |ptr_celltype_name, tment|
|
---|
517 | if @@VM_ptr_list[ vm_name ] then
|
---|
518 | @@VM_ptr_list[ vm_name ][ ptr_celltype_name ] = tment
|
---|
519 | else
|
---|
520 | @@VM_ptr_list[ vm_name ] = { ptr_celltype_name => tment }
|
---|
521 | end
|
---|
522 | }
|
---|
523 |
|
---|
524 | end
|
---|
525 |
|
---|
526 | #=== プラグインが CDL の POST コードを生成
|
---|
527 | # tmp_plugin_post_code.cdl への出力
|
---|
528 | def self.gen_post_code file
|
---|
529 | dbgPrint "#{self.name}: gen_post_code\n"
|
---|
530 | if ! @@b_gen_post_code_by_dependent then
|
---|
531 | gen_post_code_body file
|
---|
532 | end
|
---|
533 | end
|
---|
534 |
|
---|
535 | def self.set_gen_post_code_by_dependent # by MrubyBriddeCellPlugin
|
---|
536 | dbgPrint "#{self.name}: set_gen_post_code_by_dependent\n"
|
---|
537 | @@b_gen_post_code_by_dependent = true
|
---|
538 | end
|
---|
539 |
|
---|
540 | def self.gen_post_code_body file
|
---|
541 | dbgPrint "#{self.name}: gen_post_code_body\n"
|
---|
542 |
|
---|
543 | # file.print <<EOT
|
---|
544 | # namespace nMruby {
|
---|
545 | # EOT
|
---|
546 | #
|
---|
547 | # @@ptr_list.each{ |name,tment|
|
---|
548 | # file.print <<EOT
|
---|
549 | #
|
---|
550 | # // MBP600
|
---|
551 | # [singleton]
|
---|
552 | # celltype #{name} {
|
---|
553 | # entry sInitializeTECSBridge eInitialize;
|
---|
554 | # };
|
---|
555 | # EOT
|
---|
556 | # }
|
---|
557 | #
|
---|
558 | # file.print <<EOT
|
---|
559 | # };
|
---|
560 | # EOT
|
---|
561 |
|
---|
562 | # gen_post_code で生成した celltype は gen_ep_func が呼び出されない #847
|
---|
563 | # @@struct_list.each{ |name,sttype|
|
---|
564 | # file.print <<EOT
|
---|
565 | #
|
---|
566 | # [singleton]
|
---|
567 | # celltype #{name} {
|
---|
568 | # entry sInitializeTECSBridge eInitialize;
|
---|
569 | # };
|
---|
570 | #EOT
|
---|
571 | # }
|
---|
572 |
|
---|
573 | file.print "\n // MrubyBridgeSignaturePlugin: MBP601\n"
|
---|
574 | @@VM_celltypes.each{ |vm_name, instance_list|
|
---|
575 | instance_list.each { |celltype_name, array|
|
---|
576 | cell = array[0]
|
---|
577 | if cell.get_celltype then
|
---|
578 | ct_name = cell.get_celltype.get_name
|
---|
579 | file.print <<EOT
|
---|
580 | cell nMruby::#{ct_name}_Initializer #{vm_name}_#{ct_name}_Initializer{ };
|
---|
581 | EOT
|
---|
582 | end
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | file.print " // MBP602\n"
|
---|
587 | @@ptr_list.each{ |name,tment|
|
---|
588 | file.print <<EOT
|
---|
589 | cell nMruby::#{name} C#{name} { };
|
---|
590 | EOT
|
---|
591 | }
|
---|
592 |
|
---|
593 | file.print " // MBP603\n"
|
---|
594 | @@struct_list.each{ |name,sttype|
|
---|
595 | file.print <<EOT
|
---|
596 | cell nMruby::#{name} C#{name} { };
|
---|
597 | EOT
|
---|
598 | }
|
---|
599 |
|
---|
600 | if @@VM_celltypes == nil
|
---|
601 | raise "MrubyBridgeSignaturePlugin: are0"
|
---|
602 | end
|
---|
603 | @@VM_celltypes.each{ |vm_name, instance_list|
|
---|
604 | file.print " /* === VM name is '#{vm_name}' === (MBP610) */\n"
|
---|
605 | init_cell_name = "#{vm_name}_TECSInitializer"
|
---|
606 |
|
---|
607 | file.print " cell nMruby::tTECSInitializer #{init_cell_name} {\n"
|
---|
608 |
|
---|
609 | instance_list.each { |celltype_name, array|
|
---|
610 | # array.each{ |cell|
|
---|
611 | # ct_name = cell.get_celltype.get_name
|
---|
612 | ct_name = celltype_name
|
---|
613 | file.print " cInitialize[] = #{vm_name}_#{ct_name}_Initializer.eInitialize;\n"
|
---|
614 | # }
|
---|
615 | }
|
---|
616 | if @@VM_ptr_list[vm_name] then
|
---|
617 | @@VM_ptr_list[vm_name].each{ |name, tment|
|
---|
618 | file.print " cInitialize[] = C#{name}.eInitialize;\n"
|
---|
619 | }
|
---|
620 | end
|
---|
621 | if @@VM_struct_list[vm_name] then
|
---|
622 | @@VM_struct_list[vm_name].each{ |name, sttype|
|
---|
623 | file.print " cInitialize[] = C#{name}.eInitialize;\n"
|
---|
624 | }
|
---|
625 | end
|
---|
626 | file.print " };"
|
---|
627 | }
|
---|
628 |
|
---|
629 | end
|
---|
630 |
|
---|
631 | ####### 以下コード生成段階 ######
|
---|
632 |
|
---|
633 | #=== 受け口関数の本体コードを生成(頭部と末尾は別途出力)
|
---|
634 | #ct_name:: Symbol (プラグインで生成された) セルタイプ名 .Symbol として送られてくる
|
---|
635 | def gen_ep_func_body( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
636 | if @@celltypes[ ct_name ] then
|
---|
637 | gen_ep_func_body_bridge( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
638 | elsif @@init_celltypes[ ct_name ] then
|
---|
639 | gen_ep_func_body_bridge_init( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
640 | elsif @@ptr_list[ ct_name ] then
|
---|
641 | gen_ep_func_body_ptr( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
642 | elsif @@struct_list[ ct_name ] then
|
---|
643 | gen_ep_func_body_struct( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
644 | else
|
---|
645 | raise "MrubyBridgeSignaturePlugin: Unknown #{ct_name}"
|
---|
646 | end
|
---|
647 | end
|
---|
648 |
|
---|
649 | def gen_ep_func_body_bridge( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
650 | raise "MrubyBridgeSignaturePlugin: unexpected "
|
---|
651 | end
|
---|
652 |
|
---|
653 | def gen_ep_func_body_bridge_init( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
654 | file.print <<EOT
|
---|
655 | // CELLCB *p_cellcb = GET_CELLCB( idx ); /* no error check */ /* MBP700 */
|
---|
656 | struct RClass *rc;
|
---|
657 |
|
---|
658 | rc = mrb_define_class_under( mrb, TECS, \"#{@class_name}\", mrb->object_class );
|
---|
659 | mrb_define_method( mrb, rc, "initialize", MrubyBridge_#{@celltype_name}_initialize, MRB_ARGS_REQ(1) );
|
---|
660 | MRB_SET_INSTANCE_TT(rc, MRB_TT_DATA);
|
---|
661 | EOT
|
---|
662 |
|
---|
663 | @func_head_array.each{ |f|
|
---|
664 | if ! f.is_function? then
|
---|
665 | next
|
---|
666 | end
|
---|
667 | if f.get_name != :initialize then
|
---|
668 | func_name = f.get_name
|
---|
669 | else
|
---|
670 | func_name = :initialize_cell
|
---|
671 | end
|
---|
672 |
|
---|
673 | ret_type = f.get_return_type
|
---|
674 | n_param = 0
|
---|
675 | f.get_paramlist.get_items.each{ |param|
|
---|
676 | case param.get_direction
|
---|
677 | when :IN, :INOUT, :OUT
|
---|
678 | n_param += 1
|
---|
679 | when :SEND, :RECEIVE
|
---|
680 | raise "MrubyBridgeSignaturePlugin: send, receive"
|
---|
681 | end
|
---|
682 | }
|
---|
683 | if n_param > 0 then
|
---|
684 | p_str = "MRB_ARGS_REQ( #{n_param} )"
|
---|
685 | else
|
---|
686 | p_str = "MRB_ARGS_NONE()"
|
---|
687 | end
|
---|
688 | file.print <<EOT
|
---|
689 | mrb_define_method( mrb, rc, "#{func_name}", MrubyBridge_#{@celltype_name}_#{func_name}, #{p_str} );
|
---|
690 | EOT
|
---|
691 | }
|
---|
692 | end
|
---|
693 |
|
---|
694 | def gen_ep_func_body_ptr( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
695 |
|
---|
696 | t = @@ptr_list[ct_name]
|
---|
697 | type = t[1]
|
---|
698 | file.print <<EOT
|
---|
699 | struct RClass *a; /* MBP710 */
|
---|
700 |
|
---|
701 | a = mrb_define_class_under(mrb, TECS, "#{type}Pointer", mrb->object_class);
|
---|
702 | MRB_SET_INSTANCE_TT(a, MRB_TT_DATA);
|
---|
703 |
|
---|
704 | mrb_define_method(mrb, a, "initialize", #{type}Pointer_initialize, MRB_ARGS_REQ(1));
|
---|
705 | mrb_define_method(mrb, a, "[]", #{type}Pointer_aget, MRB_ARGS_REQ(1));
|
---|
706 | mrb_define_method(mrb, a, "value", #{type}Pointer_get_val, MRB_ARGS_NONE());
|
---|
707 | mrb_define_method(mrb, a, "[]=", #{type}Pointer_aset, MRB_ARGS_REQ(2));
|
---|
708 | mrb_define_method(mrb, a, "value=", #{type}Pointer_set_val, MRB_ARGS_REQ(1));
|
---|
709 | mrb_define_method(mrb, a, "size", #{type}Pointer_size, MRB_ARGS_NONE());
|
---|
710 | mrb_define_method(mrb, a, "length", #{type}Pointer_size, MRB_ARGS_NONE());
|
---|
711 | EOT
|
---|
712 |
|
---|
713 | if t[2] == :Char then
|
---|
714 | file.print <<EOT
|
---|
715 | mrb_define_method(mrb, a, "to_s", CharPointer_to_s, MRB_ARGS_NONE());
|
---|
716 | mrb_define_method(mrb, a, "from_s", CharPointer_from_s, MRB_ARGS_REQ(1));
|
---|
717 | EOT
|
---|
718 | end
|
---|
719 | end
|
---|
720 |
|
---|
721 | def gen_ep_func_body_struct( file, b_singleton, ct_name, global_ct_name, sig_name, ep_name, func_name, func_global_name, func_type, params )
|
---|
722 | tag = ct_name
|
---|
723 | structType = @@struct_list[ tag ]
|
---|
724 | file.print <<EOT
|
---|
725 | struct RClass *a; /* MBP720 */
|
---|
726 |
|
---|
727 | a = mrb_define_class_under(mrb, TECS, "Struct#{tag}", mrb->object_class);
|
---|
728 | MRB_SET_INSTANCE_TT(a, MRB_TT_DATA);
|
---|
729 |
|
---|
730 | mrb_define_method(mrb, a, "initialize", Struct_#{tag}_initialize, MRB_ARGS_NONE());
|
---|
731 | EOT
|
---|
732 |
|
---|
733 | structType.get_members_decl.get_items.each{ |d|
|
---|
734 | file.print " STRUCT_INIT_MEMBER( #{tag}, #{d.get_name} )\n"
|
---|
735 | }
|
---|
736 | end
|
---|
737 |
|
---|
738 | #=== 受け口関数の preamble (C言語)を生成する
|
---|
739 | # 必要なら preamble 部に出力する
|
---|
740 | #file:: FILE 出力先ファイル
|
---|
741 | #b_singleton:: bool true if singleton
|
---|
742 | #ct_name:: Symbol
|
---|
743 | #global_ct_name:: string
|
---|
744 | def gen_preamble( file, b_singleton, ct_name, global_ct_name )
|
---|
745 | if @@celltypes[ ct_name ] then
|
---|
746 | gen_preamble_mruby( file, b_singleton, ct_name, global_ct_name )
|
---|
747 | gen_preamble_instance( file, b_singleton, ct_name, global_ct_name )
|
---|
748 | gen_preamble_instance_initialize( file, b_singleton, ct_name, global_ct_name )
|
---|
749 | gen_preamble_bridge_func( file, b_singleton, ct_name, global_ct_name )
|
---|
750 | elsif @@init_celltypes[ ct_name ] then
|
---|
751 | gen_preamble_mruby( file, b_singleton, ct_name, global_ct_name )
|
---|
752 | gen_preamble_instance_proto( file, b_singleton, ct_name, global_ct_name )
|
---|
753 | elsif @@ptr_list[ ct_name ] then
|
---|
754 | gen_preamble_ptr( file, b_singleton, ct_name, global_ct_name )
|
---|
755 | elsif @@struct_list[ ct_name ] then
|
---|
756 | gen_preamble_struct( file, b_singleton, ct_name, global_ct_name )
|
---|
757 | else
|
---|
758 | raise "MrubyBridgeSignaturePlugin: MrubyBridgeSignaturePlugin: Unknown #{ct_name}"
|
---|
759 | end
|
---|
760 | end
|
---|
761 |
|
---|
762 | def gen_preamble_mruby( file, b_singleton, ct_name, global_ct_name )
|
---|
763 | file.print <<EOT
|
---|
764 | /* MBP: MrubyBridgePlugin: MBP000 */
|
---|
765 | #include "mruby.h"
|
---|
766 | #include "mruby/class.h"
|
---|
767 | #include "mruby/data.h"
|
---|
768 | #include "mruby/string.h"
|
---|
769 | #include "TECSPointer.h"
|
---|
770 | #include "TECSStruct.h"
|
---|
771 |
|
---|
772 | #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
|
---|
773 | #define DLLEXPORT __declspec(dllexport)
|
---|
774 | #else
|
---|
775 | #define DLLEXPORT
|
---|
776 | #endif
|
---|
777 |
|
---|
778 | #ifndef NULL
|
---|
779 | #define NULL 0
|
---|
780 | #endif
|
---|
781 | EOT
|
---|
782 | end
|
---|
783 |
|
---|
784 | def gen_preamble_instance( file, b_singleton, ct_name, global_ct_name )
|
---|
785 | # 「#1005 MrubyBridgePlugin-test のビルドに失敗」にて追加
|
---|
786 | # 「#1004 idx_is_id が true の場合(domain の場合は未指定でも true) の CBP のプロトタイプ宣言」にて不要になる見込み
|
---|
787 | nsp = NamespacePath.new( :nMruby, true )
|
---|
788 | nsp.append! ct_name
|
---|
789 | ct = Namespace.find nsp
|
---|
790 | if ct.idx_is_id_act? then
|
---|
791 | if ct.has_CB? then
|
---|
792 | inib_cb = "CB"
|
---|
793 | elsif ct.has_INIB? then
|
---|
794 | inib_cb = "INIB"
|
---|
795 | else
|
---|
796 | inib_cb = nil
|
---|
797 | end
|
---|
798 | if inib_cb then
|
---|
799 | ct.get_cell_list.each{ |cell|
|
---|
800 | if cell.is_generate? then
|
---|
801 | name_array = ct.get_name_array( cell )
|
---|
802 | file.print "extern #{ct.get_global_name}_CB #{cell.get_global_name}_#{inib_cb};\n"
|
---|
803 | end
|
---|
804 | }
|
---|
805 | end
|
---|
806 | end
|
---|
807 |
|
---|
808 | file.print <<EOT
|
---|
809 |
|
---|
810 | /* RData MBP001 */
|
---|
811 | static void
|
---|
812 | #{@celltype_name}_free( mrb_state *mrb, void *p )
|
---|
813 | {
|
---|
814 | if( p )
|
---|
815 | (void)mrb_free( mrb, p );
|
---|
816 | }
|
---|
817 |
|
---|
818 | /* RData MBP002 */
|
---|
819 | struct mrb_data_type data_type_#{@celltype_name} =
|
---|
820 | {
|
---|
821 | "#{@celltype_name}",
|
---|
822 | #{@celltype_name}_free
|
---|
823 | };
|
---|
824 |
|
---|
825 | /* RData MBP003 */
|
---|
826 | struct tecs_#{@celltype_name} {
|
---|
827 | CELLCB *cbp;
|
---|
828 | };
|
---|
829 |
|
---|
830 | /* name_to_cbp MBP010 */
|
---|
831 | const struct name_to_cbp_#{@celltype_name} {
|
---|
832 | char *name; /* Cell Name */
|
---|
833 | CELLCB *cbp;
|
---|
834 | } Name_to_cbp_#{@celltype_name}[] = {
|
---|
835 | EOT
|
---|
836 |
|
---|
837 | nsp = NamespacePath.new( :nMruby, true )
|
---|
838 | nsp.append! ct_name
|
---|
839 | ct = Namespace.find nsp
|
---|
840 |
|
---|
841 | ct.get_cell_list.each{ |cell|
|
---|
842 | if cell.is_generate? then
|
---|
843 | join_list = cell.get_join_list
|
---|
844 | join = join_list.get_item( :bridgeName )
|
---|
845 | if join == nil then
|
---|
846 | name = "\"#{cell.get_name}\""
|
---|
847 | else
|
---|
848 | rhs = join.get_rhs
|
---|
849 | attr_list = cell.get_celltype.get_attribute_list
|
---|
850 | name = rhs.eval_const join_list, attr_list
|
---|
851 | end
|
---|
852 |
|
---|
853 | name_array = ct.get_name_array( cell )
|
---|
854 | file.print "\t{ #{name}, #{name_array[8]} },\n"
|
---|
855 | end
|
---|
856 | }
|
---|
857 |
|
---|
858 | file.print <<EOT
|
---|
859 | \t{ 0, 0 },
|
---|
860 | };
|
---|
861 |
|
---|
862 | EOT
|
---|
863 |
|
---|
864 | end
|
---|
865 |
|
---|
866 | def gen_preamble_ptr( file, b_singleton, ct_name, global_ct_name )
|
---|
867 | tment = @@ptr_list[ ct_name ]
|
---|
868 | file.print <<EOT
|
---|
869 |
|
---|
870 | GET_SET_#{tment[3]}( #{tment[1]}, #{tment[0]} )
|
---|
871 | POINTER_CLASS( #{tment[1]}, #{tment[0]} )
|
---|
872 | EOT
|
---|
873 | end
|
---|
874 |
|
---|
875 | def gen_preamble_struct( file, b_singleton, ct_name, global_ct_name )
|
---|
876 | tag = ct_name
|
---|
877 | structType = @@struct_list[ tag ]
|
---|
878 | file.print <<EOT
|
---|
879 | /* struct #{tag} */
|
---|
880 | STRUCT_CLASS( #{tag} )
|
---|
881 | EOT
|
---|
882 |
|
---|
883 | structType.get_members_decl.get_items.each{ |d|
|
---|
884 | type = d.get_type.get_original_type
|
---|
885 | case type
|
---|
886 | when IntType, CIntType
|
---|
887 | bit_size = type.get_bit_size
|
---|
888 | case bit_size
|
---|
889 | when -11, -1
|
---|
890 | tType = "Char"
|
---|
891 | ttype = "char"
|
---|
892 | when -2
|
---|
893 | tType = "Short"
|
---|
894 | ttype = "short"
|
---|
895 | when -3
|
---|
896 | tType = "Int"
|
---|
897 | ttype = "int"
|
---|
898 | when -4
|
---|
899 | tType = "Long"
|
---|
900 | ttype = "long"
|
---|
901 | when -5
|
---|
902 | tType = "IntPtr"
|
---|
903 | ttype = "intptr"
|
---|
904 | when 8, 16, 32, 64
|
---|
905 | tType = "Int#{bit_size}"
|
---|
906 | ttype = "int#{bit_size}"
|
---|
907 | else
|
---|
908 | raise "MrubyBridgeSignaturePlugin: MrubyBridgeSignaturePlugin: cannot handle bit_size #{bit_size}"
|
---|
909 | end
|
---|
910 | file.print "MEMBER_GET_SET_INT( #{tag}, #{d.get_name}, #{tType}, #{ttype} )\n"
|
---|
911 | when FloatType, CFloatType
|
---|
912 | file.print "MEMBER_GET_SET_FLOAT( #{tag}, #{d.get_name} )\n"
|
---|
913 | else
|
---|
914 | raise "MrubyBridgeSignaturePlugin: MrubyBridgeSignaturePlugin: cannot handle type"
|
---|
915 | end
|
---|
916 | }
|
---|
917 |
|
---|
918 | end
|
---|
919 |
|
---|
920 | def gen_preamble_instance_proto( file, b_singleton, ct_name, global_ct_name )
|
---|
921 | file.print <<EOT
|
---|
922 | // Prototype MBP400
|
---|
923 | mrb_value MrubyBridge_#{@celltype_name}_initialize( mrb_state *mrb, mrb_value self);
|
---|
924 | EOT
|
---|
925 |
|
---|
926 | @func_head_array.each{ |f|
|
---|
927 | if ! f.is_function? then
|
---|
928 | next
|
---|
929 | end
|
---|
930 | if f.get_name != :initialize then
|
---|
931 | func_name = f.get_name
|
---|
932 | else
|
---|
933 | func_name = :initialize_cell
|
---|
934 | end
|
---|
935 |
|
---|
936 | ret_type = f.get_return_type
|
---|
937 | ret_type0 = f.get_return_type.get_original_type
|
---|
938 | b_void = ret_type0.is_void?
|
---|
939 | plist = f.get_paramlist.get_items
|
---|
940 |
|
---|
941 | file.print <<EOT
|
---|
942 | mrb_value MrubyBridge_#{@celltype_name}_#{func_name}( mrb_state *mrb, mrb_value self );
|
---|
943 | EOT
|
---|
944 | }
|
---|
945 | end
|
---|
946 |
|
---|
947 | def gen_preamble_instance_initialize( file, b_singleton, ct_name, global_ct_name )
|
---|
948 | file.print <<EOT
|
---|
949 |
|
---|
950 | /* MBP100 */
|
---|
951 | mrb_value
|
---|
952 | MrubyBridge_#{@celltype_name}_initialize( mrb_state *mrb, mrb_value self)
|
---|
953 | {
|
---|
954 | mrb_value name;
|
---|
955 | struct tecs_#{@celltype_name} *tecs_cb;
|
---|
956 | const struct name_to_cbp_#{@celltype_name} *ntc;
|
---|
957 |
|
---|
958 | /* set DATA_TYPE earlier to avoid SEGV */
|
---|
959 | DATA_TYPE( self ) = &data_type_#{@celltype_name};
|
---|
960 |
|
---|
961 | mrb_get_args(mrb, "o", &name );
|
---|
962 | if( mrb_type( name ) != MRB_TT_STRING ){
|
---|
963 | mrb_raise(mrb, E_NAME_ERROR, "cell name not string");
|
---|
964 | }
|
---|
965 | for( ntc = &Name_to_cbp_#{@celltype_name}[0]; ntc->name != NULL; ntc++ ){
|
---|
966 | if( strcmp( ntc->name, RSTRING_PTR( name ) ) == 0 )
|
---|
967 | break;
|
---|
968 | }
|
---|
969 | if( ntc->name == 0 ){
|
---|
970 | mrb_raise(mrb, E_ARGUMENT_ERROR, "cell not found");
|
---|
971 | }
|
---|
972 | tecs_cb = (struct tecs_#{@celltype_name} *)mrb_malloc(mrb, sizeof(struct tecs_#{@celltype_name}) );
|
---|
973 | tecs_cb->cbp = ntc->cbp;
|
---|
974 | DATA_PTR( self ) = (void *)tecs_cb;
|
---|
975 |
|
---|
976 | return self;
|
---|
977 | }
|
---|
978 | EOT
|
---|
979 | end
|
---|
980 |
|
---|
981 | def gen_preamble_bridge_func( file, b_singleton, ct_name, global_ct_name )
|
---|
982 |
|
---|
983 | @func_head_array.each{ |f|
|
---|
984 | if ! f.is_function? then
|
---|
985 | next
|
---|
986 | end
|
---|
987 | if f.get_name != :initialize then
|
---|
988 | func_name = f.get_name
|
---|
989 | else
|
---|
990 | func_name = :initialize_cell
|
---|
991 | end
|
---|
992 |
|
---|
993 | ret_type = f.get_return_type
|
---|
994 | ret_type0 = f.get_return_type.get_original_type
|
---|
995 | b_void = ret_type0.is_void?
|
---|
996 | plist = f.get_paramlist.get_items
|
---|
997 |
|
---|
998 | file.print <<EOT
|
---|
999 |
|
---|
1000 | /* bridge function (MBP101) */
|
---|
1001 | mrb_value
|
---|
1002 | MrubyBridge_#{ct_name}_#{func_name}( mrb_state *mrb, mrb_value self )
|
---|
1003 | {
|
---|
1004 | /* cellcbp (MBP105) */
|
---|
1005 | CELLCB *p_cellcb = ((struct tecs_#{@celltype_name} *)DATA_PTR(self))->cbp;
|
---|
1006 | EOT
|
---|
1007 |
|
---|
1008 | file.print " /* variables for return & parameter (MBP110) */\n"
|
---|
1009 | if ! b_void then
|
---|
1010 | file.print " ", ret_type.get_type_str, "\tret_val", ret_type.get_type_str_post, ";\n"
|
---|
1011 | end
|
---|
1012 | arg_str = ""
|
---|
1013 | n_param = 0
|
---|
1014 | n_scalar = 0
|
---|
1015 | n_ptr = 0
|
---|
1016 | n_struct = 0
|
---|
1017 | plist.each{ |param|
|
---|
1018 | case param.get_direction
|
---|
1019 | when :IN, :INOUT, :OUT
|
---|
1020 | type = param.get_type.get_original_type
|
---|
1021 | case type
|
---|
1022 | when IntType
|
---|
1023 | file.print " mrb_int mrb_", param.get_name, ";\n"
|
---|
1024 | file.print " #{param.get_type.get_type_str} #{param.get_name}#{param.get_type.get_type_str_post};\n"
|
---|
1025 | arg_str += "i"
|
---|
1026 | n_param += 1
|
---|
1027 | n_scalar += 1
|
---|
1028 | when FloatType
|
---|
1029 | file.print " mrb_float mrb_", param.get_name, ";\n"
|
---|
1030 | file.print " #{param.get_type.get_type_str} #{param.get_name}#{param.get_type.get_type_str_post};\n"
|
---|
1031 | arg_str += "f"
|
---|
1032 | n_param += 1
|
---|
1033 | n_scalar += 1
|
---|
1034 | when BoolType
|
---|
1035 | file.print " mrb_value mrb_", param.get_name, ";\n"
|
---|
1036 | file.print " #{param.get_type.get_type_str} #{param.get_name}#{param.get_type.get_type_str_post};\n"
|
---|
1037 | arg_str += "o"
|
---|
1038 | n_param += 1
|
---|
1039 | n_scalar += 1
|
---|
1040 | when PtrType
|
---|
1041 | file.print " mrb_value mrb_", param.get_name, ";\n"
|
---|
1042 | file.print " #{param.get_type.get_type_str} #{param.get_name}#{param.get_type.get_type_str_post};\n"
|
---|
1043 | arg_str += "o"
|
---|
1044 | n_param += 1
|
---|
1045 | n_ptr += 1
|
---|
1046 | when StructType
|
---|
1047 | file.print " mrb_value mrb_", param.get_name, ";\n"
|
---|
1048 | file.print " #{param.get_type.get_type_str} *#{param.get_name}#{param.get_type.get_type_str_post};\n"
|
---|
1049 | arg_str += "o"
|
---|
1050 | n_param += 1
|
---|
1051 | n_struct += 1
|
---|
1052 | else
|
---|
1053 | raise "MrubyBridgeSignaturePlugin: Unknown type"
|
---|
1054 | end
|
---|
1055 | end
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | if n_param > 0 then
|
---|
1059 | file.print " /* retrieve arguments (MBP111) */\n"
|
---|
1060 | file.print " mrb_get_args(mrb, \"#{arg_str}\""
|
---|
1061 | plist.each{ |param|
|
---|
1062 | case param.get_direction
|
---|
1063 | when :IN, :INOUT, :OUT
|
---|
1064 | type = param.get_type.get_original_type
|
---|
1065 | case type
|
---|
1066 | when IntType
|
---|
1067 | file.print ", &mrb_", param.get_name
|
---|
1068 | when FloatType
|
---|
1069 | file.print ", &mrb_", param.get_name
|
---|
1070 | when BoolType
|
---|
1071 | file.print ", &mrb_", param.get_name
|
---|
1072 | when PtrType
|
---|
1073 | file.print ", &mrb_", param.get_name
|
---|
1074 | when StructType
|
---|
1075 | file.print ", &mrb_", param.get_name
|
---|
1076 | else
|
---|
1077 | raise "MrubyBridgeSignaturePlugin: Unknown type"
|
---|
1078 | end
|
---|
1079 | end
|
---|
1080 | }
|
---|
1081 | file.print " );\n"
|
---|
1082 |
|
---|
1083 | if n_scalar > 0 || n_struct > 0 then
|
---|
1084 | file.print " /* convert mrb to C (MBP112) */\n"
|
---|
1085 | end
|
---|
1086 | plist.each{ |param|
|
---|
1087 | case param.get_direction
|
---|
1088 | when :IN, :INOUT, :OUT
|
---|
1089 | type = param.get_type.get_original_type
|
---|
1090 | case type
|
---|
1091 | when IntType
|
---|
1092 | ttype = type.get_original_type
|
---|
1093 | tment = get_type_map_ent ttype
|
---|
1094 | file.print " VALCHECK_#{tment[1]}( mrb, mrb_#{param.get_name} );\n"
|
---|
1095 | file.print " #{param.get_name} = (#{param.get_type.get_type_str})mrb_#{param.get_name};\n"
|
---|
1096 | when FloatType
|
---|
1097 | file.print " #{param.get_name} = (#{param.get_type.get_type_str})mrb_#{param.get_name};\n"
|
---|
1098 | when BoolType
|
---|
1099 | file.print " #{param.get_name} = mrb_test( mrb_#{param.get_name} );\n"
|
---|
1100 | when PtrType
|
---|
1101 | ttype = type.get_type.get_original_type
|
---|
1102 | case ttype
|
---|
1103 | when StructType
|
---|
1104 | file.print " CHECK_STRUCT( #{ttype.get_name}, mrb_#{param.get_name} );\n"
|
---|
1105 | file.print " #{param.get_name} = (struct #{ttype.get_name}*)DATA_PTR(mrb_#{param.get_name});\n"
|
---|
1106 | when IntType
|
---|
1107 | when FloatType
|
---|
1108 | when BoolType
|
---|
1109 | else
|
---|
1110 | raise "MrubyBridgeSignaturePlugin: cannot handle type"
|
---|
1111 | end
|
---|
1112 | when StructType
|
---|
1113 | file.print " CHECK_STRUCT( #{type.get_name}, mrb_#{param.get_name} );\n"
|
---|
1114 | file.print " #{param.get_name} = (struct #{type.get_name}*)DATA_PTR(mrb_#{param.get_name});\n"
|
---|
1115 | else
|
---|
1116 | raise( "MrubyBridgeSignaturePlugin: canot treat class" )
|
---|
1117 | end
|
---|
1118 | end
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | if n_ptr > 0 then
|
---|
1122 | file.print " /* convert mrb to C for pointer types (MBP113) */\n"
|
---|
1123 | end
|
---|
1124 | plist.each{ |param|
|
---|
1125 | case param.get_direction
|
---|
1126 | when :IN, :INOUT, :OUT
|
---|
1127 | type = param.get_type.get_original_type
|
---|
1128 | case type
|
---|
1129 | when IntType
|
---|
1130 | when FloatType
|
---|
1131 | when BoolType
|
---|
1132 | when PtrType
|
---|
1133 | case type.get_type.get_original_type
|
---|
1134 | when StructType
|
---|
1135 | when IntType
|
---|
1136 | ptrMrb2C file, type, param
|
---|
1137 | when FloatType
|
---|
1138 | ptrMrb2C file, type, param
|
---|
1139 | when BoolType
|
---|
1140 | ptrMrb2C file, type, param
|
---|
1141 | else
|
---|
1142 | raise "MrubyBridgeSignaturePlugin: cannot handle type"
|
---|
1143 | end
|
---|
1144 | when StructType
|
---|
1145 | else
|
---|
1146 | raise( "MrubyBridgeSignaturePlugin: canot treat class" )
|
---|
1147 | end
|
---|
1148 | end
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | end
|
---|
1152 |
|
---|
1153 | file.print " /* calling target (MBP120) */\n"
|
---|
1154 | if ! b_void then
|
---|
1155 | file.print " ret_val = "
|
---|
1156 | else
|
---|
1157 | file.print " "
|
---|
1158 | end
|
---|
1159 | delim = ""
|
---|
1160 | file.print "cTECS_", f.get_name, "( "
|
---|
1161 | plist.each{ |param|
|
---|
1162 | if param.get_type.get_original_type.kind_of? StructType then
|
---|
1163 | aster = "*"
|
---|
1164 | else
|
---|
1165 | aster = ""
|
---|
1166 | end
|
---|
1167 | file.print delim, aster, param.get_name
|
---|
1168 | delim = ", "
|
---|
1169 | }
|
---|
1170 | file.print " );\n"
|
---|
1171 |
|
---|
1172 | file.print " /* return (MBP130) */\n"
|
---|
1173 | case ret_type0
|
---|
1174 | when BoolType
|
---|
1175 | file.print " return ret_val ? mrb_true_value() : mrb_false_value();\n"
|
---|
1176 | when IntType
|
---|
1177 | file.print " return mrb_fixnum_value( ret_val );\n"
|
---|
1178 | when FloatType
|
---|
1179 | file.print " return mrb_float_value( mrb, ret_val );\n"
|
---|
1180 | when VoidType
|
---|
1181 | file.print " return mrb_nil_value();\n"
|
---|
1182 | else
|
---|
1183 | raise "MrubyBridgeSignaturePlugin: unknown type"
|
---|
1184 | end
|
---|
1185 |
|
---|
1186 | file.print "}\n"
|
---|
1187 | }
|
---|
1188 | end
|
---|
1189 |
|
---|
1190 | def ptrMrb2C file, type, param
|
---|
1191 | ttype = type.get_type.get_original_type
|
---|
1192 | tment = get_type_map_ent ttype
|
---|
1193 | tstr = tment[1]
|
---|
1194 | =begin
|
---|
1195 | case ttype
|
---|
1196 | when IntType
|
---|
1197 | bit_size = ttype.get_bit_size
|
---|
1198 | case bit_size
|
---|
1199 | when -1, -11
|
---|
1200 | tstr = "Char"
|
---|
1201 | when 8, 16, 32, 64
|
---|
1202 | tstr = "Int#{bit_size}"
|
---|
1203 | when -2
|
---|
1204 | tstr = "Short"
|
---|
1205 | when -3
|
---|
1206 | tstr = "Int"
|
---|
1207 | when -4
|
---|
1208 | tstr = "Long"
|
---|
1209 | when -5
|
---|
1210 | tstr = "IntPtr"
|
---|
1211 | else
|
---|
1212 | raise "MrubyBridgeSignaturePlugin: not handle type"
|
---|
1213 | end
|
---|
1214 | when FloatType
|
---|
1215 | if ttype.get_bit_size == 32 then
|
---|
1216 | tstr = "Float32"
|
---|
1217 | else
|
---|
1218 | tstr = "Double64"
|
---|
1219 | end
|
---|
1220 | when BoolType
|
---|
1221 | tstr = "Bool"
|
---|
1222 | when StructType
|
---|
1223 | raise "MrubyBridgeSignaturePlugin: not handle type 2 #{ttype}"
|
---|
1224 | else
|
---|
1225 | raise "MrubyBridgeSignaturePlugin: not handle type 2 #{ttype}"
|
---|
1226 | end
|
---|
1227 | =end
|
---|
1228 | if( param.get_size ) then
|
---|
1229 | sz_str = param.get_size.to_s
|
---|
1230 | elsif param.get_string then # mikan とりあえず size_is と string の同時指定 (二重ポインタ) はなし
|
---|
1231 | sz_str = param.get_string.to_s
|
---|
1232 | else
|
---|
1233 | sz_str = "1"
|
---|
1234 | end
|
---|
1235 | # unsigned 型の場合には cast が必要
|
---|
1236 | if ttype.get_original_type.get_type_str != param.get_type.get_type.get_type_str then
|
---|
1237 | cast_str = "(#{param.get_type.get_type_str})"
|
---|
1238 | else
|
---|
1239 | cast_str = ""
|
---|
1240 | end
|
---|
1241 |
|
---|
1242 | modify = ""
|
---|
1243 | case param.get_direction
|
---|
1244 | when :OUT, :INOUT
|
---|
1245 | case tstr
|
---|
1246 | when "Char", "SChar", "UChar"
|
---|
1247 | modify = "Mod"
|
---|
1248 | end
|
---|
1249 | end
|
---|
1250 | if param.is_nullable? then
|
---|
1251 | nullable = "Nullable"
|
---|
1252 | else
|
---|
1253 | nullable = ""
|
---|
1254 | end
|
---|
1255 |
|
---|
1256 | # file.print " CHECK_POINTER( #{tstr}, mrb_#{param.get_name}, #{sz_str} );\n"
|
---|
1257 | # file.print " #{param.get_name} = #{cast_str}((struct #{tstr}PointerBody*)(DATA_PTR(mrb_#{param.get_name})))->buf;\n"
|
---|
1258 | file.print " #{param.get_name} = CheckAndGet#{tstr}Pointer#{modify}#{nullable}( mrb, mrb_#{param.get_name}, #{sz_str} );\n"
|
---|
1259 | end
|
---|
1260 |
|
---|
1261 | def get_celltype_name
|
---|
1262 | @celltype_name
|
---|
1263 | end
|
---|
1264 |
|
---|
1265 | #=== プラグイン引数 ignoreUnsigned
|
---|
1266 | def set_ignoreUnsigned rhs
|
---|
1267 | if rhs == "true" || rhs == nil then
|
---|
1268 | @b_ignoreUnsigned = true
|
---|
1269 | end
|
---|
1270 | end
|
---|
1271 | #=== プラグイン引数 include
|
---|
1272 | def set_include rhs
|
---|
1273 | funcs = rhs.split ','
|
---|
1274 | funcs.each{ |rhs_func|
|
---|
1275 | found = false
|
---|
1276 | rhs_func.gsub!( /\s/, "" )
|
---|
1277 | @signature.get_function_head_array.each{ |a|
|
---|
1278 | if rhs_func.to_sym == a.get_name then
|
---|
1279 | found = true
|
---|
1280 | end
|
---|
1281 | }
|
---|
1282 | if found == false then
|
---|
1283 | cdl_error( "MRB1009 include function '$1' not found in signagture '$2'", rhs, @signature.get_name )
|
---|
1284 | else
|
---|
1285 | @includes << rhs_func.to_sym
|
---|
1286 | end
|
---|
1287 | }
|
---|
1288 | end
|
---|
1289 | #=== プラグイン引数 exclude
|
---|
1290 | def set_exclude rhs
|
---|
1291 | funcs = rhs.split ','
|
---|
1292 | funcs.each{ |rhs_func|
|
---|
1293 | rhs_func.gsub!( /\s/, "" )
|
---|
1294 | func_head = @signature.get_function_head rhs_func.to_sym
|
---|
1295 | if func_head == false then
|
---|
1296 | cdl_error( "MRB1010 exclude function '$1' not found in signagture '$2", rhs, @signature.get_name )
|
---|
1297 | else
|
---|
1298 | @excludes << rhs_func.to_sym
|
---|
1299 | end
|
---|
1300 | }
|
---|
1301 | end
|
---|
1302 | #=== プラグイン引数 auto_exclude
|
---|
1303 | def set_auto_exclude rhs
|
---|
1304 | if rhs == "false" then
|
---|
1305 | @b_auto_exclude = false
|
---|
1306 | elsif rhs == "true" then
|
---|
1307 | @b_auto_exclude = true # auto_exclude = true by default
|
---|
1308 | else
|
---|
1309 | cdl_warning( "MRB9999 auto_exclude: unknown rhs value ignored. specify true or false" )
|
---|
1310 | end
|
---|
1311 | end
|
---|
1312 | end
|
---|
1313 |
|
---|