1 #
  2 # Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
  3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4 #
  5 # This code is free software; you can redistribute it and/or modify it
  6 # under the terms of the GNU General Public License version 2 only, as
  7 # published by the Free Software Foundation.  Oracle designates this
  8 # particular file as subject to the "Classpath" exception as provided
  9 # by Oracle in the LICENSE file that accompanied this code.
 10 #
 11 # This code is distributed in the hope that it will be useful, but WITHOUT
 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14 # version 2 for more details (a copy is included in the LICENSE file that
 15 # accompanied this code).
 16 #
 17 # You should have received a copy of the GNU General Public License version
 18 # 2 along with this work; if not, write to the Free Software Foundation,
 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20 #
 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22 # or visit www.oracle.com if you need additional information or have any
 23 # questions.
 24 #
 25 
 26 $(eval $(call IncludeCustomExtension, hotspot/gensrc/GensrcAdlc.gmk))
 27 
 28 ifeq ($(call check-jvm-feature, compiler2), true)
 29 
 30   ADLC_SUPPORT_DIR := $(JVM_SUPPORT_DIR)/adlc
 31 
 32   ##############################################################################
 33   # Build the ad compiler (the adlc build tool)
 34 
 35   # Flags depending on the build platform/tool chain
 36   # NOTE: No optimization or debug flags set here
 37   ifeq ($(call isBuildOs, linux), true)
 38     ADLC_CFLAGS := -fno-exceptions -DLINUX
 39   else ifeq ($(call isBuildOs, aix), true)
 40     ifeq ($(TOOLCHAIN_TYPE), clang)
 41       ADLC_LDFLAGS += -m64
 42       ADLC_CFLAGS := -fno-rtti -fexceptions -ffunction-sections -m64 -DAIX -mcpu=pwr8
 43     else
 44       ADLC_LDFLAGS += -q64
 45       ADLC_CFLAGS := -qnortti -qeh -q64 -DAIX
 46     endif
 47   else ifeq ($(call isBuildOs, windows), true)
 48     ADLC_LDFLAGS += -nologo
 49     ADLC_CFLAGS := -nologo -EHsc
 50     ADLC_CFLAGS_WARNINGS := -W3 -D_CRT_SECURE_NO_WARNINGS
 51   endif
 52 
 53   # Set the C++ standard
 54   ADLC_CFLAGS += $(ADLC_LANGSTD_CXXFLAG)
 55 
 56   # NOTE: The old build didn't set -DASSERT for windows but it doesn't seem to
 57   # hurt.
 58   ADLC_CFLAGS += -DASSERT
 59 
 60   ADLC_CFLAGS += -D$(HOTSPOT_TARGET_CPU_DEFINE)
 61 
 62   ADLC_CFLAGS += -I$(TOPDIR)/src/hotspot/share
 63 
 64   # Add file macro mappings
 65   ADLC_CFLAGS += $(FILE_MACRO_CFLAGS) $(REPRODUCIBLE_CFLAGS)
 66 
 67   ifeq ($(UBSAN_ENABLED), true)
 68     ADLC_CFLAGS += $(UBSAN_CFLAGS)
 69     ADLC_LDFLAGS += $(UBSAN_LDFLAGS)
 70   endif
 71 
 72   $(eval $(call SetupNativeCompilation, BUILD_ADLC, \
 73       NAME := adlc, \
 74       TYPE := EXECUTABLE, \
 75       TOOLCHAIN := TOOLCHAIN_BUILD_LINK_CXX, \
 76       SRC := $(TOPDIR)/src/hotspot/share/adlc, \
 77       EXTRA_FILES := $(TOPDIR)/src/hotspot/share/opto/opcodes.cpp, \
 78       CFLAGS := $(ADLC_CFLAGS) $(ADLC_CFLAGS_WARNINGS), \
 79       LDFLAGS := $(ADLC_LDFLAGS), \
 80       LIBS := $(ADLC_LIBS), \
 81       OBJECT_DIR := $(JVM_VARIANT_OUTPUTDIR)/tools/adlc/objs, \
 82       OUTPUT_DIR := $(JVM_VARIANT_OUTPUTDIR)/tools/adlc, \
 83       DEBUG_SYMBOLS := false, \
 84       DISABLED_WARNINGS_clang := tautological-compare, \
 85   ))
 86 
 87   ADLC_TOOL := $(BUILD_ADLC_TARGET)
 88 
 89   ##############################################################################
 90   # Transform the ad source files into C++ source files using adlc
 91 
 92   # Setup flags for the adlc build tool (ADLCFLAGS).
 93   ADLCFLAGS += -q -T
 94 
 95   # ADLC flags depending on target OS
 96   ifeq ($(call isTargetOs, linux), true)
 97     ADLCFLAGS += -DLINUX=1 -D_GNU_SOURCE=1
 98   else ifeq ($(call isTargetOs, aix), true)
 99     ADLCFLAGS += -DAIX=1
