[337] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 | #
|
---|
| 3 | # TECS Generator
|
---|
| 4 | # Generator for TOPPERS Embedded Component System
|
---|
| 5 | #
|
---|
[374] | 6 | # Copyright (C) 2008-2017 by TOPPERS Project
|
---|
[337] | 7 | #--
|
---|
| 8 | # 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 9 | # ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 10 | # 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 11 | # (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 12 | # 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 13 | # スコード中に含まれていること.
|
---|
| 14 | # (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 15 | # 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 16 | # 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 17 | # の無保証規定を掲載すること.
|
---|
| 18 | # (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 19 | # 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 20 | # と.
|
---|
| 21 | # (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 22 | # 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 23 | # (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 24 | # 報告すること.
|
---|
| 25 | # (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 26 | # 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 27 | # また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 28 | # 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 29 | # 免責すること.
|
---|
| 30 | #
|
---|
| 31 | # 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 32 | # よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 33 | # に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 34 | # アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 35 | # の責任を負わない.
|
---|
| 36 | #
|
---|
[359] | 37 | # $Id$
|
---|
[337] | 38 | #++
|
---|
| 39 |
|
---|
| 40 | #= TOOL_INFO class
|
---|
| 41 | # The syntax of the contents of __tool_info__ is same as JSON.
|
---|
| 42 | # Home made schema is used to validate the contents of __tool_info__.
|
---|
| 43 | # the schema is not defiened well. see saveload.rb for example of the schema.
|
---|
| 44 | #
|
---|
| 45 | class TOOL_INFO
|
---|
| 46 |
|
---|
| 47 | # tool_info
|
---|
| 48 | @@tool_info = { }
|
---|
| 49 |
|
---|
| 50 | # tool_info schema for tecsgen
|
---|
| 51 | @@TECSGEN_schema = {
|
---|
| 52 | :tecsgen => { # require
|
---|
| 53 | :base_dir => [ :string ], # dir where the cde created initially
|
---|
| 54 | :direct_import => [ :string ], # .cdl (sometimes .cde) specified in argments
|
---|
| 55 | :import_path => [ :string ], # -I of tecsgen
|
---|
| 56 | :define_macro => [ :string ], # -D of tecsgen
|
---|
| 57 | :tecscde_version=> :string, # TECSCDE version
|
---|
| 58 | :cde_format_version=> :string, # CDE format version
|
---|
| 59 | :save_date => :string, # last save date & time
|
---|
| 60 | },
|
---|
| 61 | :__tecsgen => { # optioanl
|
---|
| 62 | :cpp => :string # -c or TECS_CPP environment variable
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | def initialize( name, val )
|
---|
| 67 | @@tool_info[name] = val
|
---|
| 68 |
|
---|
| 69 | # __tool_info__( "tecsgen" ): validate & reflect immediately
|
---|
| 70 | p "tool_info: tecsgen #{name}"
|
---|
| 71 | if name == :tecsgen then
|
---|
| 72 | set_tecsgen_tool_info
|
---|
| 73 | end
|
---|
| 74 | end
|
---|
| 75 |
|
---|
| 76 | def self.get_tool_info name
|
---|
| 77 | @@tool_info[ name ]
|
---|
| 78 | end
|
---|
| 79 |
|
---|
| 80 | #=== TOOL_INFO#set_tecsgen_tool_info
|
---|
| 81 | # __tool_info__( "tecsgen" )
|
---|
| 82 | def set_tecsgen_tool_info
|
---|
| 83 | validator = TOOL_INFO::VALIDATOR.new( :tecsgen, @@TECSGEN_schema )
|
---|
| 84 | if validator.validate || $b_force_apply_tool_info
|
---|
| 85 | info = TOOL_INFO.get_tool_info( :tecsgen )
|
---|
| 86 | (info[ :base_dir ]).each{ |bd|
|
---|
| 87 | if ! $base_dir.include? bd
|
---|
| 88 | $base_dir[ bd ] = true
|
---|
| 89 | end
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | info[ :import_path ].each{ |path|
|
---|
| 93 | if ! $import_path.include?( path )
|
---|
| 94 | $import_path << path
|
---|
| 95 | end
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | info[ :define_macro ].each{ |define|
|
---|
| 99 | if ! $define.include?( define )
|
---|
| 100 | $define << define
|
---|
| 101 | end
|
---|
| 102 | }
|
---|
| 103 | if info[ :cpp ]
|
---|
| 104 | if ! $b_cpp_specified
|
---|
| 105 | $cpp = info[ :cpp ]
|
---|
| 106 | $b_cpp_specified = true
|
---|
| 107 | end
|
---|
| 108 | end
|
---|
| 109 |
|
---|
| 110 | info[ :direct_import ] && info[ :direct_import ].each{ |import|
|
---|
| 111 | Import.new( import, false, false )
|
---|
| 112 | }
|
---|
| 113 | end
|
---|
| 114 | end
|
---|
| 115 |
|
---|
| 116 | #== simple JSON validator
|
---|
| 117 | # this validator use simple schema.
|
---|
| 118 | # array length cannot be checked.
|
---|
| 119 | # you have to check array length in your code, if necessary.
|
---|
| 120 | class VALIDATOR
|
---|
| 121 |
|
---|
| 122 | #@b_ok::Bool
|
---|
| 123 |
|
---|
| 124 | def error( msg )
|
---|
| 125 | STDERR.print( "__tool_info__: " + msg )
|
---|
| 126 | @b_ok = false
|
---|
| 127 | end
|
---|
| 128 |
|
---|
| 129 | def initialize name, schema
|
---|
| 130 | @name = name
|
---|
| 131 | @schema = schema
|
---|
| 132 | @b_ok = true
|
---|
| 133 | end
|
---|
| 134 |
|
---|
| 135 | #=== VALIDATOR#validate
|
---|
| 136 | def validate
|
---|
| 137 | info = TOOL_INFO.get_tool_info @name
|
---|
| 138 | if info == nil
|
---|
| 139 | error( "\"#{@name}\" not found\n" )
|
---|
| 140 | return @b_ok
|
---|
| 141 | end
|
---|
| 142 | validate_object info, @name, @name
|
---|
| 143 | return @b_ok
|
---|
| 144 | end
|
---|
| 145 |
|
---|
| 146 | def validate_object( object, require_type, path )
|
---|
| 147 | obj_type = @schema[ require_type ]
|
---|
| 148 | dbgPrint "validate_object: #{path} object=#{obj_type[ :type ]} required=#{object[:type]}\n"
|
---|
| 149 |
|
---|
| 150 | obj_type.each{ |name, val_type|
|
---|
| 151 | val = object[name]
|
---|
| 152 | path2 = path.to_s + "." + name.to_s
|
---|
| 153 | if val == nil
|
---|
| 154 | error( "#{path}: required property not found '#{name}'\n" )
|
---|
| 155 | next
|
---|
| 156 | end
|
---|
| 157 | if val_type.kind_of? Array
|
---|
| 158 | validate_array_member val, val_type, path2
|
---|
| 159 | else
|
---|
| 160 | validate_types val, val_type, path2
|
---|
| 161 | end
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | optional = @schema[ ("__" + require_type.to_s).to_sym ]
|
---|
| 165 | if optional
|
---|
| 166 | dbgPrint "#{require_type.to_s} has optional\n"
|
---|
| 167 |
|
---|
| 168 | optional.each{ |name, val_type|
|
---|
| 169 | val = object[name]
|
---|
| 170 | path2 = path.to_s + "." + name.to_s
|
---|
| 171 | if val == nil
|
---|
| 172 | # no need to occur error
|
---|
| 173 | # error( "#{path}: required property not found '#{name}'\n" )
|
---|
| 174 | next
|
---|
| 175 | end
|
---|
| 176 | if val_type.kind_of? Array
|
---|
| 177 | validate_array_member val, val_type, path2
|
---|
| 178 | else
|
---|
| 179 | validate_types val, val_type, path2
|
---|
| 180 | end
|
---|
| 181 | }
|
---|
| 182 | end
|
---|
| 183 |
|
---|
| 184 | end
|
---|
| 185 |
|
---|
| 186 | def validate_array_member array, val_types, path
|
---|
| 187 | if ! array.kind_of? Array
|
---|
| 188 | error( "#{path2}: array required as value\n" )
|
---|
| 189 | return
|
---|
| 190 | end
|
---|
| 191 | index=0
|
---|
| 192 | array.each{ |member|
|
---|
| 193 | type = get_object_type member
|
---|
| 194 | i = val_types.find_index type
|
---|
| 195 | if i == nil
|
---|
| 196 | if type == :integer
|
---|
| 197 | i = val_types.find_index :number
|
---|
| 198 | end
|
---|
| 199 | end
|
---|
| 200 | if i == nil
|
---|
| 201 | error( "#{path}: array member type mismatch, #{type} for #{val_types}\n" )
|
---|
| 202 | next
|
---|
| 203 | end
|
---|
| 204 | val_type = val_types[ i ]
|
---|
| 205 | validate_types member, val_type, (path.to_s + '[' + index.to_s + '].')
|
---|
| 206 | index += 1
|
---|
| 207 | }
|
---|
| 208 | end
|
---|
| 209 |
|
---|
| 210 | #=== TOOL_INFO::VALIDATOR#validate_types
|
---|
| 211 | #obj::Object (Integer, Floating, String, Hash)
|
---|
| 212 | #val_type::Symbol : required object type
|
---|
| 213 | def validate_types obj, val_type, path
|
---|
| 214 | type = get_object_type obj
|
---|
| 215 | case val_type
|
---|
| 216 | when :integer
|
---|
| 217 | if type == :integer
|
---|
| 218 | return
|
---|
| 219 | end
|
---|
| 220 | when :number
|
---|
| 221 | if type == :integer || type == :number
|
---|
| 222 | return
|
---|
| 223 | end
|
---|
| 224 | when :string
|
---|
| 225 | if type == :string
|
---|
| 226 | return
|
---|
| 227 | end
|
---|
| 228 | # when :nil # mikan
|
---|
| 229 | # when :bool # mikan
|
---|
| 230 | else # object or fixed string
|
---|
| 231 | if val_type.kind_of? String
|
---|
| 232 | # val is specified by String
|
---|
| 233 | if type == :string
|
---|
| 234 | if obj == val_type
|
---|
| 235 | return
|
---|
| 236 | end
|
---|
| 237 | end
|
---|
| 238 | else
|
---|
| 239 | if type.to_sym == val_type
|
---|
| 240 | validate_object( obj, val_type, path + val_type.to_s )
|
---|
| 241 | return
|
---|
| 242 | end
|
---|
| 243 | end
|
---|
| 244 | end
|
---|
| 245 | error( "#{path}: type mismatch, #{type} for #{val_type}\n" )
|
---|
| 246 | end
|
---|
| 247 |
|
---|
| 248 | def get_object_type obj
|
---|
| 249 | # p "#{obj.class} #{obj.to_s}"
|
---|
| 250 | if obj.kind_of? Integer
|
---|
| 251 | return :integer
|
---|
| 252 | elsif obj.kind_of? Float
|
---|
| 253 | return :number
|
---|
| 254 | elsif obj.kind_of? String
|
---|
| 255 | return :string
|
---|
| 256 | elsif obj.kind_of? Hash
|
---|
| 257 | return obj[ :type ].to_sym
|
---|
| 258 | end
|
---|
| 259 | return nil
|
---|
| 260 | end
|
---|
| 261 | end
|
---|
| 262 |
|
---|
| 263 | end
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | #---------- obsolete -------#
|
---|
| 267 |
|
---|
| 268 | #--- TOOL_INFO replaced location information ---#
|
---|
| 269 |
|
---|
| 270 | class TECSGEN
|
---|
| 271 |
|
---|
| 272 | #------ manupulate location information --------#
|
---|
| 273 | def self.new_cell_location cell_location
|
---|
| 274 | @@current_tecsgen.new_cell_location cell_location
|
---|
| 275 | end
|
---|
| 276 | def new_cell_location cell_location
|
---|
| 277 | @cell_location_list << cell_location
|
---|
| 278 | end
|
---|
| 279 | def get_cell_location_list
|
---|
| 280 | @cell_location_list
|
---|
| 281 | end
|
---|
| 282 |
|
---|
| 283 | def self.new_join_location join_location
|
---|
| 284 | @@current_tecsgen.new_join_location join_location
|
---|
| 285 | end
|
---|
| 286 | def new_join_location join_location
|
---|
| 287 | @join_location_list << join_location
|
---|
| 288 | end
|
---|
| 289 | def get_join_location_list
|
---|
| 290 | @join_location_list
|
---|
| 291 | end
|
---|
| 292 |
|
---|
| 293 | #== Cell_location
|
---|
| 294 | # tecscde の位置情報
|
---|
| 295 | class Cell_location
|
---|
| 296 |
|
---|
| 297 | #=== Join_location#initialize
|
---|
| 298 | #cell_nspath::NamespacePath
|
---|
| 299 | #x,y,w,h::Expression
|
---|
| 300 | #port_location_list::[ [Symbol(ep_or_cp_name), Symbol(edge_name), Expression(offset)], ... ]
|
---|
| 301 | #ep_name::Symbol
|
---|
| 302 | def initialize( cell_nspath, x, y, w, h, port_location_list )
|
---|
| 303 | # p "Cell_location: #{cell_nspath}, #{x}, #{y}, #{w}, #{h}, #{port_location_list}"
|
---|
| 304 | @cell_nspath = cell_nspath
|
---|
| 305 | @x = x.eval_const nil
|
---|
| 306 | @y = y.eval_const nil
|
---|
| 307 | @w = w.eval_const nil
|
---|
| 308 | @h = h.eval_const nil
|
---|
| 309 | @port_location_list = port_location_list
|
---|
| 310 |
|
---|
| 311 | TECSGEN.new_cell_location self
|
---|
| 312 | end
|
---|
| 313 |
|
---|
| 314 | def get_location
|
---|
| 315 | [ @cell_nspath, @x, @y, @w, @h, @port_location_list ]
|
---|
| 316 | end
|
---|
| 317 |
|
---|
| 318 | end # Cell_location
|
---|
| 319 |
|
---|
| 320 | #== Join_location
|
---|
| 321 | # tecscde の位置情報
|
---|
| 322 | class Join_location
|
---|
| 323 | @@join_location_list = []
|
---|
| 324 |
|
---|
| 325 | #=== Join_location#initialize
|
---|
| 326 | #cp_cell_nspath::NamespacePath
|
---|
| 327 | #cp_name::Symbol
|
---|
| 328 | #ep_cell_nspath::NamespacePath
|
---|
| 329 | #ep_name::Symbol
|
---|
| 330 | #bar_list::[[Symbol (VBar or HBar), Expression(position mm)], ....]
|
---|
| 331 | def initialize( cp_cell_nspath, cp_name, ep_cell_path, ep_name, bar_list )
|
---|
| 332 | # p "Join_location #{cp_cell_nspath}, #{cp_name}, #{ep_cell_path}, #{ep_name} #{bar_list}"
|
---|
| 333 | @cp_cell_nspath = cp_cell_nspath
|
---|
| 334 | @cp_name = cp_name
|
---|
| 335 | @ep_cell_path = ep_cell_path
|
---|
| 336 | @ep_name = ep_name
|
---|
| 337 | @bar_list = bar_list
|
---|
| 338 |
|
---|
| 339 | TECSGEN.new_join_location self
|
---|
| 340 | end
|
---|
| 341 |
|
---|
| 342 | def get_location
|
---|
| 343 | [@cp_cell_nspath, @cp_name, @ep_cell_path, @ep_name, @bar_list]
|
---|
| 344 | end
|
---|
| 345 |
|
---|
| 346 | end # Join_location
|
---|
| 347 |
|
---|
| 348 | end # TECSGEN
|
---|