Skip to content

Commit 5897590

Browse files
committed
[GR-15609] Remove usage of truffle.h.
PullRequest: truffleruby/845
2 parents 251f56d + 767fc92 commit 5897590

File tree

9 files changed

+97
-67
lines changed

9 files changed

+97
-67
lines changed

lib/cext/include/ruby/ruby.h

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ void *alloca();
9494
# endif /* HAVE_ALLOCA_H */
9595
#endif /* __GNUC__ */
9696

97+
#if defined HAVE_UINTPTR_T && 0
98+
typedef uintptr_t VALUE;
99+
typedef uintptr_t ID;
100+
# define SIGNED_VALUE intptr_t
101+
# define SIZEOF_VALUE SIZEOF_UINTPTR_T
102+
# undef PRI_VALUE_PREFIX
103+
#elif SIZEOF_LONG == SIZEOF_VOIDP
104+
#ifndef TRUFFLERUBY
105+
typedef unsigned long VALUE;
106+
typedef unsigned long ID;
107+
#endif
108+
# define SIGNED_VALUE long
109+
# define SIZEOF_VALUE SIZEOF_LONG
110+
# define PRI_VALUE_PREFIX "l"
111+
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
112+
typedef unsigned LONG_LONG VALUE;
113+
typedef unsigned LONG_LONG ID;
114+
# define SIGNED_VALUE LONG_LONG
115+
# define LONG_LONG_VALUE 1
116+
# define SIZEOF_VALUE SIZEOF_LONG_LONG
117+
# define PRI_VALUE_PREFIX PRI_LL_PREFIX
118+
#else
119+
# error ---->> ruby requires sizeof(void*) == sizeof(long) or sizeof(LONG_LONG) to be compiled. <<----
120+
#endif
121+
97122
typedef char ruby_check_sizeof_int[SIZEOF_INT == sizeof(int) ? 1 : -1];
98123
typedef char ruby_check_sizeof_long[SIZEOF_LONG == sizeof(long) ? 1 : -1];
99124
#ifdef HAVE_LONG_LONG
@@ -129,16 +154,28 @@ typedef char ruby_check_sizeof_voidp[SIZEOF_VOIDP == sizeof(void*) ? 1 : -1];
129154
#endif
130155

131156
#define RUBY_PRI_VALUE_MARK "\v"
132-
#define PRI_VALUE_PREFIX "l"
133-
#define PRI_LONG_PREFIX "l"
134-
#define PRI_64_PREFIX PRI_LONG_PREFIX
135-
#define PRIdVALUE PRI_VALUE_PREFIX"d"
136-
#define PRIoVALUE PRI_VALUE_PREFIX"o"
137-
#define PRIuVALUE PRI_VALUE_PREFIX"u"
138-
#define PRIxVALUE PRI_VALUE_PREFIX"x"
139-
#define PRIXVALUE PRI_VALUE_PREFIX"X"
140-
#define PRIsVALUE "Y"
141-
157+
#if defined PRIdPTR && !defined PRI_VALUE_PREFIX
158+
#define PRIdVALUE PRIdPTR
159+
#define PRIoVALUE PRIoPTR
160+
#define PRIuVALUE PRIuPTR
161+
#define PRIxVALUE PRIxPTR
162+
#define PRIXVALUE PRIXPTR
163+
#define PRIsVALUE PRIiPTR"" RUBY_PRI_VALUE_MARK
164+
#else
165+
#define PRIdVALUE PRI_VALUE_PREFIX"d"
166+
#define PRIoVALUE PRI_VALUE_PREFIX"o"
167+
#define PRIuVALUE PRI_VALUE_PREFIX"u"
168+
#define PRIxVALUE PRI_VALUE_PREFIX"x"
169+
#define PRIXVALUE PRI_VALUE_PREFIX"X"
170+
#ifdef TRUFFLERUBY
171+
#define PRIsVALUE "Y"
172+
#else
173+
#define PRIsVALUE PRI_VALUE_PREFIX"i" RUBY_PRI_VALUE_MARK
174+
#endif
175+
#endif
176+
#ifndef PRI_VALUE_PREFIX
177+
# define PRI_VALUE_PREFIX ""
178+
#endif
142179