100   else ifeq ($(call isTargetOs, macosx), true)
101     ADLCFLAGS += -D_ALLBSD_SOURCE=1 -D_GNU_SOURCE=1
102     ifeq ($(HOTSPOT_TARGET_CPU_ARCH), aarch64)
103       ADLCFLAGS += -DR18_RESERVED
104     endif
105   else ifeq ($(call isTargetOs, windows), true)
106     ifeq ($(call isTargetCpuBits, 64), true)
107       ADLCFLAGS += -D_WIN64=1
108     endif
109     ifeq ($(HOTSPOT_TARGET_CPU_ARCH), aarch64)
110       ADLCFLAGS += -DR18_RESERVED
111     endif
112   endif
113 
114   ifeq ($(call isTargetOs, windows), false)
115     # NOTE: Windows adlc flags was different in the old build. Is this really
116     # correct?
117 
118     # -g makes #line directives in the generated C++ files.
119     ADLCFLAGS += -g
120 
121     ADLCFLAGS += -D$(HOTSPOT_TARGET_CPU_DEFINE)=1
122   endif
123 
124   # This generates checks in the generated C++ files that _LP64 is correctly
125   # (un)defined when compiling them.
126   ifeq ($(call isTargetCpuBits, 64), true)
127     ADLCFLAGS += -D_LP64=1
128   else
129     ADLCFLAGS += -U_LP64
130   endif
131 
132   ifeq ($(HOTSPOT_TARGET_CPU_ARCH), arm)
133     ADLCFLAGS += -DARM=1
134   endif
135 
136   # Set ASSERT, NDEBUG and PRODUCT flags just like in JvmFlags.gmk
137   ifeq ($(DEBUG_LEVEL), release)
138     # release builds disable uses of assert macro from <assert.h>.
139     ADLCFLAGS += -DNDEBUG
140     # For hotspot, release builds differ internally between "optimized" and "product"
141     # in that "optimize" does not define PRODUCT.
142     ifneq ($(HOTSPOT_DEBUG_LEVEL), optimized)
143       ADLCFLAGS += -DPRODUCT
144     endif
145   else ifeq ($(DEBUG_LEVEL), fastdebug)
146     ADLCFLAGS += -DASSERT
147   else ifeq ($(DEBUG_LEVEL), slowdebug)
148     ADLCFLAGS += -DASSERT
149   endif
150 
151   ##############################################################################
152   # Concatenate all ad source files into a single file, which will be fed to
153   # adlc. Also include a #line directive at the start of every included file
154   # (after the initial header block), stating the original source file name.
155   #
156   # Normally, debugging is done directly on the ad_<arch>*.cpp files, but the
157   # #line directives in those files will be pointing back to <arch>.ad.
158 
159   # AD_SRC_ROOTS might have been added to by a custom extension
160   AD_SRC_ROOTS += $(TOPDIR)/src/hotspot
161 
162   AD_SRC_FILES := $(call uniq, $(wildcard $(foreach d, $(AD_SRC_ROOTS), \
163       $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/$(HOTSPOT_TARGET_CPU).ad \
164       $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/$(HOTSPOT_TARGET_CPU_ARCH).ad \
165       $d/os_cpu/$(HOTSPOT_TARGET_OS)_$(HOTSPOT_TARGET_CPU_ARCH)/$(HOTSPOT_TARGET_OS)_$(HOTSPOT_TARGET_CPU_ARCH).ad \
166     )))
167 
168   ifeq ($(HOTSPOT_TARGET_CPU_ARCH), aarch64)
169     AD_SRC_FILES += $(call uniq, $(wildcard $(foreach d, $(AD_SRC_ROOTS), \
170         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/$(HOTSPOT_TARGET_CPU_ARCH)_vector.ad \
171     )))
172   endif
173 
174   ifeq ($(HOTSPOT_TARGET_CPU_ARCH), riscv)
175     AD_SRC_FILES += $(call uniq, $(wildcard $(foreach d, $(AD_SRC_ROOTS), \
176         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/$(HOTSPOT_TARGET_CPU_ARCH)_v.ad \
177         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/$(HOTSPOT_TARGET_CPU_ARCH)_b.ad \
178     )))
179   endif
180 
181   ifeq ($(call check-jvm-feature, shenandoahgc), true)
182     AD_SRC_FILES += $(call uniq, $(wildcard $(foreach d, $(AD_SRC_ROOTS), \
183         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/gc/shenandoah/shenandoah_$(HOTSPOT_TARGET_CPU).ad \
184         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/gc/shenandoah/shenandoah_$(HOTSPOT_TARGET_CPU_ARCH).ad \
185       )))
186   endif
187 
188   ifeq ($(call check-jvm-feature, zgc), true)
189     AD_SRC_FILES += $(call uniq, $(wildcard $(foreach d, $(AD_SRC_ROOTS), \
190         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/gc/x/x_$(HOTSPOT_TARGET_CPU).ad \
191         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/gc/x/x_$(HOTSPOT_TARGET_CPU_ARCH).ad \
192         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/gc/z/z_$(HOTSPOT_TARGET_CPU).ad \
193         $d/cpu/$(HOTSPOT_TARGET_CPU_ARCH)/gc/z/z_$(HOTSPOT_TARGET_CPU_ARCH).ad \
194       )))
195   endif
196 
197   SINGLE_AD_SRCFILE := $(ADLC_SUPPORT_DIR)/all-ad-src.ad
198 
199   INSERT_FILENAME_AWK_SCRIPT := \
200       '{ \
201          if (CUR_FN != FILENAME) { CUR_FN=FILENAME; NR_BASE=NR-1; need_lineno=1 } \
202          if (need_lineno && $$0 !~ /\/\//) \
203            { print "\n\n\#line " (NR-NR_BASE) " \"" FILENAME "\""; need_lineno=0 }; \
204          print \
205        }'
206 
207   $(SINGLE_AD_SRCFILE): $(AD_SRC_FILES)
208 	$(call LogInfo, Preprocessing adlc files $(^F))
209 	$(call MakeDir, $(@D))
210 	$(AWK) $(INSERT_FILENAME_AWK_SCRIPT) $^ > $@
211 
212   ##############################################################################
213   # Run the adlc tool on the single concatenated ad source file, and store the
214   # output in support/adlc for further processing.
215   $(eval $(call SetupExecute, adlc_run, \
216       INFO := Generating adlc files, \
217       DEPS := $(BUILD_ADLC) $(SINGLE_AD_SRCFILE), \
218       OUTPUT_DIR := $(ADLC_SUPPORT_DIR), \
219       COMMAND := $(FIXPATH) $(ADLC_TOOL) $(ADLCFLAGS) $(SINGLE_AD_SRCFILE) \
220           -c$(ADLC_SUPPORT_DIR)/ad_$(HOTSPOT_TARGET_CPU_ARCH).cpp \
221           -h$(ADLC_SUPPORT_DIR)/ad_$(HOTSPOT_TARGET_CPU_ARCH).hpp \
222           -a$(ADLC_SUPPORT_DIR)/dfa_$(HOTSPOT_TARGET_CPU_ARCH).cpp \
223           -v$(ADLC_SUPPORT_DIR)/adGlobals_$(HOTSPOT_TARGET_CPU_ARCH).hpp, \
224   ))
225 
226   ##############################################################################
227   # Finally copy the generated files from support/adlc into gensrc/adfiles,
228   # and postprocess them by fixing dummy #line directives.
229 
230   ADLC_GENERATED_FILES := $(addprefix $(JVM_VARIANT_OUTPUTDIR)/gensrc/adfiles/, \
231       ad_$(HOTSPOT_TARGET_CPU_ARCH).cpp \
232       ad_$(HOTSPOT_TARGET_CPU_ARCH).hpp \
233       ad_$(HOTSPOT_TARGET_CPU_ARCH)_clone.cpp \
234       ad_$(HOTSPOT_TARGET_CPU_ARCH)_expand.cpp \
235       ad_$(HOTSPOT_TARGET_CPU_ARCH)_format.cpp \
236       ad_$(HOTSPOT_TARGET_CPU_ARCH)_gen.cpp \
237       ad_$(HOTSPOT_TARGET_CPU_ARCH)_misc.cpp \
238       ad_$(HOTSPOT_TARGET_CPU_ARCH)_peephole.cpp \
239       ad_$(HOTSPOT_TARGET_CPU_ARCH)_pipeline.cpp \
240       adGlobals_$(HOTSPOT_TARGET_CPU_ARCH).hpp \
241       dfa_$(HOTSPOT_TARGET_CPU_ARCH).cpp \
242   )
243 
244   $(JVM_VARIANT_OUTPUTDIR)/gensrc/adfiles/%: $(adlc_run_TARGET)
245 	$(call LogInfo, Postprocessing adlc file $*)
246 	$(call MakeDir, $(@D))
247 	$(AWK) \
248 	    'BEGIN { print "#line 1 \"$*\""; } \
249 	     /^#line 999999$$/ {print "#line " (NR+1) " \"$*\""; next} \
250 	     $(if $(call equals, $(ALLOW_ABSOLUTE_PATHS_IN_OUTPUT), false), \
251 	       /^#line .*$$/ {sub("$(WORKSPACE_ROOT)/","")} \
252 	     ) \
253 	     {print}' \
254 	    < $(ADLC_SUPPORT_DIR)/$* > $@
255 
256   TARGETS += $(ADLC_GENERATED_FILES)
257 
258 endif