source: asp3_tinet_ecnl_arm/trunk/asp3_dcre/tecsgen/tecsgen.rb@ 352

Last change on this file since 352 was 352, checked in by coas-nagasima, 6 years ago

arm向けASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 21.8 KB
Line 
1#!/usr/bin/env ruby
2# -*- coding: utf-8 -*-
3#
4# TECS Generator
5# Generator for TOPPERS Embedded Component System
6#
7# Copyright (C) 2008-2017 by TOPPERS Project
8#--
9# 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
10# ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
11# 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
12# (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
13# 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
14# スコード中に含まれていること.
15# (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
16# 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
17# 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
18# の無保証規定を掲載すること.
19# (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
20# 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
21# と.
22# (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
23# 作権表示,この利用条件および下記の無保証規定を掲載すること.
24# (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
25# 報告すること.
26# (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
27# 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
28# また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
29# 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
30# 免責すること.
31#
32# 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
33# よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
34# に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
35# アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
36# の責任を負わない.
37#
38# $Id$
39#++
40
41#= tecsgen : TECS のジェネレータ
42#
43#Authors:: 石川 拓也(HRP2Plugin)
44#Authors:: 原  拓(動的結合仕様)
45#Authors:: 河田 智明(NotifierPlugin)
46#Authors:: 高田 広章(ASP3 への TECS 組込み)
47#Authors:: 高木 信尚(ランタイムヘッダ)
48#Authors:: 成瀬 有美(TECS flow 実装)
49#Authors:: 鵜飼 敬幸(ATK1Plugin)
50#Authors:: 大山 博司(ジェネレータ実装, RPCPlugin, TracePlugin, MrubyBridgePlugin)
51#Authors:: 山本 将也(ジェネレータ初期プロトタイプ実装)
52#Authors:: 小南 靖雄(テストコードの一部)
53#Authors:: 安積 卓也(ASP+TECS, EV3RT+TECS, mruby on TECS等実装)
54# Authors list is in i-ro-ha order.
55#Version:: see version.rb
56$Copyright = "Copyright(c) 2008-2017, TOPPERS project. All rights reserved."
57$License = "TOPPERS License"
58
59# This doesn't work as expected in exerb version (Ruby 1.8.7?)
60$tecsgen_base_path = File.dirname( File.expand_path __FILE__ )
61
62
63###
64#= Array initializer の '{', '}' で囲まれた場合
65# mikan AggregateInitializer など、クラスを変更すべきである
66class Array
67 #=== CDL の文字列を生成する
68 def to_CDL_str
69 str = '{ '
70 delim = ''
71 self.each{ |v|
72 str += delim + v.to_CDL_str
73 delim = ', '
74 }
75 str += ' }'
76 return str
77 end
78
79 def show_tree( indent )
80 indent.times{ print " " }
81 print( "Array\n" )
82 self.each{ |m|
83 m.show_tree( indent+1 )
84 }
85 end
86end
87
88#=== RUBY ライブラリをロードする
89#
90# -L, $RUBYLIB, システム(/usr/lib/rub..など) の順にサーチが行われる
91# exerb 対応のため、上記パスにファイルが見つからない場合 require を実行してみる
92#
93# プラグインの場合は b_fatal = false を指定。ファイルがなくてもエラー出力後、処理続行
94# b_fatal = false の場合 tecslib/core がサーチパスに追加される
95#RETURN::Bool : true=成功、 false=失敗 失敗した場合、Generator.error は呼び元で出力する
96def require_tecsgen_lib( fname, b_fatal = true )
97 dbgPrint( "require_lib: #{fname}\n")
98 set_kcode $KCODE_TECSGEN
99 begin
100 b_require = false
101 b_exception = false
102
103 # -L 、 $RUBYLIB で指定されたパスおよびシステムのパスからサーチ
104 # exerb では $LOAD_PATH は ["."] のみ入っているようだ
105 ($library_path+$LOAD_PATH).each{ |path|
106 [ "", "tecslib/plugin/" ].each { |lp|
107 lib = File.expand_path( path ) + '/' + lp + fname
108
109 if File.exist? lib then # ファイル存否と他のエラーを区別するため存在確認をする
110 begin
111 require( lib )
112 b_require = true
113 rescue Exception => evar
114 b_exception = true
115 print_exception( evar )
116 end
117 break
118 end
119 }
120 if b_require then
121 break
122 end
123 }
124
125 if b_require == false && b_exception == false then
126 # exerb 対応 "." をサーチパスの最初に加える
127 # "tecslib/" は RPCPlugin.rb, TracePlugin.rb のために用意してある
128 # RPCPlugin.rb, TracePlugin.rb が tecslib 下でなければ不要になるが、このようにしておく
129 ["","tecslib/plugin/"].each{ |lp|
130 path = lp + fname
131 begin
132 require path
133 return true
134 rescue LoadError => e
135 # p "LoadError to load #{fname}"
136 # 2015.12.18 exerb 版でプラグインのロードでエラーが出るので、無視する. おそらく昔からエラーは出ていた
137 # print_exception( e )
138 rescue Exception => e
139 b_exception = true
140 # 文法エラーなどが発生
141 print_exception( e )
142 break
143 end
144 }
145 end
146
147 if b_require == false then
148 # 見つからなかった
149 if b_exception == false then
150 STDERR << "tecsgen: Fail to load #{fname}. Check $RUBYLIB environment variable or -L option\n"
151 end
152 # tecsgen を構成するファイルの場合は中止する
153 if b_fatal then
154 STDERR << "tecsgen: Exit because of unrecoverble error\n"
155 exit 1
156 end
157 return false
158 end
159 return true
160 ensure
161 # $KCODE を CDL の文字コードに戻しておく
162 set_kcode $KCODE_CDL
163 end
164end
165
166#=== 例外の表示
167#evar:: Exception
168def print_exception( evar )
169# もしスタックトレースが出るまでい時間がかかるようならば、次をコメントアウトしてみるべし
170 print "*** Begin Ruby exception message ***\n"
171 puts( evar.to_s )
172
173 if $debug then
174 puts "#### stack trace ####"
175 pp evar.backtrace
176 end
177 print "*** End Ruby exception message ***\n"
178end
179
180def dbgPrint( str )
181 if $debug then
182 print str
183 end
184end
185
186def dbgPrintf( *param )
187 if $debug then
188 printf *param
189 end
190end
191
192#=== エラーおよび警告のレポート
193def print_report
194 msg = nil
195
196 if Generator.get_n_error != 0 then
197 msg = "#{Generator.get_n_error} error"
198 msg = "#{msg}s" if Generator.get_n_error >= 2
199 end
200
201 if Generator.get_n_warning != 0 then
202 msg = "#{msg} " if msg
203 msg = "#{msg}#{Generator.get_n_warning} warning"
204 msg = "#{msg}s" if Generator.get_n_warning >= 2
205 end
206
207 puts msg if msg
208end
209
210#=== $KCODE を設定
211def set_kcode kcode
212 if ! $b_no_kcode then
213 $KCODE = kcode
214 end
215end
216#----- class TECSGEN -------#
217class TECSGEN
218
219 @@current_tecsgen = nil
220
221 def self.init( addtional_option_parser = nil )
222 initialize_global_var
223 analyze_option addtional_option_parser
224 load_modules
225 setup
226
227 dbgPrint "tecspath: #{$tecsgen_base_path}, __FILE__=#{__FILE__}\n"
228 dbgPrint "ARGV(remained): #{ARGV}, argments=#{$arguments}\n"
229 end
230
231 #----- initialize -------#
232 def initialize
233 @cell_list = nil
234 @cell_list2 = nil
235 @celltype_list = nil
236 @root_namespace = nil
237
238 #--- obsolete ---# replaced to TOOL_INFO
239 @cell_location_list = []
240 @join_location_list = []
241 end
242
243 def run1
244 @@current_tecsgen = self
245
246 syntax_analisys ARGV
247 semantics_analisys_1
248 semantics_analisys_2
249
250 @celltype_list = Celltype.get_celltype_list
251 @cell_list = Cell.get_cell_list
252 @cell_list2 = Cell.get_cell_list2
253
254 @@current_tecsgen = nil
255 end
256
257 def run2
258 @@current_tecsgen = self
259
260 optimize_and_generate
261 finalize
262
263 @@current_tecsgen = nil
264 end
265
266 #----- initialize_global_var -----#
267 def self.initialize_global_var
268
269 require 'optparse'
270 #2.0 require 'runit/assert.rb'
271 require 'kconv'
272 $b_no_kcode = RUBY_VERSION >= "1.9.0" ? true : false
273 # Use Ruby 1.9 M17N code (use Ruby 1.8 code if false).
274 if ! $b_no_kcode then
275 require 'jcode'
276 end
277 require 'pp'
278 # include RUNIT::Assert
279
280 ### グローバル変数定義 ###
281
282 # コマンドライン引数  (Makefile.templ へ出力)
283 $arguments = ""
284 ARGV.each { |a| $arguments += " " + a }
285
286 $unopt = false # bool: disable optimizing
287 $gen_base = "gen" # string: folder path to place generated files
288 $gen = $gen_base # string: folder path to place generated files
289 $generate_all_template = false # bool: generarete template files for all celltypes (if non cell exist or system celltypes)
290 $generate_no_template = false # bool: generarete no template file (neither celltype code nor Makefile)
291 $idx_is_id = false # bool: all components are idx_is_id
292 $unique_id = false # bool: assign unique id to each cell (otherwise begin from 1 for each celltype)
293 $debug = false # bool: tecsgen debug message
294 $dryrun = false # bool: dryrun mode: syntax is checked, but not generate any files
295 $show_tree = false # bool: show parsing tree
296 $verbose = false # bool: verbose mode: show some messages
297 $yydebug = false # bool: yydebug: parser debug mode (need bnf-deb.tab.rb)
298 $run_dir = Dir.pwd # string: tecsgen/tecscde start up directory
299 $base_dir = { } # string=>bool: base dir for import_path (key:base_dir, val:actually used or specified directly)
300 $import_path = [ "." ] # string array : import/import_C path
301 $import_path_opt = [] # [String]
302 $library_path = [ $tecsgen_base_path ] # string array : path to dir where tecsgen.rb placed
303 $define = [ ] # string array : define
304 $ram_initializer = false # bool: generate ram initializer
305 $region_list = {} #string array : region path which is generated
306 $generating_region = nil # Region: Region to optimisze & generate code # コマンドラインオプションではない
307 # Cell#is_generate? にて参照される
308 $unit_test = false # bool: unit test verification
309 $kcode = nil # nil | String: Kanji code type "euc"|"sjis"|"none"|"utf8"
310 $force_overwrite = false # bool: force overwrite all files if file contents not differ
311 $no_banner = false # bool: not print banner
312 $print_version = false # bool: print version
313 $target = "tecs" # String: target name, ARGV[0] から再設定する("tecs" は仮のターゲット)
314 $no_default_import_path = false # bool: no default import path
315 $c_suffix = "c" # suffix for C progorams (for C++ source)
316 $h_suffix = "h" # suffix for C progoram headers (for C++ source)
317
318 if ENV['TECSGEN_DEFAULT_RAM'] then
319 rom_ram_defalult = "ram"
320 else
321 rom_ram_defalult = "rom"
322 end
323 if rom_ram_defalult == "rom" then
324 $rom = true # bool: ROM support : generate CB separately
325 else
326 $rom = false # bool: ROM support : generate CB separately
327 end
328 $b_cpp_specified = false
329 if $cpp == nil then
330 $cpp = "gcc -E -DTECSGEN"
331 end
332 if ENV[ 'TECS_CPP' ]then
333 $cpp = ENV[ 'TECS_CPP' ]
334 $b_cpp_specified = true
335 end
336 if ENV[ 'TECSPATH' ] then
337 $tecspath = ENV[ 'TECSPATH' ]
338 else
339 $tecspath = "#{$tecsgen_base_path}/tecs"
340 end
341
342 # # 文字コードの設定
343 if $IN_EXERB then
344 # KCODE_CDL, $KCONV_CDL を仮に設定する (tecs_lang.rb ですぐに再設定される)
345 $KCODE_CDL = "SJIS" # string: "EUC" | "SJIS" | "NONE" | "UTF8"
346 $KCONV_CDL = Kconv::SJIS # const: NONE には ASCII を対応させる
347 else
348 $KCODE_CDL = "EUC" # string: "EUC" | "SJIS" | "NONE" | "UTF8"
349 $KCONV_CDL = Kconv::EUC # const: NONE には ASCII を対応させる
350 end
351 # $KCODE_TECSGEN, $KCONV_TECSGEN を仮に設定する (tecs_lang.rb ですぐに再設定される)
352 $KCODE_TECSGEN = "UTF8" # string: "EUC" このファイルの文字コード(オプションではなく定数)
353 $KCONV_TECSGEN = Kconv::UTF8 # const:
354 set_kcode( $KCODE_TECSGEN ) # このファイルの文字コードを設定
355
356 end # initialize_global_var
357
358 def self.analyze_option( additional_option_parser )
359
360 ### tecsgen コマンドオプション解析 ###
361 ARGV.options {|parser|
362 parser.banner = "Usage: tecsgen [options] files"
363 parser.on('-D', '--define=def', 'define cpp symbol for import_C') { |define|
364 $define << define
365 }
366 parser.on('-G', '--generate-region=path', 'generate region') { |path|
367 if path =~ /^::/ then
368 gen_path = path
369 else
370 gen_path = "::" + path
371 end
372 $region_list[ gen_path ] = true
373 }
374 parser.on('-I', '--import-path=path', 'imoprt/import_C path') { |path|
375 $import_path << path
376 $import_path_opt << path
377 }
378 parser.on('-L', '--library-path=path', 'path to dir where tecsgen.rb (obsolete, unnecessary to specify -L, those passes are gotten from tecsgen.rb') { |path|
379 $library_path << path
380 }
381 parser.on('-R', '--RAM-initializer', 'generate RAM initializer. INITIALIZE_TECS() must be called before running any TECS code.' ){
382 $ram_initializer = true
383 }
384 parser.on('-U', '--unoptimize', 'unoptimize') {
385 $unopt = true
386 }
387 parser.on('-c', '--cpp=cpp_cmd', 'C pre-processor command used import_C (default: gcc -E -DTECSGEN), you can also specify by environment variable TECS_CPP' ){
388 |arg|
389 $cpp = arg
390 $b_cpp_specified = true
391 }
392 parser.on('-d', '--dryrun', 'dryrun' ){
393 $dryrun = true
394 }
395 parser.on('-f', '--force-overwrite', 'force overwrite all files') {
396 $force_overwrite = true
397 }
398 parser.on('-g', '--gen=dir', 'generate dir') { |dir|
399 $gen = $gen_base = dir
400 }
401 parser.on('-i', '--idx_is_id', 'set idx_is_id to all celltypes') {
402 $idx_is_id = true
403 }
404 # parser.on('-k', '--kcode=code', 'set kanji code: euc|sjis|none|utf8, none is default') { |code|
405 parser.on('-k', '--kcode=code', "set kanji code: euc|sjis|none|utf8") { |code|
406 $kcode = code
407 }
408 # old_mode は V1.0.C.22 で廃止
409 # parser.on('-o', '--old-mode', 'old mode' ){
410 # $old_mode = true
411 # }
412 parser.on('-r', '--ram', 'RAM only' ){
413 $rom = false
414 }
415 parser.on('-s', '--show-tree', 'show parsing tree' ){
416 $show_tree = true
417 }
418 parser.on('-t', '--generator-debug', 'generator debug' ){
419 $debug = true
420 $verbose = true
421 }
422 parser.on('-u', '--unique-id', 'assign unique id for each cell' ){
423 $unique_id = true
424 }
425 parser.on('-v', '--verbose', 'verbose mode' ){
426 $verbose = true
427 }
428 parser.on('-y', '--yydebug', 'yydebug' ){
429 $yydebug = true
430 }
431 parser.on( '--no-banner', 'not display banner') {
432 $no_banner = true
433 }
434 parser.on( '--version', 'print version') {
435 $print_version = true
436 }
437 parser.on( '--unit-test', 'unit verification (test tecsgen itself)') {
438 $unit_test = true
439 }
440 parser.on( '--generate-all-template', 'generate all celltypes\' templates') {
441 $generate_all_template = true
442 }
443 parser.on( '--generate-no-template', 'generate no template') {
444 $generate_no_template = true
445 }
446 parser.on( '--no-default-import-path', 'no default import path' ){
447 $no_default_import_path = true
448 }
449 parser.on( '--c-suffix=c', 'C program suffix (default: c)' ){
450 | suffix |
451 $c_suffix = suffix
452 }
453 parser.on( '--h-suffix=h', 'C program header suffix (default: h)' ){
454 | suffix |
455 $h_suffix = suffix
456 }
457 # parser.on( '--include_path_opt_format', 'cpp include path option format, default: "-I %s"' ){
458 # }
459 parser.version = #{$version}
460 parser.release = nil
461 if additional_option_parser
462 additional_option_parser.call( parser )
463 end
464 parser.parse!
465 }
466
467 if ARGV.empty? && ! $print_version && ! $unit_test
468 ARGV.options{|parser|
469 puts parser.help
470 exit 1
471 }
472 end
473
474 end # analyze_option
475
476 def self.load_modules
477 ### tecsgen モジュールのロード ####
478 # -L でパス指定可能としたため、ここからロードを開始する
479
480 # tecsgen バージョンファイルのロード
481 # これを実行するまで tecsgen のバージョンを表示できない
482 # このファイルを誤って読み込むと、異なるバージョン名を表示してしまう
483 require_tecsgen_lib 'tecslib/version.rb'
484 if $tecscde_version then
485 STDERR << "tecscde version #{$tecscde_version} (tecsgen version #{$version}) #{$Copyright}\n"
486 elsif ! $no_banner || $print_version
487 STDERR << "tecsgen version #{$version} #{$Copyright}\n"
488 end
489 if $verbose then
490 STDERR << "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE} patchlevel #{RUBY_PATCHLEVEL}) [#{RUBY_PLATFORM}]\n"
491 end
492 if $print_version && ARGV.empty?
493 exit
494 end
495
496 # 文字コード決定のため最初に読みこむ
497 require_tecsgen_lib 'tecslib/core/tecs_lang.rb'
498
499 unless $yydebug then
500 require_tecsgen_lib 'tecslib/core/bnf.tab.rb'
501 else
502 require_tecsgen_lib 'tecslib/core/bnf-deb.tab.rb'
503 end
504
505 # syntaxobj.rb には Node が定義されているので、早い段階で require
506 require_tecsgen_lib 'tecslib/core/syntaxobj.rb'
507 require_tecsgen_lib 'tecslib/core/pluginModule.rb'
508 require_tecsgen_lib 'tecslib/core/plugin.rb'
509 require_tecsgen_lib 'tecslib/core/messages.rb'
510 require_tecsgen_lib 'tecslib/core/types.rb'
511 require_tecsgen_lib 'tecslib/core/value.rb'
512 require_tecsgen_lib 'tecslib/core/componentobj.rb'
513 require_tecsgen_lib 'tecslib/core/expression.rb'
514 require_tecsgen_lib 'tecslib/core/optimize.rb'
515 require_tecsgen_lib 'tecslib/core/tecsgen.rb'
516 require_tecsgen_lib 'tecslib/core/generate.rb'
517 require_tecsgen_lib 'tecslib/core/gen_xml.rb'
518 require_tecsgen_lib 'tecslib/core/tool_info.rb'
519 require_tecsgen_lib 'tecslib/core/tecsinfo.rb'
520 require_tecsgen_lib 'tecslib/plugin/CelltypePlugin.rb'
521 require_tecsgen_lib 'tecslib/plugin/CompositePlugin.rb'
522 require_tecsgen_lib 'tecslib/plugin/CellPlugin.rb'
523 require_tecsgen_lib 'tecslib/plugin/SignaturePlugin.rb'
524 require_tecsgen_lib 'tecslib/plugin/ThroughPlugin.rb'
525 require_tecsgen_lib 'tecslib/plugin/DomainPlugin.rb'
526 require_tecsgen_lib 'tecslib/plugin/MultiPlugin.rb'
527
528 # C 言語パーサ
529 require_tecsgen_lib 'tecslib/core/C_parser.tab.rb'
530 require_tecsgen_lib 'tecslib/core/ctypes.rb'
531
532 if $unit_test then
533 exit 1
534 end
535
536 end # load_modules
537
538 def self.setup
539 # $import_path, $tecspath を調整
540 TECSGEN.adjust_exerb_path
541
542 # $import_path に環境変数 $TECSGEN およびその直下を追加
543 if $no_default_import_path == false then
544 # $TECSGEN および、その直下のディレクトリをパスに追加
545 if $tecspath != "." then
546 TECSGEN.add_import_path $tecspath
547 dir = nil
548 begin
549 Dir.foreach( $tecspath ){ |f|
550 if f != "." && f != ".." && File.directory?( $tecspath + '/' + f ) then
551 TECSGEN.add_import_path( $tecspath + '/' + f )
552 end
553 }
554 rescue
555 # 無視
556 end
557 end
558 end
559
560 # デフォルト設定
561 TECSGEN.set_default_config
562
563 # $target の設定
564 $target = ARGV[0]
565 pos = $target.rindex( /[:\\\/]/ )
566 if pos then
567 $target = $target[pos+1..-1] # ディレクトリ区切りを除いた文字列
568 end
569 pos = $target.rindex( /\./ )
570 if pos then
571 $target = $target[0..pos-1] # 拡張子を取り除いた文字列
572 end
573
574 # gen ディレクトリの作成
575 begin
576 if ! File.directory?( $gen_base ) then
577 Dir.mkdir( $gen_base )
578 end
579 rescue
580 print( "Cannot mkdir #{$gen_base}. If the path has hierarchy, please create directory by manual.\n" )
581 exit 1
582 end
583 end # setup
584
585 #=== TECSGEN#get_celltype_list
586 def get_celltype_list
587 @celltype_list
588 end
589
590 #=== TECSGEN#get_cell_list
591 def get_cell_list
592 @cell_list
593 end
594
595 def get_root_namespace
596 @root_namespace
597 end
598end # TECSGEN
599
600# 複数のジェネレータインスタンスを生成することは、可能だが、以下の問題がある
601# クラス変数のリセットを確実に行う必要がある
602
603if $TECSCDE != true then
604 begin
605 TECSGEN.init
606 tecsgen = TECSGEN.new
607 tecsgen.run1
608 tecsgen.run2
609 rescue => evar
610 print_exception( evar )
611 STDERR << "tecsgen: exit because of unrecoverble error.\n"
612 STDERR << " please retry after resolve early error.\n"
613 STDERR << " if no error has occured, please report to TOPPERS TECS WG (users@toppers.jp or com-wg@toppers.jp).\n"
614 exit 1
615 end
616end
Note: See TracBrowser for help on using the repository browser.