1# This directory contains a large amount of C code which provides
2# generic implementations of the core runtime library along with optimized
3# architecture-specific code in various subdirectories.
4
5if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
6  cmake_minimum_required(VERSION 3.4.3)
7
8  project(CompilerRTBuiltins C ASM)
9  set(COMPILER_RT_STANDALONE_BUILD TRUE)
10  set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
11  list(INSERT CMAKE_MODULE_PATH 0
12    "${CMAKE_SOURCE_DIR}/../../cmake"
13    "${CMAKE_SOURCE_DIR}/../../cmake/Modules")
14  include(base-config-ix)
15  include(CompilerRTUtils)
16
17  load_llvm_config()
18  construct_compiler_rt_default_triple()
19
20  if(APPLE)
21    include(CompilerRTDarwinUtils)
22  endif()
23  if(CMAKE_HOST_APPLE AND APPLE)
24    include(UseLibtool)
25  endif()
26  include(AddCompilerRT)
27endif()
28
29include(builtin-config-ix)
30
31# TODO: Need to add a mechanism for logging errors when builtin source files are
32# added to a sub-directory and not this CMakeLists file.
33set(GENERIC_SOURCES
34  absvdi2.c
35  absvsi2.c
36  absvti2.c
37  adddf3.c
38  addsf3.c
39  addtf3.c
40  addvdi3.c
41  addvsi3.c
42  addvti3.c
43  apple_versioning.c
44  ashldi3.c
45  ashlti3.c
46  ashrdi3.c
47  ashrti3.c
48  bswapdi2.c
49  bswapsi2.c
50  clzdi2.c
51  clzsi2.c
52  clzti2.c
53  cmpdi2.c
54  cmpti2.c
55  comparedf2.c
56  comparesf2.c
57  ctzdi2.c
58  ctzsi2.c
59  ctzti2.c
60  divdc3.c
61  divdf3.c
62  divdi3.c
63  divmoddi4.c
64  divmodsi4.c
65  divsc3.c
66  divsf3.c
67  divsi3.c
68  divtc3.c
69  divti3.c
70  divtf3.c
71  extendsfdf2.c
72  extendhfsf2.c
73  ffsdi2.c
74  ffssi2.c
75  ffsti2.c
76  fixdfdi.c
77  fixdfsi.c
78  fixdfti.c
79  fixsfdi.c
80  fixsfsi.c
81  fixsfti.c
82  fixunsdfdi.c
83  fixunsdfsi.c
84  fixunsdfti.c
85  fixunssfdi.c
86  fixunssfsi.c
87  fixunssfti.c
88  floatdidf.c
89  floatdisf.c
90  floatsidf.c
91  floatsisf.c
92  floattidf.c
93  floattisf.c
94  floatundidf.c
95  floatundisf.c
96  floatunsidf.c
97  floatunsisf.c
98  floatuntidf.c
99  floatuntisf.c
100  fp_mode.c
101  int_util.c
102  lshrdi3.c
103  lshrti3.c
104  moddi3.c
105  modsi3.c
106  modti3.c
107  muldc3.c
108  muldf3.c
109  muldi3.c
110  mulodi4.c
111  mulosi4.c
112  muloti4.c
113  mulsc3.c
114  mulsf3.c
115  multi3.c
116  multf3.c
117  mulvdi3.c
118  mulvsi3.c
119  mulvti3.c
120  negdf2.c
121  negdi2.c
122  negsf2.c
123  negti2.c
124  negvdi2.c
125  negvsi2.c
126  negvti2.c
127  os_version_check.c
128  paritydi2.c
129  paritysi2.c
130  parityti2.c
131  popcountdi2.c
132  popcountsi2.c
133  popcountti2.c
134  powidf2.c
135  powisf2.c
136  powitf2.c
137  subdf3.c
138  subsf3.c
139  subvdi3.c
140  subvsi3.c
141  subvti3.c
142  subtf3.c
143  trampoline_setup.c
144  truncdfhf2.c
145  truncdfsf2.c
146  truncsfhf2.c
147  ucmpdi2.c
148  ucmpti2.c
149  udivdi3.c
150  udivmoddi4.c
151  udivmodsi4.c
152  udivmodti4.c
153  udivsi3.c
154  udivti3.c
155  umoddi3.c
156  umodsi3.c
157  umodti3.c
158)
159
160set(GENERIC_TF_SOURCES
161  comparetf2.c
162  extenddftf2.c
163  extendsftf2.c
164  fixtfdi.c
165  fixtfsi.c
166  fixtfti.c
167  fixunstfdi.c
168  fixunstfsi.c
169  fixunstfti.c
170  floatditf.c
171  floatsitf.c
172  floattitf.c
173  floatunditf.c
174  floatunsitf.c
175  floatuntitf.c
176  multc3.c
177  trunctfdf2.c
178  trunctfsf2.c
179)
180
181option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
182  "Skip the atomic builtin (these should normally be provided by a shared library)"
183  On)
184
185if(NOT FUCHSIA AND NOT COMPILER_RT_BAREMETAL_BUILD)
186  set(GENERIC_SOURCES
187    ${GENERIC_SOURCES}
188    emutls.c
189    enable_execute_stack.c
190    eprintf.c
191  )
192endif()
193
194if(COMPILER_RT_HAS_ATOMIC_KEYWORD AND NOT COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN)
195  set(GENERIC_SOURCES
196    ${GENERIC_SOURCES}
197    atomic.c
198  )
199endif()
200
201if(APPLE)
202  set(GENERIC_SOURCES
203    ${GENERIC_SOURCES}
204    atomic_flag_clear.c
205    atomic_flag_clear_explicit.c
206    atomic_flag_test_and_set.c
207    atomic_flag_test_and_set_explicit.c
208    atomic_signal_fence.c
209    atomic_thread_fence.c
210  )
211endif()
212
213if (HAVE_UNWIND_H)
214  set(GENERIC_SOURCES
215    ${GENERIC_SOURCES}
216    gcc_personality_v0.c
217  )
218endif ()
219
220if (NOT FUCHSIA)
221  set(GENERIC_SOURCES
222    ${GENERIC_SOURCES}
223    clear_cache.c
224  )
225endif()
226
227# These sources work on all x86 variants, but only x86 variants.
228set(x86_ARCH_SOURCES
229  cpu_model.c
230  divxc3.c
231  fixxfdi.c
232  fixxfti.c
233  fixunsxfdi.c
234  fixunsxfsi.c
235  fixunsxfti.c
236  floatdixf.c
237  floattixf.c
238  floatundixf.c
239  floatuntixf.c
240  mulxc3.c
241  powixf2.c
242)
243
244if (NOT MSVC)
245  set(x86_ARCH_SOURCES
246    ${x86_ARCH_SOURCES}
247    i386/fp_mode.c
248  )
249endif ()
250
251if (NOT MSVC)
252  set(x86_64_SOURCES
253    ${GENERIC_TF_SOURCES}
254    x86_64/floatdidf.c
255    x86_64/floatdisf.c
256    x86_64/floatdixf.c
257    x86_64/floatundidf.S
258    x86_64/floatundisf.S
259    x86_64/floatundixf.S
260  )
261  filter_builtin_sources(x86_64_SOURCES EXCLUDE x86_64_SOURCES "${x86_64_SOURCES};${GENERIC_SOURCES}")
262  set(x86_64h_SOURCES ${x86_64_SOURCES})
263
264  if (WIN32)
265    set(x86_64_SOURCES
266      ${x86_64_SOURCES}
267      x86_64/chkstk.S
268      x86_64/chkstk2.S
269    )
270  endif()
271
272  set(i386_SOURCES
273    i386/ashldi3.S
274    i386/ashrdi3.S
275    i386/divdi3.S
276    i386/floatdidf.S
277    i386/floatdisf.S
278    i386/floatdixf.S
279    i386/floatundidf.S
280    i386/floatundisf.S
281    i386/floatundixf.S
282    i386/lshrdi3.S
283    i386/moddi3.S
284    i386/muldi3.S
285    i386/udivdi3.S
286    i386/umoddi3.S
287  )
288  filter_builtin_sources(i386_SOURCES EXCLUDE i386_SOURCES "${i386_SOURCES};${GENERIC_SOURCES}")
289
290  if (WIN32)
291    set(i386_SOURCES
292      ${i386_SOURCES}
293      i386/chkstk.S
294      i386/chkstk2.S
295    )
296  endif()
297else () # MSVC
298  # Use C versions of functions when building on MSVC
299  # MSVC's assembler takes Intel syntax, not AT&T syntax.
300  # Also use only MSVC compilable builtin implementations.
301  set(x86_64_SOURCES
302    x86_64/floatdidf.c
303    x86_64/floatdisf.c
304    x86_64/floatdixf.c
305    ${GENERIC_SOURCES}
306  )
307  set(x86_64h_SOURCES ${x86_64_SOURCES})
308  set(i386_SOURCES ${GENERIC_SOURCES})
309endif () # if (NOT MSVC)
310
311set(x86_64h_SOURCES ${x86_64h_SOURCES} ${x86_ARCH_SOURCES})
312set(x86_64_SOURCES ${x86_64_SOURCES} ${x86_ARCH_SOURCES})
313set(i386_SOURCES ${i386_SOURCES} ${x86_ARCH_SOURCES})
314set(i686_SOURCES ${i686_SOURCES} ${x86_ARCH_SOURCES})
315
316set(arm_SOURCES
317  arm/fp_mode.c
318  arm/bswapdi2.S
319  arm/bswapsi2.S
320  arm/clzdi2.S
321  arm/clzsi2.S
322  arm/comparesf2.S
323  arm/divmodsi4.S
324  arm/divsi3.S
325  arm/modsi3.S
326  arm/sync_fetch_and_add_4.S
327  arm/sync_fetch_and_add_8.S
328  arm/sync_fetch_and_and_4.S
329  arm/sync_fetch_and_and_8.S
330  arm/sync_fetch_and_max_4.S
331  arm/sync_fetch_and_max_8.S
332  arm/sync_fetch_and_min_4.S
333  arm/sync_fetch_and_min_8.S
334  arm/sync_fetch_and_nand_4.S
335  arm/sync_fetch_and_nand_8.S
336  arm/sync_fetch_and_or_4.S
337  arm/sync_fetch_and_or_8.S
338  arm/sync_fetch_and_sub_4.S
339  arm/sync_fetch_and_sub_8.S
340  arm/sync_fetch_and_umax_4.S
341  arm/sync_fetch_and_umax_8.S
342  arm/sync_fetch_and_umin_4.S
343  arm/sync_fetch_and_umin_8.S
344  arm/sync_fetch_and_xor_4.S
345  arm/sync_fetch_and_xor_8.S
346  arm/udivmodsi4.S
347  arm/udivsi3.S
348  arm/umodsi3.S
349)
350filter_builtin_sources(arm_SOURCES EXCLUDE arm_SOURCES "${arm_SOURCES};${GENERIC_SOURCES}")
351
352set(thumb1_SOURCES
353  arm/divsi3.S
354  arm/udivsi3.S
355  arm/comparesf2.S
356  arm/addsf3.S
357  ${GENERIC_SOURCES}
358)
359
360set(arm_EABI_SOURCES
361  arm/aeabi_cdcmp.S
362  arm/aeabi_cdcmpeq_check_nan.c
363  arm/aeabi_cfcmp.S
364  arm/aeabi_cfcmpeq_check_nan.c
365  arm/aeabi_dcmp.S
366  arm/aeabi_div0.c
367  arm/aeabi_drsub.c
368  arm/aeabi_fcmp.S
369  arm/aeabi_frsub.c
370  arm/aeabi_idivmod.S
371  arm/aeabi_ldivmod.S
372  arm/aeabi_memcmp.S
373  arm/aeabi_memcpy.S
374  arm/aeabi_memmove.S
375  arm/aeabi_memset.S
376  arm/aeabi_uidivmod.S
377  arm/aeabi_uldivmod.S
378)
379
380set(arm_Thumb1_JT_SOURCES
381  arm/switch16.S
382  arm/switch32.S
383  arm/switch8.S
384  arm/switchu8.S
385)
386set(arm_Thumb1_SjLj_EH_SOURCES
387  arm/restore_vfp_d8_d15_regs.S
388  arm/save_vfp_d8_d15_regs.S
389)
390set(arm_Thumb1_VFPv2_SOURCES
391  arm/adddf3vfp.S
392  arm/addsf3vfp.S
393  arm/divdf3vfp.S
394  arm/divsf3vfp.S
395  arm/eqdf2vfp.S
396  arm/eqsf2vfp.S
397  arm/extendsfdf2vfp.S
398  arm/fixdfsivfp.S
399  arm/fixsfsivfp.S
400  arm/fixunsdfsivfp.S
401  arm/fixunssfsivfp.S
402  arm/floatsidfvfp.S
403  arm/floatsisfvfp.S
404  arm/floatunssidfvfp.S
405  arm/floatunssisfvfp.S
406  arm/gedf2vfp.S
407  arm/gesf2vfp.S
408  arm/gtdf2vfp.S
409  arm/gtsf2vfp.S
410  arm/ledf2vfp.S
411  arm/lesf2vfp.S
412  arm/ltdf2vfp.S
413  arm/ltsf2vfp.S
414  arm/muldf3vfp.S
415  arm/mulsf3vfp.S
416  arm/nedf2vfp.S
417  arm/negdf2vfp.S
418  arm/negsf2vfp.S
419  arm/nesf2vfp.S
420  arm/subdf3vfp.S
421  arm/subsf3vfp.S
422  arm/truncdfsf2vfp.S
423  arm/unorddf2vfp.S
424  arm/unordsf2vfp.S
425)
426set(arm_Thumb1_icache_SOURCES
427  arm/sync_synchronize.S
428)
429set(arm_Thumb1_SOURCES
430  ${arm_Thumb1_JT_SOURCES}
431  ${arm_Thumb1_SjLj_EH_SOURCES}
432  ${arm_Thumb1_VFPv2_SOURCES}
433  ${arm_Thumb1_icache_SOURCES}
434)
435
436if(MINGW)
437  set(arm_SOURCES
438    arm/aeabi_idivmod.S
439    arm/aeabi_ldivmod.S
440    arm/aeabi_uidivmod.S
441    arm/aeabi_uldivmod.S
442    arm/chkstk.S
443    mingw_fixfloat.c
444  )
445  filter_builtin_sources(arm_SOURCES EXCLUDE arm_SOURCES "${arm_SOURCES};${GENERIC_SOURCES}")
446elseif(NOT WIN32)
447  # TODO the EABI sources should only be added to EABI targets
448  set(arm_SOURCES
449    ${arm_SOURCES}
450    ${arm_EABI_SOURCES}
451    ${arm_Thumb1_SOURCES}
452  )
453
454  set(thumb1_SOURCES
455    ${thumb1_SOURCES}
456    ${arm_EABI_SOURCES}
457  )
458endif()
459
460set(aarch64_SOURCES
461  ${GENERIC_TF_SOURCES}
462  ${GENERIC_SOURCES}
463  aarch64/fp_mode.c
464)
465
466if (MINGW)
467  set(aarch64_SOURCES
468    ${aarch64_SOURCES}
469    aarch64/chkstk.S
470  )
471endif()
472
473set(armhf_SOURCES ${arm_SOURCES})
474set(armv7_SOURCES ${arm_SOURCES})
475set(armv7s_SOURCES ${arm_SOURCES})
476set(armv7k_SOURCES ${arm_SOURCES})
477set(arm64_SOURCES ${aarch64_SOURCES})
478
479# macho_embedded archs
480set(armv6m_SOURCES ${thumb1_SOURCES})
481set(armv7m_SOURCES ${arm_SOURCES})
482set(armv7em_SOURCES ${arm_SOURCES})
483
484# hexagon arch
485set(hexagon_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
486set(hexagon_SOURCES
487  hexagon/common_entry_exit_abi1.S
488  hexagon/common_entry_exit_abi2.S
489  hexagon/common_entry_exit_legacy.S
490  hexagon/dfaddsub.S
491  hexagon/dfdiv.S
492  hexagon/dffma.S
493  hexagon/dfminmax.S
494  hexagon/dfmul.S
495  hexagon/dfsqrt.S
496  hexagon/divdi3.S
497  hexagon/divsi3.S
498  hexagon/fabs_opt.S
499  hexagon/fastmath2_dlib_asm.S
500  hexagon/fastmath2_ldlib_asm.S
501  hexagon/fastmath_dlib_asm.S
502  hexagon/fma_opt.S
503  hexagon/fmax_opt.S
504  hexagon/fmin_opt.S
505  hexagon/memcpy_forward_vp4cp4n2.S
506  hexagon/memcpy_likely_aligned.S
507  hexagon/moddi3.S
508  hexagon/modsi3.S
509  hexagon/sfdiv_opt.S
510  hexagon/sfsqrt_opt.S
511  hexagon/udivdi3.S
512  hexagon/udivmoddi4.S
513  hexagon/udivmodsi4.S
514  hexagon/udivsi3.S
515  hexagon/umoddi3.S
516  hexagon/umodsi3.S
517)
518
519
520set(mips_SOURCES ${GENERIC_SOURCES})
521set(mipsel_SOURCES ${mips_SOURCES})
522set(mips64_SOURCES ${GENERIC_TF_SOURCES}
523                   ${mips_SOURCES})
524set(mips64el_SOURCES ${GENERIC_TF_SOURCES}
525                     ${mips_SOURCES})
526
527set(powerpc64_SOURCES
528  ppc/divtc3.c
529  ppc/fixtfti.c
530  ppc/fixtfdi.c
531  ppc/fixunstfti.c
532  ppc/fixunstfdi.c
533  ppc/floattitf.c
534  ppc/floatditf.c
535  ppc/floatunditf.c
536  ppc/gcc_qadd.c
537  ppc/gcc_qdiv.c
538  ppc/gcc_qmul.c
539  ppc/gcc_qsub.c
540  ppc/multc3.c
541  ${GENERIC_SOURCES}
542)
543set(powerpc64le_SOURCES ${powerpc64_SOURCES})
544
545set(riscv_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
546set(riscv32_SOURCES
547  riscv/mulsi3.S
548  ${riscv_SOURCES}
549)
550set(riscv64_SOURCES ${riscv_SOURCES})
551
552set(sparc_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
553set(sparcv9_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
554
555set(wasm32_SOURCES
556  ${GENERIC_TF_SOURCES}
557  ${GENERIC_SOURCES}
558)
559set(wasm64_SOURCES
560  ${GENERIC_TF_SOURCES}
561  ${GENERIC_SOURCES}
562)
563
564add_custom_target(builtins)
565set_target_properties(builtins PROPERTIES FOLDER "Compiler-RT Misc")
566
567if (APPLE)
568  add_subdirectory(Darwin-excludes)
569  add_subdirectory(macho_embedded)
570  darwin_add_builtin_libraries(${BUILTIN_SUPPORTED_OS})
571else ()
572  set(BUILTIN_CFLAGS "")
573
574  append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)
575
576  # These flags would normally be added to CMAKE_C_FLAGS by the llvm
577  # cmake step. Add them manually if this is a standalone build.
578  if(COMPILER_RT_STANDALONE_BUILD)
579    append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC BUILTIN_CFLAGS)
580    append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin BUILTIN_CFLAGS)
581    if(NOT ANDROID)
582      append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG -fvisibility=hidden BUILTIN_CFLAGS)
583    endif()
584    if(NOT COMPILER_RT_DEBUG)
585      append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fomit-frame-pointer BUILTIN_CFLAGS)
586    endif()
587  endif()
588
589  set(BUILTIN_DEFS "")
590
591  if(NOT ANDROID)
592    append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG VISIBILITY_HIDDEN BUILTIN_DEFS)
593  endif()
594
595  foreach (arch ${BUILTIN_SUPPORTED_ARCH})
596    if (CAN_TARGET_${arch})
597      # For ARM archs, exclude any VFP builtins if VFP is not supported
598      if (${arch} MATCHES "^(arm|armhf|armv7|armv7s|armv7k|armv7m|armv7em)$")
599        string(REPLACE ";" " " _TARGET_${arch}_CFLAGS "${TARGET_${arch}_CFLAGS}")
600        check_compile_definition(__VFP_FP__ "${CMAKE_C_FLAGS} ${_TARGET_${arch}_CFLAGS}" COMPILER_RT_HAS_${arch}_VFP)
601        if(NOT COMPILER_RT_HAS_${arch}_VFP)
602          list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_SOURCES} ${arm_Thumb1_SjLj_EH_SOURCES})
603        endif()
604      endif()
605
606      # Filter out generic versions of routines that are re-implemented in
607      # architecture specific manner.  This prevents multiple definitions of the
608      # same symbols, making the symbol selection non-deterministic.
609      foreach (_file ${${arch}_SOURCES})
610        get_filename_component(_file_dir "${_file}" DIRECTORY)
611        if (NOT "${_file_dir}" STREQUAL "")
612          # Architecture specific file. We follow the convention that a source
613          # file that exists in a sub-directory (e.g. `ppc/divtc3.c`) is
614          # architecture specific and that if a generic implementation exists
615          # it will be a top-level source file with the same name modulo the
616          # file extension (e.g. `divtc3.c`).
617          get_filename_component(_name ${_file} NAME)
618          string(REPLACE ".S" ".c" _cname "${_name}")
619          if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_cname}")
620            message(STATUS "For ${arch} builtins preferring ${_file} to ${_cname}")
621            list(REMOVE_ITEM ${arch}_SOURCES ${_cname})
622          endif()
623        endif ()
624      endforeach ()
625
626      # Needed for clear_cache on debug mode, due to r7's usage in inline asm.
627      # Release mode already sets it via -O2/3, Debug mode doesn't.
628      if (${arch} STREQUAL "armhf")
629        list(APPEND BUILTIN_CFLAGS -fomit-frame-pointer -DCOMPILER_RT_ARMHF_TARGET)
630      endif()
631
632      # For RISCV32, we must force enable int128 for compiling long
633      # double routines.
634      if("${arch}" STREQUAL "riscv32")
635        list(APPEND BUILTIN_CFLAGS -fforce-enable-int128)
636      endif()
637
638      add_compiler_rt_runtime(clang_rt.builtins
639                              STATIC
640                              ARCHS ${arch}
641                              SOURCES ${${arch}_SOURCES}
642                              DEFS ${BUILTIN_DEFS}
643                              CFLAGS ${BUILTIN_CFLAGS}
644                              PARENT_TARGET builtins)
645    endif ()
646  endforeach ()
647endif ()
648
649add_dependencies(compiler-rt builtins)
650