143180
#ifndef PRI_TIMET_PREFIX
144181
# if SIZEOF_TIME_T == SIZEOF_INT
@@ -378,7 +415,6 @@ VALUE rb_id2sym(ID);
378415
#define ID2SYM(x) RB_ID2SYM(x)
379416
#define SYM2ID(x) RB_SYM2ID(x)
380417

381-
382418
#ifndef USE_FLONUM
383419
#if SIZEOF_VALUE >= SIZEOF_DOUBLE
384420
#define USE_FLONUM 1
@@ -425,16 +461,14 @@ enum ruby_special_consts {
425461
RUBY_SPECIAL_SHIFT = 8
426462
};
427463

428-
extern void* rb_tr_undef;
429-
extern void* rb_tr_true;
430-
extern void* rb_tr_false;
431-
extern void* rb_tr_nil;
432-
433-
#define Qfalse ((VALUE)(&rb_tr_false))
434-
#define Qtrue ((VALUE)(&rb_tr_true))
435-
#define Qnil ((VALUE)(&rb_tr_nil))
436-
#define Qundef ((VALUE)(&rb_tr_undef))
437-
464+
#define RUBY_Qfalse ((VALUE)RUBY_Qfalse)
465+
#define RUBY_Qtrue ((VALUE)RUBY_Qtrue)
466+
#define RUBY_Qnil ((VALUE)RUBY_Qnil)
467+
#define RUBY_Qundef ((VALUE)RUBY_Qundef) /* undefined value for placeholder */
468+
#define Qfalse RUBY_Qfalse
469+
#define Qtrue RUBY_Qtrue
470+
#define Qnil RUBY_Qnil
471+
#define Qundef RUBY_Qundef
438472
#define IMMEDIATE_MASK RUBY_IMMEDIATE_MASK
439473
#define FIXNUM_FLAG RUBY_FIXNUM_FLAG
440474
#if USE_FLONUM
@@ -444,8 +478,8 @@ extern void* rb_tr_nil;
444478
#define SYMBOL_FLAG RUBY_SYMBOL_FLAG
445479

446480
int RTEST(VALUE value);
447-
#define NIL_P RB_NIL_P
448481
int RB_NIL_P(VALUE value); /* Defined as a function in TruffleRuby. */
482+
#define NIL_P(v) RB_NIL_P(v)
449483

450484
#define CLASS_OF(v) rb_class_of((VALUE)(v))
451485

@@ -526,11 +560,22 @@ MUST_INLINE int RB_FLOAT_TYPE_P(VALUE obj);
526560

527561
bool RB_TYPE_P(VALUE value, int type);
528562

563+
#ifdef __GNUC__
529564
#define RB_GC_GUARD(v) \
530565
(*__extension__ ({ \
531-
volatile VALUE *rb_gc_guarded_ptr = rb_tr_gc_guard(&v); \
532-
rb_gc_guarded_ptr; \
566+
volatile VALUE *rb_gc_guarded_ptr = rb_tr_gc_guard(&v); \
567+
rb_gc_guarded_ptr; \
533568
}))
569+
#elif defined _MSC_VER
570+
#pragma optimize("", off)
571+
static inline volatile VALUE *rb_gc_guarded_ptr(volatile VALUE *ptr) {return ptr;}
572+
#pragma optimize("", on)
573+
#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
574+
#else
575+
volatile VALUE *rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val);
576+
#define HAVE_RB_GC_GUARDED_PTR_VAL 1
577+
#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
578+
#endif
534579

535580
#ifdef __GNUC__
536581
#define RB_UNUSED_VAR(x) x __attribute__ ((unused))

