Try ripping out inferred types because it might be a performance improvement
[WebKit-https.git] / Source / JavaScriptCore / generator / DSL.rb
1 # Copyright (C) 2018 Apple Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1. Redistributions of source code must retain the above copyright
7 #    notice, this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright
9 #    notice, this list of conditions and the following disclaimer in the
10 #    documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
13 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
14 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
16 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
22 # THE POSSIBILITY OF SUCH DAMAGE.
23
24 require_relative 'Assertion'
25 require_relative 'Section'
26 require_relative 'Template'
27 require_relative 'Type'
28 require_relative 'GeneratedFile'
29
30 module DSL
31     @sections = []
32     @current_section = nil
33     @context = binding()
34     @namespaces = []
35
36     def self.begin_section(name, config={})
37         assert("must call `end_section` before beginning a new section") { @current_section.nil? }
38         @current_section = Section.new name, config
39     end
40
41     def self.end_section(name)
42         assert("current section's name is `#{@current_section.name}`, but end_section was called with `#{name}`") { @current_section.name == name }
43         @current_section.sort!
44         @sections << @current_section
45         @current_section = nil
46     end
47
48     def self.op(name, config = {})
49         assert("`op` can only be called in between `begin_section` and `end_section`") { not @current_section.nil? }
50         @current_section.add_opcode(name, config)
51     end
52
53     def self.op_group(desc, ops, config)
54         assert("`op_group` can only be called in between `begin_section` and `end_section`") { not @current_section.nil? }
55         @current_section.add_opcode_group(desc, ops, config)
56     end
57
58     def self.types(types)
59         types.map do |type|
60             type = (@namespaces + [type]).join "::"
61             @context.eval("#{type} = Type.new '#{type}'")
62         end
63     end
64
65     def self.templates(types)
66         types.map do |type|
67             type = (@namespaces + [type]).join "::"
68             @context.eval("#{type} = Template.new '#{type}'")
69         end
70     end
71
72     def self.namespace(name)
73         @namespaces << name.to_s
74         ctx = @context
75         @context = @context.eval("
76             module #{name}
77               def self.get_binding
78                 binding()
79               end
80             end
81             #{name}.get_binding
82          ")
83         yield
84         @context = ctx
85         @namespaces.pop
86     end
87
88     def self.run(options)
89         bytecodeListPath = options[:bytecodeList]
90         bytecodeList = File.open(bytecodeListPath)
91         @context.eval(bytecodeList.read, bytecodeListPath)
92         assert("must end last section") { @current_section.nil? }
93
94         write_bytecodes(bytecodeList, options[:bytecodesFilename])
95         write_bytecode_structs(bytecodeList, options[:bytecodeStructsFilename])
96         write_init_asm(bytecodeList, options[:initAsmFilename])
97         write_indices(bytecodeList, options[:bytecodeIndicesFilename])
98     end
99
100     def self.write_bytecodes(bytecode_list, bytecodes_filename)
101         GeneratedFile::create(bytecodes_filename, bytecode_list) do |template|
102             template.prefix = "#pragma once\n"
103             num_opcodes = @sections.map(&:opcodes).flatten.size
104             template.body = <<-EOF
105 #{@sections.map { |s| s.header_helpers(num_opcodes) }.join("\n")}
106 #define FOR_EACH_BYTECODE_STRUCT(macro) \\
107 #{opcodes_for(:emit_in_structs_file).map { |op| "    macro(#{op.capitalized_name}) \\" }.join("\n")}
108             EOF
109         end
110     end
111
112     def self.write_bytecode_structs(bytecode_list, bytecode_structs_filename)
113         GeneratedFile::create(bytecode_structs_filename, bytecode_list) do |template|
114             opcodes = opcodes_for(:emit_in_structs_file)
115
116             template.prefix = <<-EOF
117 #pragma once
118
119 #include "ArithProfile.h"
120 #include "BytecodeDumper.h"
121 #include "BytecodeGenerator.h"
122 #include "Fits.h"
123 #include "GetByIdMetadata.h"
124 #include "Instruction.h"
125 #include "Opcode.h"
126 #include "PutByIdStatus.h"
127 #include "PutByIdFlags.h"
128 #include "ToThisStatus.h"
129
130 namespace JSC {
131 EOF
132
133             template.body = <<-EOF
134 #{opcodes.map(&:struct).join("\n")}
135 #{Opcode.dump_bytecode(opcodes)}
136 EOF
137             template.suffix = "} // namespace JSC"
138         end
139     end
140
141     def self.write_init_asm(bytecode_list, init_asm_filename)
142         opcodes = opcodes_for(:emit_in_asm_file)
143
144         GeneratedFile::create(init_asm_filename, bytecode_list) do |template|
145             template.multiline_comment = nil
146             template.line_comment = "#"
147             template.body = (opcodes.map.with_index(&:set_entry_address) + opcodes.map.with_index(&:set_entry_address_wide)) .join("\n")
148         end
149     end
150
151     def self.write_indices(bytecode_list, indices_filename)
152         opcodes = opcodes_for(:emit_in_structs_file)
153
154         GeneratedFile::create(indices_filename, bytecode_list) do |template|
155             template.prefix = "namespace JSC {\n"
156             template.body = opcodes.map(&:struct_indices).join("\n")
157             template.suffix = "\n} // namespace JSC"
158         end
159     end
160
161     def self.opcodes_for(file)
162         sections = @sections.select { |s| s.config[file] }
163         sections.map(&:opcodes).flatten
164     end
165 end