lib/cext/include/truffleruby/truffleruby-pre.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ extern "C" {
2020

2121
#define TRUFFLERUBY
2222

23-
#ifdef SULONG_TRUFFLE_H
24-
#include SULONG_TRUFFLE_H
25-
#else
26-
#include <sulong/truffle.h>
27-
#endif
28-
2923
#ifdef SULONG_POLYGLOT_H
3024
#include SULONG_POLYGLOT_H
3125
#else
@@ -34,16 +28,21 @@ extern "C" {
3428

3529
#include <ctype.h> // isdigit
3630

31+
// Configuration
32+
33+
// We disable USE_FLONUM, as we do not use pointer tagging for Float.
34+
// Enabling USE_FLONUM also changes the value of Qtrue/Qnil/Qundef.
35+
#define USE_FLONUM 0
36+
3737
// Value types
3838

3939
typedef void *VALUE;
40-
typedef long SIGNED_VALUE;
4140
typedef VALUE ID;
4241

4342
// Support
4443

4544
extern void* rb_tr_cext;
46-
#define RUBY_CEXT ((void *)(&rb_tr_cext))
45+
#define RUBY_CEXT rb_tr_cext
4746

4847
#define MUST_INLINE __attribute__((always_inline)) inline
4948

lib/truffle/rbconfig-for-mkmf.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
debugflags = '-g' # Show debug information such as line numbers in backtrace
6969
warnflags = [
7070
'-Wimplicit-function-declaration', # To make missing C ext functions clear
71+
'-Wundef', # Warn for undefined preprocessor macros
7172
'-Wno-unknown-warning-option', # If we're on an earlier version of clang without a warning option, ignore it
7273
'-Wno-int-conversion', # MRI has VALUE defined as long while we have it as void*
7374
'-Wno-int-to-pointer-cast', # Same as above
@@ -96,8 +97,7 @@
9697

9798
relative_debug_paths = "-fdebug-prefix-map=#{ruby_home}=."
9899
polyglot_h = "-DSULONG_POLYGLOT_H='\"#{ENV.fetch('SULONG_POLYGLOT_H')}\"'"
99-
truffle_h = "-DSULONG_TRUFFLE_H='\"#{ENV.fetch('SULONG_TRUFFLE_H')}\"'"
100-
mkconfig['CPPFLAGS'] = "#{relative_debug_paths} #{polyglot_h} #{truffle_h}"
100+
mkconfig['CPPFLAGS'] = "#{relative_debug_paths} #{polyglot_h}"
101101
expanded['CPPFLAGS'] = mkconfig['CPPFLAGS']
102102

103103
# Default to the ruby in $PATH to build core C extensions faster

mx.truffleruby/suite.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,11 @@
257257
"TRUFFLERUBY-LAUNCHER", # We need this jar to run extconf.rb
258258
"truffle:TRUFFLE_NFI_NATIVE", # trufflenfi.h
259259
"sulong:SULONG_HOME", # polyglot.h
260-
"sulong:SULONG_LEGACY", # truffle.h
261260
],
262261
"buildEnv": {
263262
"NFI_HEADERS_DIR": "<path:truffle:TRUFFLE_NFI_NATIVE>/include",
264263
"SULONG_HEADERS_DIR": "<path:SULONG_HOME>/include",
265264
"SULONG_POLYGLOT_H": "<path:SULONG_HOME>/include/polyglot.h",
266-
"SULONG_TRUFFLE_H": "<path:SULONG_LEGACY>/include/truffle.h",
267-
"SULONG_TRUFFLE_DIR": "<path:SULONG_LEGACY>/include",
268265
},
269266
"output": ".",
270267
"results": [
@@ -425,7 +422,6 @@
425422
"file:lib/cext/include/*.h",
426423
],
427424
"lib/cext/include/sulong/": [
428-
"extracted-dependency:sulong:SULONG_LEGACY/include/truffle.h",
429425
"link:../../sulong-libs/include/polyglot.h",
430426
],
431427
"lib/cext/sulong-libs/": [

src/main/c/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CFLAGS := -Wall -Werror -Wno-unused-function -fPIC -std=c99
1717
LDFLAGS := -m64
1818

1919
TRUFFLE_POSIX := truffleposix/libtruffleposix.$(DLEXT)
20-
SULONG_HEADERS := $(SULONG_TRUFFLE_H) $(SULONG_POLYGLOT_H)
20+
SULONG_HEADERS := $(SULONG_POLYGLOT_H)
2121
GENERATE_SULONG_MOCK := $(ROOT)/tool/generate-sulongmock.rb
2222

2323
RUBY_HEADERS := $(wildcard $(ROOT)/lib/cext/include/*.h) $(wildcard $(ROOT)/lib/cext/include/*/*.h) $(wildcard $(ROOT)/lib/cext/include/*/*/*.h)
@@ -60,11 +60,11 @@ sulongmock/sulongmock.c: $(GENERATE_SULONG_MOCK) $(SULONG_HEADERS) $(TRUFFLE_POS
6060
$(RUBY) $(GENERATE_SULONG_MOCK) $(SULONG_HEADERS)
6161

6262
sulongmock/sulongmock.o: sulongmock/sulongmock.c
63-
$(CC) -o $@ -c $(CFLAGS) $(LDFLAGS) -I$(ROOT)/lib/cext/include -I$(SULONG_TRUFFLE_DIR) -I$(SULONG_HEADERS_DIR) $<
63+
$(CC) -o $@ -c $(CFLAGS) $(LDFLAGS) -I$(ROOT)/lib/cext/include -I$(SULONG_HEADERS_DIR) $<
6464

6565
# ruby.o
6666
cext/ruby.o: cext/*.c $(RUBY_HEADERS)
67-
$(CC) -o $@ -c $(CFLAGS) $(LDFLAGS) -DSULONG_TRUFFLE_H=\"$(SULONG_TRUFFLE_H)\" -DSULONG_POLYGLOT_H=\"$(SULONG_POLYGLOT_H)\" -I$(ROOT)/lib/cext/include cext/ruby.c
67+
$(CC) -o $@ -c $(CFLAGS) $(LDFLAGS) -DSULONG_POLYGLOT_H=\"$(SULONG_POLYGLOT_H)\" -I$(ROOT)/lib/cext/include cext/ruby.c
6868

6969
# ruby.su
7070
cext/Makefile: cext/extconf.rb $(BASIC_EXTCONF_DEPS)

src/main/c/cext/ruby.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
#include <fcntl.h>
2727
#include <printf.h>
2828

29+
/*
30+
* Note that some functions are #undef'd just before declaration.
31+
* This is needed because these functions are declared by MRI as macros in e.g., ruby.h,
32+
* and so would produce invalid syntax when using the function name for definition.
33+
*/
34+
2935
void* rb_tr_cext;
30-
void* rb_tr_undef;
31-
void* rb_tr_true;
32-
void* rb_tr_false;
33-
void* rb_tr_nil;
3436

3537
#ifdef __APPLE__
3638
static printf_domain_t printf_domain;
@@ -91,11 +93,8 @@ static int rb_tr_fprintf_value(FILE *stream,
9193
// Run when loading C-extension support
9294

9395
void rb_tr_init(void *ruby_cext) {
94-
truffle_assign_managed(&rb_tr_cext, ruby_cext);
95-
truffle_assign_managed(&rb_tr_undef, rb_tr_get_undef());
96-
truffle_assign_managed(&rb_tr_true, rb_tr_get_true());
97-
truffle_assign_managed(&rb_tr_false, rb_tr_get_false());
98-
truffle_assign_managed(&rb_tr_nil, rb_tr_get_nil());
96+
rb_tr_cext = ruby_cext;
97+
9998
#ifdef __APPLE__
10099
printf_domain = new_printf_domain();
101100
register_printf_domain_function(printf_domain, 'Y', rb_tr_fprintf_value, rb_tr_fprintf_value_arginfo, NULL);
@@ -1296,7 +1295,6 @@ bool rb_tr_hidden_p(VALUE value) {
12961295
return false;
12971296
}
12981297

1299-
// Undef conflicting macro from encoding.h like MRI
13001298
#undef rb_enc_str_new
13011299
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc) {
13021300
return RUBY_INVOKE(rb_str_new(ptr, len), "force_encoding", rb_enc_from_encoding(enc));
@@ -2822,6 +2820,7 @@ static RUBY_DATA_FUNC rb_tr_free_function(RUBY_DATA_FUNC dfree) {
28222820
return (dfree == (RUBY_DATA_FUNC)RUBY_DEFAULT_FREE) ? free : dfree;
28232821
}
28242822

2823+
#undef rb_data_object_wrap
28252824
VALUE rb_data_object_wrap(VALUE klass, void *data, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree) {
28262825
return rb_tr_wrap(polyglot_invoke(RUBY_CEXT, "rb_data_object_wrap",
28272826
rb_tr_unwrap(klass), data, dmark, rb_tr_free_function(dfree) ));
@@ -2911,6 +2910,9 @@ void* rb_tr_new_managed_struct_internal(void *type) {
29112910
return polyglot_invoke(RUBY_CEXT, "rb_tr_new_managed_struct", type);
29122911
}
29132912

2913+
// Deprecated truffle LLVM intrinsic only used internally here
2914+
void truffle_load_library(const char *string);
2915+
29142916
void rb_tr_load_library(VALUE library) {
29152917
truffle_load_library(RSTRING_PTR(library));
29162918
}

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,6 @@ private void initializeConstants() {
720720
setConstant(objectClass, "ARGV", argv);
721721

722722
setConstant(truffleModule, "UNDEFINED", supportUndefined);
723-
setConstant(truffleModule, "LIBC", frozenUSASCIIString(Platform.LIBC));
724723

725724
setConstant(encodingConverterClass, "INVALID_MASK", EConvFlags.INVALID_MASK);
726725
setConstant(encodingConverterClass, "INVALID_REPLACE", EConvFlags.INVALID_REPLACE);

src/main/java/org/truffleruby/platform/Platform.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,10 @@
3131

3232
public abstract class Platform extends BasicPlatform {
3333

34-
public static final String LIBPREFIX = OS == OS_TYPE.WINDOWS ? "" : "lib";
3534
public static final String LIBSUFFIX = determineLibExt();
36-
public static final String LIBC = determineLibC();
3735

3836
public static final boolean IS_WINDOWS = OS == OS_TYPE.WINDOWS;
3937

40-
private static final String determineLibC() {
41-
switch (OS) {
42-
case WINDOWS:
43-
return "msvcrt.dll";
44-
case LINUX:
45-
return "libc.so.6";
46-
default:
47-
return LIBPREFIX + "c." + LIBSUFFIX;
48-
}
49-
}
50-
5138
private static final String determineLibExt() {
5239
switch (OS) {
5340
case WINDOWS:

tool/generate-sulongmock.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
break if line.start_with?('#ifdef DOXYGEN')
2626
next if line.start_with?('//') || line.start_with?(' *')
2727

28-
match = /^(\S.+?)\b(truffle|polyglot|__polyglot)(.+)\)(?=;)/.match(line)
28+
match = /^(\S.+?)\b(polyglot|__polyglot)(.+)\)(?=;)/.match(line)
2929
if match
3030
signature, return_type = match[0], match[1]
3131
return_value = types.fetch(return_type.gsub(' ', ''), '0')
3232
methods << [signature, return_value]
3333
end
3434
end
3535

36+
methods << ['void truffle_load_library(const char *string)', nil]
37+
3638
File.write('src/main/c/sulongmock/sulongmock.c', ERB.new(<<TRC).result)
3739
/*
3840
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. This
@@ -49,7 +51,7 @@
4951
5052
#include <stdio.h>
5153
#include <stdint.h>
52-
#include <truffle.h>
54+
#include <stdlib.h>
5355
#include <polyglot.h>
5456
5557
void rb_tr_mock() {

0 commit comments

Comments
 (0)