From 3c55e7fe0c20d092045d727491d1abe5a6e671b6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 15:06:15 -0500 Subject: [PATCH 001/345] Add NAN: literal syntax for NANs with a payload --- basis/prettyprint/backend/backend.factor | 5 +++++ core/bootstrap/syntax.factor | 1 + core/math/math-docs.factor | 27 +++++++++++++++++++++++- core/parser/parser.factor | 5 ++++- core/syntax/syntax-docs.factor | 14 ++++++++++++ core/syntax/syntax.factor | 2 ++ 6 files changed, 52 insertions(+), 2 deletions(-) diff --git a/basis/prettyprint/backend/backend.factor b/basis/prettyprint/backend/backend.factor index 90e2388934..f8bcb66b1e 100644 --- a/basis/prettyprint/backend/backend.factor +++ b/basis/prettyprint/backend/backend.factor @@ -47,6 +47,11 @@ M: method-body pprint* M: real pprint* number>string text ; +M: float pprint* + dup fp-nan? [ + \ NAN: [ fp-nan-payload >hex text ] pprint-prefix + ] [ call-next-method ] if ; + M: f pprint* drop \ f pprint-word ; : pprint-effect ( effect -- ) diff --git a/core/bootstrap/syntax.factor b/core/bootstrap/syntax.factor index 906b73934e..57be2fb90f 100644 --- a/core/bootstrap/syntax.factor +++ b/core/bootstrap/syntax.factor @@ -33,6 +33,7 @@ IN: bootstrap.syntax "MAIN:" "MATH:" "MIXIN:" + "NAN:" "OCT:" "P\"" "POSTPONE:" diff --git a/core/math/math-docs.factor b/core/math/math-docs.factor index ab2a5ab8be..d98685fb48 100644 --- a/core/math/math-docs.factor +++ b/core/math/math-docs.factor @@ -277,7 +277,32 @@ HELP: fp-bitwise= { "x" float } { "y" float } { "?" boolean } } -{ $description "Compares two floating point numbers for bit equality." } ; +{ $description "Compares two floating point numbers for bit equality." } +{ $notes "Unlike " { $link = } " or " { $link number= } ", this word will consider NaNs with equal payloads to be equal, and positive zero and negative zero to be not equal." } +{ $examples + "Not-a-number equality:" + { $example + "USING: math prettyprint ;" + "0.0 0.0 / dup number= ." + "f" + } + { $example + "USING: math prettyprint ;" + "0.0 0.0 / dup fp-bitwise= ." + "t" + } + "Signed zero equality:" + { $example + "USING: math prettyprint ;" + "-0.0 0.0 fp-bitwise= ." + "f" + } + { $example + "USING: math prettyprint ;" + "-0.0 0.0 = ." + "t" + } +} ; HELP: fp-special? { $values { "x" real } { "?" "a boolean" } } diff --git a/core/parser/parser.factor b/core/parser/parser.factor index 94eb0a865c..276030d770 100644 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -99,8 +99,11 @@ M: f parse-quotation \ ] parse-until >quotation ; ERROR: bad-number ; +: scan-base ( base -- n ) + scan swap base> [ bad-number ] unless* ; + : parse-base ( parsed base -- parsed ) - scan swap base> [ bad-number ] unless* parsed ; + scan-base parsed ; SYMBOL: bootstrap-syntax diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index fd5590fde1..19e644cb68 100644 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -72,6 +72,8 @@ ARTICLE: "syntax-floats" "Float syntax" { "Negative infinity" { $snippet "-1/0." } } { "Not-a-number" { $snippet "0/0." } } } +"A Not-a-number with an arbitrary payload can be parsed in:" +{ $subsection POSTPONE: NAN: } "More information on floats can be found in " { $link "floats" } "." ; ARTICLE: "syntax-complex-numbers" "Complex number syntax" @@ -603,6 +605,18 @@ HELP: BIN: { $description "Adds an integer read from an binary literal to the parse tree." } { $examples { $example "USE: prettyprint" "BIN: 100 ." "4" } } ; +HELP: NAN: +{ $syntax "NAN: payload" } +{ $values { "payload" "64-bit hexadecimal integer" } } +{ $description "Adds a floating point Not-a-Number literal to the parse tree." } +{ $examples + { $example + "USE: prettyprint" + "NAN: deadbeef ." + "NAN: deadbeef" + } +} ; + HELP: GENERIC: { $syntax "GENERIC: word ( stack -- effect )" } { $values { "word" "a new word to define" } } diff --git a/core/syntax/syntax.factor b/core/syntax/syntax.factor index f01f90c027..16645e3342 100644 --- a/core/syntax/syntax.factor +++ b/core/syntax/syntax.factor @@ -73,6 +73,8 @@ IN: bootstrap.syntax "OCT:" [ 8 parse-base ] define-core-syntax "BIN:" [ 2 parse-base ] define-core-syntax + "NAN:" [ 16 scan-base parsed ] define-core-syntax + "f" [ f parsed ] define-core-syntax "t" "syntax" lookup define-singleton-class From a456f79f9c7cd34b9e01bf217f1db35bd4b62129 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 16:59:30 -0400 Subject: [PATCH 002/345] cpu-x86: clear the x87 stack when rewinding; fixes math.floats.env failures on Linux --- vm/cpu-x86.S | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index 3f2626d405..09e742bed8 100644 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -56,6 +56,11 @@ DEF(F_FASTCALL void,c_to_factor,(CELL quot)): ret DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to)): + /* clear x87 stack, but preserve rounding mode and exception flags */ + sub $2,STACK_REG + fnstcw (STACK_REG) + fninit + fldcw (STACK_REG) /* rewind_to */ mov ARG1,STACK_REG jmp *QUOT_XT_OFFSET(ARG0) From 87c7f882ca71033b53ff9803e89df221354a99c4 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 16:05:05 -0500 Subject: [PATCH 003/345] db: fix with-book-db combinator in db tutorial --- basis/db/db-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/db/db-docs.factor b/basis/db/db-docs.factor index 154d8961a2..a8b03398c7 100644 --- a/basis/db/db-docs.factor +++ b/basis/db/db-docs.factor @@ -254,7 +254,7 @@ ARTICLE: "db-lowlevel-tutorial" "Low-level database tutorial" { $code <" USING: db.sqlite db io.files ; : with-book-db ( quot -- ) - "book.db" temp-file swap with-db ;"> } + "book.db" temp-file swap with-db ; inline"> } "Now let's create the table manually:" { $code <" "create table books (id integer primary key, title text, author text, date_published timestamp, From 11f984e7343710406531ec13b40dfe86558a1943 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 16:24:07 -0500 Subject: [PATCH 004/345] math.floats: fix abs on floats; -0.0 abs should be 0.0 not -0.0 --- core/math/floats/floats-tests.factor | 8 ++++++++ core/math/floats/floats.factor | 8 ++++++-- core/math/math.factor | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/math/floats/floats-tests.factor b/core/math/floats/floats-tests.factor index de84346a58..220eb33960 100644 --- a/core/math/floats/floats-tests.factor +++ b/core/math/floats/floats-tests.factor @@ -67,3 +67,11 @@ unit-test [ t ] [ 0/0. 1.0 unordered? ] unit-test [ f ] [ 1.0 1.0 unordered? ] unit-test +[ t ] [ -0.0 fp-sign ] unit-test +[ t ] [ -1.0 fp-sign ] unit-test +[ f ] [ 0.0 fp-sign ] unit-test +[ f ] [ 1.0 fp-sign ] unit-test + +[ t ] [ -0.0 abs 0.0 fp-bitwise= ] unit-test +[ 1.5 ] [ -1.5 abs ] unit-test +[ 1.5 ] [ 1.5 abs ] unit-test diff --git a/core/math/floats/floats.factor b/core/math/floats/floats.factor index aa55e2d0ee..9c49e99231 100644 --- a/core/math/floats/floats.factor +++ b/core/math/floats/floats.factor @@ -50,7 +50,7 @@ M: float fp-snan? M: float fp-infinity? dup fp-special? [ fp-nan-payload zero? ] [ drop f ] if ; inline -M: float next-float ( m -- n ) +M: float next-float double>bits dup -0.0 double>bits > [ 1 - bits>double ] [ ! negative non-zero dup -0.0 double>bits = [ drop 0.0 ] [ ! negative zero @@ -60,10 +60,14 @@ M: float next-float ( m -- n ) M: float unordered? [ fp-nan? ] bi@ or ; inline -M: float prev-float ( m -- n ) +M: float prev-float double>bits dup -0.0 double>bits >= [ 1 + bits>double ] [ ! negative dup 0.0 double>bits = [ drop -0.0 ] [ ! positive zero 1 - bits>double ! positive non-zero ] if ] if ; inline + +M: float fp-sign double>bits 63 bit? ; inline + +M: float abs double>bits 63 2^ bitnot bitand bits>double ; inline diff --git a/core/math/math.factor b/core/math/math.factor index 4fb39f93f7..900c1e1cee 100755 --- a/core/math/math.factor +++ b/core/math/math.factor @@ -99,13 +99,13 @@ GENERIC: fp-qnan? ( x -- ? ) GENERIC: fp-snan? ( x -- ? ) GENERIC: fp-infinity? ( x -- ? ) GENERIC: fp-nan-payload ( x -- bits ) +GENERIC: fp-sign ( x -- ? ) M: object fp-special? drop f ; inline M: object fp-nan? drop f ; inline M: object fp-qnan? drop f ; inline M: object fp-snan? drop f ; inline M: object fp-infinity? drop f ; inline -M: object fp-nan-payload drop f ; inline : ( payload -- nan ) HEX: 7ff0000000000000 bitor bits>double ; inline From 1337f82ce649335898d4e9fc41a09b5dfb0eff42 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 16:33:42 -0500 Subject: [PATCH 005/345] math.order: better docs --- basis/math/functions/functions-docs.factor | 7 ++---- core/math/order/order-docs.factor | 28 +++++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/basis/math/functions/functions-docs.factor b/basis/math/functions/functions-docs.factor index 134cbd398c..d61ad9a14a 100644 --- a/basis/math/functions/functions-docs.factor +++ b/basis/math/functions/functions-docs.factor @@ -20,10 +20,6 @@ ARTICLE: "arithmetic-functions" "Arithmetic functions" "Computing additive and multiplicative inverses:" { $subsection neg } { $subsection recip } -"Minimum, maximum, clamping:" -{ $subsection min } -{ $subsection max } -{ $subsection clamp } "Complex conjugation:" { $subsection conjugate } "Tests:" @@ -41,7 +37,8 @@ ARTICLE: "arithmetic-functions" "Arithmetic functions" { $subsection truncate } { $subsection round } "Inexact comparison:" -{ $subsection ~ } ; +{ $subsection ~ } +"Numbers implement the " { $link "math.order" } ", therefore operations such as " { $link min } " and " { $link max } " can be used with numbers." ; ARTICLE: "power-functions" "Powers and logarithms" "Squares:" diff --git a/core/math/order/order-docs.factor b/core/math/order/order-docs.factor index b2c2eeb973..707dd6b79f 100644 --- a/core/math/order/order-docs.factor +++ b/core/math/order/order-docs.factor @@ -44,39 +44,41 @@ HELP: compare } ; HELP: max -{ $values { "x" real } { "y" real } { "z" real } } -{ $description "Outputs the greatest of two real numbers." } ; +{ $values { "x" object } { "y" object } { "z" object } } +{ $description "Outputs the greatest of two ordered values." } +{ $notes "If one value is a floating point positive zero and the other is a negative zero, the result is undefined." } ; HELP: min -{ $values { "x" real } { "y" real } { "z" real } } -{ $description "Outputs the smallest of two real numbers." } ; +{ $values { "x" object } { "y" object } { "z" object } } +{ $description "Outputs the smallest of two ordered values." } +{ $notes "If one value is a floating point positive zero and the other is a negative zero, the result is undefined." } ; HELP: clamp -{ $values { "x" real } { "min" real } { "max" real } { "y" real } } +{ $values { "x" object } { "min" object } { "max" object } { "y" object } } { $description "Outputs " { $snippet "x" } " if contained in the interval " { $snippet "[min,max]" } " or outputs one of the endpoints." } ; HELP: between? -{ $values { "x" real } { "y" real } { "z" real } { "?" "a boolean" } } +{ $values { "x" object } { "y" object } { "z" real } { "?" "a boolean" } } { $description "Tests if " { $snippet "x" } " is in the interval " { $snippet "[y,z]" } "." } { $notes "As per the closed interval notation, the end-points are included in the interval." } ; HELP: before? -{ $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } } +{ $values { "obj1" object } { "obj2" object } { "?" "a boolean" } } { $description "Tests if " { $snippet "obj1" } " comes before " { $snippet "obj2" } " using an intrinsic total order." } { $notes "Implemented using " { $link <=> } "." } ; HELP: after? -{ $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } } +{ $values { "obj1" object } { "obj2" object } { "?" "a boolean" } } { $description "Tests if " { $snippet "obj1" } " comes after " { $snippet "obj2" } " using an intrinsic total order." } { $notes "Implemented using " { $link <=> } "." } ; HELP: before=? -{ $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } } +{ $values { "obj1" object } { "obj2" object } { "?" "a boolean" } } { $description "Tests if " { $snippet "obj1" } " comes before or equals " { $snippet "obj2" } " using an intrinsic total order." } { $notes "Implemented using " { $link <=> } "." } ; HELP: after=? -{ $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } } +{ $values { "obj1" object } { "obj2" object } { "?" "a boolean" } } { $description "Tests if " { $snippet "obj1" } " comes after or equals " { $snippet "obj2" } " using an intrinsic total order." } { $notes "Implemented using " { $link <=> } "." } ; @@ -100,7 +102,7 @@ ARTICLE: "math.order.example" "Linear order example" } ; ARTICLE: "math.order" "Linear order protocol" -"Some classes have an intrinsic order amongst instances:" +"Some classes define an intrinsic order amongst instances. This includes numbers, sequences (in particular, strings), and words." { $subsection <=> } { $subsection >=< } { $subsection compare } @@ -112,6 +114,10 @@ ARTICLE: "math.order" "Linear order protocol" { $subsection before? } { $subsection after=? } { $subsection before=? } +"Minimum, maximum, clamping:" +{ $subsection min } +{ $subsection max } +{ $subsection clamp } "Out of the above generic words, it suffices to implement " { $link <=> } " alone. The others may be provided as an optimization." { $subsection "math.order.example" } { $see-also "sequences-sorting" } ; From 6d328be69b66e97498b53a22937e1e674a2b11bf Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 12 Sep 2009 18:13:25 -0500 Subject: [PATCH 006/345] all-fp-exceptions constant --- basis/math/floats/env/env.factor | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/basis/math/floats/env/env.factor b/basis/math/floats/env/env.factor index 6a8110c4c1..91419c1fdf 100644 --- a/basis/math/floats/env/env.factor +++ b/basis/math/floats/env/env.factor @@ -18,6 +18,15 @@ UNION: fp-exception +fp-zero-divide+ +fp-inexact+ ; +CONSTANT: all-fp-exceptions + { + +fp-invalid-operation+ + +fp-overflow+ + +fp-underflow+ + +fp-zero-divide+ + +fp-inexact+ + } + SINGLETONS: +round-nearest+ +round-down+ From 9ccf5811b30074921912b54207a0e9dfda0f0388 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 18:15:16 -0500 Subject: [PATCH 007/345] vm: fix fp_trap_error() so that it can work properly in signal handlers --- vm/errors.cpp | 6 +++--- vm/mach_signal.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/vm/errors.cpp b/vm/errors.cpp index 1dcee889a3..c9d2a94e56 100644 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -130,9 +130,9 @@ void divide_by_zero_error() general_error(ERROR_DIVIDE_BY_ZERO,F,F,NULL); } -void fp_trap_error() +void fp_trap_error(stack_frame *signal_callstack_top) { - general_error(ERROR_FP_TRAP,F,F,NULL); + general_error(ERROR_FP_TRAP,F,F,signal_callstack_top); } PRIMITIVE(call_clear) @@ -158,7 +158,7 @@ void misc_signal_handler_impl() void fp_signal_handler_impl() { - fp_trap_error(); + fp_trap_error(signal_callstack_top); } } diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index 50a924f8e4..facf512b77 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -55,12 +55,12 @@ static void call_fault_handler( MACH_PROGRAM_COUNTER(thread_state) = (cell)memory_signal_handler_impl; } else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV) - { + { MACH_PROGRAM_COUNTER(thread_state) = (cell)fp_signal_handler_impl; - } - else - { - signal_number = exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT; + } + else + { + signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); MACH_PROGRAM_COUNTER(thread_state) = (cell)misc_signal_handler_impl; } } From 6ce3c1d62c02e969e58e406d1a279c5254a454b8 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 12 Sep 2009 19:43:23 -0500 Subject: [PATCH 008/345] CFSTRING: syntax for defining CF/Cocoa string constants. update core-foundation.run-loop to use CFSTRING: --- basis/core-foundation/run-loop/run-loop.factor | 6 +----- basis/core-foundation/strings/strings.factor | 5 +++++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/basis/core-foundation/run-loop/run-loop.factor b/basis/core-foundation/run-loop/run-loop.factor index 6446eacd08..10d858a32f 100644 --- a/basis/core-foundation/run-loop/run-loop.factor +++ b/basis/core-foundation/run-loop/run-loop.factor @@ -54,11 +54,7 @@ FUNCTION: void CFRunLoopRemoveTimer ( CFStringRef mode ) ; -: CFRunLoopDefaultMode ( -- alien ) - #! Ugly, but we don't have static NSStrings - \ CFRunLoopDefaultMode [ - "kCFRunLoopDefaultMode" - ] initialize-alien ; +CFSTRING: CFRunLoopDefaultMode "kCFRunLoopDefaultMode" TUPLE: run-loop fds sources timers ; diff --git a/basis/core-foundation/strings/strings.factor b/basis/core-foundation/strings/strings.factor index 413709d142..45f4460d13 100644 --- a/basis/core-foundation/strings/strings.factor +++ b/basis/core-foundation/strings/strings.factor @@ -83,3 +83,8 @@ FUNCTION: CFStringRef CFStringCreateWithCString ( : ( seq -- alien ) [ [ &CFRelease ] map ] with-destructors ; + +SYNTAX: CFSTRING: + CREATE scan-object + [ drop ] [ '[ _ [ _ ] initialize-alien ] ] 2bi + (( -- alien )) define-declared ; From e70fa134b299cd934386785d548a686f85f91cee Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 12 Sep 2009 19:43:57 -0500 Subject: [PATCH 009/345] add full complement of [SU]Int[0-9]+ typedefs to core-foundation --- basis/core-foundation/core-foundation.factor | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/basis/core-foundation/core-foundation.factor b/basis/core-foundation/core-foundation.factor index 63bfaf37ce..2ef388563e 100644 --- a/basis/core-foundation/core-foundation.factor +++ b/basis/core-foundation/core-foundation.factor @@ -8,11 +8,16 @@ TYPEDEF: void* CFTypeRef TYPEDEF: void* CFAllocatorRef CONSTANT: kCFAllocatorDefault f -TYPEDEF: bool Boolean -TYPEDEF: long CFIndex -TYPEDEF: char UInt8 -TYPEDEF: int SInt32 -TYPEDEF: uint UInt32 +TYPEDEF: bool Boolean +TYPEDEF: long CFIndex +TYPEDEF: uchar UInt8 +TYPEDEF: ushort UInt16 +TYPEDEF: uint UInt32 +TYPEDEF: ulonglong UInt64 +TYPEDEF: char SInt8 +TYPEDEF: short SInt16 +TYPEDEF: int SInt32 +TYPEDEF: longlong SInt64 TYPEDEF: ulong CFTypeID TYPEDEF: UInt32 CFOptionFlags TYPEDEF: void* CFUUIDRef @@ -32,3 +37,4 @@ FUNCTION: CFTypeRef CFRetain ( CFTypeRef cf ) ; FUNCTION: void CFRelease ( CFTypeRef cf ) ; DESTRUCTOR: CFRelease + From 569db73858a1289c1c4c69d3a6f71bab2f2db2c0 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 12 Sep 2009 19:44:13 -0500 Subject: [PATCH 010/345] QTKit binding --- extra/qtkit/qtkit.factor | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 extra/qtkit/qtkit.factor diff --git a/extra/qtkit/qtkit.factor b/extra/qtkit/qtkit.factor new file mode 100644 index 0000000000..d0567bdd48 --- /dev/null +++ b/extra/qtkit/qtkit.factor @@ -0,0 +1,76 @@ +USING: classes.struct cocoa core-foundation.strings ; +IN: qtkit + +STRUCT: QTTime + { timeValue longlong } + { timeScale long } + { flags long } ; + +STRUCT: QTTimeRange + { time QTTime } + { duration QTTime } ; + +STRUCT: SMPTETime + { mSubframes SInt16 } + { mSubframeDivisor SInt16 } + { mCounter UInt32 } + { mType UInt32 } + { mFlags UInt32 } + { mHours SInt16 } + { mMinutes SInt16 } + { mSeconds SInt16 } + { mFrames SInt16 } ; + +CFSTRING: QTKitErrorDomain "QTKitErrorDomain" +CFSTRING: QTErrorCaptureInputKey "QTErrorCaptureInputKey" +CFSTRING: QTErrorCaptureOutputKey "QTErrorCaptureOutputKey" +CFSTRING: QTErrorDeviceKey "QTErrorDeviceKey" +CFSTRING: QTErrorExcludingDeviceKey "QTErrorExcludingDeviceKey" +CFSTRING: QTErrorTimeKey "QTErrorTimeKey" +CFSTRING: QTErrorFileSizeKey "QTErrorFileSizeKey" +CFSTRING: QTErrorRecordingSuccesfullyFinishedKey "QTErrorRecordingSuccesfullyFinishedKey" + +CONSTANT: QTErrorUnknown -1 +CONSTANT: QTErrorIncompatibleInput 1002 +CONSTANT: QTErrorIncompatibleOutput 1003 +CONSTANT: QTErrorInvalidInputsOrOutputs 1100 +CONSTANT: QTErrorDeviceAlreadyUsedbyAnotherSession 1101 +CONSTANT: QTErrorNoDataCaptured 1200 +CONSTANT: QTErrorSessionConfigurationChanged 1201 +CONSTANT: QTErrorDiskFull 1202 +CONSTANT: QTErrorDeviceWasDisconnected 1203 +CONSTANT: QTErrorMediaChanged 1204 +CONSTANT: QTErrorMaximumDurationReached 1205 +CONSTANT: QTErrorMaximumFileSizeReached 1206 +CONSTANT: QTErrorMediaDiscontinuity 1207 +CONSTANT: QTErrorMaximumNumberOfSamplesForFileFormatReached 1208 +CONSTANT: QTErrorDeviceNotConnected 1300 +CONSTANT: QTErrorDeviceInUseByAnotherApplication 1301 +CONSTANT: QTErrorDeviceExcludedByAnotherDevice 1302 + +FRAMEWORK: /System/Library/Frameworks/QTKit.framework + +IMPORT: QTCaptureAudioPreviewOutput +IMPORT: QTCaptureConnection +IMPORT: QTCaptureDecompressedAudioOutput +IMPORT: QTCaptureDecompressedVideoOutput +IMPORT: QTCaptureDevice +IMPORT: QTCaptureDeviceInput +IMPORT: QTCaptureFileOutput +IMPORT: QTCaptureInput +IMPORT: QTCaptureLayer +IMPORT: QTCaptureMovieFileOutput +IMPORT: QTCaptureOutput +IMPORT: QTCaptureSession +IMPORT: QTCaptureVideoPreviewOutput +IMPORT: QTCaptureView +IMPORT: QTCompressionOptions +IMPORT: QTDataReference +IMPORT: QTFormatDescription +IMPORT: QTMedia +IMPORT: QTMovie +IMPORT: QTMovieLayer +IMPORT: QTMovieView +IMPORT: QTSampleBuffer +IMPORT: QTTrack + From 018677319cb314615759ece038e6690e38cc1269 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 21:07:31 -0400 Subject: [PATCH 011/345] math, syntax: fix help lint --- core/math/math-docs.factor | 6 +++--- core/syntax/syntax-docs.factor | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/math/math-docs.factor b/core/math/math-docs.factor index d98685fb48..97e0a1e7cf 100644 --- a/core/math/math-docs.factor +++ b/core/math/math-docs.factor @@ -282,12 +282,12 @@ HELP: fp-bitwise= { $examples "Not-a-number equality:" { $example - "USING: math prettyprint ;" + "USING: kernel math prettyprint ;" "0.0 0.0 / dup number= ." "f" } { $example - "USING: math prettyprint ;" + "USING: kernel math prettyprint ;" "0.0 0.0 / dup fp-bitwise= ." "t" } @@ -299,7 +299,7 @@ HELP: fp-bitwise= } { $example "USING: math prettyprint ;" - "-0.0 0.0 = ." + "-0.0 0.0 number= ." "t" } } ; diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index 19e644cb68..0a11f62c16 100644 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -612,8 +612,8 @@ HELP: NAN: { $examples { $example "USE: prettyprint" - "NAN: deadbeef ." - "NAN: deadbeef" + "NAN: 80000deadbeef ." + "NAN: 80000deadbeef" } } ; From 53e23de104e92edceb300c7bdec4ca920b6dfd42 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sat, 12 Sep 2009 20:39:41 -0500 Subject: [PATCH 012/345] set altivec denormal flag when with-denormal-mode is used --- basis/math/floats/env/ppc/ppc.factor | 42 +++++++++++++++++++++++++++- vm/Config.macosx.ppc | 2 +- vm/cpu-ppc.S | 23 +++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/basis/math/floats/env/ppc/ppc.factor b/basis/math/floats/env/ppc/ppc.factor index c4c81471ca..748f149ccd 100644 --- a/basis/math/floats/env/ppc/ppc.factor +++ b/basis/math/floats/env/ppc/ppc.factor @@ -7,19 +7,32 @@ STRUCT: ppc-fpu-env { padding uint } { fpscr uint } ; +STRUCT: ppc-vmx-env + { vscr uint } ; + ! defined in the vm, cpu-ppc*.S FUNCTION: void get_ppc_fpu_env ( ppc-fpu-env* env ) ; FUNCTION: void set_ppc_fpu_env ( ppc-fpu-env* env ) ; +FUNCTION: void get_ppc_vmx_env ( ppc-vmx-env* env ) ; +FUNCTION: void set_ppc_vmx_env ( ppc-vmx-env* env ) ; + : ( -- ppc-fpu-env ) ppc-fpu-env (struct) [ get_ppc_fpu_env ] keep ; +: ( -- ppc-fpu-env ) + ppc-vmx-env (struct) + [ get_ppc_vmx_env ] keep ; + M: ppc-fpu-env (set-fp-env-register) set_ppc_fpu_env ; +M: ppc-vmx-env (set-fp-env-register) + set_ppc_vmx_env ; + M: ppc (fp-env-registers) - 1array ; + 2array ; CONSTANT: ppc-exception-flag-bits HEX: 3e00,0000 CONSTANT: ppc-exception-flag>bit @@ -77,3 +90,30 @@ M: ppc-fpu-env (set-denormal-mode) ( register mode -- register' ) } case ] curry change-fpscr ; inline +CONSTANT: vmx-denormal-mode-bits HEX: 8000 + +M: ppc-vmx-env (get-exception-flags) ( register -- exceptions ) + drop { } ; inline +M: ppc-vmx-env (set-exception-flags) ( register exceptions -- register' ) + drop ; + +M: ppc-vmx-env (get-fp-traps) ( register -- exceptions ) + drop { } ; inline +M: ppc-vmx-env (set-fp-traps) ( register exceptions -- register' ) + drop ; + +M: ppc-vmx-env (get-rounding-mode) ( register -- mode ) + drop +round-nearest+ ; +M: ppc-vmx-env (set-rounding-mode) ( register mode -- register' ) + drop ; + +M: ppc-vmx-env (get-denormal-mode) ( register -- mode ) + vscr>> vmx-denormal-mode-bits mask zero? +denormal-keep+ +denormal-flush+ ? ; inline +M: ppc-vmx-env (get-denormal-mode) ( register mode -- register ) + [ + { + { +denormal-keep+ [ vmx-denormal-mode-bits unmask ] } + { +denormal-flush+ [ vmx-denormal-mode-bits bitor ] } + } case + ] curry change-vscr ; inline + diff --git a/vm/Config.macosx.ppc b/vm/Config.macosx.ppc index ed3c0d5a19..9fb84d6185 100644 --- a/vm/Config.macosx.ppc +++ b/vm/Config.macosx.ppc @@ -1,3 +1,3 @@ include vm/Config.macosx include vm/Config.ppc -CFLAGS += -arch ppc +CFLAGS += -arch ppc -force_cpusubtype_ALL diff --git a/vm/cpu-ppc.S b/vm/cpu-ppc.S index 67c9e8d142..007638189a 100644 --- a/vm/cpu-ppc.S +++ b/vm/cpu-ppc.S @@ -254,3 +254,26 @@ DEF(void,set_ppc_fpu_env,(const void*)): lfd f0,0(r3) mtfsf 0xff,f0 blr + +DEF(void,get_ppc_vmx_env,(void*)): + mfvscr v0 + subi r4,r1,16 + li r5,0xf + andc r4,r4,r5 + li r5,0xc + stvewx v0,r5,r4 + lwzx r6,r5,r4 + stw r6,0(r3) + blr + +DEF(void,set_ppc_vmx_env,(const void*)): + subi r4,r1,16 + li r5,0xf + andc r4,r4,r5 + li r5,0xc + lwz r6,0(r3) + stwx r6,r5,r4 + lvewx v0,r5,r4 + mtvscr v0 + blr + From 00e8e11d189f644b47b3731800adef5a5de07c54 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 12 Sep 2009 21:05:26 -0500 Subject: [PATCH 013/345] list file-systems with statfs instead of statvfs on openbsd --- basis/io/files/info/unix/openbsd/openbsd.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/io/files/info/unix/openbsd/openbsd.factor b/basis/io/files/info/unix/openbsd/openbsd.factor index ef1b55cda3..fe94f70fd8 100755 --- a/basis/io/files/info/unix/openbsd/openbsd.factor +++ b/basis/io/files/info/unix/openbsd/openbsd.factor @@ -49,6 +49,6 @@ M: openbsd statvfs>file-system-info ( file-system-info statvfs -- file-system-in M: openbsd file-systems ( -- seq ) f 0 0 getfsstat dup io-error - + [ dup byte-length 0 getfsstat io-error ] [ [ f_mntonname>> utf8 alien>string file-system-info ] { } map-as ] bi ; From a2b864e4f5f2e62c0248f8a98db9865f3d386c62 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 21:17:53 -0500 Subject: [PATCH 014/345] ALIEN: now reads a hexadecimal integer instead of a decimal one, since in general hex is more useful for addresses --- basis/alien/prettyprint/prettyprint.factor | 5 +++-- basis/alien/syntax/syntax-docs.factor | 2 +- basis/alien/syntax/syntax.factor | 2 +- basis/compiler/tests/intrinsics.factor | 10 +++++----- core/alien/alien-tests.factor | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/basis/alien/prettyprint/prettyprint.factor b/basis/alien/prettyprint/prettyprint.factor index 0794ab7789..0ffd5023a7 100644 --- a/basis/alien/prettyprint/prettyprint.factor +++ b/basis/alien/prettyprint/prettyprint.factor @@ -1,14 +1,15 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel combinators alien alien.strings alien.syntax -prettyprint.backend prettyprint.custom prettyprint.sections ; +math.parser prettyprint.backend prettyprint.custom +prettyprint.sections ; IN: alien.prettyprint M: alien pprint* { { [ dup expired? ] [ drop \ BAD-ALIEN pprint-word ] } { [ dup pinned-c-ptr? not ] [ drop "( displaced alien )" text ] } - [ \ ALIEN: [ alien-address pprint* ] pprint-prefix ] + [ \ ALIEN: [ alien-address >hex text ] pprint-prefix ] } cond ; M: dll pprint* dll-path dup "DLL\" " "\"" pprint-string ; diff --git a/basis/alien/syntax/syntax-docs.factor b/basis/alien/syntax/syntax-docs.factor index c9e03724f5..c9190f68c0 100644 --- a/basis/alien/syntax/syntax-docs.factor +++ b/basis/alien/syntax/syntax-docs.factor @@ -9,7 +9,7 @@ HELP: DLL" HELP: ALIEN: { $syntax "ALIEN: address" } -{ $values { "address" "a non-negative integer" } } +{ $values { "address" "a non-negative hexadecimal integer" } } { $description "Creates an alien object at parse time." } { $notes "Alien objects are invalidated between image saves and loads, and hence source files should not contain alien literals; this word is for interactive use only. See " { $link "alien-expiry" } " for details." } ; diff --git a/basis/alien/syntax/syntax.factor b/basis/alien/syntax/syntax.factor index 2b0270d5f5..e8206c6968 100644 --- a/basis/alien/syntax/syntax.factor +++ b/basis/alien/syntax/syntax.factor @@ -9,7 +9,7 @@ IN: alien.syntax SYNTAX: DLL" lexer get skip-blank parse-string dlopen parsed ; -SYNTAX: ALIEN: scan string>number parsed ; +SYNTAX: ALIEN: 16 scan-base parsed ; SYNTAX: BAD-ALIEN parsed ; diff --git a/basis/compiler/tests/intrinsics.factor b/basis/compiler/tests/intrinsics.factor index 988164143f..ad2d2c8be5 100644 --- a/basis/compiler/tests/intrinsics.factor +++ b/basis/compiler/tests/intrinsics.factor @@ -472,15 +472,15 @@ cell 8 = [ ] unit-test [ ALIEN: 123 ] [ - 123 [ ] compile-call + HEX: 123 [ ] compile-call ] unit-test [ ALIEN: 123 ] [ - 123 [ { fixnum } declare ] compile-call + HEX: 123 [ { fixnum } declare ] compile-call ] unit-test [ ALIEN: 123 ] [ - [ 123 ] compile-call + [ HEX: 123 ] compile-call ] unit-test [ f ] [ @@ -522,8 +522,8 @@ cell 8 = [ [ ALIEN: 1234 ALIEN: 2234 ] [ ALIEN: 234 [ { c-ptr } declare - [ 1000 swap ] - [ 2000 swap ] bi + [ HEX: 1000 swap ] + [ HEX: 2000 swap ] bi ] compile-call ] unit-test diff --git a/core/alien/alien-tests.factor b/core/alien/alien-tests.factor index 2d2cec168f..7eaa5cc50b 100644 --- a/core/alien/alien-tests.factor +++ b/core/alien/alien-tests.factor @@ -55,7 +55,7 @@ cell 8 = [ ] unit-test ] when -[ "ALIEN: 1234" ] [ 1234 unparse ] unit-test +[ "ALIEN: 1234" ] [ HEX: 1234 unparse ] unit-test [ ] [ 0 B{ 1 2 3 } drop ] unit-test From aad68418d26acb5cf1a11416013d8737ea6cfa3f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 21:18:17 -0500 Subject: [PATCH 015/345] math, syntax: document hexadecimal float literal syntax --- core/math/parser/parser-docs.factor | 16 ++++++++++++++-- core/syntax/syntax-docs.factor | 11 ++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/core/math/parser/parser-docs.factor b/core/math/parser/parser-docs.factor index 1e3ff4f996..af4c712836 100644 --- a/core/math/parser/parser-docs.factor +++ b/core/math/parser/parser-docs.factor @@ -5,7 +5,7 @@ IN: math.parser ARTICLE: "number-strings" "Converting between numbers and strings" "These words only convert between real numbers and strings. Complex numbers are constructed by the parser (" { $link "parser" } ") and printed by the prettyprinter (" { $link "prettyprint" } ")." $nl -"Note that only integers can be converted to and from strings using a representation other than base 10. Calling a word such as " { $link >oct } " on a float will give a result in base 10." +"Integers can be converted to and from arbitrary bases. Floating point numbers can only be converted to and from base 10 and 16." $nl "Converting numbers to strings:" { $subsection number>string } @@ -93,7 +93,19 @@ HELP: >oct HELP: >hex { $values { "n" real } { "str" string } } -{ $description "Outputs a string representation of a number using base 16." } ; +{ $description "Outputs a string representation of a number using base 16." } +{ $examples + { $example + "USING: math.parser prettyprint ;" + "3735928559 >hex ." + "\"deadbeef\"" + } + { $example + "USING: math.parser prettyprint ;" + "-15.5 >hex ." + "\"-f.8p0\"" + } +} ; HELP: string>float ( str -- n/f ) { $values { "str" string } { "n/f" "a real number or " { $link f } } } diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index 0a11f62c16..e34fb0957f 100644 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -59,20 +59,25 @@ ARTICLE: "syntax-ratios" "Ratio syntax" "More information on ratios can be found in " { $link "rationals" } ; ARTICLE: "syntax-floats" "Float syntax" -"Floating point literals must contain a decimal point, and may contain an exponent:" +"Floating point literals can be input in base 10 or 16. Base 10 literals must contain a decimal point, and may contain an exponent after " { $snippet "e" } ":" { $code "10.5" "-3.1456" "7.e13" "1.0e-5" } -"There are three special float values:" +"Base 16 literals use " { $snippet "p" } " instead of " { $snippet "e" } " for the exponent, which is still decimal:" +{ $example + "10.125 HEX: 1.44p3 = ." + "t" +} +"Syntax for special float values:" { $table { "Positive infinity" { $snippet "1/0." } } { "Negative infinity" { $snippet "-1/0." } } { "Not-a-number" { $snippet "0/0." } } } -"A Not-a-number with an arbitrary payload can be parsed in:" +"A Not-a-number with an arbitrary payload can also be parsed in:" { $subsection POSTPONE: NAN: } "More information on floats can be found in " { $link "floats" } "." ; From f890f39d7c5b7e87818406b56a8f962b54aaddde Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 21:45:57 -0500 Subject: [PATCH 016/345] math.parser: fix example --- core/math/parser/parser-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/math/parser/parser-docs.factor b/core/math/parser/parser-docs.factor index af4c712836..ebb9c8aa5e 100644 --- a/core/math/parser/parser-docs.factor +++ b/core/math/parser/parser-docs.factor @@ -103,7 +103,7 @@ HELP: >hex { $example "USING: math.parser prettyprint ;" "-15.5 >hex ." - "\"-f.8p0\"" + "\"-1.fp3\"" } } ; From 32b95c2cdfda7b9e7139127e4f8c78199bf80b58 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 22:20:13 -0500 Subject: [PATCH 017/345] math: add unordered comparison operators u< u<= u> u>= which behave exactly like < <= > >= except no floating point exceptions are set if one or both inputs are NaNs; also add efficient intrinsic for unordered? predicate, and fix propagation type functions for abs, absq, and bitnot --- .../cfg/intrinsics/float/float.factor | 5 +- .../compiler/cfg/intrinsics/intrinsics.factor | 15 +- basis/compiler/tests/float.factor | 12 ++ .../tree/comparisons/comparisons.factor | 36 +++-- .../known-words/known-words.factor | 56 ++++---- .../tree/propagation/propagation-tests.factor | 65 +++++++++ basis/math/floats/env/env-tests.factor | 130 ++++++++++++------ basis/math/floats/env/env.factor | 2 +- .../partial-dispatch/partial-dispatch.factor | 6 + .../known-words/known-words.factor | 18 ++- core/bootstrap/primitives.factor | 4 + core/math/floats/floats-docs.factor | 79 +++++++++-- core/math/floats/floats.factor | 13 +- core/math/integers/integers.factor | 10 ++ core/math/math-docs.factor | 43 +++++- core/math/math.factor | 7 +- vm/primitives.cpp | 6 + 17 files changed, 396 insertions(+), 111 deletions(-) diff --git a/basis/compiler/cfg/intrinsics/float/float.factor b/basis/compiler/cfg/intrinsics/float/float.factor index 8dab157f4e..8a65de5805 100644 --- a/basis/compiler/cfg/intrinsics/float/float.factor +++ b/basis/compiler/cfg/intrinsics/float/float.factor @@ -7,7 +7,10 @@ IN: compiler.cfg.intrinsics.float : emit-float-op ( insn -- ) [ 2inputs ] dip call ds-push ; inline -: emit-float-comparison ( cc -- ) +: emit-float-ordered-comparison ( cc -- ) + [ 2inputs ] dip ^^compare-float-ordered ds-push ; inline + +: emit-float-unordered-comparison ( cc -- ) [ 2inputs ] dip ^^compare-float-unordered ds-push ; inline : emit-float>fixnum ( -- ) diff --git a/basis/compiler/cfg/intrinsics/intrinsics.factor b/basis/compiler/cfg/intrinsics/intrinsics.factor index ec567558bd..a54caf23de 100644 --- a/basis/compiler/cfg/intrinsics/intrinsics.factor +++ b/basis/compiler/cfg/intrinsics/intrinsics.factor @@ -86,13 +86,18 @@ IN: compiler.cfg.intrinsics { math.private:float- [ drop [ ^^sub-float ] emit-float-op ] } { math.private:float* [ drop [ ^^mul-float ] emit-float-op ] } { math.private:float/f [ drop [ ^^div-float ] emit-float-op ] } - { math.private:float< [ drop cc< emit-float-comparison ] } - { math.private:float<= [ drop cc<= emit-float-comparison ] } - { math.private:float>= [ drop cc>= emit-float-comparison ] } - { math.private:float> [ drop cc> emit-float-comparison ] } - { math.private:float= [ drop cc= emit-float-comparison ] } + { math.private:float< [ drop cc< emit-float-ordered-comparison ] } + { math.private:float<= [ drop cc<= emit-float-ordered-comparison ] } + { math.private:float>= [ drop cc>= emit-float-ordered-comparison ] } + { math.private:float> [ drop cc> emit-float-ordered-comparison ] } + { math.private:float-u< [ drop cc< emit-float-unordered-comparison ] } + { math.private:float-u<= [ drop cc<= emit-float-unordered-comparison ] } + { math.private:float-u>= [ drop cc>= emit-float-unordered-comparison ] } + { math.private:float-u> [ drop cc> emit-float-unordered-comparison ] } + { math.private:float= [ drop cc= emit-float-unordered-comparison ] } { math.private:float>fixnum [ drop emit-float>fixnum ] } { math.private:fixnum>float [ drop emit-fixnum>float ] } + { math.floats.private:float-unordered? [ drop cc/<>= emit-float-unordered-comparison ] } { alien.accessors:alien-float [ float-rep emit-alien-float-getter ] } { alien.accessors:set-alien-float [ float-rep emit-alien-float-setter ] } { alien.accessors:alien-double [ double-rep emit-alien-float-getter ] } diff --git a/basis/compiler/tests/float.factor b/basis/compiler/tests/float.factor index 86d7899fab..14b347008c 100644 --- a/basis/compiler/tests/float.factor +++ b/basis/compiler/tests/float.factor @@ -88,3 +88,15 @@ IN: compiler.tests.float [ 17.5 ] [ 17.5 -11.3 [ float-max ] compile-call ] unit-test [ -11.3 ] [ -11.3 17.5 [ float-min ] compile-call ] unit-test [ -11.3 ] [ 17.5 -11.3 [ float-min ] compile-call ] unit-test + +[ t ] [ 0/0. 0/0. [ float-unordered? ] compile-call ] unit-test +[ t ] [ 0/0. 1.0 [ float-unordered? ] compile-call ] unit-test +[ t ] [ 1.0 0/0. [ float-unordered? ] compile-call ] unit-test +[ f ] [ 3.0 1.0 [ float-unordered? ] compile-call ] unit-test +[ f ] [ 1.0 3.0 [ float-unordered? ] compile-call ] unit-test + +[ 1 ] [ 0/0. 0/0. [ float-unordered? [ 1 ] [ 2 ] if ] compile-call ] unit-test +[ 1 ] [ 0/0. 1.0 [ float-unordered? [ 1 ] [ 2 ] if ] compile-call ] unit-test +[ 1 ] [ 1.0 0/0. [ float-unordered? [ 1 ] [ 2 ] if ] compile-call ] unit-test +[ 2 ] [ 3.0 1.0 [ float-unordered? [ 1 ] [ 2 ] if ] compile-call ] unit-test +[ 2 ] [ 1.0 3.0 [ float-unordered? [ 1 ] [ 2 ] if ] compile-call ] unit-test diff --git a/basis/compiler/tree/comparisons/comparisons.factor b/basis/compiler/tree/comparisons/comparisons.factor index 5f4b1e8dab..b8e79e33ca 100644 --- a/basis/compiler/tree/comparisons/comparisons.factor +++ b/basis/compiler/tree/comparisons/comparisons.factor @@ -1,28 +1,36 @@ -! Copyright (C) 2008 Slava Pestov. +! Copyright (C) 2008, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: math math.order math.intervals assocs combinators ; IN: compiler.tree.comparisons ! Some utilities for working with comparison operations. -CONSTANT: comparison-ops { < > <= >= } +CONSTANT: comparison-ops { < > <= >= u< u> u<= u>= } CONSTANT: generic-comparison-ops { before? after? before=? after=? } : assumption ( i1 i2 op -- i3 ) { - { \ < [ assume< ] } - { \ > [ assume> ] } - { \ <= [ assume<= ] } - { \ >= [ assume>= ] } + { \ < [ assume< ] } + { \ > [ assume> ] } + { \ <= [ assume<= ] } + { \ >= [ assume>= ] } + { \ u< [ assume< ] } + { \ u> [ assume> ] } + { \ u<= [ assume<= ] } + { \ u>= [ assume>= ] } } case ; : interval-comparison ( i1 i2 op -- result ) { - { \ < [ interval< ] } - { \ > [ interval> ] } - { \ <= [ interval<= ] } - { \ >= [ interval>= ] } + { \ < [ interval< ] } + { \ > [ interval> ] } + { \ <= [ interval<= ] } + { \ >= [ interval>= ] } + { \ u< [ interval< ] } + { \ u> [ interval> ] } + { \ u<= [ interval<= ] } + { \ u>= [ interval>= ] } } case ; : swap-comparison ( op -- op' ) @@ -31,6 +39,10 @@ CONSTANT: generic-comparison-ops { before? after? before=? after=? } { > < } { <= >= } { >= <= } + { u< u> } + { u> u< } + { u<= u>= } + { u>= u<= } } at ; : negate-comparison ( op -- op' ) @@ -39,6 +51,10 @@ CONSTANT: generic-comparison-ops { before? after? before=? after=? } { > <= } { <= > } { >= < } + { u< u>= } + { u> u<= } + { u<= u> } + { u>= u< } } at ; : specific-comparison ( op -- op' ) diff --git a/basis/compiler/tree/propagation/known-words/known-words.factor b/basis/compiler/tree/propagation/known-words/known-words.factor index 5fe7d5ee1b..63d2df543d 100644 --- a/basis/compiler/tree/propagation/known-words/known-words.factor +++ b/basis/compiler/tree/propagation/known-words/known-words.factor @@ -23,7 +23,7 @@ IN: compiler.tree.propagation.known-words { + - * / } [ { number number } "input-classes" set-word-prop ] each -{ /f < > <= >= } +{ /f < > <= >= u< u> u<= u>= } [ { real real } "input-classes" set-word-prop ] each { /i mod /mod } @@ -34,21 +34,6 @@ IN: compiler.tree.propagation.known-words \ bitnot { integer } "input-classes" set-word-prop -: real-op ( info quot -- quot' ) - [ - dup class>> real classes-intersect? - [ clone ] [ drop real ] if - ] dip - change-interval ; inline - -{ bitnot fixnum-bitnot bignum-bitnot } [ - [ [ interval-bitnot ] real-op ] "outputs" set-word-prop -] each - -\ abs [ [ interval-abs ] real-op ] "outputs" set-word-prop - -\ absq [ [ interval-absq ] real-op ] "outputs" set-word-prop - : math-closure ( class -- newclass ) { fixnum bignum integer rational float real number object } [ class<= ] with find nip ; @@ -56,15 +41,6 @@ IN: compiler.tree.propagation.known-words : fits-in-fixnum? ( interval -- ? ) fixnum-interval interval-subset? ; -: binary-op-class ( info1 info2 -- newclass ) - [ class>> ] bi@ - 2dup [ null-class? ] either? [ 2drop null ] [ - [ math-closure ] bi@ math-class-max - ] if ; - -: binary-op-interval ( info1 info2 quot -- newinterval ) - [ [ interval>> ] bi@ ] dip call ; inline - : won't-overflow? ( class interval -- ? ) [ fixnum class<= ] [ fits-in-fixnum? ] bi* and ; @@ -101,6 +77,36 @@ IN: compiler.tree.propagation.known-words [ drop float ] dip ] unless ; +: unary-op-class ( info -- newclass ) + class>> dup null-class? [ drop null ] [ math-closure ] if ; + +: unary-op-interval ( info quot -- newinterval ) + [ interval>> ] dip call ; inline + +: unary-op ( word interval-quot post-proc-quot -- ) + '[ + [ unary-op-class ] [ _ unary-op-interval ] bi + @ + + ] "outputs" set-word-prop ; + +{ bitnot fixnum-bitnot bignum-bitnot } [ + [ interval-bitnot ] [ integer-valued ] unary-op +] each + +\ abs [ interval-abs ] [ may-overflow real-valued ] unary-op + +\ absq [ interval-absq ] [ may-overflow real-valued ] unary-op + +: binary-op-class ( info1 info2 -- newclass ) + [ class>> ] bi@ + 2dup [ null-class? ] either? [ 2drop null ] [ + [ math-closure ] bi@ math-class-max + ] if ; + +: binary-op-interval ( info1 info2 quot -- newinterval ) + [ [ interval>> ] bi@ ] dip call ; inline + : binary-op ( word interval-quot post-proc-quot -- ) '[ [ binary-op-class ] [ _ binary-op-interval ] 2bi diff --git a/basis/compiler/tree/propagation/propagation-tests.factor b/basis/compiler/tree/propagation/propagation-tests.factor index 1b24bc0c8f..ec5fbd95cd 100644 --- a/basis/compiler/tree/propagation/propagation-tests.factor +++ b/basis/compiler/tree/propagation/propagation-tests.factor @@ -31,6 +31,8 @@ IN: compiler.tree.propagation.tests [ V{ 69 } ] [ [ [ 69 ] [ 69 ] if ] final-literals ] unit-test +[ V{ integer } ] [ [ bitnot ] final-classes ] unit-test + [ V{ fixnum } ] [ [ { fixnum } declare bitnot ] final-classes ] unit-test ! Test type propagation for math ops @@ -164,6 +166,18 @@ IN: compiler.tree.propagation.tests [ t ] [ [ absq ] final-info first interval>> [0,inf] = ] unit-test +[ t ] [ [ { fixnum } declare abs ] final-info first interval>> [0,inf] interval-subset? ] unit-test + +[ t ] [ [ { fixnum } declare absq ] final-info first interval>> [0,inf] interval-subset? ] unit-test + +[ V{ integer } ] [ [ { fixnum } declare abs ] final-classes ] unit-test + +[ V{ integer } ] [ [ { fixnum } declare absq ] final-classes ] unit-test + +[ t ] [ [ { bignum } declare abs ] final-info first interval>> [0,inf] interval-subset? ] unit-test + +[ t ] [ [ { bignum } declare absq ] final-info first interval>> [0,inf] interval-subset? ] unit-test + [ t ] [ [ { float } declare abs ] final-info first interval>> [0,inf] = ] unit-test [ t ] [ [ { float } declare absq ] final-info first interval>> [0,inf] = ] unit-test @@ -247,6 +261,13 @@ IN: compiler.tree.propagation.tests ] final-literals ] unit-test +[ V{ 1.5 } ] [ + [ + /f + dup 1.5 u<= [ dup 1.5 u>= [ ] [ drop 1.5 ] if ] [ drop 1.5 ] if + ] final-literals +] unit-test + [ V{ 1.5 } ] [ [ /f @@ -254,6 +275,13 @@ IN: compiler.tree.propagation.tests ] final-literals ] unit-test +[ V{ 1.5 } ] [ + [ + /f + dup 1.5 u<= [ dup 10 u>= [ ] [ drop 1.5 ] if ] [ drop 1.5 ] if + ] final-literals +] unit-test + [ V{ f } ] [ [ /f @@ -261,6 +289,13 @@ IN: compiler.tree.propagation.tests ] final-literals ] unit-test +[ V{ f } ] [ + [ + /f + dup 0.0 u<= [ dup 0.0 u>= [ drop 0.0 ] unless ] [ drop 0.0 ] if + ] final-literals +] unit-test + [ V{ fixnum } ] [ [ 0 dup 10 > [ 100 * ] when ] final-classes ] unit-test @@ -269,6 +304,14 @@ IN: compiler.tree.propagation.tests [ 0 dup 10 > [ drop "foo" ] when ] final-classes ] unit-test +[ V{ fixnum } ] [ + [ 0 dup 10 u> [ 100 * ] when ] final-classes +] unit-test + +[ V{ fixnum } ] [ + [ 0 dup 10 u> [ drop "foo" ] when ] final-classes +] unit-test + [ V{ fixnum } ] [ [ { fixnum } declare 3 3 - + ] final-classes ] unit-test @@ -277,6 +320,10 @@ IN: compiler.tree.propagation.tests [ dup 10 < [ 3 * 30 < ] [ drop t ] if ] final-literals ] unit-test +[ V{ t } ] [ + [ dup 10 u< [ 3 * 30 u< ] [ drop t ] if ] final-literals +] unit-test + [ V{ "d" } ] [ [ 3 { @@ -300,10 +347,18 @@ IN: compiler.tree.propagation.tests [ >fixnum dup 100 < [ 1 + ] [ "Oops" throw ] if ] final-classes ] unit-test +[ V{ fixnum } ] [ + [ >fixnum dup 100 u< [ 1 + ] [ "Oops" throw ] if ] final-classes +] unit-test + [ V{ -1 } ] [ [ 0 dup 100 < not [ 1 + ] [ 1 - ] if ] final-literals ] unit-test +[ V{ -1 } ] [ + [ 0 dup 100 u< not [ 1 + ] [ 1 - ] if ] final-literals +] unit-test + [ V{ 2 } ] [ [ [ 1 ] [ 1 ] if 1 + ] final-literals ] unit-test @@ -312,12 +367,22 @@ IN: compiler.tree.propagation.tests [ 0 * 10 < ] final-classes ] unit-test +[ V{ object } ] [ + [ 0 * 10 u< ] final-classes +] unit-test + [ V{ 27 } ] [ [ 123 bitand dup 10 < over 8 > and [ 3 * ] [ "B" throw ] if ] final-literals ] unit-test +[ V{ 27 } ] [ + [ + 123 bitand dup 10 u< over 8 u> and [ 3 * ] [ "B" throw ] if + ] final-literals +] unit-test + [ V{ 27 } ] [ [ dup number? over sequence? and [ diff --git a/basis/math/floats/env/env-tests.factor b/basis/math/floats/env/env-tests.factor index a0ffa0713c..0c38d69ea9 100644 --- a/basis/math/floats/env/env-tests.factor +++ b/basis/math/floats/env/env-tests.factor @@ -1,5 +1,6 @@ USING: kernel math math.floats.env math.floats.env.private -math.functions math.libm sequences tools.test ; +math.functions math.libm sequences tools.test locals +compiler.units kernel.private fry compiler math.private words ; IN: math.floats.env.tests : set-default-fp-env ( -- ) @@ -8,45 +9,29 @@ IN: math.floats.env.tests ! In case the tests screw up the FP env because of bugs in math.floats.env set-default-fp-env -[ t ] [ - [ 1.0 0.0 / drop ] collect-fp-exceptions - +fp-zero-divide+ swap member? -] unit-test +: test-fp-exception ( exception inputs quot -- quot' ) + '[ _ [ @ @ ] collect-fp-exceptions nip member? ] ; -[ t ] [ - [ 1.0 3.0 / drop ] collect-fp-exceptions - +fp-inexact+ swap member? -] unit-test +: test-fp-exception-compiled ( exception inputs quot -- quot' ) + '[ _ @ [ _ collect-fp-exceptions ] compile-call nip member? ] ; -[ t ] [ - [ 1.0e250 1.0e100 * drop ] collect-fp-exceptions - +fp-overflow+ swap member? -] unit-test +[ t ] +fp-zero-divide+ [ 1.0 0.0 ] [ /f ] test-fp-exception unit-test +[ t ] +fp-inexact+ [ 1.0 3.0 ] [ /f ] test-fp-exception unit-test +[ t ] +fp-overflow+ [ 1.0e250 1.0e100 ] [ * ] test-fp-exception unit-test +[ t ] +fp-underflow+ [ 1.0e-250 1.0e-100 ] [ * ] test-fp-exception unit-test +[ t ] +fp-overflow+ [ 2.0 100,000.0 ] [ fpow ] test-fp-exception unit-test +[ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception unit-test +[ t ] +fp-invalid-operation+ [ 0.0 0.0 ] [ /f ] test-fp-exception unit-test +[ t ] +fp-invalid-operation+ [ -1.0 ] [ fsqrt ] test-fp-exception unit-test -[ t ] [ - [ 1.0e-250 1.0e-100 * drop ] collect-fp-exceptions - +fp-underflow+ swap member? -] unit-test - -[ t ] [ - [ 2.0 100,000.0 ^ drop ] collect-fp-exceptions - +fp-overflow+ swap member? -] unit-test - -[ t ] [ - [ 2.0 -100,000.0 ^ drop ] collect-fp-exceptions - +fp-underflow+ swap member? -] unit-test - -[ t ] [ - [ 0.0 0.0 /f drop ] collect-fp-exceptions - +fp-invalid-operation+ swap member? -] unit-test - -[ t ] [ - [ -1.0 fsqrt drop ] collect-fp-exceptions - +fp-invalid-operation+ swap member? -] unit-test +[ t ] +fp-zero-divide+ [ 1.0 0.0 ] [ /f ] test-fp-exception-compiled unit-test +[ t ] +fp-inexact+ [ 1.0 3.0 ] [ /f ] test-fp-exception-compiled unit-test +[ t ] +fp-overflow+ [ 1.0e250 1.0e100 ] [ * ] test-fp-exception-compiled unit-test +[ t ] +fp-underflow+ [ 1.0e-250 1.0e-100 ] [ * ] test-fp-exception-compiled unit-test +[ t ] +fp-overflow+ [ 2.0 100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test +[ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test +[ t ] +fp-invalid-operation+ [ 0.0 0.0 ] [ /f ] test-fp-exception-compiled unit-test +[ t ] +fp-invalid-operation+ [ -1.0 ] [ fsqrt ] test-fp-exception-compiled unit-test [ HEX: 3fd5,5555,5555,5555 @@ -117,11 +102,72 @@ set-default-fp-env -1.0 3.0 /f double>bits ] unit-test -[ { +fp-zero-divide+ } [ 1.0 0.0 /f ] with-fp-traps ] must-fail -[ { +fp-inexact+ } [ 1.0 3.0 /f ] with-fp-traps ] must-fail -[ { +fp-invalid-operation+ } [ -1.0 fsqrt ] with-fp-traps ] must-fail -[ { +fp-overflow+ } [ 2.0 100,000.0 ^ ] with-fp-traps ] must-fail -[ { +fp-underflow+ } [ 2.0 -100,000.0 ^ ] with-fp-traps ] must-fail +: test-traps ( traps inputs quot -- quot' ) + append '[ _ _ with-fp-traps ] ; + +: test-traps-compiled ( traps inputs quot -- quot' ) + swapd '[ _ [ _ _ with-fp-traps ] compile-call ] ; + +{ +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps must-fail +{ +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps must-fail +{ +fp-invalid-operation+ } [ -1.0 ] [ fsqrt ] test-traps must-fail +{ +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps must-fail +{ +fp-underflow+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps must-fail + +{ +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps-compiled must-fail +{ +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps-compiled must-fail +{ +fp-invalid-operation+ } [ -1.0 ] [ fsqrt ] test-traps-compiled must-fail +{ +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps-compiled must-fail +{ +fp-underflow+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps-compiled must-fail + +! Ensure ordered comparisons raise traps +:: test-comparison-quot ( word -- quot ) + [ + { float float } declare + { +fp-invalid-operation+ } [ word execute ] with-fp-traps + ] ; + +: test-comparison ( inputs word -- quot ) + test-comparison-quot append ; + +: test-comparison-compiled ( inputs word -- quot ) + test-comparison-quot '[ @ _ compile-call ] ; + +\ float< "intrinsic" word-prop [ + [ 0/0. -15.0 ] \ < test-comparison must-fail + [ 0/0. -15.0 ] \ < test-comparison-compiled must-fail + [ -15.0 0/0. ] \ < test-comparison must-fail + [ -15.0 0/0. ] \ < test-comparison-compiled must-fail + [ 0/0. -15.0 ] \ <= test-comparison must-fail + [ 0/0. -15.0 ] \ <= test-comparison-compiled must-fail + [ -15.0 0/0. ] \ <= test-comparison must-fail + [ -15.0 0/0. ] \ <= test-comparison-compiled must-fail + [ 0/0. -15.0 ] \ > test-comparison must-fail + [ 0/0. -15.0 ] \ > test-comparison-compiled must-fail + [ -15.0 0/0. ] \ > test-comparison must-fail + [ -15.0 0/0. ] \ > test-comparison-compiled must-fail + [ 0/0. -15.0 ] \ >= test-comparison must-fail + [ 0/0. -15.0 ] \ >= test-comparison-compiled must-fail + [ -15.0 0/0. ] \ >= test-comparison must-fail + [ -15.0 0/0. ] \ >= test-comparison-compiled must-fail + + [ f ] [ 0/0. -15.0 ] \ u< test-comparison unit-test + [ f ] [ 0/0. -15.0 ] \ u< test-comparison-compiled unit-test + [ f ] [ -15.0 0/0. ] \ u< test-comparison unit-test + [ f ] [ -15.0 0/0. ] \ u< test-comparison-compiled unit-test + [ f ] [ 0/0. -15.0 ] \ u<= test-comparison unit-test + [ f ] [ 0/0. -15.0 ] \ u<= test-comparison-compiled unit-test + [ f ] [ -15.0 0/0. ] \ u<= test-comparison unit-test + [ f ] [ -15.0 0/0. ] \ u<= test-comparison-compiled unit-test + [ f ] [ 0/0. -15.0 ] \ u> test-comparison unit-test + [ f ] [ 0/0. -15.0 ] \ u> test-comparison-compiled unit-test + [ f ] [ -15.0 0/0. ] \ u> test-comparison unit-test + [ f ] [ -15.0 0/0. ] \ u> test-comparison-compiled unit-test + [ f ] [ 0/0. -15.0 ] \ u>= test-comparison unit-test + [ f ] [ 0/0. -15.0 ] \ u>= test-comparison-compiled unit-test + [ f ] [ -15.0 0/0. ] \ u>= test-comparison unit-test + [ f ] [ -15.0 0/0. ] \ u>= test-comparison-compiled unit-test +] when ! Ensure traps get cleared [ 1/0. ] [ 1.0 0.0 /f ] unit-test diff --git a/basis/math/floats/env/env.factor b/basis/math/floats/env/env.factor index 6a8110c4c1..ba198168da 100644 --- a/basis/math/floats/env/env.factor +++ b/basis/math/floats/env/env.factor @@ -102,7 +102,7 @@ PRIVATE> : clear-fp-exception-flags ( -- ) { } set-fp-exception-flags ; inline : collect-fp-exceptions ( quot -- exceptions ) - clear-fp-exception-flags call fp-exception-flags ; inline + [ clear-fp-exception-flags ] dip call fp-exception-flags ; inline : denormal-mode ( -- mode ) fp-env-register (get-denormal-mode) ; diff --git a/basis/math/partial-dispatch/partial-dispatch.factor b/basis/math/partial-dispatch/partial-dispatch.factor index 6679e81fcd..7c66c911de 100644 --- a/basis/math/partial-dispatch/partial-dispatch.factor +++ b/basis/math/partial-dispatch/partial-dispatch.factor @@ -197,6 +197,12 @@ SYMBOL: fast-math-ops \ <= define-math-ops \ > define-math-ops \ >= define-math-ops + + \ u< define-math-ops + \ u<= define-math-ops + \ u> define-math-ops + \ u>= define-math-ops + \ number= define-math-ops { { shift bignum bignum } bignum-shift } , diff --git a/basis/stack-checker/known-words/known-words.factor b/basis/stack-checker/known-words/known-words.factor index ea8f6f5f49..0de957b785 100644 --- a/basis/stack-checker/known-words/known-words.factor +++ b/basis/stack-checker/known-words/known-words.factor @@ -455,12 +455,12 @@ M: bad-executable summary \ float/f { float float } { float } define-primitive \ float/f make-foldable -\ float< { float float } { object } define-primitive -\ float< make-foldable - \ float-mod { float float } { float } define-primitive \ float-mod make-foldable +\ float< { float float } { object } define-primitive +\ float< make-foldable + \ float<= { float float } { object } define-primitive \ float<= make-foldable @@ -470,6 +470,18 @@ M: bad-executable summary \ float>= { float float } { object } define-primitive \ float>= make-foldable +\ float-u< { float float } { object } define-primitive +\ float-u< make-foldable + +\ float-u<= { float float } { object } define-primitive +\ float-u<= make-foldable + +\ float-u> { float float } { object } define-primitive +\ float-u> make-foldable + +\ float-u>= { float float } { object } define-primitive +\ float-u>= make-foldable + \ { object object } { word } define-primitive \ make-flushable diff --git a/core/bootstrap/primitives.factor b/core/bootstrap/primitives.factor index 13e17f90fd..355fa8ed58 100644 --- a/core/bootstrap/primitives.factor +++ b/core/bootstrap/primitives.factor @@ -409,6 +409,10 @@ tuple { "float<=" "math.private" (( x y -- ? )) } { "float>" "math.private" (( x y -- ? )) } { "float>=" "math.private" (( x y -- ? )) } + { "float-u<" "math.private" (( x y -- ? )) } + { "float-u<=" "math.private" (( x y -- ? )) } + { "float-u>" "math.private" (( x y -- ? )) } + { "float-u>=" "math.private" (( x y -- ? )) } { "" "words" (( name vocab -- word )) } { "word-xt" "words" (( word -- start end )) } { "getenv" "kernel.private" (( n -- obj )) } diff --git a/core/math/floats/floats-docs.factor b/core/math/floats/floats-docs.factor index ed4947e1f5..6e903a37e2 100644 --- a/core/math/floats/floats-docs.factor +++ b/core/math/floats/floats-docs.factor @@ -69,20 +69,54 @@ HELP: float> ( x y -- ? ) HELP: float>= ( x y -- ? ) { $values { "x" float } { "y" float } { "?" "a boolean" } } -{ $description "Primitive version of " { $link >= } "." } -{ $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link >= } " instead." } ; +{ $description "Primitive version of " { $link u>= } "." } +{ $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u>= } " instead." } ; -ARTICLE: "floats" "Floats" -{ $subsection float } -"Rational numbers represent " { $emphasis "exact" } " quantities. On the other hand, a floating point number is an " { $emphasis "approximate" } " value. While rationals can grow to any required precision, floating point numbers have limited precision, and manipulating them is usually faster than manipulating ratios or bignums." +HELP: float-u< ( x y -- ? ) +{ $values { "x" float } { "y" float } { "?" "a boolean" } } +{ $description "Primitive version of " { $link u< } "." } +{ $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u< } " instead." } ; + +HELP: float-u<= ( x y -- ? ) +{ $values { "x" float } { "y" float } { "?" "a boolean" } } +{ $description "Primitive version of " { $link u<= } "." } +{ $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u<= } " instead." } ; + +HELP: float-u> ( x y -- ? ) +{ $values { "x" float } { "y" float } { "?" "a boolean" } } +{ $description "Primitive version of " { $link u> } "." } +{ $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u> } " instead." } ; + +HELP: float-u>= ( x y -- ? ) +{ $values { "x" float } { "y" float } { "?" "a boolean" } } +{ $description "Primitive version of " { $link u>= } "." } +{ $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u>= } " instead." } ; + +ARTICLE: "math.floats.compare" "Floating point comparison operations" +"In mathematics, real numbers are linearly ordered; for any two numbers " { $snippet "a" } " and " { $snippet "b" } ", exactly one of the following is true:" +{ $code + "a < b" + "a = b" + "a > b" +} +"With floating point values, there is a fourth possibility; " { $snippet "a" } " and " { $snippet "b" } " may be " { $emphasis "unordered" } ". This happens if one or both values are Not-a-Number values." $nl -"Introducing a floating point number in a computation forces the result to be expressed in floating point." -{ $example "5/4 1/2 + ." "1+3/4" } -{ $example "5/4 0.5 + ." "1.75" } -"Integers and rationals can be converted to floats:" -{ $subsection >float } -"Two real numbers can be divided yielding a float result:" -{ $subsection /f } +"All comparison operators, including " { $link number= } ", return " { $link f } " in the unordered case (and in particular, this means that a NaN is not equal to itself)." +$nl +"The " { $emphasis "ordered" } " comparison operators set floating point exception flags if the result of the comparison is unordered. The standard comparison operators (" { $link < } ", " { $link <= } ", " { $link > } ", " { $link >= } ") perform ordered comparisons." +$nl +"The " { $link number= } " operation performs an unordered comparison. The following set of operators also perform unordered comparisons:" +{ $subsection u< } +{ $subsection u<= } +{ $subsection u> } +{ $subsection u>= } +"A word to check if two values are unordered with respect to each other:" +{ $subsection unordered? } +"To test for floating point exceptions, use the " { $vocab-link "math.floats.env" } " vocabulary." +$nl +"If neither input to a comparison operator is a floating point value, then " { $link u< } ", " { $link u<= } ", " { $link u> } " and " { $link u>= } " are equivalent to the ordered operators." ; + +ARTICLE: "math.floats.bitwise" "Bitwise operations on floats" "Floating point numbers are represented internally in IEEE 754 double-precision format. This internal representation can be accessed for advanced operations and input/output purposes." { $subsection float>bits } { $subsection double>bits } @@ -100,8 +134,25 @@ $nl { $subsection fp-snan? } { $subsection fp-infinity? } { $subsection fp-nan-payload } -"Comparing two floating point numbers:" +"Comparing two floating point numbers for bitwise equality:" { $subsection fp-bitwise= } -{ $see-also "syntax-floats" } ; +{ $see-also POSTPONE: NAN: } ; + +ARTICLE: "floats" "Floats" +{ $subsection float } +"Rational numbers represent " { $emphasis "exact" } " quantities. On the other hand, a floating point number is an " { $emphasis "approximate" } " value. While rationals can grow to any required precision, floating point numbers have limited precision, and manipulating them is usually faster than manipulating ratios or bignums." +$nl +"Introducing a floating point number in a computation forces the result to be expressed in floating point." +{ $example "5/4 1/2 + ." "1+3/4" } +{ $example "5/4 0.5 + ." "1.75" } +"Floating point literal syntax is documented in " { $link "syntax-floats" } "." +$nl +"Integers and rationals can be converted to floats:" +{ $subsection >float } +"Two real numbers can be divided yielding a float result:" +{ $subsection /f } +{ $subsection "math.floats.bitwise" } +{ $subsection "math.floats.compare" } +"The " { $vocab-link "math.floats.env" } " vocabulary provides functionality for controlling floating point exceptions, rounding modes, and denormal behavior." ; ABOUT: "floats" diff --git a/core/math/floats/floats.factor b/core/math/floats/floats.factor index 9c49e99231..bc419b94c5 100644 --- a/core/math/floats/floats.factor +++ b/core/math/floats/floats.factor @@ -3,6 +3,7 @@ USING: kernel math math.private ; IN: math.floats.private +: float-unordered? ( x y -- ? ) [ fp-nan? ] bi@ or ; : float-min ( x y -- z ) [ float< ] most ; foldable : float-max ( x y -- z ) [ float> ] most ; foldable @@ -17,11 +18,17 @@ M: float hashcode* nip float>bits ; inline M: float equal? over float? [ float= ] [ 2drop f ] if ; inline M: float number= float= ; inline -M: float < float< ; inline +M: float < float< ; inline M: float <= float<= ; inline -M: float > float> ; inline +M: float > float> ; inline M: float >= float>= ; inline +M: float unordered? float-unordered? ; inline +M: float u< float-u< ; inline +M: float u<= float-u<= ; inline +M: float u> float-u> ; inline +M: float u>= float-u>= ; inline + M: float + float+ ; inline M: float - float- ; inline M: float * float* ; inline @@ -58,8 +65,6 @@ M: float next-float ] if ] if ; inline -M: float unordered? [ fp-nan? ] bi@ or ; inline - M: float prev-float double>bits dup -0.0 double>bits >= [ 1 + bits>double ] [ ! negative diff --git a/core/math/integers/integers.factor b/core/math/integers/integers.factor index ed25e3bfa6..e684b8edfb 100644 --- a/core/math/integers/integers.factor +++ b/core/math/integers/integers.factor @@ -24,6 +24,11 @@ M: fixnum <= fixnum<= ; inline M: fixnum > fixnum> ; inline M: fixnum >= fixnum>= ; inline +M: fixnum u< fixnum< ; inline +M: fixnum u<= fixnum<= ; inline +M: fixnum u> fixnum> ; inline +M: fixnum u>= fixnum>= ; inline + M: fixnum + fixnum+ ; inline M: fixnum - fixnum- ; inline M: fixnum * fixnum* ; inline @@ -65,6 +70,11 @@ M: bignum <= bignum<= ; inline M: bignum > bignum> ; inline M: bignum >= bignum>= ; inline +M: bignum u< bignum< ; inline +M: bignum u<= bignum<= ; inline +M: bignum u> bignum> ; inline +M: bignum u>= bignum>= ; inline + M: bignum + bignum+ ; inline M: bignum - bignum- ; inline M: bignum * bignum* ; inline diff --git a/core/math/math-docs.factor b/core/math/math-docs.factor index 97e0a1e7cf..e5de106bbb 100644 --- a/core/math/math-docs.factor +++ b/core/math/math-docs.factor @@ -5,7 +5,9 @@ IN: math HELP: number= { $values { "x" number } { "y" number } { "?" "a boolean" } } { $description "Tests if two numbers have the same numeric value." } -{ $notes "This word differs from " { $link = } " in that it disregards differences in type when comparing numbers." } +{ $notes "This word differs from " { $link = } " in that it disregards differences in type when comparing numbers." +$nl +"This word performs an unordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } { $examples { $example "USING: math prettyprint ;" "3.0 3 number= ." "t" } { $example "USING: kernel math prettyprint ;" "3.0 3 = ." "f" } @@ -13,20 +15,47 @@ HELP: number= HELP: < { $values { "x" real } { "y" real } { "?" boolean } } -{ $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." } ; +{ $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." } +{ $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ; HELP: <= { $values { "x" real } { "y" real } { "?" boolean } } -{ $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." } ; +{ $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." } +{ $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ; HELP: > { $values { "x" real } { "y" real } { "?" boolean } } -{ $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." } ; +{ $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." } +{ $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ; HELP: >= { $values { "x" real } { "y" real } { "?" boolean } } -{ $description "Tests if " { $snippet "x" } " is greater than or equal to " { $snippet "y" } "." } ; +{ $description "Tests if " { $snippet "x" } " is greater than or equal to " { $snippet "y" } "." } +{ $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ; +HELP: unordered? +{ $values { "x" real } { "y" real } { "?" boolean } } +{ $description "Tests if " { $snippet "x" } " is unordered with respect to " { $snippet "y" } ". This can only occur if one or both values is a floating-point Not-a-Number value." } ; + +HELP: u< +{ $values { "x" real } { "y" real } { "?" boolean } } +{ $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." } +{ $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link < } ". See " { $link "math.floats.compare" } " for an explanation." } ; + +HELP: u<= +{ $values { "x" real } { "y" real } { "?" boolean } } +{ $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." } +{ $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link <= } ". See " { $link "math.floats.compare" } " for an explanation." } ; + +HELP: u> +{ $values { "x" real } { "y" real } { "?" boolean } } +{ $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." } +{ $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link > } ". See " { $link "math.floats.compare" } " for an explanation." } ; + +HELP: u>= +{ $values { "x" real } { "y" real } { "?" boolean } } +{ $description "Tests if " { $snippet "x" } " is greater than or equal to " { $snippet "y" } "." } +{ $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link >= } ". See " { $link "math.floats.compare" } " for an explanation." } ; HELP: + { $values { "x" number } { "y" number } { "z" number } } @@ -328,6 +357,10 @@ HELP: fp-infinity? { $example "USING: io kernel math ;" "-1/0. [ fp-infinity? ] [ 0 < ] bi and [ \"negative infinity\" print ] when" "negative infinity" } } ; +HELP: fp-sign +{ $values { "x" float } { "?" "a boolean" } } +{ $description "Outputs the sign bit of " { $snippet "x" } ". For ordered non-zero values, this is equivalent to calling " { $snippet "0 <" } ". For zero values, this outputs the zero's sign bit." } ; + HELP: fp-nan-payload { $values { "x" real } { "bits" integer } } { $description "If " { $snippet "x" } " is an IEEE Not-a-Number value, returns the payload encoded in the value. Returns " { $link f } " if " { $snippet "x" } " is not a " { $link float } "." } ; diff --git a/core/math/math.factor b/core/math/math.factor index 900c1e1cee..8ef4f38f9a 100755 --- a/core/math/math.factor +++ b/core/math/math.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2003, 2009 Slava Pestov. +! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff. ! See http://factorcode.org/license.txt for BSD license. USING: kernel math.private ; IN: math @@ -22,7 +22,12 @@ MATH: < ( x y -- ? ) foldable MATH: <= ( x y -- ? ) foldable MATH: > ( x y -- ? ) foldable MATH: >= ( x y -- ? ) foldable + MATH: unordered? ( x y -- ? ) foldable +MATH: u< ( x y -- ? ) foldable +MATH: u<= ( x y -- ? ) foldable +MATH: u> ( x y -- ? ) foldable +MATH: u>= ( x y -- ? ) foldable M: object unordered? 2drop f ; diff --git a/vm/primitives.cpp b/vm/primitives.cpp index 2359173d9b..6dbe281d0c 100644 --- a/vm/primitives.cpp +++ b/vm/primitives.cpp @@ -51,6 +51,12 @@ const primitive_type primitives[] = { primitive_float_lesseq, primitive_float_greater, primitive_float_greatereq, + /* The unordered comparison primitives don't have a non-optimizing + compiler implementation */ + primitive_float_less, + primitive_float_lesseq, + primitive_float_greater, + primitive_float_greatereq, primitive_word, primitive_word_xt, primitive_getenv, From 3a61107f1dab168787a4ae41b9297022dba97810 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 12 Sep 2009 22:30:11 -0500 Subject: [PATCH 018/345] typos in altivec env --- basis/math/floats/env/ppc/ppc.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/math/floats/env/ppc/ppc.factor b/basis/math/floats/env/ppc/ppc.factor index 748f149ccd..4ce3f0512e 100644 --- a/basis/math/floats/env/ppc/ppc.factor +++ b/basis/math/floats/env/ppc/ppc.factor @@ -90,7 +90,7 @@ M: ppc-fpu-env (set-denormal-mode) ( register mode -- register' ) } case ] curry change-fpscr ; inline -CONSTANT: vmx-denormal-mode-bits HEX: 8000 +CONSTANT: vmx-denormal-mode-bits HEX: 10000 M: ppc-vmx-env (get-exception-flags) ( register -- exceptions ) drop { } ; inline @@ -109,7 +109,7 @@ M: ppc-vmx-env (set-rounding-mode) ( register mode -- register' ) M: ppc-vmx-env (get-denormal-mode) ( register -- mode ) vscr>> vmx-denormal-mode-bits mask zero? +denormal-keep+ +denormal-flush+ ? ; inline -M: ppc-vmx-env (get-denormal-mode) ( register mode -- register ) +M: ppc-vmx-env (set-denormal-mode) ( register mode -- register ) [ { { +denormal-keep+ [ vmx-denormal-mode-bits unmask ] } From 7b36689416819ca0bc290f5b0fa5341e7cc78c41 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 00:17:24 -0500 Subject: [PATCH 019/345] core-foundation.strings: fix load error --- basis/core-foundation/strings/strings.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/core-foundation/strings/strings.factor b/basis/core-foundation/strings/strings.factor index 45f4460d13..4bbe050230 100644 --- a/basis/core-foundation/strings/strings.factor +++ b/basis/core-foundation/strings/strings.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: alien.syntax alien.strings io.encodings.string kernel sequences byte-arrays io.encodings.utf8 math core-foundation -core-foundation.arrays destructors ; +core-foundation.arrays destructors parser fry alien words ; IN: core-foundation.strings TYPEDEF: void* CFStringRef From 4686063d0fe65a03d69f07e1b0d8b26b60aa3641 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 00:17:45 -0500 Subject: [PATCH 020/345] qtkit: add tags and authors --- extra/qtkit/authors.txt | 1 + extra/qtkit/tags.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 extra/qtkit/authors.txt create mode 100644 extra/qtkit/tags.txt diff --git a/extra/qtkit/authors.txt b/extra/qtkit/authors.txt new file mode 100644 index 0000000000..f13c9c1e77 --- /dev/null +++ b/extra/qtkit/authors.txt @@ -0,0 +1 @@ +Joe Groff diff --git a/extra/qtkit/tags.txt b/extra/qtkit/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/qtkit/tags.txt @@ -0,0 +1 @@ +unportable From 4f094a7ce52c40a70061722a33d452d2c6cb57d8 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 13 Sep 2009 00:21:57 -0500 Subject: [PATCH 021/345] fix bootstrap on openbsd --- basis/io/files/info/unix/openbsd/openbsd.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/io/files/info/unix/openbsd/openbsd.factor b/basis/io/files/info/unix/openbsd/openbsd.factor index fe94f70fd8..be88929f2e 100755 --- a/basis/io/files/info/unix/openbsd/openbsd.factor +++ b/basis/io/files/info/unix/openbsd/openbsd.factor @@ -6,7 +6,7 @@ sequences system unix unix.getfsstat.openbsd grouping unix.statfs.openbsd unix.statvfs.openbsd unix.types arrays io.files.info.unix classes.struct specialized-arrays io.encodings.utf8 ; -SPECIALIZED-ARRAY: statvfs +SPECIALIZED-ARRAY: statfs IN: io.files.unix.openbsd TUPLE: openbsd-file-system-info < unix-file-system-info From 16209bf68d1e38109ed403f7212e0b7dbf5bcb48 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 00:24:31 -0500 Subject: [PATCH 022/345] specialized-arrays: fix unit tests now that ALIEN: expects a hex literal --- basis/specialized-arrays/specialized-arrays-tests.factor | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/basis/specialized-arrays/specialized-arrays-tests.factor b/basis/specialized-arrays/specialized-arrays-tests.factor index ebc21eec56..2698149bac 100755 --- a/basis/specialized-arrays/specialized-arrays-tests.factor +++ b/basis/specialized-arrays/specialized-arrays-tests.factor @@ -100,12 +100,12 @@ SPECIALIZED-ARRAY: test-struct ] unit-test ! Regression -STRUCT: fixed-string { text char[100] } ; +STRUCT: fixed-string { text char[64] } ; SPECIALIZED-ARRAY: fixed-string -[ { ALIEN: 123 ALIEN: 223 ALIEN: 323 ALIEN: 423 } ] [ - ALIEN: 123 4 [ (underlying)>> ] { } map-as +[ { ALIEN: 100 ALIEN: 140 ALIEN: 180 ALIEN: 1c0 } ] [ + ALIEN: 100 4 [ (underlying)>> ] { } map-as ] unit-test ! Ensure that byte-length works with direct arrays From 0d4845de2678121c81c3082884acfc2610925256 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 00:54:04 -0500 Subject: [PATCH 023/345] benchmark.gc1: reduce memory usage --- extra/benchmark/gc1/gc1.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/benchmark/gc1/gc1.factor b/extra/benchmark/gc1/gc1.factor index 8b0a3e6a43..da3b6bab66 100644 --- a/extra/benchmark/gc1/gc1.factor +++ b/extra/benchmark/gc1/gc1.factor @@ -3,6 +3,6 @@ USING: math sequences kernel ; IN: benchmark.gc1 -: gc1 ( -- ) 6000000 [ >bignum 1 + ] map drop ; +: gc1 ( -- ) 10 [ 600000 [ >bignum 1 + ] map drop ] times ; MAIN: gc1 From b469dc29fab54a1f6f489b167886383633877600 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sun, 13 Sep 2009 13:18:24 -0500 Subject: [PATCH 024/345] save vector registers, save control register, and enable denormals before calling factor in powerpc c_to_factor() --- vm/cpu-ppc.S | 58 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/vm/cpu-ppc.S b/vm/cpu-ppc.S index 007638189a..342ec83d7e 100644 --- a/vm/cpu-ppc.S +++ b/vm/cpu-ppc.S @@ -63,7 +63,9 @@ multiply_overflow: #define SAVED_FP_REGS_SIZE 144 -#define FRAME (RESERVED_SIZE + PARAM_SIZE + SAVED_INT_REGS_SIZE + SAVED_FP_REGS_SIZE + 8) +#define SAVED_V_REGS_SIZE 208 + +#define FRAME (RESERVED_SIZE + PARAM_SIZE + SAVED_INT_REGS_SIZE + SAVED_FP_REGS_SIZE + SAVED_V_REGS_SIZE + 8) #if defined( __APPLE__) #define LR_SAVE 8 @@ -85,6 +87,13 @@ multiply_overflow: #define SAVE_FP(register,offset) stfd register,SAVE_AT(offset)(r1) #define RESTORE_FP(register,offset) lfd register,SAVE_AT(offset)(r1) +#define SAVE_V(register,offset) \ + li r2,SAVE_AT(offset) XX \ + stvxl register,r2,r1 +#define RESTORE_V(register,offset) \ + li r2,SAVE_AT(offset) XX \ + lvxl register,r2,r1 + #define PROLOGUE \ mflr r0 XX /* get caller's return address */ \ stwu r1,-FRAME(r1) XX /* create a stack frame to hold non-volatile registers */ \ @@ -95,6 +104,8 @@ multiply_overflow: lwz r1,0(r1) XX /* destroy the stack frame */ \ mtlr r0 /* get ready to return */ + + /* We have to save and restore nonvolatile registers because the Factor compiler treats the entire register file as volatile. */ DEF(void,c_to_factor,(CELL quot)): @@ -137,6 +148,31 @@ DEF(void,c_to_factor,(CELL quot)): SAVE_FP(f30,52) SAVE_FP(f31,54) + SAVE_V(v20,56) + SAVE_V(v21,60) + SAVE_V(v22,64) + SAVE_V(v23,68) + SAVE_V(v24,72) + SAVE_V(v25,76) + SAVE_V(v26,80) + SAVE_V(v27,84) + SAVE_V(v28,88) + SAVE_V(v29,92) + SAVE_V(v30,96) + SAVE_V(v31,100) + + mfvscr v0 + li r2,SAVE_AT(104) + stvxl v0,r2,r1 + addi r2,r2,0xc + lwzx r4,r2,r1 + lis r5,0x1 + andc r4,r4,r5 + stwx r4,r2,r1 + subi r2,r2,0xc + lvxl v0,r2,r1 + mtvscr v0 + SAVE_INT(r3,19) /* save quotation since we're about to mangle it */ mr r3,r1 /* pass call stack pointer as an argument */ @@ -145,6 +181,22 @@ DEF(void,c_to_factor,(CELL quot)): RESTORE_INT(r3,19) /* restore quotation */ CALL_QUOT + RESTORE_V(v0,104) + mtvscr v0 + + RESTORE_V(v31,100) + RESTORE_V(v30,96) + RESTORE_V(v29,92) + RESTORE_V(v28,88) + RESTORE_V(v27,84) + RESTORE_V(v26,80) + RESTORE_V(v25,76) + RESTORE_V(v24,72) + RESTORE_V(v23,68) + RESTORE_V(v22,64) + RESTORE_V(v21,60) + RESTORE_V(v20,56) + RESTORE_FP(f31,54) RESTORE_FP(f30,52) RESTORE_FP(f29,50) @@ -260,8 +312,8 @@ DEF(void,get_ppc_vmx_env,(void*)): subi r4,r1,16 li r5,0xf andc r4,r4,r5 + stvxl v0,0,r4 li r5,0xc - stvewx v0,r5,r4 lwzx r6,r5,r4 stw r6,0(r3) blr @@ -273,7 +325,7 @@ DEF(void,set_ppc_vmx_env,(const void*)): li r5,0xc lwz r6,0(r3) stwx r6,r5,r4 - lvewx v0,r5,r4 + lvxl v0,0,r4 mtvscr v0 blr From a8d1cd313565865210a5616d57ea382e414026b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Br=C3=BCschweiler?= Date: Sun, 13 Sep 2009 21:40:58 +0200 Subject: [PATCH 025/345] xml-rpc: fix post-rpc --- basis/xml-rpc/xml-rpc.factor | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/basis/xml-rpc/xml-rpc.factor b/basis/xml-rpc/xml-rpc.factor index 690ebe94f8..370c778787 100644 --- a/basis/xml-rpc/xml-rpc.factor +++ b/basis/xml-rpc/xml-rpc.factor @@ -1,9 +1,10 @@ ! Copyright (C) 2005, 2009 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. -USING: accessors kernel xml arrays math generic http.client -combinators hashtables namespaces io base64 sequences strings -calendar xml.data xml.writer xml.traversal assocs math.parser -debugger calendar.format math.order xml.syntax ; +USING: accessors arrays assocs base64 calendar calendar.format +combinators debugger generic hashtables http http.client +http.client.private io io.encodings.string io.encodings.utf8 +kernel math math.order math.parser namespaces sequences strings +xml xml.data xml.syntax xml.traversal xml.writer ; IN: xml-rpc ! * Sending RPC requests @@ -174,9 +175,20 @@ TAG: array xml>item ] [ "Bad main tag name" server-error ] if ] if ; +string utf8 encode "text/xml" swap >>data ; + +: rpc-post-request ( xml url -- request ) + [ send-rpc xml-post-data ] [ "POST" ] bi* + swap >>post-data ; + +PRIVATE> + : post-rpc ( rpc url -- rpc ) ! This needs to do something in the event of an error - [ send-rpc ] dip http-post nip string>xml receive-rpc ; + rpc-post-request http-request nip string>xml receive-rpc ; : invoke-method ( params method url -- response ) [ swap ] dip post-rpc ; From 044139aa88f30cb00e4b4c051039f5f4ba4426ab Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sun, 13 Sep 2009 15:36:43 -0500 Subject: [PATCH 026/345] add a :NewFactorVocab command to vim plugin --- misc/vim/README | 6 ++++++ misc/vim/plugin/factor.vim | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/misc/vim/README b/misc/vim/README index 0a11654f15..db7e4f09a3 100644 --- a/misc/vim/README +++ b/misc/vim/README @@ -24,6 +24,8 @@ navigating Factor source: :FactorVocab factor.vocab.name Opens the source file implementing the "factor.vocab.name" vocabulary. + :NewFactorVocab factor.vocab.name + Creates a new factor vocabulary under the working vocabulary root. :FactorVocabImpl Opens the main implementation file for the current vocabulary (name.factor). The keyboard shortcut "\fi" is bound to this @@ -46,6 +48,10 @@ variables in your vimrc file: This variable should be set to a list of Factor vocabulary roots. The paths may be either relative to g:FactorRoot or absolute paths. The default value is ["core", "basis", "extra", "work"]. + g:FactorNewVocabRoot + This variable should be set to the vocabulary root in which + vocabularies created with NewFactorVocab should be created. The + default value is "work". Note: The syntax-highlighting file is automatically generated to include the names of all the vocabularies Factor knows about. To regenerate it manually, diff --git a/misc/vim/plugin/factor.vim b/misc/vim/plugin/factor.vim index 61a587aa42..aedae9770f 100644 --- a/misc/vim/plugin/factor.vim +++ b/misc/vim/plugin/factor.vim @@ -10,7 +10,12 @@ if !exists("g:FactorVocabRoots") let g:FactorVocabRoots = ["core", "basis", "extra", "work"] endif +if !exists("g:FactorNewVocabRoot") + let g:FactorNewVocabRoot = "work" +endif + command! -nargs=1 -complete=customlist,FactorCompleteVocab FactorVocab :call GoToFactorVocab("") +command! -nargs=1 -complete=customlist,FactorCompleteVocab NewFactorVocab :call MakeFactorVocab("") command! FactorVocabImpl :call GoToFactorVocabImpl() command! FactorVocabDocs :call GoToFactorVocabDocs() command! FactorVocabTests :call GoToFactorVocabTests() @@ -49,11 +54,11 @@ function! FactorCompleteVocab(arglead, cmdline, cursorpos) return vocabs endfunction -function! FactorVocabFile(root, vocab) +function! FactorVocabFile(root, vocab, mustexist) let vocabpath = substitute(a:vocab, "\\.", "/", "g") let vocabfile = FactorVocabRoot(a:root) . vocabpath . "/" . fnamemodify(vocabpath, ":t") . ".factor" - if getftype(vocabfile) != "" + if !a:mustexist || getftype(vocabfile) != "" return vocabfile else return "" @@ -62,7 +67,7 @@ endfunction function! GoToFactorVocab(vocab) for root in g:FactorVocabRoots - let vocabfile = FactorVocabFile(root, a:vocab) + let vocabfile = FactorVocabFile(root, a:vocab, 1) if vocabfile != "" exe "edit " fnameescape(vocabfile) return @@ -71,6 +76,15 @@ function! GoToFactorVocab(vocab) echo "Vocabulary " vocab " not found" endfunction +function! MakeFactorVocab(vocab) + let vocabfile = FactorVocabFile(g:FactorNewVocabRoot, a:vocab, 0) + echo vocabfile + let vocabdir = fnamemodify(vocabfile, ":h") + echo vocabdir + exe "!mkdir -p " shellescape(vocabdir) + exe "edit " fnameescape(vocabfile) +endfunction + function! FactorFileBase() let filename = expand("%:r") let filename = substitute(filename, "-docs", "", "") From e062cd34dd0d98ef50ebe0b3c195bad6b3b29e6f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 17:19:59 -0500 Subject: [PATCH 027/345] benchmark.simd-1, struct-arrays: reduce memory usage --- extra/benchmark/simd-1/simd-1.factor | 2 +- extra/benchmark/struct-arrays/struct-arrays.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/benchmark/simd-1/simd-1.factor b/extra/benchmark/simd-1/simd-1.factor index d5576b8cf5..4f57cca0bb 100644 --- a/extra/benchmark/simd-1/simd-1.factor +++ b/extra/benchmark/simd-1/simd-1.factor @@ -25,6 +25,6 @@ IN: benchmark.simd-1 >fixnum make-points [ normalize-points ] [ max-points ] bi print-point ; : main ( -- ) - 5000000 simd-benchmark ; + 10 [ 500000 simd-benchmark ] times ; MAIN: main diff --git a/extra/benchmark/struct-arrays/struct-arrays.factor b/extra/benchmark/struct-arrays/struct-arrays.factor index 799ef2d467..24c3ec965d 100644 --- a/extra/benchmark/struct-arrays/struct-arrays.factor +++ b/extra/benchmark/struct-arrays/struct-arrays.factor @@ -47,6 +47,6 @@ SPECIALIZED-ARRAY: point : struct-array-benchmark ( len -- ) make-points [ normalize-points ] [ max-points ] bi print-point ; -: main ( -- ) 5000000 struct-array-benchmark ; +: main ( -- ) 10 [ 500000 struct-array-benchmark ] times ; MAIN: main From 3ab6dbac22fb462cbe527dbec88445b04b8547eb Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 18:22:49 -0500 Subject: [PATCH 028/345] math.floats.env: fix compiled trap unit tests --- basis/math/floats/env/env-tests.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/math/floats/env/env-tests.factor b/basis/math/floats/env/env-tests.factor index 0c38d69ea9..91b699130a 100644 --- a/basis/math/floats/env/env-tests.factor +++ b/basis/math/floats/env/env-tests.factor @@ -106,7 +106,7 @@ set-default-fp-env append '[ _ _ with-fp-traps ] ; : test-traps-compiled ( traps inputs quot -- quot' ) - swapd '[ _ [ _ _ with-fp-traps ] compile-call ] ; + swapd '[ @ [ _ _ with-fp-traps ] compile-call ] ; { +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps must-fail { +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps must-fail From abedea0ccb8618a3a2065662c890677fd0ca3431 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 22:33:12 -0500 Subject: [PATCH 029/345] math.functions: loosen tests up a bit since exp(1) on FreeBSD x86/64 differs from e in the last bit --- basis/math/functions/functions-tests.factor | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/basis/math/functions/functions-tests.factor b/basis/math/functions/functions-tests.factor index cde1c64f94..7a6da72005 100644 --- a/basis/math/functions/functions-tests.factor +++ b/basis/math/functions/functions-tests.factor @@ -33,9 +33,9 @@ IN: math.functions.tests [ 0.0 ] [ 1.0 log ] unit-test [ 1.0 ] [ e log ] unit-test -[ t ] [ 1 exp e = ] unit-test -[ t ] [ 1.0 exp e = ] unit-test -[ 1.0 ] [ -1 exp e * ] unit-test +[ t ] [ 1 exp e 1.e-10 ~ ] unit-test +[ t ] [ 1.0 exp e 1.e-10 ~ ] unit-test +[ t ] [ -1 exp e * 1.0 1.e-10 ~ ] unit-test [ 1.0 ] [ 0 cosh ] unit-test [ 1.0 ] [ 0.0 cosh ] unit-test From a2de9d9e54575e30df2c53930dd62d6dd2688b05 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 23:12:47 -0500 Subject: [PATCH 030/345] compiler.cfg.builder: don't run certain tests if float intrinsics are not available --- .../compiler/cfg/builder/builder-tests.factor | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/basis/compiler/cfg/builder/builder-tests.factor b/basis/compiler/cfg/builder/builder-tests.factor index 8da73a1e0e..db0dd65a83 100644 --- a/basis/compiler/cfg/builder/builder-tests.factor +++ b/basis/compiler/cfg/builder/builder-tests.factor @@ -192,14 +192,16 @@ IN: compiler.cfg.builder.tests [ [ ##unbox-alien? ] contains-insn? ] bi ] unit-test -[ f t ] [ - [ { byte-array fixnum } declare alien-cell 4 alien-float ] - [ [ ##box-alien? ] contains-insn? ] - [ [ ##box-float? ] contains-insn? ] bi -] unit-test +\ alien-float "intrinsic" word-prop [ + [ f t ] [ + [ { byte-array fixnum } declare alien-cell 4 alien-float ] + [ [ ##box-alien? ] contains-insn? ] + [ [ ##box-float? ] contains-insn? ] bi + ] unit-test -[ f t ] [ - [ { byte-array fixnum } declare alien-cell { simple-alien } declare 4 alien-float ] - [ [ ##box-alien? ] contains-insn? ] - [ [ ##box-float? ] contains-insn? ] bi -] unit-test \ No newline at end of file + [ f t ] [ + [ { byte-array fixnum } declare alien-cell { simple-alien } declare 4 alien-float ] + [ [ ##box-alien? ] contains-insn? ] + [ [ ##box-float? ] contains-insn? ] bi + ] unit-test +] when \ No newline at end of file From 05b51d27393f10c956c87c0699016175866ec418 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 13 Sep 2009 23:26:09 -0500 Subject: [PATCH 031/345] math.floats.env: modify tests to take buggy Linux/x86-64 pow() into account --- basis/math/floats/env/env-tests.factor | 27 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/basis/math/floats/env/env-tests.factor b/basis/math/floats/env/env-tests.factor index 91b699130a..c1d8913703 100644 --- a/basis/math/floats/env/env-tests.factor +++ b/basis/math/floats/env/env-tests.factor @@ -1,6 +1,7 @@ USING: kernel math math.floats.env math.floats.env.private math.functions math.libm sequences tools.test locals -compiler.units kernel.private fry compiler math.private words ; +compiler.units kernel.private fry compiler math.private words +system ; IN: math.floats.env.tests : set-default-fp-env ( -- ) @@ -29,7 +30,13 @@ set-default-fp-env [ t ] +fp-overflow+ [ 1.0e250 1.0e100 ] [ * ] test-fp-exception-compiled unit-test [ t ] +fp-underflow+ [ 1.0e-250 1.0e-100 ] [ * ] test-fp-exception-compiled unit-test [ t ] +fp-overflow+ [ 2.0 100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test -[ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test + +! No underflow on Linux with this test, just inexact. Reported as an Ubuntu bug: +! https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/429113 +os linux? cpu x86.64? and [ + [ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test +] unless + [ t ] +fp-invalid-operation+ [ 0.0 0.0 ] [ /f ] test-fp-exception-compiled unit-test [ t ] +fp-invalid-operation+ [ -1.0 ] [ fsqrt ] test-fp-exception-compiled unit-test @@ -108,17 +115,17 @@ set-default-fp-env : test-traps-compiled ( traps inputs quot -- quot' ) swapd '[ @ [ _ _ with-fp-traps ] compile-call ] ; -{ +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps must-fail -{ +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps must-fail +{ +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps must-fail +{ +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps must-fail { +fp-invalid-operation+ } [ -1.0 ] [ fsqrt ] test-traps must-fail -{ +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps must-fail -{ +fp-underflow+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps must-fail +{ +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps must-fail +{ +fp-underflow+ +fp-inexact+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps must-fail -{ +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps-compiled must-fail -{ +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps-compiled must-fail +{ +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps-compiled must-fail +{ +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps-compiled must-fail { +fp-invalid-operation+ } [ -1.0 ] [ fsqrt ] test-traps-compiled must-fail -{ +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps-compiled must-fail -{ +fp-underflow+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps-compiled must-fail +{ +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps-compiled must-fail +{ +fp-underflow+ +fp-inexact+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps-compiled must-fail ! Ensure ordered comparisons raise traps :: test-comparison-quot ( word -- quot ) From fb43ae2daf20c0dc1443663e1ad8e80a0c7b05a4 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Sun, 13 Sep 2009 23:37:28 -0500 Subject: [PATCH 032/345] save the FP status out of the signal context and use it as part of the fp trap factor exception. clear the FP status before continuing after an exception --- basis/math/floats/env/ppc/ppc.factor | 2 +- vm/cpu-ppc.hpp | 18 +++++++++++ vm/cpu-x86.hpp | 18 +++++++++++ vm/errors.cpp | 7 ++-- vm/errors.hpp | 5 +-- vm/layouts.hpp | 10 ++++++ vm/mach_signal.cpp | 37 ++++++++++++++++----- vm/os-freebsd-x86.32.hpp | 27 ++++++++++++++++ vm/os-freebsd-x86.64.hpp | 21 ++++++++++++ vm/os-linux-x86.32.hpp | 14 ++++++++ vm/os-linux-x86.64.hpp | 14 ++++++++ vm/os-macosx-ppc.hpp | 45 +++++++++++++++++++++++--- vm/os-macosx-x86.32.hpp | 48 +++++++++++++++++++++++++--- vm/os-macosx-x86.64.hpp | 48 +++++++++++++++++++++++++--- vm/os-netbsd-x86.32.hpp | 3 ++ vm/os-netbsd-x86.64.hpp | 3 ++ vm/os-openbsd-x86.32.hpp | 3 ++ vm/os-openbsd-x86.64.hpp | 3 ++ vm/os-unix.cpp | 2 ++ vm/os-windows-nt.cpp | 3 ++ 20 files changed, 303 insertions(+), 28 deletions(-) diff --git a/basis/math/floats/env/ppc/ppc.factor b/basis/math/floats/env/ppc/ppc.factor index 4ce3f0512e..dd8fd88b13 100644 --- a/basis/math/floats/env/ppc/ppc.factor +++ b/basis/math/floats/env/ppc/ppc.factor @@ -34,7 +34,7 @@ M: ppc-vmx-env (set-fp-env-register) M: ppc (fp-env-registers) 2array ; -CONSTANT: ppc-exception-flag-bits HEX: 3e00,0000 +CONSTANT: ppc-exception-flag-bits HEX: fff8,0000 CONSTANT: ppc-exception-flag>bit H{ { +fp-invalid-operation+ HEX: 2000,0000 } diff --git a/vm/cpu-ppc.hpp b/vm/cpu-ppc.hpp index 6ae2cce27d..db02a72959 100644 --- a/vm/cpu-ppc.hpp +++ b/vm/cpu-ppc.hpp @@ -62,6 +62,24 @@ inline static bool tail_call_site_p(cell return_address) return (insn & 0x1) == 0; } +inline static unsigned int fpu_status(unsigned int status) +{ + unsigned int r = 0; + + if (status & 0x20000000) + r |= FP_TRAP_INVALID_OPERATION; + if (status & 0x10000000) + r |= FP_TRAP_OVERFLOW; + if (status & 0x08000000) + r |= FP_TRAP_UNDERFLOW; + if (status & 0x04000000) + r |= FP_TRAP_ZERO_DIVIDE; + if (status & 0x02000000) + r |= FP_TRAP_INEXACT; + + return r; +} + /* Defined in assembly */ VM_ASM_API void c_to_factor(cell quot); VM_ASM_API void throw_impl(cell quot, stack_frame *rewind); diff --git a/vm/cpu-x86.hpp b/vm/cpu-x86.hpp index e5852f9ad9..7054f90735 100644 --- a/vm/cpu-x86.hpp +++ b/vm/cpu-x86.hpp @@ -50,6 +50,24 @@ inline static bool tail_call_site_p(cell return_address) return call_site_opcode(return_address) == jmp_opcode; } +inline static unsigned int fpu_status(unsigned int status) +{ + unsigned int r = 0; + + if (status & 0x01) + r |= FP_TRAP_INVALID_OPERATION; + if (status & 0x04) + r |= FP_TRAP_ZERO_DIVIDE; + if (status & 0x08) + r |= FP_TRAP_OVERFLOW; + if (status & 0x10) + r |= FP_TRAP_UNDERFLOW; + if (status & 0x20) + r |= FP_TRAP_INEXACT; + + return r; +} + /* Defined in assembly */ VM_ASM_API void c_to_factor(cell quot); VM_ASM_API void throw_impl(cell quot, stack_frame *rewind_to); diff --git a/vm/errors.cpp b/vm/errors.cpp index c9d2a94e56..ebe6201f72 100644 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -7,6 +7,7 @@ namespace factor user-space */ cell signal_number; cell signal_fault_addr; +unsigned int signal_fpu_status; stack_frame *signal_callstack_top; void out_of_memory() @@ -130,9 +131,9 @@ void divide_by_zero_error() general_error(ERROR_DIVIDE_BY_ZERO,F,F,NULL); } -void fp_trap_error(stack_frame *signal_callstack_top) +void fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top) { - general_error(ERROR_FP_TRAP,F,F,signal_callstack_top); + general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),F,signal_callstack_top); } PRIMITIVE(call_clear) @@ -158,7 +159,7 @@ void misc_signal_handler_impl() void fp_signal_handler_impl() { - fp_trap_error(signal_callstack_top); + fp_trap_error(signal_fpu_status,signal_callstack_top); } } diff --git a/vm/errors.hpp b/vm/errors.hpp index e4be61cdbf..7f3c4dcd4a 100644 --- a/vm/errors.hpp +++ b/vm/errors.hpp @@ -20,7 +20,7 @@ enum vm_error_type ERROR_RS_UNDERFLOW, ERROR_RS_OVERFLOW, ERROR_MEMORY, - ERROR_FP_TRAP, + ERROR_FP_TRAP, }; void out_of_memory(); @@ -36,7 +36,7 @@ void memory_protection_error(cell addr, stack_frame *native_stack); void signal_error(int signal, stack_frame *native_stack); void type_error(cell type, cell tagged); void not_implemented_error(); -void fp_trap_error(); +void fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top); PRIMITIVE(call_clear); PRIMITIVE(unimplemented); @@ -45,6 +45,7 @@ PRIMITIVE(unimplemented); user-space */ extern cell signal_number; extern cell signal_fault_addr; +extern unsigned int signal_fpu_status; extern stack_frame *signal_callstack_top; void memory_signal_handler_impl(); diff --git a/vm/layouts.hpp b/vm/layouts.hpp index 7736143c50..a14c234aaa 100644 --- a/vm/layouts.hpp +++ b/vm/layouts.hpp @@ -67,6 +67,16 @@ inline static cell align8(cell a) /* Not a real type, but code_block's type field can be set to this */ #define PIC_TYPE 69 +/* Constants used when floating-point trap exceptions are thrown */ +enum +{ + FP_TRAP_INVALID_OPERATION = 1 << 0, + FP_TRAP_OVERFLOW = 1 << 1, + FP_TRAP_UNDERFLOW = 1 << 2, + FP_TRAP_ZERO_DIVIDE = 1 << 3, + FP_TRAP_INEXACT = 1 << 4, +}; + inline static bool immediate_p(cell obj) { return (obj == F || TAG(obj) == FIXNUM_TYPE); diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index facf512b77..d8eea06f0b 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -32,7 +32,8 @@ static void call_fault_handler( exception_type_t exception, exception_data_type_t code, MACH_EXC_STATE_TYPE *exc_state, - MACH_THREAD_STATE_TYPE *thread_state) + MACH_THREAD_STATE_TYPE *thread_state, + MACH_FLOAT_STATE_TYPE *float_state) { /* There is a race condition here, but in practice an exception delivered during stack frame setup/teardown or while transitioning @@ -56,6 +57,8 @@ static void call_fault_handler( } else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV) { + signal_fpu_status = fpu_status(mach_fpu_status(float_state)); + mach_clear_fpu_status(float_state); MACH_PROGRAM_COUNTER(thread_state) = (cell)fp_signal_handler_impl; } else @@ -78,14 +81,15 @@ catch_exception_raise (mach_port_t exception_port, { MACH_EXC_STATE_TYPE exc_state; MACH_THREAD_STATE_TYPE thread_state; - mach_msg_type_number_t state_count; + MACH_FLOAT_STATE_TYPE float_state; + mach_msg_type_number_t exc_state_count, thread_state_count, float_state_count; /* Get fault information and the faulting thread's register contents.. See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_get_state.html. */ - state_count = MACH_EXC_STATE_COUNT; + exc_state_count = MACH_EXC_STATE_COUNT; if (thread_get_state (thread, MACH_EXC_STATE_FLAVOR, - (natural_t *)&exc_state, &state_count) + (natural_t *)&exc_state, &exc_state_count) != KERN_SUCCESS) { /* The thread is supposed to be suspended while the exception @@ -93,9 +97,19 @@ catch_exception_raise (mach_port_t exception_port, return KERN_FAILURE; } - state_count = MACH_THREAD_STATE_COUNT; + thread_state_count = MACH_THREAD_STATE_COUNT; if (thread_get_state (thread, MACH_THREAD_STATE_FLAVOR, - (natural_t *)&thread_state, &state_count) + (natural_t *)&thread_state, &thread_state_count) + != KERN_SUCCESS) + { + /* The thread is supposed to be suspended while the exception + handler is called. This shouldn't fail. */ + return KERN_FAILURE; + } + + float_state_count = MACH_FLOAT_STATE_COUNT; + if (thread_get_state (thread, MACH_FLOAT_STATE_FLAVOR, + (natural_t *)&float_state, &float_state_count) != KERN_SUCCESS) { /* The thread is supposed to be suspended while the exception @@ -105,13 +119,20 @@ catch_exception_raise (mach_port_t exception_port, /* Modify registers so to have the thread resume executing the fault handler */ - call_fault_handler(exception,code[0],&exc_state,&thread_state); + call_fault_handler(exception,code[0],&exc_state,&thread_state,&float_state); /* Set the faulting thread's register contents.. See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_state.html. */ + if (thread_set_state (thread, MACH_FLOAT_STATE_FLAVOR, + (natural_t *)&float_state, float_state_count) + != KERN_SUCCESS) + { + return KERN_FAILURE; + } + if (thread_set_state (thread, MACH_THREAD_STATE_FLAVOR, - (natural_t *)&thread_state, state_count) + (natural_t *)&thread_state, thread_state_count) != KERN_SUCCESS) { return KERN_FAILURE; diff --git a/vm/os-freebsd-x86.32.hpp b/vm/os-freebsd-x86.32.hpp index c276ce6174..e386532b0c 100644 --- a/vm/os-freebsd-x86.32.hpp +++ b/vm/os-freebsd-x86.32.hpp @@ -1,4 +1,5 @@ #include +#include namespace factor { @@ -9,6 +10,32 @@ inline static void *ucontext_stack_pointer(void *uap) return (void *)ucontext->uc_mcontext.mc_esp; } +inline static unsigned int uap_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { + struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); + return x87->en_sw; + } else if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + return xmm->en_sw | xmm->en_mxcsr; + } else + return 0; +} + +inline static void uap_clear_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { + struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); + x87->en_sw = 0; + } else if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + xmm->en_sw = 0; + xmm->en_mxcsr &= 0xffffffc0; + } +} + #define UAP_PROGRAM_COUNTER(ucontext) (((ucontext_t *)(ucontext))->uc_mcontext.mc_eip) } diff --git a/vm/os-freebsd-x86.64.hpp b/vm/os-freebsd-x86.64.hpp index 6ee491f3ae..78c08447bd 100644 --- a/vm/os-freebsd-x86.64.hpp +++ b/vm/os-freebsd-x86.64.hpp @@ -1,4 +1,5 @@ #include +#include namespace factor { @@ -9,6 +10,26 @@ inline static void *ucontext_stack_pointer(void *uap) return (void *)ucontext->uc_mcontext.mc_rsp; } +inline static unsigned int uap_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + return xmm->en_sw | xmm->en_mxcsr; + } else + return 0; +} + +inline static void uap_clear_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + xmm->en_sw = 0; + xmm->en_mxcsr &= 0xffffffc0; + } +} + #define UAP_PROGRAM_COUNTER(ucontext) (((ucontext_t *)(ucontext))->uc_mcontext.mc_rip) } diff --git a/vm/os-linux-x86.32.hpp b/vm/os-linux-x86.32.hpp index 4ba7c77e4b..e4fd8402a8 100644 --- a/vm/os-linux-x86.32.hpp +++ b/vm/os-linux-x86.32.hpp @@ -9,6 +9,20 @@ inline static void *ucontext_stack_pointer(void *uap) return (void *)ucontext->uc_mcontext.gregs[7]; } +inline static unsigned int uap_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + return ucontext->uc_mcontext.fpregs->swd + | ucontext->uc_mcontext.fpregs->mxcsr; +} + +inline static void uap_clear_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + ucontext->uc_mcontext.fpregs->swd = 0; + ucontext->uc_mcontext.fpregs->mxcsr &= 0xffffffc0; +} + #define UAP_PROGRAM_COUNTER(ucontext) \ (((ucontext_t *)(ucontext))->uc_mcontext.gregs[14]) diff --git a/vm/os-linux-x86.64.hpp b/vm/os-linux-x86.64.hpp index 477e21708c..42adb3c6b8 100644 --- a/vm/os-linux-x86.64.hpp +++ b/vm/os-linux-x86.64.hpp @@ -9,6 +9,20 @@ inline static void *ucontext_stack_pointer(void *uap) return (void *)ucontext->uc_mcontext.gregs[15]; } +inline static unsigned int uap_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + return ucontext->uc_mcontext.fpregs->swd + | ucontext->uc_mcontext.fpregs->mxcsr; +} + +inline static void uap_clear_fpu_status(void *uap) +{ + ucontext_t *ucontext = (ucontext_t *)uap; + ucontext->uc_mcontext.fpregs->swd = 0; + ucontext->uc_mcontext.fpregs->mxcsr &= 0xffffffc0; +} + #define UAP_PROGRAM_COUNTER(ucontext) \ (((ucontext_t *)(ucontext))->uc_mcontext.gregs[16]) diff --git a/vm/os-macosx-ppc.hpp b/vm/os-macosx-ppc.hpp index 62e71bfa69..31a1e22882 100644 --- a/vm/os-macosx-ppc.hpp +++ b/vm/os-macosx-ppc.hpp @@ -18,28 +18,63 @@ Modified for Factor by Slava Pestov */ #define MACH_EXC_STATE_TYPE ppc_exception_state_t #define MACH_EXC_STATE_FLAVOR PPC_EXCEPTION_STATE #define MACH_EXC_STATE_COUNT PPC_EXCEPTION_STATE_COUNT + #define MACH_EXC_INTEGER_DIV EXC_PPC_ZERO_DIVIDE + #define MACH_THREAD_STATE_TYPE ppc_thread_state_t #define MACH_THREAD_STATE_FLAVOR PPC_THREAD_STATE #define MACH_THREAD_STATE_COUNT PPC_THREAD_STATE_COUNT +#define MACH_FLOAT_STATE_TYPE ppc_float_state_t +#define MACH_FLOAT_STATE_FLAVOR PPC_FLOAT_STATE +#define MACH_FLOAT_STATE_COUNT PPC_FLOAT_STATE_COUNT + #if __DARWIN_UNIX03 #define MACH_EXC_STATE_FAULT(exc_state) (exc_state)->__dar #define MACH_STACK_POINTER(thr_state) (thr_state)->__r1 #define MACH_PROGRAM_COUNTER(thr_state) (thr_state)->__srr0 - #define UAP_PROGRAM_COUNTER(ucontext) \ - MACH_PROGRAM_COUNTER(&(((ucontext_t *)(ucontext))->uc_mcontext->__ss)) + + #define UAP_SS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->__ss) + #define UAP_FS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->__fs) + + #define FPSCR(float_state) (float_state)->__fpscr #else #define MACH_EXC_STATE_FAULT(exc_state) (exc_state)->dar #define MACH_STACK_POINTER(thr_state) (thr_state)->r1 #define MACH_PROGRAM_COUNTER(thr_state) (thr_state)->srr0 - #define UAP_PROGRAM_COUNTER(ucontext) \ - MACH_PROGRAM_COUNTER(&(((ucontext_t *)(ucontext))->uc_mcontext->ss)) + + #define UAP_SS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->ss) + #define UAP_FS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->fs) + + #define FPSCR(float_state) (float_state)->fpscr #endif +#define UAP_PROGRAM_COUNTER(ucontext) \ + MACH_PROGRAM_COUNTER(UAP_SS(ucontext)) + +inline static unsigned int mach_fpu_status(ppc_float_state_t *float_state) +{ + return FPSCR(float_state); +} + +inline static unsigned int uap_fpu_status(void *uap) +{ + return mach_fpu_status(UAP_FS(uap)); +} + inline static cell fix_stack_pointer(cell sp) { - return sp; + return sp; +} + +inline static void mach_clear_fpu_status(ppc_float_state_t *float_state) +{ + FPSCR(float_state) &= 0x0007ffff; +} + +inline static void uap_clear_fpu_status(void *uap) +{ + mach_clear_fpu_status(UAP_FS(uap)); } } diff --git a/vm/os-macosx-x86.32.hpp b/vm/os-macosx-x86.32.hpp index 2275555846..01ad28df4f 100644 --- a/vm/os-macosx-x86.32.hpp +++ b/vm/os-macosx-x86.32.hpp @@ -16,28 +16,68 @@ Modified for Factor by Slava Pestov */ #define MACH_EXC_STATE_TYPE i386_exception_state_t #define MACH_EXC_STATE_FLAVOR i386_EXCEPTION_STATE #define MACH_EXC_STATE_COUNT i386_EXCEPTION_STATE_COUNT + #define MACH_EXC_INTEGER_DIV EXC_I386_DIV + #define MACH_THREAD_STATE_TYPE i386_thread_state_t #define MACH_THREAD_STATE_FLAVOR i386_THREAD_STATE #define MACH_THREAD_STATE_COUNT i386_THREAD_STATE_COUNT +#define MACH_FLOAT_STATE_TYPE i386_float_state_t +#define MACH_FLOAT_STATE_FLAVOR i386_FLOAT_STATE +#define MACH_FLOAT_STATE_COUNT i386_FLOAT_STATE_COUNT + #if __DARWIN_UNIX03 #define MACH_EXC_STATE_FAULT(exc_state) (exc_state)->__faultvaddr #define MACH_STACK_POINTER(thr_state) (thr_state)->__esp #define MACH_PROGRAM_COUNTER(thr_state) (thr_state)->__eip - #define UAP_PROGRAM_COUNTER(ucontext) \ - MACH_PROGRAM_COUNTER(&(((ucontext_t *)(ucontext))->uc_mcontext->__ss)) + + #define UAP_SS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->__ss) + #define UAP_FS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->__fs) + + #define MXCSR(float_state) (float_state)->__fpu_mxcsr + #define X87SW(float_state) (float_state)->__fpu_fsw #else #define MACH_EXC_STATE_FAULT(exc_state) (exc_state)->faultvaddr #define MACH_STACK_POINTER(thr_state) (thr_state)->esp #define MACH_PROGRAM_COUNTER(thr_state) (thr_state)->eip - #define UAP_PROGRAM_COUNTER(ucontext) \ - MACH_PROGRAM_COUNTER(&(((ucontext_t *)(ucontext))->uc_mcontext->ss)) + + #define UAP_SS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->ss) + #define UAP_FS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->fs) + + #define MXCSR(float_state) (float_state)->fpu_mxcsr + #define X87SW(float_state) (float_state)->fpu_fsw #endif +#define UAP_PROGRAM_COUNTER(ucontext) \ + MACH_PROGRAM_COUNTER(UAP_SS(ucontext)) + +inline static unsigned int mach_fpu_status(i386_float_state_t *float_state) +{ + unsigned short x87sw; + memcpy(&x87sw, &X87SW(float_state), sizeof(x87sw)); + return MXCSR(float_state) | x87sw; +} + +inline static unsigned int uap_fpu_status(void *uap) +{ + return mach_fpu_status(UAP_FS(uap)); +} + inline static cell fix_stack_pointer(cell sp) { return ((sp + 4) & ~15) - 4; } +inline static void mach_clear_fpu_status(i386_float_state_t *float_state) +{ + MXCSR(float_state) &= 0xffffffc0; + memset(&X87SW(float_state), 0, sizeof(X87SW(float_state))); +} + +inline static void uap_clear_fpu_status(void *uap) +{ + mach_clear_fpu_status(UAP_FS(uap)); +} + } diff --git a/vm/os-macosx-x86.64.hpp b/vm/os-macosx-x86.64.hpp index b97eb55f26..f56ada23fd 100644 --- a/vm/os-macosx-x86.64.hpp +++ b/vm/os-macosx-x86.64.hpp @@ -16,28 +16,66 @@ Modified for Factor by Slava Pestov and Daniel Ehrenberg */ #define MACH_EXC_STATE_TYPE x86_exception_state64_t #define MACH_EXC_STATE_FLAVOR x86_EXCEPTION_STATE64 #define MACH_EXC_STATE_COUNT x86_EXCEPTION_STATE64_COUNT + #define MACH_EXC_INTEGER_DIV EXC_I386_DIV + #define MACH_THREAD_STATE_TYPE x86_thread_state64_t #define MACH_THREAD_STATE_FLAVOR x86_THREAD_STATE64 #define MACH_THREAD_STATE_COUNT MACHINE_THREAD_STATE_COUNT +#define MACH_FLOAT_STATE_TYPE x86_float_state64_t +#define MACH_FLOAT_STATE_FLAVOR x86_FLOAT_STATE64 +#define MACH_FLOAT_STATE_COUNT x86_FLOAT_STATE64_COUNT + #if __DARWIN_UNIX03 #define MACH_EXC_STATE_FAULT(exc_state) (exc_state)->__faultvaddr #define MACH_STACK_POINTER(thr_state) (thr_state)->__rsp #define MACH_PROGRAM_COUNTER(thr_state) (thr_state)->__rip - #define UAP_PROGRAM_COUNTER(ucontext) \ - MACH_PROGRAM_COUNTER(&(((ucontext_t *)(ucontext))->uc_mcontext->__ss)) + #define UAP_SS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->__ss) + #define UAP_FS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->__fs) + + #define MXCSR(float_state) (float_state)->__fpu_mxcsr + #define X87SW(float_state) (float_state)->__fpu_fsw #else #define MACH_EXC_STATE_FAULT(exc_state) (exc_state)->faultvaddr #define MACH_STACK_POINTER(thr_state) (thr_state)->rsp #define MACH_PROGRAM_COUNTER(thr_state) (thr_state)->rip - #define UAP_PROGRAM_COUNTER(ucontext) \ - MACH_PROGRAM_COUNTER(&(((ucontext_t *)(ucontext))->uc_mcontext->ss)) + #define UAP_SS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->ss) + #define UAP_FS(ucontext) &(((ucontext_t *)(ucontext))->uc_mcontext->fs) + + #define MXCSR(float_state) (float_state)->fpu_mxcsr + #define X87SW(float_state) (float_state)->fpu_fsw #endif +#define UAP_PROGRAM_COUNTER(ucontext) \ + MACH_PROGRAM_COUNTER(UAP_SS(ucontext)) + +inline static unsigned int mach_fpu_status(x86_float_state64_t *float_state) +{ + unsigned short x87sw; + memcpy(&x87sw, &X87SW(float_state), sizeof(x87sw)); + return MXCSR(float_state) | x87sw; +} + +inline static unsigned int uap_fpu_status(void *uap) +{ + return mach_fpu_status(UAP_FS(uap)); +} + inline static cell fix_stack_pointer(cell sp) { - return ((sp + 8) & ~15) - 8; + return ((sp + 8) & ~15) - 8; +} + +inline static void mach_clear_fpu_status(x86_float_state64_t *float_state) +{ + MXCSR(float_state) &= 0xffffffc0; + memset(&X87SW(float_state), 0, sizeof(X87SW(float_state))); +} + +inline static void uap_clear_fpu_status(void *uap) +{ + mach_clear_fpu_status(UAP_FS(uap)); } } diff --git a/vm/os-netbsd-x86.32.hpp b/vm/os-netbsd-x86.32.hpp index ebba4f356d..f2f47ecf6c 100644 --- a/vm/os-netbsd-x86.32.hpp +++ b/vm/os-netbsd-x86.32.hpp @@ -5,4 +5,7 @@ namespace factor #define ucontext_stack_pointer(uap) ((void *)_UC_MACHINE_SP((ucontext_t *)uap)) +static inline unsigned int uap_fpu_status(void *uap) { return 0; } +static inline void uap_clear_fpu_status(void *uap) { } + } diff --git a/vm/os-netbsd-x86.64.hpp b/vm/os-netbsd-x86.64.hpp index 1a062cc6ef..a9d52a6c2b 100644 --- a/vm/os-netbsd-x86.64.hpp +++ b/vm/os-netbsd-x86.64.hpp @@ -6,4 +6,7 @@ namespace factor #define ucontext_stack_pointer(uap) \ ((void *)(((ucontext_t *)(uap))->uc_mcontext.__gregs[_REG_URSP])) +static inline unsigned int uap_fpu_status(void *uap) { return 0; } +static inline void uap_clear_fpu_status(void *uap) { } + } diff --git a/vm/os-openbsd-x86.32.hpp b/vm/os-openbsd-x86.32.hpp index 6065d96a5f..0abd019219 100644 --- a/vm/os-openbsd-x86.32.hpp +++ b/vm/os-openbsd-x86.32.hpp @@ -12,4 +12,7 @@ inline static void *openbsd_stack_pointer(void *uap) #define ucontext_stack_pointer openbsd_stack_pointer #define UAP_PROGRAM_COUNTER(uap) (((struct sigcontext*)(uap))->sc_eip) +static inline unsigned int uap_fpu_status(void *uap) { return 0; } +static inline void uap_clear_fpu_status(void *uap) { } + } diff --git a/vm/os-openbsd-x86.64.hpp b/vm/os-openbsd-x86.64.hpp index 7338b04e6f..9dce48ee91 100644 --- a/vm/os-openbsd-x86.64.hpp +++ b/vm/os-openbsd-x86.64.hpp @@ -12,4 +12,7 @@ inline static void *openbsd_stack_pointer(void *uap) #define ucontext_stack_pointer openbsd_stack_pointer #define UAP_PROGRAM_COUNTER(uap) (((struct sigcontext*)(uap))->sc_rip) +static inline unsigned int uap_fpu_status(void *uap) { return 0; } +static inline void uap_clear_fpu_status(void *uap) { } + } diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 735c614b7a..189fca0cf7 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -136,6 +136,8 @@ void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap) { signal_number = signal; signal_callstack_top = uap_stack_pointer(uap); + signal_fpu_status = fpu_status(uap_fpu_status(uap)); + uap_clear_fpu_status(uap); UAP_PROGRAM_COUNTER(uap) = (siginfo->si_code == FPE_INTDIV || siginfo->si_code == FPE_INTOVF) ? (cell)misc_signal_handler_impl diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index e2d959aace..c2b4e2af9e 100644 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -34,6 +34,9 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) case EXCEPTION_FLT_OVERFLOW: case EXCEPTION_FLT_STACK_CHECK: case EXCEPTION_FLT_UNDERFLOW: + /* XXX MxCsr is not available in CONTEXT structure on x86.32 */ + signal_fpu_status = c->FloatSave.StatusWord; + c->FloatSave.StatusWord = 0; c->EIP = (cell)fp_signal_handler_impl; break; From 8befecbc94ff879a0f82745581853b26104bcc9c Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 13 Sep 2009 23:46:21 -0500 Subject: [PATCH 033/345] add tc-lisp-talk vocab --- extra/tc-lisp-talk/authors.txt | 1 + extra/tc-lisp-talk/tc-lisp-talk.factor | 534 +++++++++++++++++++++++++ 2 files changed, 535 insertions(+) create mode 100644 extra/tc-lisp-talk/authors.txt create mode 100644 extra/tc-lisp-talk/tc-lisp-talk.factor diff --git a/extra/tc-lisp-talk/authors.txt b/extra/tc-lisp-talk/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/tc-lisp-talk/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/tc-lisp-talk/tc-lisp-talk.factor b/extra/tc-lisp-talk/tc-lisp-talk.factor new file mode 100644 index 0000000000..cecbc9cb98 --- /dev/null +++ b/extra/tc-lisp-talk/tc-lisp-talk.factor @@ -0,0 +1,534 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: assocs combinators constructors eval help.markup kernel +multiline namespaces parser sequences sequences.private slides +vocabs.refresh words fry ; +IN: tc-lisp-talk + +CONSTANT: tc-lisp-slides +{ + { $slide "Factor!" + { $url "http://factorcode.org" } + "Development started in 2003" + "Open source (BSD license)" + "Influenced by Forth, Lisp, and Smalltalk" + "Blurs the line between language and library" + "Interactive development" + } + { $slide "First, some examples" + { $code "3 weeks ago noon monday ." } + { $code "USE: roman 2009 >roman ." } + { $code <" : average ( seq -- x ) + [ sum ] [ length ] bi / ;"> } + { $code "1 miles [ km ] undo >float ." } + { $code "[ readln eval>string print t ] loop" } + } + { $slide "XML Literals" + { $code + <" USING: splitting xml.writer xml.syntax ; +{ "one" "two" "three" } +[ [XML <-> XML] ] map +<-> XML> pprint-xml"> + } + } + { $slide "Differences between Factor and Lisp" + "Single-implementation language" + "Less nesting, shorter word length" + { "Dynamic reloading of code from files with " { $link refresh-all } } + "More generic protocols -- sequences, assocs, streams" + "More cross-platform" + "No standard for the language" + "Evaluates left to right" + } + { $slide "Terminology" + { "Words - functions" } + { "Vocabularies - collections of code in the same namespace" } + { "Quotations - blocks of code" { $code "[ dup reverse append ]" } } + { "Combinators - higher order functions" } + { "Static stack effect - known stack effect at compile-time" } + } + { $slide "Defining a word" + "Defined at parse time" + "Parts: name, stack effect, definition" + "Composed of tokens separated by whitespace" + { $code ": palindrome? ( string -- ? ) dup reverse = ;" } + } + { $slide "Non-static stack effect" + "Not a good practice, nor useful" + "Not compiled by the optimizing compiler" + { $code "100 iota [ ] each" } + } + { $slide "Module system" + "Code divided up into vocabulary roots" + "core/ -- just enough code to bootstrap Factor" + "basis/ -- optimizing compiler, the UI, tools, libraries" + "extra/ -- demos, unpolished code, experiments" + "work/ -- your works in progress" + } + { $slide "Module system (part 2)" + "Each vocabulary corresponds to a directory on disk, with documentation and test files" + { "Code for the " { $snippet "math" } " vocabulary: " { $snippet "~/factor/core/math/math.factor" } } + { "Documentation for the " { $snippet "math" } " vocabulary: " { $snippet "~/factor/core/math/math-docs.factor" } } + { "Unit tests for the " { $snippet "math" } " vocabulary: " { $snippet " ~/factor/core/math/math-tests.factor" } } + } + { $slide "Using a library" + "Each file starts with a USING: list" + "To use a library, simply include it in this list" + "Refreshing code loads dependencies correctly" + } + { $slide "Object system" + "Based on CLOS" + { "We define generic words that operate on the top of the stack with " { $link POSTPONE: GENERIC: } " or on an implicit parameter with " { $link POSTPONE: HOOK: } } + } + { $slide "Object system example: shape protocol" + "In ~/factor/work/shapes/shapes.factor" + { $code <" IN: shapes + +GENERIC: area ( shape -- x ) +GENERIC: perimeter ( shape -- x )"> + } + } + { $slide "Implementing the shape protocol: circles" + "In ~/factor/work/shapes/circle/circle.factor" + { $code <" USING: shapes constructors math +math.constants ; +IN: shapes.circle + +TUPLE: circle radius ; +CONSTRUCTOR: circle ( radius -- obj ) ; +M: circle area radius>> sq pi * ; +M: circle perimeter radius>> pi * 2 * ;"> + } + } + { $slide "Dynamic variables" + "Implemented as a stack of hashtables" + { "Useful words are " { $link get } ", " { $link set } } + "Input, output, error streams are stored in dynamic variables" + { $code <" "Today is the first day of the rest of your life." +[ + readln print +] with-string-reader"> + } + } + { $slide "The global namespace" + "The global namespace is just the namespace at the bottom of the namespace stack" + { "Useful words are " { $link get-global } ", " { $link set-global } } + "Factor idiom for changing a particular namespace" + { $code <" SYMBOL: king +global [ "Henry VIII" king set ] bind"> + } + { $code "with-scope" } + { $code "namestack" } + } + { $slide "Hooks" + "Dispatch on a dynamic variable" + { $code <" HOOK: computer-name os ( -- string ) +M: macosx computer-name uname first ; +macosx \ os set-global +computer-name"> + } + } + { $slide "Interpolate" + "Replaces variables in a string" + { $code +<" "Dawg" "name" set +"rims" "noun" set +"bling" "verb1" set +"roll" "verb2" set +[ + "Sup ${name}, we heard you liked ${noun}, so we put ${noun} on your car so you can ${verb1} while you ${verb2}." + interpolate +] with-string-writer print "> + } + } + { $slide "Sequence protocol" + "All sequences obey a protocol of generics" + { "Is an object a " { $link sequence? } } + { "Getting the " { $link length } } + { "Accessing the " { $link nth } " element" } + { "Setting an element - " { $link set-nth } } + } + { $slide "Examples of sequences in Factor" + "Arrays are mutable" + "Vectors are mutable and growable" + { "Arrays " { $code "{ \"abc\" \"def\" 50 }" } } + { "Vectors " { $code "V{ \"abc\" \"def\" 50 }" } } + { "Byte-arrays " { $code "B{ 1 2 3 }" } } + { "Byte-vectors " { $code "BV{ 11 22 33 }" } } + } + { $slide "Specialized arrays and vectors" + { "Specialized int arrays " { $code "int-array{ -20 -30 40 }" } } + { "Specialized uint arrays " { $code "uint-array{ 20 30 40 }" } } + { "Specialized float vectors " { $code "float-vector{ 20 30 40 }" } } + "35 others C-type arrays" + } + { $slide "Specialized arrays code" + "One line per array/vector" + { "In ~/factor/basis/specialized-arrays/float/float.factor" + { $code <" << "float" define-array >>"> } + } + { "In ~/factor/basis/specialized-vectors/float/float.factor" + { $code <" << "float" define-vector >>"> } + } + } + + { $slide "Speciailzied arrays are implemented using functors" + "Like C++ templates" + "Eliminate boilerplate in ways other abstractions don't" + "Contains a definition section and a functor body" + "Uses the interpolate vocabulary" + } + { $slide "Functor for sorting" + { $code + <" FUNCTOR: define-sorting ( NAME QUOT -- ) + +NAME<=> DEFINES ${NAME}<=> +NAME>=< DEFINES ${NAME}>=< + +WHERE + +: NAME<=> ( obj1 obj2 -- <=> ) QUOT compare ; +: NAME>=< ( obj1 obj2 -- >=< ) + NAME<=> invert-comparison ; + +;FUNCTOR"> + } + } + { $slide "Example of sorting functor" + { $code <" USING: sorting.functor ; +<< "length" [ length ] define-sorting >>"> + } + { $code + <" { { 1 2 3 } { 1 2 } { 1 } } +[ length<=> ] sort"> + } + } + { $slide "Combinators" + "Used to implement higher order functions (dataflow and control flow)" + "Compiler optimizes away quotations completely" + "Optimized code is just tight loops in registers" + "Most loops can be expressed with combinators or tail-recursion" + } + { $slide "Combinators that act on one value" + { $link bi } + { $code "10 [ 1 - ] [ 1 + ] bi" } + { $link tri } + { $code "10 [ 1 - ] [ 1 + ] [ 2 * ] tri" } + } + { $slide "Combinators that act on two values" + { $link 2bi } + { $code "10 1 [ - ] [ + ] 2bi" } + { $link bi* } + { $code "10 20 [ 1 - ] [ 1 + ] bi*" } + { $link bi@ } + { $code "5 9 [ sq ] bi@" } + } + { $slide "Sequence combinators" + + { $link each } + { $code "{ 1 2 3 4 5 } [ sq . ] each" } + { $link map } + { $code "{ 1 2 3 4 5 } [ sq ] map" } + { $link filter } + { $code "{ 1 2 3 4 5 } [ even? ] filter" } + } + { $slide "Multiple sequence combinators" + + { $link 2each } + { $code "{ 1 2 3 } { 10 20 30 } [ + . ] 2each" } + { $link 2map } + { $code "{ 1 2 3 } { 10 20 30 } [ + ] 2map" } + } + { $slide "Control flow: if" + { $link if } + { $code <" 10 random dup even? [ 2 / ] [ 1 - ] if"> } + { $link when } + { $code <" 10 random dup even? [ 2 / ] when"> } + { $link unless } + { $code <" 10 random dup even? [ 1 - ] unless"> } + } + { $slide "Control flow: case" + { $link case } + { $code <" ERROR: not-possible obj ; +10 random 5 <=> { + { +lt+ [ "Less" ] } + { +gt+ [ "More" ] } + { +eq+ [ "Equal" ] } + [ not-possible ] +} case"> + } + } + { $slide "Fry" + "Used to construct quotations" + { "'Holes', represented by " { $snippet "_" } " are filled left to right" } + { $code "10 4 '[ _ + ] call" } + { $code "3 4 '[ _ sq _ + ] call" } + } + { $slide "Locals" + "When data flow combinators and shuffle words are not enough" + "Name your input parameters" + "Used in about 1% of all words" + } + { $slide "Locals example" + "Area of a triangle using Heron's formula" + { $code + <" :: area ( a b c -- x ) + a b c + + 2 / :> p + p + p a - * + p b - * + p c - * sqrt ;"> + } + } + { $slide "Previous example without locals" + "A bit unwieldy..." + { $code + <" : area ( a b c -- x ) + [ ] [ + + 2 / ] 3bi + [ '[ _ - ] tri@ ] [ neg ] bi + * * * sqrt ;"> } + } + { $slide "More idiomatic version" + "But there's a trick: put the lengths in an array" + { $code <" : v-n ( v n -- w ) '[ _ - ] map ; + +: area ( seq -- x ) + [ 0 suffix ] [ sum 2 / ] bi + v-n product sqrt ;"> } + } + { $slide "Implementing an abstraction" + { "Suppose we want to get the price of the customer's first order, but any one of the steps along the way could be a nil value (" { $link f } " in Factor):" } + { $code + "dup [ orders>> ] when" + "dup [ first ] when" + "dup [ price>> ] when" + } + } + { $slide "This is hard with mainstream syntax!" + { $code + <" var customer = ...; +var orders = (customer == null ? null : customer.orders); +var order = (orders == null ? null : orders[0]); +var price = (order == null ? null : order.price);"> } + } + { $slide "An ad-hoc solution" + "Something like..." + { $code "var price = customer.?orders.?[0].?price;" } + } + { $slide "Macros in Factor" + "Expand at compile-time" + "Return a quotation to be compiled" + "Can express non-static stack effects" + "Not as widely used as combinators, 60 macros so far" + { $code "{ 1 2 3 4 5 } 5 firstn" } + } + { $slide "A macro solution" + "Returns a quotation to the compiler" + "Constructed using map, fry, and concat" + { $code <" MACRO: plox ( seq -- quot ) + [ + '[ dup _ when ] + ] map [ ] concat-as ;"> + } + } + { $slide "Macro example" + "Return the caaar of a sequence" + { "Return " { $snippet f } " on failure" } + { $code <" : caaar ( seq/f -- x/f ) + { + [ first ] + [ first ] + [ first ] + } plox ;"> + } + { $code <" { { f } } caaar"> } + { $code <" { { { 1 2 3 } } } caaar"> } + } + { $slide "Smart combinators" + "Use stack checker to infer inputs and outputs" + "Even fewer uses than macros" + { $code "{ 1 10 20 34 } sum" } + { $code "[ 1 10 20 34 ] sum-outputs" } + { $code "[ 2 2 [ even? ] both? ] [ + ] [ - ] smart-if" } + } + { $slide "Fibonacci" + "Not tail recursive" + "Call tree is huge" + { $code <" : fib ( n -- x ) + dup 1 <= [ + [ 1 - fib ] [ 2 - fib ] bi + + ] unless ;"> + } + { $code "36 iota [ fib ] map ." } + } + { $slide "Memoized Fibonacci" + "Change one word and it's efficient" + { $code <" MEMO: fib ( n -- x ) + dup 1 <= [ + [ 1 - fib ] [ 2 - fib ] bi + + ] unless ;"> + } + { $code "36 iota [ fib ] map ." } + } + { $slide "Destructors" + "Deterministic resource disposal" + "Any step can fail and we don't want to leak resources" + "We want to conditionally clean up sometimes -- if everything succeeds, we might wish to retain the buffer" + } + + { $slide "Example in C" + { $code +<" void do_stuff() +{ + void *obj1, *obj2; + if(!(*obj1 = malloc(256))) goto end; + if(!(*obj2 = malloc(256))) goto cleanup1; + ... work goes here... +cleanup2: free(*obj2); +cleanup1: free(*obj1); +end: return; +}"> + } + } + { $slide "Example: allocating and disposing two buffers" + { $code <" : do-stuff ( -- ) + [ + 256 malloc &free + 256 malloc &free + ... work goes here ... + ] with-destructors ;"> + } + } + { $slide "Example: allocating two buffers for later" + { $code <" : do-stuff ( -- ) + [ + 256 malloc |free + 256 malloc |free + ... work goes here ... + ] with-destructors ;"> + } + } + { $slide "Example: disposing of an output port" + { $code <" M: output-port dispose* + [ + { + [ handle>> &dispose drop ] + [ buffer>> &dispose drop ] + [ port-flush ] + [ handle>> shutdown ] + } cleave + ] with-destructors ;"> + } + } + { $slide "Rapid application development" + "We lost the dice to Settlers of Catan: Cities and Knights" + "Two regular dice, one special die" + { $vocab-link "dice" } + } + { $slide "The essence of Factor" + "Nicely named words abstract away the stack, leaving readable code" + { $code <" : surround ( seq left right -- seq' ) + swapd 3append ;"> + } + { $code <" : glue ( left right middle -- seq' ) + swap 3append ;"> + } + { $code HEREDOC: xyz +"a" "b" "c" 3append +"a" "<" ">" surround +"a" "b" ", " glue +xyz + } + } + { $slide "C FFI demo" + "Easy to call C functions from Factor" + "Handles C structures, C types, callbacks" + "Used extensively in the Windows and Unix backends" + { $code + <" FUNCTION: double pow ( double x, double y ) ; +2 5.0 pow ."> + } + } + { $slide "Windows win32 example" + { $code +<" M: windows gmt-offset + ( -- hours minutes seconds ) + "TIME_ZONE_INFORMATION" + dup GetTimeZoneInformation { + { TIME_ZONE_ID_INVALID [ + win32-error-string throw + ] } + { TIME_ZONE_ID_STANDARD [ + TIME_ZONE_INFORMATION-Bias + ] } + } case neg 60 /mod 0 ;"> + } + } + { $slide "Struct and function" + { $code <" C-STRUCT: TIME_ZONE_INFORMATION + { "LONG" "Bias" } + { { "WCHAR" 32 } "StandardName" } + { "SYSTEMTIME" "StandardDate" } + { "LONG" "StandardBias" } + { { "WCHAR" 32 } "DaylightName" } + { "SYSTEMTIME" "DaylightDate" } + { "LONG" "DaylightBias" } ;"> + } + { $code <" FUNCTION: DWORD GetTimeZoneInformation ( + LPTIME_ZONE_INFORMATION + lpTimeZoneInformation +) ;"> + } + + } + { $slide "Cocoa FFI" + { $code <" IMPORT: NSAlert [ + NSAlert -> new + [ -> retain ] [ + "Raptor" &CFRelease + -> setMessageText: + ] [ + "Look out!" &CFRelease + -> setInformativeText: + ] tri -> runModal drop +] with-destructors"> + } + } + { $slide "Deployment demo" + "Vocabularies can be deployed" + "Standalone .app on Mac" + "An executable and dll on Windows" + { $vocab-link "webkit-demo" } + } + { $slide "Interesting programs" + { $vocab-link "terrain" } + { $vocab-link "gpu.demos.raytrace" } + { $vocab-link "gpu.demos.bunny" } + } + { $slide "Factor's source tree" + "Lines of code in core/: 9,500" + "Lines of code in basis/: 120,000" + "Lines of code in extra/: 51,000" + "Lines of tests: 44,000" + "Lines of documentation: 44,500" + } + { $slide "VM trivia" + "Lines of C++ code: 12860" + "Generational garbage collection" + "Non-optimizing compiler" + "Loads an image file and runs it" + } + { $slide "Why should I use Factor?" + "More abstractions over time" + "We fix reported bugs quickly" + "Stackable, fluent language" + "Supports extreme programming" + "Beer-friendly programming" + } + { $slide "Questions?" + } +} + +: tc-lisp-talk ( -- ) tc-lisp-slides slides-window ; + +MAIN: tc-lisp-talk From 198874aea896c56359d388cdf6c78480009820ac Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 00:00:01 -0500 Subject: [PATCH 034/345] fix compilation on freebsd --- vm/os-freebsd-x86.32.hpp | 14 +++++++------- vm/os-freebsd-x86.64.hpp | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/vm/os-freebsd-x86.32.hpp b/vm/os-freebsd-x86.32.hpp index e386532b0c..800b343dfd 100644 --- a/vm/os-freebsd-x86.32.hpp +++ b/vm/os-freebsd-x86.32.hpp @@ -13,12 +13,12 @@ inline static void *ucontext_stack_pointer(void *uap) inline static unsigned int uap_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { + if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); - return x87->en_sw; - } else if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { + return x87->sv_env.en_sw; + } else if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - return xmm->en_sw | xmm->en_mxcsr; + return xmm->sv_env.en_sw | xmm->sv_env.en_mxcsr; } else return 0; } @@ -28,11 +28,11 @@ inline static void uap_clear_fpu_status(void *uap) ucontext_t *ucontext = (ucontext_t *)uap; if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); - x87->en_sw = 0; + x87->sv_env.en_sw = 0; } else if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - xmm->en_sw = 0; - xmm->en_mxcsr &= 0xffffffc0; + xmm->sv_env.en_sw = 0; + xmm->sv_env.en_mxcsr &= 0xffffffc0; } } diff --git a/vm/os-freebsd-x86.64.hpp b/vm/os-freebsd-x86.64.hpp index 78c08447bd..b2dd096137 100644 --- a/vm/os-freebsd-x86.64.hpp +++ b/vm/os-freebsd-x86.64.hpp @@ -13,9 +13,9 @@ inline static void *ucontext_stack_pointer(void *uap) inline static unsigned int uap_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { + if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - return xmm->en_sw | xmm->en_mxcsr; + return xmm->sv_env.en_sw | xmm->sv_env.en_mxcsr; } else return 0; } @@ -23,10 +23,10 @@ inline static unsigned int uap_fpu_status(void *uap) inline static void uap_clear_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { + if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - xmm->en_sw = 0; - xmm->en_mxcsr &= 0xffffffc0; + xmm->sv_env.en_sw = 0; + xmm->sv_env.en_mxcsr &= 0xffffffc0; } } From c4f3a4226930fe682adba546b0bc76379df3b182 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 02:39:12 -0500 Subject: [PATCH 035/345] math.floats.env: Fix linux x86.64 some more --- basis/math/floats/env/env-tests.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/math/floats/env/env-tests.factor b/basis/math/floats/env/env-tests.factor index c1d8913703..7f5a20efd0 100644 --- a/basis/math/floats/env/env-tests.factor +++ b/basis/math/floats/env/env-tests.factor @@ -21,7 +21,6 @@ set-default-fp-env [ t ] +fp-overflow+ [ 1.0e250 1.0e100 ] [ * ] test-fp-exception unit-test [ t ] +fp-underflow+ [ 1.0e-250 1.0e-100 ] [ * ] test-fp-exception unit-test [ t ] +fp-overflow+ [ 2.0 100,000.0 ] [ fpow ] test-fp-exception unit-test -[ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception unit-test [ t ] +fp-invalid-operation+ [ 0.0 0.0 ] [ /f ] test-fp-exception unit-test [ t ] +fp-invalid-operation+ [ -1.0 ] [ fsqrt ] test-fp-exception unit-test @@ -34,6 +33,7 @@ set-default-fp-env ! No underflow on Linux with this test, just inexact. Reported as an Ubuntu bug: ! https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/429113 os linux? cpu x86.64? and [ + [ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception unit-test [ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test ] unless From 83c992173e70c8eeabee55d649fcfeac1d75f3c1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 03:09:03 -0500 Subject: [PATCH 036/345] vm: fix indentation in Joe's changes --- vm/cpu-ppc.hpp | 10 +++---- vm/cpu-x86.hpp | 12 ++++----- vm/float_bits.hpp | 8 +++--- vm/layouts.hpp | 10 +++---- vm/os-freebsd-x86.32.hpp | 37 +++++++++++++++----------- vm/os-freebsd-x86.64.hpp | 21 ++++++++------- vm/os-macosx-ppc.hpp | 10 +++---- vm/os-macosx-x86.32.hpp | 10 +++---- vm/os-macosx-x86.64.hpp | 16 +++++------ vm/os-netbsd.hpp | 2 -- vm/os-windows-nt.cpp | 57 ++++++++++++++++++++-------------------- 11 files changed, 100 insertions(+), 93 deletions(-) diff --git a/vm/cpu-ppc.hpp b/vm/cpu-ppc.hpp index db02a72959..2124e03350 100644 --- a/vm/cpu-ppc.hpp +++ b/vm/cpu-ppc.hpp @@ -67,15 +67,15 @@ inline static unsigned int fpu_status(unsigned int status) unsigned int r = 0; if (status & 0x20000000) - r |= FP_TRAP_INVALID_OPERATION; + r |= FP_TRAP_INVALID_OPERATION; if (status & 0x10000000) - r |= FP_TRAP_OVERFLOW; + r |= FP_TRAP_OVERFLOW; if (status & 0x08000000) - r |= FP_TRAP_UNDERFLOW; + r |= FP_TRAP_UNDERFLOW; if (status & 0x04000000) - r |= FP_TRAP_ZERO_DIVIDE; + r |= FP_TRAP_ZERO_DIVIDE; if (status & 0x02000000) - r |= FP_TRAP_INEXACT; + r |= FP_TRAP_INEXACT; return r; } diff --git a/vm/cpu-x86.hpp b/vm/cpu-x86.hpp index 7054f90735..4a37a17889 100644 --- a/vm/cpu-x86.hpp +++ b/vm/cpu-x86.hpp @@ -53,17 +53,17 @@ inline static bool tail_call_site_p(cell return_address) inline static unsigned int fpu_status(unsigned int status) { unsigned int r = 0; - + if (status & 0x01) - r |= FP_TRAP_INVALID_OPERATION; + r |= FP_TRAP_INVALID_OPERATION; if (status & 0x04) - r |= FP_TRAP_ZERO_DIVIDE; + r |= FP_TRAP_ZERO_DIVIDE; if (status & 0x08) - r |= FP_TRAP_OVERFLOW; + r |= FP_TRAP_OVERFLOW; if (status & 0x10) - r |= FP_TRAP_UNDERFLOW; + r |= FP_TRAP_UNDERFLOW; if (status & 0x20) - r |= FP_TRAP_INEXACT; + r |= FP_TRAP_INEXACT; return r; } diff --git a/vm/float_bits.hpp b/vm/float_bits.hpp index 000bd49482..73a04639ee 100644 --- a/vm/float_bits.hpp +++ b/vm/float_bits.hpp @@ -5,8 +5,8 @@ namespace factor representations and vice versa */ union double_bits_pun { - double x; - u64 y; + double x; + u64 y; }; inline static u64 double_bits(double x) @@ -24,8 +24,8 @@ inline static double bits_double(u64 y) } union float_bits_pun { - float x; - u32 y; + float x; + u32 y; }; inline static u32 float_bits(float x) diff --git a/vm/layouts.hpp b/vm/layouts.hpp index a14c234aaa..dceb9a208a 100644 --- a/vm/layouts.hpp +++ b/vm/layouts.hpp @@ -70,11 +70,11 @@ inline static cell align8(cell a) /* Constants used when floating-point trap exceptions are thrown */ enum { - FP_TRAP_INVALID_OPERATION = 1 << 0, - FP_TRAP_OVERFLOW = 1 << 1, - FP_TRAP_UNDERFLOW = 1 << 2, - FP_TRAP_ZERO_DIVIDE = 1 << 3, - FP_TRAP_INEXACT = 1 << 4, + FP_TRAP_INVALID_OPERATION = 1 << 0, + FP_TRAP_OVERFLOW = 1 << 1, + FP_TRAP_UNDERFLOW = 1 << 2, + FP_TRAP_ZERO_DIVIDE = 1 << 3, + FP_TRAP_INEXACT = 1 << 4, }; inline static bool immediate_p(cell obj) diff --git a/vm/os-freebsd-x86.32.hpp b/vm/os-freebsd-x86.32.hpp index 800b343dfd..a0888e1f5b 100644 --- a/vm/os-freebsd-x86.32.hpp +++ b/vm/os-freebsd-x86.32.hpp @@ -13,26 +13,33 @@ inline static void *ucontext_stack_pointer(void *uap) inline static unsigned int uap_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { - struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); - return x87->sv_env.en_sw; - } else if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { - struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - return xmm->sv_env.en_sw | xmm->sv_env.en_mxcsr; - } else - return 0; + if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_387) + { + struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); + return x87->sv_env.en_sw; + } + else if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) + { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + return xmm->sv_env.en_sw | xmm->sv_env.en_mxcsr; + } + else + return 0; } inline static void uap_clear_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { - struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); - x87->sv_env.en_sw = 0; - } else if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { - struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - xmm->sv_env.en_sw = 0; - xmm->sv_env.en_mxcsr &= 0xffffffc0; + if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_387) + { + struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); + x87->sv_env.en_sw = 0; + } + else if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) + { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + xmm->sv_env.en_sw = 0; + xmm->sv_env.en_mxcsr &= 0xffffffc0; } } diff --git a/vm/os-freebsd-x86.64.hpp b/vm/os-freebsd-x86.64.hpp index b2dd096137..6200a0f5f3 100644 --- a/vm/os-freebsd-x86.64.hpp +++ b/vm/os-freebsd-x86.64.hpp @@ -13,20 +13,23 @@ inline static void *ucontext_stack_pointer(void *uap) inline static unsigned int uap_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { - struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - return xmm->sv_env.en_sw | xmm->sv_env.en_mxcsr; - } else - return 0; + if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) + { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + return xmm->sv_env.en_sw | xmm->sv_env.en_mxcsr; + } + else + return 0; } inline static void uap_clear_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { - struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); - xmm->sv_env.en_sw = 0; - xmm->sv_env.en_mxcsr &= 0xffffffc0; + if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) + { + struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + xmm->sv_env.en_sw = 0; + xmm->sv_env.en_mxcsr &= 0xffffffc0; } } diff --git a/vm/os-macosx-ppc.hpp b/vm/os-macosx-ppc.hpp index 31a1e22882..338e3e812f 100644 --- a/vm/os-macosx-ppc.hpp +++ b/vm/os-macosx-ppc.hpp @@ -54,27 +54,27 @@ Modified for Factor by Slava Pestov */ inline static unsigned int mach_fpu_status(ppc_float_state_t *float_state) { - return FPSCR(float_state); + return FPSCR(float_state); } inline static unsigned int uap_fpu_status(void *uap) { - return mach_fpu_status(UAP_FS(uap)); + return mach_fpu_status(UAP_FS(uap)); } inline static cell fix_stack_pointer(cell sp) { - return sp; + return sp; } inline static void mach_clear_fpu_status(ppc_float_state_t *float_state) { - FPSCR(float_state) &= 0x0007ffff; + FPSCR(float_state) &= 0x0007ffff; } inline static void uap_clear_fpu_status(void *uap) { - mach_clear_fpu_status(UAP_FS(uap)); + mach_clear_fpu_status(UAP_FS(uap)); } } diff --git a/vm/os-macosx-x86.32.hpp b/vm/os-macosx-x86.32.hpp index 01ad28df4f..89906cd9a4 100644 --- a/vm/os-macosx-x86.32.hpp +++ b/vm/os-macosx-x86.32.hpp @@ -54,14 +54,14 @@ Modified for Factor by Slava Pestov */ inline static unsigned int mach_fpu_status(i386_float_state_t *float_state) { - unsigned short x87sw; - memcpy(&x87sw, &X87SW(float_state), sizeof(x87sw)); - return MXCSR(float_state) | x87sw; + unsigned short x87sw; + memcpy(&x87sw, &X87SW(float_state), sizeof(x87sw)); + return MXCSR(float_state) | x87sw; } inline static unsigned int uap_fpu_status(void *uap) { - return mach_fpu_status(UAP_FS(uap)); + return mach_fpu_status(UAP_FS(uap)); } inline static cell fix_stack_pointer(cell sp) @@ -77,7 +77,7 @@ inline static void mach_clear_fpu_status(i386_float_state_t *float_state) inline static void uap_clear_fpu_status(void *uap) { - mach_clear_fpu_status(UAP_FS(uap)); + mach_clear_fpu_status(UAP_FS(uap)); } } diff --git a/vm/os-macosx-x86.64.hpp b/vm/os-macosx-x86.64.hpp index f56ada23fd..fd6db4d68c 100644 --- a/vm/os-macosx-x86.64.hpp +++ b/vm/os-macosx-x86.64.hpp @@ -52,30 +52,30 @@ Modified for Factor by Slava Pestov and Daniel Ehrenberg */ inline static unsigned int mach_fpu_status(x86_float_state64_t *float_state) { - unsigned short x87sw; - memcpy(&x87sw, &X87SW(float_state), sizeof(x87sw)); - return MXCSR(float_state) | x87sw; + unsigned short x87sw; + memcpy(&x87sw, &X87SW(float_state), sizeof(x87sw)); + return MXCSR(float_state) | x87sw; } inline static unsigned int uap_fpu_status(void *uap) { - return mach_fpu_status(UAP_FS(uap)); + return mach_fpu_status(UAP_FS(uap)); } inline static cell fix_stack_pointer(cell sp) { - return ((sp + 8) & ~15) - 8; + return ((sp + 8) & ~15) - 8; } inline static void mach_clear_fpu_status(x86_float_state64_t *float_state) { - MXCSR(float_state) &= 0xffffffc0; - memset(&X87SW(float_state), 0, sizeof(X87SW(float_state))); + MXCSR(float_state) &= 0xffffffc0; + memset(&X87SW(float_state), 0, sizeof(X87SW(float_state))); } inline static void uap_clear_fpu_status(void *uap) { - mach_clear_fpu_status(UAP_FS(uap)); + mach_clear_fpu_status(UAP_FS(uap)); } } diff --git a/vm/os-netbsd.hpp b/vm/os-netbsd.hpp index 635361e3e4..d45b2ac163 100644 --- a/vm/os-netbsd.hpp +++ b/vm/os-netbsd.hpp @@ -5,6 +5,4 @@ namespace factor #define UAP_PROGRAM_COUNTER(uap) _UC_MACHINE_PC((ucontext_t *)uap) -#define DIRECTORY_P(file) ((file)->d_type == DT_DIR) - } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index c2b4e2af9e..017a96bb7c 100644 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -21,40 +21,39 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) else signal_callstack_top = NULL; - switch (e->ExceptionCode) { - case EXCEPTION_ACCESS_VIOLATION: + switch (e->ExceptionCode) + { + case EXCEPTION_ACCESS_VIOLATION: signal_fault_addr = e->ExceptionInformation[1]; c->EIP = (cell)memory_signal_handler_impl; - break; + break; - case EXCEPTION_FLT_DENORMAL_OPERAND: - case EXCEPTION_FLT_DIVIDE_BY_ZERO: - case EXCEPTION_FLT_INEXACT_RESULT: - case EXCEPTION_FLT_INVALID_OPERATION: - case EXCEPTION_FLT_OVERFLOW: - case EXCEPTION_FLT_STACK_CHECK: - case EXCEPTION_FLT_UNDERFLOW: - /* XXX MxCsr is not available in CONTEXT structure on x86.32 */ - signal_fpu_status = c->FloatSave.StatusWord; - c->FloatSave.StatusWord = 0; - c->EIP = (cell)fp_signal_handler_impl; - break; - - /* If the Widcomm bluetooth stack is installed, the BTTray.exe process - injects code into running programs. For some reason this results in - random SEH exceptions with this (undocumented) exception code being - raised. The workaround seems to be ignoring this altogether, since that - is what happens if SEH is not enabled. Don't really have any idea what - this exception means. */ - case 0x40010006: - break; - - default: + case EXCEPTION_FLT_DENORMAL_OPERAND: + case EXCEPTION_FLT_DIVIDE_BY_ZERO: + case EXCEPTION_FLT_INEXACT_RESULT: + case EXCEPTION_FLT_INVALID_OPERATION: + case EXCEPTION_FLT_OVERFLOW: + case EXCEPTION_FLT_STACK_CHECK: + case EXCEPTION_FLT_UNDERFLOW: + /* XXX MxCsr is not available in CONTEXT structure on x86.32 */ + signal_fpu_status = c->FloatSave.StatusWord; + c->FloatSave.StatusWord = 0; + c->EIP = (cell)fp_signal_handler_impl; + break; + case 0x40010006: + /* If the Widcomm bluetooth stack is installed, the BTTray.exe + process injects code into running programs. For some reason this + results in random SEH exceptions with this (undocumented) + exception code being raised. The workaround seems to be ignoring + this altogether, since that is what happens if SEH is not + enabled. Don't really have any idea what this exception means. */ + break; + default: signal_number = e->ExceptionCode; c->EIP = (cell)misc_signal_handler_impl; - break; - } - return EXCEPTION_CONTINUE_EXECUTION; + break; + } + return EXCEPTION_CONTINUE_EXECUTION; } void c_to_factor_toplevel(cell quot) From 448b89f585fb52bc1d34fdea7c4adabe1790e854 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 03:14:48 -0500 Subject: [PATCH 037/345] vm: More indentation fixes --- vm/cpu-ppc.S | 73 ++++++++++++++++---------------- vm/cpu-x86.32.S | 28 ++++++------ vm/cpu-x86.64.S | 20 ++++----- vm/cpu-x86.S | 110 ++++++++++++++++++++++++------------------------ 4 files changed, 115 insertions(+), 116 deletions(-) diff --git a/vm/cpu-ppc.S b/vm/cpu-ppc.S index 342ec83d7e..e7a210b7aa 100644 --- a/vm/cpu-ppc.S +++ b/vm/cpu-ppc.S @@ -88,11 +88,12 @@ multiply_overflow: #define RESTORE_FP(register,offset) lfd register,SAVE_AT(offset)(r1) #define SAVE_V(register,offset) \ - li r2,SAVE_AT(offset) XX \ - stvxl register,r2,r1 + li r2,SAVE_AT(offset) XX \ + stvxl register,r2,r1 + #define RESTORE_V(register,offset) \ - li r2,SAVE_AT(offset) XX \ - lvxl register,r2,r1 + li r2,SAVE_AT(offset) XX \ + lvxl register,r2,r1 #define PROLOGUE \ mflr r0 XX /* get caller's return address */ \ @@ -104,8 +105,6 @@ multiply_overflow: lwz r1,0(r1) XX /* destroy the stack frame */ \ mtlr r0 /* get ready to return */ - - /* We have to save and restore nonvolatile registers because the Factor compiler treats the entire register file as volatile. */ DEF(void,c_to_factor,(CELL quot)): @@ -288,44 +287,44 @@ DEF(void,flush_icache,(void *start, int len)): blr DEF(void,primitive_inline_cache_miss,(void)): - mflr r6 + mflr r6 DEF(void,primitive_inline_cache_miss_tail,(void)): - PROLOGUE - mr r3,r6 - bl MANGLE(inline_cache_miss) - EPILOGUE - mtctr r3 - bctr + PROLOGUE + mr r3,r6 + bl MANGLE(inline_cache_miss) + EPILOGUE + mtctr r3 + bctr DEF(void,get_ppc_fpu_env,(void*)): - mffs f0 - stfd f0,0(r3) - blr + mffs f0 + stfd f0,0(r3) + blr DEF(void,set_ppc_fpu_env,(const void*)): - lfd f0,0(r3) - mtfsf 0xff,f0 - blr + lfd f0,0(r3) + mtfsf 0xff,f0 + blr DEF(void,get_ppc_vmx_env,(void*)): - mfvscr v0 - subi r4,r1,16 - li r5,0xf - andc r4,r4,r5 - stvxl v0,0,r4 - li r5,0xc - lwzx r6,r5,r4 - stw r6,0(r3) - blr + mfvscr v0 + subi r4,r1,16 + li r5,0xf + andc r4,r4,r5 + stvxl v0,0,r4 + li r5,0xc + lwzx r6,r5,r4 + stw r6,0(r3) + blr DEF(void,set_ppc_vmx_env,(const void*)): - subi r4,r1,16 - li r5,0xf - andc r4,r4,r5 - li r5,0xc - lwz r6,0(r3) - stwx r6,r5,r4 - lvxl v0,0,r4 - mtvscr v0 - blr + subi r4,r1,16 + li r5,0xf + andc r4,r4,r5 + li r5,0xc + lwz r6,0(r3) + stwx r6,r5,r4 + lvxl v0,0,r4 + mtvscr v0 + blr diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index 0c4166cfe5..87a0e03f99 100644 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -58,26 +58,26 @@ DEF(void,primitive_inline_cache_miss_tail,(void)): jmp *%eax DEF(void,get_sse_env,(void*)): - movl 4(%esp), %eax - stmxcsr (%eax) - ret + movl 4(%esp), %eax + stmxcsr (%eax) + ret DEF(void,set_sse_env,(const void*)): - movl 4(%esp), %eax - ldmxcsr (%eax) - ret + movl 4(%esp), %eax + ldmxcsr (%eax) + ret DEF(void,get_x87_env,(void*)): - movl 4(%esp), %eax - fnstsw (%eax) - fnstcw 2(%eax) - ret + movl 4(%esp), %eax + fnstsw (%eax) + fnstcw 2(%eax) + ret DEF(void,set_x87_env,(const void*)): - movl 4(%esp), %eax - fnclex - fldcw 2(%eax) - ret + movl 4(%esp), %eax + fnclex + fldcw 2(%eax) + ret #include "cpu-x86.S" diff --git a/vm/cpu-x86.64.S b/vm/cpu-x86.64.S index e6d9d88810..0da360e675 100644 --- a/vm/cpu-x86.64.S +++ b/vm/cpu-x86.64.S @@ -89,21 +89,21 @@ DEF(void,primitive_inline_cache_miss_tail,(void)): jmp *%rax DEF(void,get_sse_env,(void*)): - stmxcsr (%rdi) - ret + stmxcsr (%rdi) + ret DEF(void,set_sse_env,(const void*)): - ldmxcsr (%rdi) - ret + ldmxcsr (%rdi) + ret DEF(void,get_x87_env,(void*)): - fnstsw (%rdi) - fnstcw 2(%rdi) - ret + fnstsw (%rdi) + fnstcw 2(%rdi) + ret DEF(void,set_x87_env,(const void*)): - fnclex - fldcw 2(%rdi) - ret + fnclex + fldcw 2(%rdi) + ret #include "cpu-x86.S" diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index 09e742bed8..d229b2cb79 100644 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -1,38 +1,38 @@ DEF(void,primitive_fixnum_add,(void)): - mov (DS_REG),ARG0 - mov -CELL_SIZE(DS_REG),ARG1 - sub $CELL_SIZE,DS_REG - mov ARG1,ARITH_TEMP_1 - add ARG0,ARITH_TEMP_1 - jo MANGLE(overflow_fixnum_add) - mov ARITH_TEMP_1,(DS_REG) - ret + mov (DS_REG),ARG0 + mov -CELL_SIZE(DS_REG),ARG1 + sub $CELL_SIZE,DS_REG + mov ARG1,ARITH_TEMP_1 + add ARG0,ARITH_TEMP_1 + jo MANGLE(overflow_fixnum_add) + mov ARITH_TEMP_1,(DS_REG) + ret DEF(void,primitive_fixnum_subtract,(void)): - mov (DS_REG),ARG1 - mov -CELL_SIZE(DS_REG),ARG0 - sub $CELL_SIZE,DS_REG - mov ARG0,ARITH_TEMP_1 - sub ARG1,ARITH_TEMP_1 - jo MANGLE(overflow_fixnum_subtract) - mov ARITH_TEMP_1,(DS_REG) - ret + mov (DS_REG),ARG1 + mov -CELL_SIZE(DS_REG),ARG0 + sub $CELL_SIZE,DS_REG + mov ARG0,ARITH_TEMP_1 + sub ARG1,ARITH_TEMP_1 + jo MANGLE(overflow_fixnum_subtract) + mov ARITH_TEMP_1,(DS_REG) + ret DEF(void,primitive_fixnum_multiply,(void)): - mov (DS_REG),ARITH_TEMP_1 - mov ARITH_TEMP_1,DIV_RESULT - mov -CELL_SIZE(DS_REG),ARITH_TEMP_2 - sar $3,ARITH_TEMP_2 - sub $CELL_SIZE,DS_REG - imul ARITH_TEMP_2 - jo multiply_overflow - mov DIV_RESULT,(DS_REG) - ret + mov (DS_REG),ARITH_TEMP_1 + mov ARITH_TEMP_1,DIV_RESULT + mov -CELL_SIZE(DS_REG),ARITH_TEMP_2 + sar $3,ARITH_TEMP_2 + sub $CELL_SIZE,DS_REG + imul ARITH_TEMP_2 + jo multiply_overflow + mov DIV_RESULT,(DS_REG) + ret multiply_overflow: - sar $3,ARITH_TEMP_1 - mov ARITH_TEMP_1,ARG0 - mov ARITH_TEMP_2,ARG1 - jmp MANGLE(overflow_fixnum_multiply) + sar $3,ARITH_TEMP_1 + mov ARITH_TEMP_1,ARG0 + mov ARITH_TEMP_2,ARG1 + jmp MANGLE(overflow_fixnum_multiply) DEF(F_FASTCALL void,c_to_factor,(CELL quot)): PUSH_NONVOLATILE @@ -77,38 +77,38 @@ DEF(F_FASTCALL void,lazy_jit_compile,(CELL quot)): DEF(bool,sse_version,(void)): mov $0x1,RETURN_REG cpuid - /* test $0x100000,%ecx - jnz sse_42 - test $0x80000,%ecx - jnz sse_41 - test $0x200,%ecx - jnz ssse_3 */ - test $0x1,%ecx - jnz sse_3 - test $0x4000000,%edx - jnz sse_2 - test $0x2000000,%edx - jnz sse_1 - mov $0,%eax - ret + /* test $0x100000,%ecx + jnz sse_42 + test $0x80000,%ecx + jnz sse_41 + test $0x200,%ecx + jnz ssse_3 */ + test $0x1,%ecx + jnz sse_3 + test $0x4000000,%edx + jnz sse_2 + test $0x2000000,%edx + jnz sse_1 + mov $0,%eax + ret sse_42: - mov $42,RETURN_REG - ret + mov $42,RETURN_REG + ret sse_41: - mov $41,RETURN_REG - ret + mov $41,RETURN_REG + ret ssse_3: - mov $33,RETURN_REG - ret + mov $33,RETURN_REG + ret sse_3: - mov $30,RETURN_REG - ret + mov $30,RETURN_REG + ret sse_2: - mov $20,RETURN_REG - ret + mov $20,RETURN_REG + ret sse_1: - mov $10,RETURN_REG - ret + mov $10,RETURN_REG + ret #ifdef WINDOWS .section .drectve .ascii " -export:sse_version" From d0652d9d0bf98ccd5edad2df91a2db0451e87727 Mon Sep 17 00:00:00 2001 From: Keith Lazuka Date: Mon, 14 Sep 2009 08:50:58 -0400 Subject: [PATCH 038/345] help.stylesheet: fixed black border bug around nav links in HTML documentation --- basis/help/stylesheet/stylesheet.factor | 4 ++-- word-at,assocs.html | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 word-at,assocs.html diff --git a/basis/help/stylesheet/stylesheet.factor b/basis/help/stylesheet/stylesheet.factor index 2475fba0f6..88fe81de6e 100644 --- a/basis/help/stylesheet/stylesheet.factor +++ b/basis/help/stylesheet/stylesheet.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2005, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: colors colors.constants io.styles literals namespaces ; +USING: colors colors.constants io.styles namespaces ; IN: help.stylesheet SYMBOL: default-span-style @@ -42,7 +42,7 @@ SYMBOL: help-path-style H{ { font-size 10 } { table-gap { 5 5 } } - { table-border $ transparent } + { table-border COLOR: FactorLightLightTan } } help-path-style set-global SYMBOL: heading-style diff --git a/word-at,assocs.html b/word-at,assocs.html new file mode 100644 index 0000000000..f0be85855a --- /dev/null +++ b/word-at,assocs.html @@ -0,0 +1,13 @@ + + + + + at ( key assoc -- value/f ) + + +
Vocabulary
assocs

Inputs and outputs
keyan object
assocan assoc
value/fthe value associated to the key, or f if the key is not present in the assoc


Word description
Looks up the value associated with a key. This word makes no distinction between a missing value and a value set to f; if the difference is important, use at*.

See also
at*, key?, ?at

Definition
: at ( key assoc -- value/f ) at* drop ; inline

+ \ No newline at end of file From 91c7eb9eea9366c59762dcb942ff3627730effdb Mon Sep 17 00:00:00 2001 From: Keith Lazuka Date: Mon, 14 Sep 2009 09:01:03 -0400 Subject: [PATCH 039/345] Renamed a Factor theme color to something more sensible. --- basis/colors/constants/factor-colors.txt | 4 ++-- basis/help/stylesheet/stylesheet.factor | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/basis/colors/constants/factor-colors.txt b/basis/colors/constants/factor-colors.txt index c032aae5c4..b8af9d3949 100644 --- a/basis/colors/constants/factor-colors.txt +++ b/basis/colors/constants/factor-colors.txt @@ -1,6 +1,6 @@ ! Factor UI theme colors -243 242 234 FactorLightLightTan -227 226 219 FactorLightTan +243 242 234 FactorLightTan +227 226 219 FactorTan 172 167 147 FactorDarkTan 81 91 105 FactorLightSlateBlue 55 62 72 FactorDarkSlateBlue diff --git a/basis/help/stylesheet/stylesheet.factor b/basis/help/stylesheet/stylesheet.factor index 88fe81de6e..8a119823cc 100644 --- a/basis/help/stylesheet/stylesheet.factor +++ b/basis/help/stylesheet/stylesheet.factor @@ -34,7 +34,7 @@ H{ { font-style bold } { wrap-margin 500 } { foreground COLOR: gray20 } - { page-color COLOR: FactorLightLightTan } + { page-color COLOR: FactorLightTan } { inset { 5 5 } } } title-style set-global @@ -42,7 +42,7 @@ SYMBOL: help-path-style H{ { font-size 10 } { table-gap { 5 5 } } - { table-border COLOR: FactorLightLightTan } + { table-border COLOR: FactorLightTan } } help-path-style set-global SYMBOL: heading-style @@ -75,7 +75,7 @@ H{ SYMBOL: code-style H{ - { page-color COLOR: FactorLightLightTan } + { page-color COLOR: FactorLightTan } { inset { 5 5 } } { wrap-margin f } } code-style set-global @@ -113,7 +113,7 @@ H{ SYMBOL: table-style H{ { table-gap { 5 5 } } - { table-border COLOR: FactorLightTan } + { table-border COLOR: FactorTan } } table-style set-global SYMBOL: list-style From 6ae07e8a087f8491cfb714b9c8039159991601d5 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 09:48:32 -0500 Subject: [PATCH 040/345] fix compilation on linux 32 --- vm/os-linux-x86.32.hpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/vm/os-linux-x86.32.hpp b/vm/os-linux-x86.32.hpp index e4fd8402a8..1db87f995d 100644 --- a/vm/os-linux-x86.32.hpp +++ b/vm/os-linux-x86.32.hpp @@ -5,22 +5,21 @@ namespace factor inline static void *ucontext_stack_pointer(void *uap) { - ucontext_t *ucontext = (ucontext_t *)uap; - return (void *)ucontext->uc_mcontext.gregs[7]; + ucontext_t *ucontext = (ucontext_t *)uap; + return (void *)ucontext->uc_mcontext.gregs[7]; } inline static unsigned int uap_fpu_status(void *uap) { - ucontext_t *ucontext = (ucontext_t *)uap; - return ucontext->uc_mcontext.fpregs->swd - | ucontext->uc_mcontext.fpregs->mxcsr; + // XXX mxcsr not available in i386 ucontext + ucontext_t *ucontext = (ucontext_t *)uap; + return ucontext->uc_mcontext.fpregs->sw; } inline static void uap_clear_fpu_status(void *uap) { - ucontext_t *ucontext = (ucontext_t *)uap; - ucontext->uc_mcontext.fpregs->swd = 0; - ucontext->uc_mcontext.fpregs->mxcsr &= 0xffffffc0; + ucontext_t *ucontext = (ucontext_t *)uap; + ucontext->uc_mcontext.fpregs->sw = 0; } #define UAP_PROGRAM_COUNTER(ucontext) \ From 578c977a7e8447a444a36223f27f62f3f61d68e8 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 09:56:38 -0500 Subject: [PATCH 041/345] more freebsd compilation fixes --- vm/os-freebsd-x86.32.hpp | 4 ++-- vm/os-freebsd-x86.64.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vm/os-freebsd-x86.32.hpp b/vm/os-freebsd-x86.32.hpp index a0888e1f5b..e682fec13c 100644 --- a/vm/os-freebsd-x86.32.hpp +++ b/vm/os-freebsd-x86.32.hpp @@ -30,12 +30,12 @@ inline static unsigned int uap_fpu_status(void *uap) inline static void uap_clear_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_387) + if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_387) { struct save87 *x87 = (struct save87 *)(&ucontext->uc_mcontext.mc_fpstate); x87->sv_env.en_sw = 0; } - else if (uap->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) + else if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); xmm->sv_env.en_sw = 0; diff --git a/vm/os-freebsd-x86.64.hpp b/vm/os-freebsd-x86.64.hpp index 6200a0f5f3..8f8d218a10 100644 --- a/vm/os-freebsd-x86.64.hpp +++ b/vm/os-freebsd-x86.64.hpp @@ -15,7 +15,7 @@ inline static unsigned int uap_fpu_status(void *uap) ucontext_t *ucontext = (ucontext_t *)uap; if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { - struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + struct savefpu *xmm = (struct savefpu *)(&ucontext->uc_mcontext.mc_fpstate); return xmm->sv_env.en_sw | xmm->sv_env.en_mxcsr; } else @@ -27,7 +27,7 @@ inline static void uap_clear_fpu_status(void *uap) ucontext_t *ucontext = (ucontext_t *)uap; if (ucontext->uc_mcontext.mc_fpformat == _MC_FPFMT_XMM) { - struct savexmm *xmm = (struct savexmm *)(&ucontext->uc_mcontext.mc_fpstate); + struct savefpu *xmm = (struct savefpu *)(&ucontext->uc_mcontext.mc_fpstate); xmm->sv_env.en_sw = 0; xmm->sv_env.en_mxcsr &= 0xffffffc0; } From fad3d47a8b05c358b5b07041f1dc79674411b226 Mon Sep 17 00:00:00 2001 From: Keith Lazuka Date: Mon, 14 Sep 2009 11:13:45 -0400 Subject: [PATCH 042/345] help.vocabs: inform the user when browsing a vocab that is not loaded. --- basis/help/vocabs/vocabs.factor | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/basis/help/vocabs/vocabs.factor b/basis/help/vocabs/vocabs.factor index e8b145d37e..a7cd70707d 100644 --- a/basis/help/vocabs/vocabs.factor +++ b/basis/help/vocabs/vocabs.factor @@ -227,6 +227,15 @@ C: vocab-author ] bi ] unless-empty ; +: vocab-is-not-loaded ( vocab -- ) + "Attention" $heading + vocab-name dup "The " " vocabulary is not loaded. In order to browse " + "its documentation, you must first load it." append surround print-element + "USE: " prepend 1array $code ; + +: describe-words ( vocab -- ) + dup vocab [ words $words ] [ vocab-is-not-loaded ] if ; + : words. ( vocab -- ) last-element off [ require ] [ words $words ] bi nl ; @@ -243,7 +252,7 @@ C: vocab-author first { [ describe-help ] [ describe-metadata ] - [ words $words ] + [ describe-words ] [ describe-files ] [ describe-children ] } cleave ; From c1bc5f22e46e11e3cfd1781141611512e4a39e6e Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 12:02:02 -0500 Subject: [PATCH 043/345] use secret sauce to clear MXCSR in win32 context, and handle secret STATUS_FLOAT_MULTIPLE_* SEH codes raised by SSE traps --- vm/os-windows-nt.32.hpp | 29 +++++++++++++++++++++++++++++ vm/os-windows-nt.64.hpp | 3 +++ vm/os-windows-nt.cpp | 22 ++++++++++++---------- vm/os-windows-nt.hpp | 5 +++++ 4 files changed, 49 insertions(+), 10 deletions(-) mode change 100644 => 100755 vm/os-windows-nt.32.hpp mode change 100644 => 100755 vm/os-windows-nt.64.hpp mode change 100644 => 100755 vm/os-windows-nt.cpp mode change 100644 => 100755 vm/os-windows-nt.hpp diff --git a/vm/os-windows-nt.32.hpp b/vm/os-windows-nt.32.hpp old mode 100644 new mode 100755 index ed67e28b8b..748272ff38 --- a/vm/os-windows-nt.32.hpp +++ b/vm/os-windows-nt.32.hpp @@ -4,4 +4,33 @@ namespace factor #define ESP Esp #define EIP Eip +typedef struct DECLSPEC_ALIGN(16) _M128A { + ULONGLONG Low; + LONGLONG High; +} M128A, *PM128A; + +/* The ExtendedRegisters field of the x86.32 CONTEXT structure uses this layout; however, + * this structure is only made available from winnt.h on x86.64 */ +typedef struct _XMM_SAVE_AREA32 { + WORD ControlWord; /* 000 */ + WORD StatusWord; /* 002 */ + BYTE TagWord; /* 004 */ + BYTE Reserved1; /* 005 */ + WORD ErrorOpcode; /* 006 */ + DWORD ErrorOffset; /* 008 */ + WORD ErrorSelector; /* 00c */ + WORD Reserved2; /* 00e */ + DWORD DataOffset; /* 010 */ + WORD DataSelector; /* 014 */ + WORD Reserved3; /* 016 */ + DWORD MxCsr; /* 018 */ + DWORD MxCsr_Mask; /* 01c */ + M128A FloatRegisters[8]; /* 020 */ + M128A XmmRegisters[16]; /* 0a0 */ + BYTE Reserved4[96]; /* 1a0 */ +} XMM_SAVE_AREA32, *PXMM_SAVE_AREA32; + +#define X87SW(ctx) (ctx)->FloatSave.StatusWord +#define MXCSR(ctx) ((XMM_SAVE_AREA32*)((ctx)->ExtendedRegisters))->MxCsr + } diff --git a/vm/os-windows-nt.64.hpp b/vm/os-windows-nt.64.hpp old mode 100644 new mode 100755 index 30ce150754..b64bd607cb --- a/vm/os-windows-nt.64.hpp +++ b/vm/os-windows-nt.64.hpp @@ -4,4 +4,7 @@ namespace factor #define ESP Rsp #define EIP Rip +#define X87SW(ctx) (ctx)->FloatSave.StatusWord +#define MXCSR(ctx) (ctx)->MxCsr + } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp old mode 100644 new mode 100755 index 017a96bb7c..b50c9b7af8 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -28,16 +28,18 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) c->EIP = (cell)memory_signal_handler_impl; break; - case EXCEPTION_FLT_DENORMAL_OPERAND: - case EXCEPTION_FLT_DIVIDE_BY_ZERO: - case EXCEPTION_FLT_INEXACT_RESULT: - case EXCEPTION_FLT_INVALID_OPERATION: - case EXCEPTION_FLT_OVERFLOW: - case EXCEPTION_FLT_STACK_CHECK: - case EXCEPTION_FLT_UNDERFLOW: - /* XXX MxCsr is not available in CONTEXT structure on x86.32 */ - signal_fpu_status = c->FloatSave.StatusWord; - c->FloatSave.StatusWord = 0; + case STATUS_FLOAT_DENORMAL_OPERAND: + case STATUS_FLOAT_DIVIDE_BY_ZERO: + case STATUS_FLOAT_INEXACT_RESULT: + case STATUS_FLOAT_INVALID_OPERATION: + case STATUS_FLOAT_OVERFLOW: + case STATUS_FLOAT_STACK_CHECK: + case STATUS_FLOAT_UNDERFLOW: + case STATUS_FLOAT_MULTIPLE_FAULTS: + case STATUS_FLOAT_MULTIPLE_TRAPS: + signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c)); + X87SW(c) = 0; + MXCSR(c) &= 0xffffffc0; c->EIP = (cell)fp_signal_handler_impl; break; case 0x40010006: diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp old mode 100644 new mode 100755 index 4371771c13..088103bb5b --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -23,4 +23,9 @@ void c_to_factor_toplevel(cell quot); FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe); void open_console(); +// SSE traps raise these exception codes, which are defined in internal NT headers +// but not winbase.h +#define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4 +#define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5 + } From b77d9d29608c43d2cb5304343a224cbd0861778c Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 12:30:10 -0500 Subject: [PATCH 044/345] more secret sauce to tease mxcsr out of linux-x86.32 ucontext --- vm/os-linux-x86.32.hpp | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/vm/os-linux-x86.32.hpp b/vm/os-linux-x86.32.hpp index 1db87f995d..8fa7eff842 100644 --- a/vm/os-linux-x86.32.hpp +++ b/vm/os-linux-x86.32.hpp @@ -3,6 +3,32 @@ namespace factor { +// glibc lies about the contents of the fpstate the kernel provides, hiding the FXSR +// environment +struct _fpstate { + /* Regular FPU environment */ + unsigned long cw; + unsigned long sw; + unsigned long tag; + unsigned long ipoff; + unsigned long cssel; + unsigned long dataoff; + unsigned long datasel; + struct _fpreg _st[8]; + unsigned short status; + unsigned short magic; /* 0xffff = regular FPU data only */ + + /* FXSR FPU environment */ + unsigned long _fxsr_env[6]; /* FXSR FPU env is ignored */ + unsigned long mxcsr; + unsigned long reserved; + struct _fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */ + struct _xmmreg _xmm[8]; + unsigned long padding[56]; +}; + +#define X86_FXSR_MAGIC 0x0000 + inline static void *ucontext_stack_pointer(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; @@ -11,15 +37,21 @@ inline static void *ucontext_stack_pointer(void *uap) inline static unsigned int uap_fpu_status(void *uap) { - // XXX mxcsr not available in i386 ucontext ucontext_t *ucontext = (ucontext_t *)uap; - return ucontext->uc_mcontext.fpregs->sw; + struct _fpstate *fpregs = (struct _fpstate *)uap->uc_mcontext.fpregs; + if (fpregs->magic == X86_FXSR_MAGIC) + return fpregs->sw | fpregs->mxcsr; + else + return fpregs->sw; } inline static void uap_clear_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - ucontext->uc_mcontext.fpregs->sw = 0; + struct _fpstate *fpregs = (struct _fpstate *)uap->uc_mcontext.fpregs; + fpregs->sw = 0; + if (fpregs->magic == X86_FXSR_MAGIC) + fpregs->mxcsr &= 0xffffffc0; } #define UAP_PROGRAM_COUNTER(ucontext) \ From 89ce13d4d46f61b445cd0e9b0aebaebed54d66f0 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 13:21:46 -0500 Subject: [PATCH 045/345] linux 32 typo --- vm/os-linux-x86.32.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/os-linux-x86.32.hpp b/vm/os-linux-x86.32.hpp index 8fa7eff842..bd2315ccef 100644 --- a/vm/os-linux-x86.32.hpp +++ b/vm/os-linux-x86.32.hpp @@ -38,7 +38,7 @@ inline static void *ucontext_stack_pointer(void *uap) inline static unsigned int uap_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - struct _fpstate *fpregs = (struct _fpstate *)uap->uc_mcontext.fpregs; + struct _fpstate *fpregs = (struct _fpstate *)ucontext->uc_mcontext.fpregs; if (fpregs->magic == X86_FXSR_MAGIC) return fpregs->sw | fpregs->mxcsr; else @@ -48,7 +48,7 @@ inline static unsigned int uap_fpu_status(void *uap) inline static void uap_clear_fpu_status(void *uap) { ucontext_t *ucontext = (ucontext_t *)uap; - struct _fpstate *fpregs = (struct _fpstate *)uap->uc_mcontext.fpregs; + struct _fpstate *fpregs = (struct _fpstate *)ucontext->uc_mcontext.fpregs; fpregs->sw = 0; if (fpregs->magic == X86_FXSR_MAGIC) fpregs->mxcsr &= 0xffffffc0; From b6d57a4d19c02f68f5dc2c1d4d243b511baab0f2 Mon Sep 17 00:00:00 2001 From: Keith Lazuka Date: Mon, 14 Sep 2009 14:27:30 -0400 Subject: [PATCH 046/345] help.vocabs: tweaked the vocab-not-loaded msg --- basis/help/vocabs/vocabs.factor | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/basis/help/vocabs/vocabs.factor b/basis/help/vocabs/vocabs.factor index a7cd70707d..6e2fd6f278 100644 --- a/basis/help/vocabs/vocabs.factor +++ b/basis/help/vocabs/vocabs.factor @@ -228,10 +228,9 @@ C: vocab-author ] unless-empty ; : vocab-is-not-loaded ( vocab -- ) - "Attention" $heading - vocab-name dup "The " " vocabulary is not loaded. In order to browse " - "its documentation, you must first load it." append surround print-element - "USE: " prepend 1array $code ; + "Words" $heading + "You must first load (USE:) this vocab to browse its documentation/words." + print-element vocab-name "USE: " prepend 1array $code ; : describe-words ( vocab -- ) dup vocab [ words $words ] [ vocab-is-not-loaded ] if ; From 79505168cfddd38d01ae307529c1af7b1b3b9af9 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 14 Sep 2009 13:47:37 -0500 Subject: [PATCH 047/345] faster number-length and some unit tests --- extra/project-euler/common/common-tests.factor | 17 +++++++++++++++++ extra/project-euler/common/common.factor | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 extra/project-euler/common/common-tests.factor diff --git a/extra/project-euler/common/common-tests.factor b/extra/project-euler/common/common-tests.factor new file mode 100644 index 0000000000..1f7a3668e2 --- /dev/null +++ b/extra/project-euler/common/common-tests.factor @@ -0,0 +1,17 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: tools.test project-euler.common ; +IN: project-euler.common.tests + +[ 4 ] [ -1000 number-length ] unit-test +[ 3 ] [ -999 number-length ] unit-test +[ 3 ] [ -100 number-length ] unit-test +[ 2 ] [ -99 number-length ] unit-test +[ 1 ] [ -9 number-length ] unit-test +[ 1 ] [ -1 number-length ] unit-test +[ 1 ] [ 0 number-length ] unit-test +[ 1 ] [ 9 number-length ] unit-test +[ 2 ] [ 99 number-length ] unit-test +[ 3 ] [ 100 number-length ] unit-test +[ 3 ] [ 999 number-length ] unit-test +[ 4 ] [ 1000 number-length ] unit-test diff --git a/extra/project-euler/common/common.factor b/extra/project-euler/common/common.factor index efec77355b..3d320fad62 100644 --- a/extra/project-euler/common/common.factor +++ b/extra/project-euler/common/common.factor @@ -76,7 +76,12 @@ PRIVATE> [ dup 0 = not ] [ 10 /mod ] produce reverse nip ; : number-length ( n -- m ) - log10 floor 1 + >integer ; + abs [ + 1 + ] [ + 1 0 [ 2over >= ] + [ [ 10 * ] [ 1 + ] bi* ] while 2nip + ] if-zero ; : nth-prime ( n -- n ) 1 - lprimes lnth ; From 2fdb16060b8e15c0f9e90da28064241c97ae219d Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 14:10:51 -0500 Subject: [PATCH 048/345] vm-error>exception-flags word to extract exception flag information from a trap exception --- basis/math/floats/env/env-docs.factor | 20 +++++++++++++++----- basis/math/floats/env/env.factor | 22 +++++++++++++++++++--- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/basis/math/floats/env/env-docs.factor b/basis/math/floats/env/env-docs.factor index ef580b9040..0fc781713c 100644 --- a/basis/math/floats/env/env-docs.factor +++ b/basis/math/floats/env/env-docs.factor @@ -1,5 +1,5 @@ ! (c)Joe Groff bsd license -USING: help help.markup help.syntax quotations ; +USING: help help.markup help.syntax kernel quotations ; IN: math.floats.env HELP: fp-exception @@ -97,13 +97,21 @@ HELP: fp-traps HELP: with-fp-traps { $values { "exceptions" "a sequence of " { $link fp-exception } " symbols" } { "quot" quotation } } -{ $description "Replaces the floating-point exception mask to enable processor traps to be raised for the set of exception conditions specified in " { $snippet "exceptions" } " for the dynamic extent of " { $snippet "quot" } ", restoring the original exception mask on " { $snippet "quot" } "'s completion." } ; +{ $description "Clears the floating-point exception flags and replaces the exception mask, enabling processor traps for the set of exception conditions specified in " { $snippet "exceptions" } " for the dynamic extent of " { $snippet "quot" } ". The original exception mask is restored on " { $snippet "quot" } "'s completion." } ; HELP: without-fp-traps { $values { "quot" quotation } } { $description "Disables all floating-pointer processor traps for the dynamic extent of " { $snippet "quot" } ", restoring the original exception mask on " { $snippet "quot" } "'s completion." } ; -{ fp-traps with-fp-traps without-fp-traps } related-words +{ fp-traps with-fp-traps without-fp-traps vm-error>exception-flags vm-error-exception-flag? } related-words + +HELP: vm-error>exception-flags +{ $values { "error" "a floating-point error object from the Factor VM" } { "exceptions" "a sequence of " { $link fp-exception } " symbols" } } +{ $description "When a floating-point trap is raised, the Factor VM reports the trap by throwing a Factor exception containing the exception flags at the time the trap was raised. This word extracts the exception flag information from " { $snippet "error" } " and converts it into a sequence of " { $link fp-exception } "s." } ; + +HELP: vm-error-exception-flag? +{ $values { "error" "a floating-point error object from the Factor VM" } { "flag" fp-exception } { "?" boolean } } +{ $description "When a floating-point trap is raised, the Factor VM reports the trap by throwing a Factor exception containing the exception flags at the time the trap was raised. This word returns a boolean indicating whether the exception " { $snippet "flag" } " was raised at the time " { $snippet "error" } " was thrown." } ; ARTICLE: "math.floats.env" "Controlling the floating-point environment" "The " { $vocab-link "math.floats.env" } " vocabulary contains words for querying and controlling the floating-point environment." @@ -117,11 +125,13 @@ $nl { $subsection fp-traps } { $subsection with-fp-traps } { $subsection without-fp-traps } +"Getting the floating-point exception state from errors raised by enabled traps:" +{ $subsection vm-error>exception-flags } +{ $subsection vm-error-exception-flag? } "Querying and controlling the rounding mode and treatment of denormals:" { $subsection rounding-mode } { $subsection with-rounding-mode } { $subsection denormal-mode } -{ $subsection with-denormal-mode } -{ $notes "On PowerPC, the above words only modify the scalar FPU's state (in FPSCR); the AltiVec unit is currently unaffected." } ; +{ $subsection with-denormal-mode } ; ABOUT: "math.floats.env" diff --git a/basis/math/floats/env/env.factor b/basis/math/floats/env/env.factor index 0b1267eb32..04fbc4f26c 100644 --- a/basis/math/floats/env/env.factor +++ b/basis/math/floats/env/env.factor @@ -1,7 +1,8 @@ ! (c)Joe Groff bsd license -USING: alien.syntax arrays assocs biassocs combinators continuations -generalizations kernel literals locals math math.bitwise -sequences sets system vocabs.loader ; +USING: alien.syntax arrays assocs biassocs combinators +combinators.short-circuit continuations generalizations kernel +literals locals math math.bitwise sequences sets system +vocabs.loader ; IN: math.floats.env SINGLETONS: @@ -102,6 +103,15 @@ GENERIC# (set-denormal-mode) 1 ( fp-env mode -- fp-env ) } spread ] 4 ncurry change-fp-env-registers ; +CONSTANT: vm-error-exception-flag>bit + H{ + { +fp-invalid-operation+ HEX: 01 } + { +fp-overflow+ HEX: 02 } + { +fp-underflow+ HEX: 04 } + { +fp-zero-divide+ HEX: 08 } + { +fp-inexact+ HEX: 10 } + } + PRIVATE> : fp-exception-flags ( -- exceptions ) @@ -113,6 +123,11 @@ PRIVATE> : collect-fp-exceptions ( quot -- exceptions ) [ clear-fp-exception-flags ] dip call fp-exception-flags ; inline +: vm-error>exception-flags ( error -- exceptions ) + third vm-error-exception-flag>bit mask> ; +: vm-error-exception-flag? ( error flag -- ? ) + vm-error>exception-flags member? ; + : denormal-mode ( -- mode ) fp-env-register (get-denormal-mode) ; :: with-denormal-mode ( mode quot -- ) @@ -131,6 +146,7 @@ PRIVATE> (fp-env-registers) [ (get-fp-traps) ] [ union ] map-reduce >array ; inline :: with-fp-traps ( exceptions quot -- ) + clear-fp-exception-flags fp-traps :> orig exceptions set-fp-traps quot [ orig set-fp-traps ] [ ] cleanup ; inline From 8c14af3f6c8ed0a6bf5e0d2c6f89b94fd12abb78 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 15:03:05 -0500 Subject: [PATCH 049/345] add a number-base configuration variable to prettyprint.config. set to 2 to print BIN:, 8 to print OCT:, 10 to print decimal, 16 to print HEX: --- basis/prettyprint/backend/backend.factor | 15 +++++++++++++-- basis/prettyprint/config/config.factor | 2 ++ basis/prettyprint/prettyprint-tests.factor | 8 ++++++++ core/math/parser/parser.factor | 6 ++---- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/basis/prettyprint/backend/backend.factor b/basis/prettyprint/backend/backend.factor index f8bcb66b1e..cba40bbff1 100644 --- a/basis/prettyprint/backend/backend.factor +++ b/basis/prettyprint/backend/backend.factor @@ -45,12 +45,23 @@ M: method-body pprint* ] "" make ] [ word-style ] bi styled-text ; -M: real pprint* number>string text ; +M: real pprint* + number-base get { + { 16 [ \ HEX: [ 16 >base text ] pprint-prefix ] } + { 8 [ \ OCT: [ 8 >base text ] pprint-prefix ] } + { 2 [ \ BIN: [ 2 >base text ] pprint-prefix ] } + [ drop number>string text ] + } case ; M: float pprint* dup fp-nan? [ \ NAN: [ fp-nan-payload >hex text ] pprint-prefix - ] [ call-next-method ] if ; + ] [ + number-base get { + { 16 [ \ HEX: [ 16 >base text ] pprint-prefix ] } + [ drop number>string text ] + } case + ] if ; M: f pprint* drop \ f pprint-word ; diff --git a/basis/prettyprint/config/config.factor b/basis/prettyprint/config/config.factor index d42b134d4c..45557925a5 100644 --- a/basis/prettyprint/config/config.factor +++ b/basis/prettyprint/config/config.factor @@ -11,9 +11,11 @@ SYMBOL: margin SYMBOL: nesting-limit SYMBOL: length-limit SYMBOL: line-limit +SYMBOL: number-base SYMBOL: string-limit? SYMBOL: boa-tuples? SYMBOL: c-object-pointers? 4 tab-size set-global 64 margin set-global +10 number-base set-global diff --git a/basis/prettyprint/prettyprint-tests.factor b/basis/prettyprint/prettyprint-tests.factor index b3897960f0..db3331305e 100644 --- a/basis/prettyprint/prettyprint-tests.factor +++ b/basis/prettyprint/prettyprint-tests.factor @@ -8,7 +8,15 @@ listener ; IN: prettyprint.tests [ "4" ] [ 4 unparse ] unit-test +[ "4096" ] [ 4096 unparse ] unit-test +[ "BIN: 1000000000000" ] [ 2 number-base [ 4096 unparse ] with-variable ] unit-test +[ "OCT: 10000" ] [ 8 number-base [ 4096 unparse ] with-variable ] unit-test +[ "HEX: 1000" ] [ 16 number-base [ 4096 unparse ] with-variable ] unit-test [ "1.0" ] [ 1.0 unparse ] unit-test +[ "8.0" ] [ 8.0 unparse ] unit-test +[ "8.0" ] [ 2 number-base [ 8.0 unparse ] with-variable ] unit-test +[ "8.0" ] [ 8 number-base [ 8.0 unparse ] with-variable ] unit-test +[ "HEX: 1.0p3" ] [ 16 number-base [ 8.0 unparse ] with-variable ] unit-test [ "1267650600228229401496703205376" ] [ 1 100 shift unparse ] unit-test [ "+" ] [ \ + unparse ] unit-test diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor index 8e911453ad..d422a2c199 100644 --- a/core/math/parser/parser.factor +++ b/core/math/parser/parser.factor @@ -109,9 +109,8 @@ SYMBOL: negative? : base>float ( str base -- n/f ) { - { 10 [ dec>float ] } { 16 [ hex>float ] } - [ "Floats can only be converted from strings in base 10 or 16" throw ] + [ drop dec>float ] } case ; : number-char? ( char -- ? ) @@ -232,9 +231,8 @@ M: ratio >base : float>base ( n base -- str ) { - { 10 [ float>decimal ] } { 16 [ float>hex ] } - [ "Floats can only be converted to strings in base 10 or 16" throw ] + [ drop float>decimal ] } case ; PRIVATE> From 77f0fbf497a37d03d9a0a648fd6b47ed0b5b843a Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Mon, 14 Sep 2009 15:17:36 -0500 Subject: [PATCH 050/345] missed a few PPC status bits that needed clearing --- basis/math/floats/env/ppc/ppc.factor | 2 +- vm/os-macosx-ppc.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/math/floats/env/ppc/ppc.factor b/basis/math/floats/env/ppc/ppc.factor index dd8fd88b13..d6a6ae6834 100644 --- a/basis/math/floats/env/ppc/ppc.factor +++ b/basis/math/floats/env/ppc/ppc.factor @@ -34,7 +34,7 @@ M: ppc-vmx-env (set-fp-env-register) M: ppc (fp-env-registers) 2array ; -CONSTANT: ppc-exception-flag-bits HEX: fff8,0000 +CONSTANT: ppc-exception-flag-bits HEX: fff8,0700 CONSTANT: ppc-exception-flag>bit H{ { +fp-invalid-operation+ HEX: 2000,0000 } diff --git a/vm/os-macosx-ppc.hpp b/vm/os-macosx-ppc.hpp index 338e3e812f..cd2097a3fd 100644 --- a/vm/os-macosx-ppc.hpp +++ b/vm/os-macosx-ppc.hpp @@ -69,7 +69,7 @@ inline static cell fix_stack_pointer(cell sp) inline static void mach_clear_fpu_status(ppc_float_state_t *float_state) { - FPSCR(float_state) &= 0x0007ffff; + FPSCR(float_state) &= 0x0007f8ff; } inline static void uap_clear_fpu_status(void *uap) From 111d298cea146a2f723e0090d6c5d4a345323d53 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 16:14:53 -0500 Subject: [PATCH 051/345] Remove bogus file --- word-at,assocs.html | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 word-at,assocs.html diff --git a/word-at,assocs.html b/word-at,assocs.html deleted file mode 100644 index f0be85855a..0000000000 --- a/word-at,assocs.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - at ( key assoc -- value/f ) - - -
Vocabulary
assocs

Inputs and outputs
keyan object
assocan assoc
value/fthe value associated to the key, or f if the key is not present in the assoc


Word description
Looks up the value associated with a key. This word makes no distinction between a missing value and a value set to f; if the difference is important, use at*.

See also
at*, key?, ?at

Definition
: at ( key assoc -- value/f ) at* drop ; inline

- \ No newline at end of file From d772bff8b903d379f474b1790fd94d17853791ce Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 16:19:36 -0500 Subject: [PATCH 052/345] help.vocabs: tweak 'not loaded' message some more --- basis/help/vocabs/vocabs.factor | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/basis/help/vocabs/vocabs.factor b/basis/help/vocabs/vocabs.factor index 6e2fd6f278..d8f351f57d 100644 --- a/basis/help/vocabs/vocabs.factor +++ b/basis/help/vocabs/vocabs.factor @@ -228,12 +228,16 @@ C: vocab-author ] unless-empty ; : vocab-is-not-loaded ( vocab -- ) - "Words" $heading - "You must first load (USE:) this vocab to browse its documentation/words." + "Not loaded" $heading + "You must first load this vocabulary to browse its documentation and words." print-element vocab-name "USE: " prepend 1array $code ; : describe-words ( vocab -- ) - dup vocab [ words $words ] [ vocab-is-not-loaded ] if ; + { + { [ dup vocab ] [ words $words ] } + { [ dup find-vocab-root ] [ vocab-is-not-loaded ] } + [ drop ] + } cond ; : words. ( vocab -- ) last-element off From 4f702de449ba65d4c9f813e8b74e77471e7066aa Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 16:19:58 -0500 Subject: [PATCH 053/345] math.functions: more accurate log10 (fixes problem reported by OneEyed) --- basis/compiler/cfg/intrinsics/intrinsics.factor | 1 + basis/math/functions/functions-tests.factor | 6 ++++++ basis/math/functions/functions.factor | 6 +++++- basis/math/libm/libm.factor | 3 +++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/basis/compiler/cfg/intrinsics/intrinsics.factor b/basis/compiler/cfg/intrinsics/intrinsics.factor index a54caf23de..0daab82395 100644 --- a/basis/compiler/cfg/intrinsics/intrinsics.factor +++ b/basis/compiler/cfg/intrinsics/intrinsics.factor @@ -129,6 +129,7 @@ IN: compiler.cfg.intrinsics { math.libm:ftanh [ drop "tanh" emit-unary-float-function ] } { math.libm:fexp [ drop "exp" emit-unary-float-function ] } { math.libm:flog [ drop "log" emit-unary-float-function ] } + { math.libm:flog10 [ drop "log10" emit-unary-float-function ] } { math.libm:fpow [ drop "pow" emit-binary-float-function ] } { math.libm:facosh [ drop "acosh" emit-unary-float-function ] } { math.libm:fasinh [ drop "asinh" emit-unary-float-function ] } diff --git a/basis/math/functions/functions-tests.factor b/basis/math/functions/functions-tests.factor index 7a6da72005..fa880f77af 100644 --- a/basis/math/functions/functions-tests.factor +++ b/basis/math/functions/functions-tests.factor @@ -33,6 +33,12 @@ IN: math.functions.tests [ 0.0 ] [ 1.0 log ] unit-test [ 1.0 ] [ e log ] unit-test +[ 0.0 ] [ 1.0 log10 ] unit-test +[ 1.0 ] [ 10.0 log10 ] unit-test +[ 2.0 ] [ 100.0 log10 ] unit-test +[ 3.0 ] [ 1000.0 log10 ] unit-test +[ 4.0 ] [ 10000.0 log10 ] unit-test + [ t ] [ 1 exp e 1.e-10 ~ ] unit-test [ t ] [ 1.0 exp e 1.e-10 ~ ] unit-test [ t ] [ -1 exp e * 1.0 1.e-10 ~ ] unit-test diff --git a/basis/math/functions/functions.factor b/basis/math/functions/functions.factor index 0cf9467795..f124c202b8 100644 --- a/basis/math/functions/functions.factor +++ b/basis/math/functions/functions.factor @@ -173,7 +173,11 @@ M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline : 10^ ( x -- y ) 10 swap ^ ; inline -: log10 ( x -- y ) log 10 log / ; inline +GENERIC: log10 ( x -- y ) foldable + +M: real log10 >float flog10 ; inline + +M: complex log10 log 10 log / ; inline GENERIC: cos ( x -- y ) foldable diff --git a/basis/math/libm/libm.factor b/basis/math/libm/libm.factor index 1ac0ec0ae7..df8b36fd28 100644 --- a/basis/math/libm/libm.factor +++ b/basis/math/libm/libm.factor @@ -39,6 +39,9 @@ IN: math.libm : flog ( x -- y ) "double" "libm" "log" { "double" } alien-invoke ; +: flog10 ( x -- y ) + "double" "libm" "log10" { "double" } alien-invoke ; + : fpow ( x y -- z ) "double" "libm" "pow" { "double" "double" } alien-invoke ; From ded68c67c03c11b9187023df9b10e98f3cb69e9d Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 20:26:59 -0500 Subject: [PATCH 054/345] Fix bootstrap --- basis/prettyprint/config/config.factor | 2 +- basis/vocabs/prettyprint/prettyprint.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/prettyprint/config/config.factor b/basis/prettyprint/config/config.factor index 45557925a5..dd61e3e23d 100644 --- a/basis/prettyprint/config/config.factor +++ b/basis/prettyprint/config/config.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2003, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: arrays generic assocs io kernel math -namespaces sequences strings io.styles vectors words +namespaces sequences strings vectors words continuations ; IN: prettyprint.config diff --git a/basis/vocabs/prettyprint/prettyprint.factor b/basis/vocabs/prettyprint/prettyprint.factor index 6b759dddde..40493e4e99 100644 --- a/basis/vocabs/prettyprint/prettyprint.factor +++ b/basis/vocabs/prettyprint/prettyprint.factor @@ -88,7 +88,7 @@ PRIVATE> "at the top of the source file:" print nl ] with-style { - { page-color COLOR: FactorLightLightTan } + { page-color COLOR: FactorLightTan } { border-color COLOR: FactorDarkTan } { inset { 5 5 } } } [ manifest get pprint-manifest ] with-nesting From 05fe9c7eadbab785dfdb46b873d9d797effd076a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 14 Sep 2009 21:03:33 -0500 Subject: [PATCH 055/345] colors.constants: clean up naming --- basis/colors/constants/constants.factor | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/basis/colors/constants/constants.factor b/basis/colors/constants/constants.factor index 3912994066..8598fc0663 100644 --- a/basis/colors/constants/constants.factor +++ b/basis/colors/constants/constants.factor @@ -11,23 +11,23 @@ IN: colors.constants [ [ string>number 255 /f ] tri@ 1.0 ] dip [ blank? ] trim-head { { CHAR: \s CHAR: - } } substitute swap ; -: parse-rgb.txt ( lines -- assoc ) +: parse-colors ( lines -- assoc ) [ "!" head? not ] filter [ 11 cut [ " \t" split harvest ] dip suffix ] map [ parse-color ] H{ } map>assoc ; -MEMO: rgb.txt ( -- assoc ) +MEMO: colors ( -- assoc ) "resource:basis/colors/constants/rgb.txt" "resource:basis/colors/constants/factor-colors.txt" - [ utf8 file-lines parse-rgb.txt ] bi@ assoc-union ; + [ utf8 file-lines parse-colors ] bi@ assoc-union ; PRIVATE> -: named-colors ( -- keys ) rgb.txt keys ; +: named-colors ( -- keys ) colors keys ; ERROR: no-such-color name ; : named-color ( name -- color ) - dup rgb.txt at [ ] [ no-such-color ] ?if ; + dup colors at [ ] [ no-such-color ] ?if ; SYNTAX: COLOR: scan named-color parsed ; \ No newline at end of file From 3551294fd42cff1e7ea1baf195f4118e10cee735 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 15 Sep 2009 00:22:19 -0500 Subject: [PATCH 056/345] vm: update Config.netbsd for NetBSD 5.0 --- vm/Config.netbsd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/Config.netbsd b/vm/Config.netbsd index a6ec997ecd..ba5ecd19a5 100644 --- a/vm/Config.netbsd +++ b/vm/Config.netbsd @@ -1,5 +1,5 @@ include vm/Config.unix PLAF_DLL_OBJS += vm/os-genunix.o vm/os-netbsd.o CFLAGS += -export-dynamic -LIBPATH = -L/usr/X11R6/lib -Wl,-rpath,/usr/X11R6/lib -L/usr/pkg/lib -Wl,-rpath,/usr/pkg/lib +LIBPATH = -L/usr/X11R7/lib -Wl,-rpath,/usr/X11R7/lib -L/usr/pkg/lib -Wl,-rpath,/usr/pkg/lib LIBS = -lm -lssl -lcrypto $(X11_UI_LIBS) From 1df869af6a9bc28f620f8dcd8f72ac6ae6aad9a8 Mon Sep 17 00:00:00 2001 From: Guillaume Nargeot Date: Tue, 15 Sep 2009 19:33:56 +0900 Subject: [PATCH 057/345] Solution to Project Euler problem 124 --- extra/project-euler/124/124-tests.factor | 4 ++ extra/project-euler/124/124.factor | 63 ++++++++++++++++++++++++ extra/project-euler/project-euler.factor | 8 +-- 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 extra/project-euler/124/124-tests.factor create mode 100644 extra/project-euler/124/124.factor diff --git a/extra/project-euler/124/124-tests.factor b/extra/project-euler/124/124-tests.factor new file mode 100644 index 0000000000..cdbb5afc18 --- /dev/null +++ b/extra/project-euler/124/124-tests.factor @@ -0,0 +1,4 @@ +USING: project-euler.124 tools.test ; +IN: project-euler.124.tests + +[ 21417 ] [ euler124 ] unit-test diff --git a/extra/project-euler/124/124.factor b/extra/project-euler/124/124.factor new file mode 100644 index 0000000000..0f4d1ee28f --- /dev/null +++ b/extra/project-euler/124/124.factor @@ -0,0 +1,63 @@ +! Copyright (c) 2009 Guillaume Nargeot. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays kernel math.primes.factors +math.ranges project-euler.common sequences sorting ; +IN: project-euler.124 + +! http://projecteuler.net/index.php?section=problems&id=124 + +! DESCRIPTION +! ----------- + +! The radical of n, rad(n), is the product of distinct prime factors of n. +! For example, 504 = 2^3 × 3^2 × 7, so rad(504) = 2 × 3 × 7 = 42. + +! If we calculate rad(n) for 1 ≤ n ≤ 10, then sort them on rad(n), +! and sorting on n if the radical values are equal, we get: + +! Unsorted Sorted +! n rad(n) n rad(n) k +! 1 1 1 1 1 +! 2 2 2 2 2 +! 3 3 4 2 3 +! 4 2 8 2 4 +! 5 5 3 3 5 +! 6 6 9 3 6 +! 7 7 5 5 7 +! 8 2 6 6 8 +! 9 3 7 7 9 +! 10 10 10 10 10 + +! Let E(k) be the kth element in the sorted n column; for example, +! E(4) = 8 and E(6) = 9. + +! If rad(n) is sorted for 1 ≤ n ≤ 100000, find E(10000). + + +! SOLUTION +! -------- + + + +: euler124 ( -- answer ) + 10000 (euler124) nth first ; + +! [ euler124 ] 100 ave-time +! 373 ms ave run time - 17.61 SD (100 trials) + +! TODO: instead of the brute-force method, making the rad +! array in the way of the sieve of eratosthene would scale +! better on bigger values. + +SOLUTION: euler124 diff --git a/extra/project-euler/project-euler.factor b/extra/project-euler/project-euler.factor index f0e40674da..eedf2272ba 100644 --- a/extra/project-euler/project-euler.factor +++ b/extra/project-euler/project-euler.factor @@ -20,10 +20,10 @@ USING: definitions io io.files io.pathnames kernel math math.parser project-euler.071 project-euler.073 project-euler.075 project-euler.076 project-euler.079 project-euler.085 project-euler.092 project-euler.097 project-euler.099 project-euler.100 project-euler.102 project-euler.112 - project-euler.116 project-euler.117 project-euler.134 project-euler.148 - project-euler.150 project-euler.151 project-euler.164 project-euler.169 - project-euler.173 project-euler.175 project-euler.186 project-euler.190 - project-euler.203 project-euler.215 ; + project-euler.116 project-euler.117 project-euler.124 project-euler.134 + project-euler.148 project-euler.150 project-euler.151 project-euler.164 + project-euler.169 project-euler.173 project-euler.175 project-euler.186 + project-euler.190 project-euler.203 project-euler.215 ; IN: project-euler Date: Tue, 15 Sep 2009 21:01:25 +0900 Subject: [PATCH 058/345] Fixed comments of project-euler.085 --- extra/project-euler/085/085.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/project-euler/085/085.factor b/extra/project-euler/085/085.factor index 6c70f65bf7..9c12367cdf 100644 --- a/extra/project-euler/085/085.factor +++ b/extra/project-euler/085/085.factor @@ -19,7 +19,7 @@ IN: project-euler.085 ! SOLUTION ! -------- -! A grid measuring x by y contains x * (x + 1) * y * (x + 1) rectangles. +! A grid measuring x by y contains x * (x + 1) * y * (x + 1) / 4 rectangles. area-of-nearest ; ! [ euler085 ] 100 ave-time -! 2285 ms ave run time - 4.8 SD (100 trials) +! 791 ms ave run time - 17.15 SD (100 trials) SOLUTION: euler085 From 3c98ec95e2b46b4d1f2e2ca76c1d4d167d8c7f09 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 15:16:53 -0500 Subject: [PATCH 059/345] make struct classes print properly in the status bar --- .../struct/prettyprint/prettyprint.factor | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/basis/classes/struct/prettyprint/prettyprint.factor b/basis/classes/struct/prettyprint/prettyprint.factor index e88834530c..8bda9dc5f9 100644 --- a/basis/classes/struct/prettyprint/prettyprint.factor +++ b/basis/classes/struct/prettyprint/prettyprint.factor @@ -1,18 +1,13 @@ ! (c)Joe Groff bsd license USING: accessors alien alien.c-types arrays assocs classes classes.struct combinators combinators.short-circuit continuations -fry kernel libc make math math.parser mirrors prettyprint.backend -prettyprint.custom prettyprint.sections see.private sequences -slots strings summary words ; +definitions fry kernel libc make math math.parser mirrors +prettyprint.backend prettyprint.custom prettyprint.sections +see see.private sequences slots strings summary words ; IN: classes.struct.prettyprint = - [ second offset>> 0 = \ UNION-STRUCT: \ STRUCT: ? ] - [ drop \ STRUCT: ] if ; - : struct>assoc ( struct -- assoc ) [ class struct-slots ] [ struct-slot-values ] bi zip ; @@ -39,8 +34,14 @@ IN: classes.struct.prettyprint PRIVATE> +M: struct-class definer + struct-slots dup length 2 >= + [ second offset>> 0 = \ UNION-STRUCT: \ STRUCT: ? ] + [ drop \ STRUCT: ] if + \ ; ; + M: struct-class see-class* - pprint-; block> ; From ac4141695381147d5aef180e9841a5dba6340120 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 15:18:54 -0500 Subject: [PATCH 060/345] create words for c-types --- basis/alien/arrays/arrays.factor | 38 ++--- basis/alien/c-types/c-types.factor | 159 +++++++++++++-------- basis/alien/parser/parser.factor | 8 +- basis/alien/prettyprint/prettyprint.factor | 16 ++- basis/alien/structs/structs.factor | 2 +- basis/alien/syntax/syntax.factor | 9 +- 6 files changed, 146 insertions(+), 86 deletions(-) diff --git a/basis/alien/arrays/arrays.factor b/basis/alien/arrays/arrays.factor index 64827ec139..86d6c2d49b 100755 --- a/basis/alien/arrays/arrays.factor +++ b/basis/alien/arrays/arrays.factor @@ -22,15 +22,15 @@ M: array c-type-align first c-type-align ; M: array c-type-stack-align? drop f ; -M: array unbox-parameter drop "void*" unbox-parameter ; +M: array unbox-parameter drop void* unbox-parameter ; -M: array unbox-return drop "void*" unbox-return ; +M: array unbox-return drop void* unbox-return ; -M: array box-parameter drop "void*" box-parameter ; +M: array box-parameter drop void* box-parameter ; -M: array box-return drop "void*" box-return ; +M: array box-return drop void* box-return ; -M: array stack-size drop "void*" stack-size ; +M: array stack-size drop void* stack-size ; M: array c-type-boxer-quot unclip @@ -50,7 +50,7 @@ M: value-type c-type-setter ( type -- quot ) '[ @ swap @ _ memcpy ] ; PREDICATE: string-type < pair - first2 [ "char*" = ] [ word? ] bi* and ; + first2 [ char* = ] [ word? ] bi* and ; M: string-type c-type ; @@ -59,37 +59,37 @@ M: string-type c-type-class drop object ; M: string-type c-type-boxed-class drop object ; M: string-type heap-size - drop "void*" heap-size ; + drop void* heap-size ; M: string-type c-type-align - drop "void*" c-type-align ; + drop void* c-type-align ; M: string-type c-type-stack-align? - drop "void*" c-type-stack-align? ; + drop void* c-type-stack-align? ; M: string-type unbox-parameter - drop "void*" unbox-parameter ; + drop void* unbox-parameter ; M: string-type unbox-return - drop "void*" unbox-return ; + drop void* unbox-return ; M: string-type box-parameter - drop "void*" box-parameter ; + drop void* box-parameter ; M: string-type box-return - drop "void*" box-return ; + drop void* box-return ; M: string-type stack-size - drop "void*" stack-size ; + drop void* stack-size ; M: string-type c-type-rep drop int-rep ; M: string-type c-type-boxer - drop "void*" c-type-boxer ; + drop void* c-type-boxer ; M: string-type c-type-unboxer - drop "void*" c-type-unboxer ; + drop void* c-type-unboxer ; M: string-type c-type-boxer-quot second '[ _ alien>string ] ; @@ -103,6 +103,8 @@ M: string-type c-type-getter M: string-type c-type-setter drop [ set-alien-cell ] ; -{ "char*" utf8 } "char*" typedef -"char*" "uchar*" typedef +{ char* utf8 } char* typedef +char* uchar* typedef +char char* "pointer-c-type" set-word-prop +uchar uchar* "pointer-c-type" set-word-prop diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index b177ab35d4..2d53e01f0f 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -5,7 +5,7 @@ namespaces make parser sequences strings words splitting math.parser cpu.architecture alien alien.accessors alien.strings quotations layouts system compiler.units io io.files io.encodings.binary io.streams.memory accessors combinators effects continuations fry -classes vocabs vocabs.loader ; +classes vocabs vocabs.loader vocabs.parser ; IN: alien.c-types DEFER: @@ -40,6 +40,11 @@ global [ ERROR: no-c-type name ; +PREDICATE: c-type-word < word + "c-type" word-prop ; + +UNION: c-type-name string c-type-word ; + : (c-type) ( name -- type/f ) c-types get-global at dup [ dup string? [ (c-type) ] when @@ -48,35 +53,48 @@ ERROR: no-c-type name ; ! C type protocol GENERIC: c-type ( name -- type ) foldable -: resolve-pointer-type ( name -- name ) +: parse-c-type-name ( name -- word/string ) + [ search ] keep or ; + +GENERIC: resolve-pointer-type ( name -- c-type ) + +M: word resolve-pointer-type + dup "pointer-c-type" word-prop + [ ] [ drop void* ] ?if c-type ; +M: string resolve-pointer-type c-types get at dup string? - [ "*" append ] [ drop "void*" ] if + [ "*" append ] [ drop void* ] if c-type ; : resolve-typedef ( name -- type ) - dup string? [ c-type ] when ; + dup c-type-name? [ c-type ] when ; : parse-array-type ( name -- array ) "[" split unclip - [ [ "]" ?tail drop string>number ] map ] dip prefix ; + [ [ "]" ?tail drop string>number ] map ] dip + parse-c-type-name prefix ; + +: parse-c-type ( string -- array ) + { + { [ CHAR: ] over member? ] [ parse-array-type ] } + { [ dup search c-type-word? ] [ parse-c-type-name resolve-typedef ] } + { [ dup c-types get at ] [ dup c-types get at resolve-typedef ] } + { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] } + [ no-c-type ] + } cond ; M: string c-type ( name -- type ) - CHAR: ] over member? [ - parse-array-type - ] [ - dup c-types get at [ - resolve-typedef - ] [ - "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if - ] ?if - ] if ; + parse-c-type ; + +M: word c-type + "c-type" word-prop resolve-typedef ; ! These words being foldable means that words need to be ! recompiled if a C type is redefined. Even so, folding the ! size facilitates some optimizations. GENERIC: heap-size ( type -- size ) foldable -M: string heap-size c-type heap-size ; +M: c-type-name heap-size c-type heap-size ; M: abstract-c-type heap-size size>> ; @@ -92,7 +110,7 @@ GENERIC: c-direct-array-constructor ( c-type -- word ) GENERIC: ( len c-type -- array ) -M: string +M: c-type-name c-array-constructor execute( len -- array ) ; inline GENERIC: (c-array) ( len c-type -- array ) @@ -102,7 +120,7 @@ M: string (c-array) GENERIC: ( alien len c-type -- array ) -M: string +M: c-type-name c-direct-array-constructor execute( alien len -- array ) ; inline : malloc-array ( n type -- alien ) @@ -115,67 +133,67 @@ GENERIC: c-type-class ( name -- class ) M: abstract-c-type c-type-class class>> ; -M: string c-type-class c-type c-type-class ; +M: c-type-name c-type-class c-type c-type-class ; GENERIC: c-type-boxed-class ( name -- class ) M: abstract-c-type c-type-boxed-class boxed-class>> ; -M: string c-type-boxed-class c-type c-type-boxed-class ; +M: c-type-name c-type-boxed-class c-type c-type-boxed-class ; GENERIC: c-type-boxer ( name -- boxer ) M: c-type c-type-boxer boxer>> ; -M: string c-type-boxer c-type c-type-boxer ; +M: c-type-name c-type-boxer c-type c-type-boxer ; GENERIC: c-type-boxer-quot ( name -- quot ) M: abstract-c-type c-type-boxer-quot boxer-quot>> ; -M: string c-type-boxer-quot c-type c-type-boxer-quot ; +M: c-type-name c-type-boxer-quot c-type c-type-boxer-quot ; GENERIC: c-type-unboxer ( name -- boxer ) M: c-type c-type-unboxer unboxer>> ; -M: string c-type-unboxer c-type c-type-unboxer ; +M: c-type-name c-type-unboxer c-type c-type-unboxer ; GENERIC: c-type-unboxer-quot ( name -- quot ) M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ; -M: string c-type-unboxer-quot c-type c-type-unboxer-quot ; +M: c-type-name c-type-unboxer-quot c-type c-type-unboxer-quot ; GENERIC: c-type-rep ( name -- rep ) M: c-type c-type-rep rep>> ; -M: string c-type-rep c-type c-type-rep ; +M: c-type-name c-type-rep c-type c-type-rep ; GENERIC: c-type-getter ( name -- quot ) M: c-type c-type-getter getter>> ; -M: string c-type-getter c-type c-type-getter ; +M: c-type-name c-type-getter c-type c-type-getter ; GENERIC: c-type-setter ( name -- quot ) M: c-type c-type-setter setter>> ; -M: string c-type-setter c-type c-type-setter ; +M: c-type-name c-type-setter c-type c-type-setter ; GENERIC: c-type-align ( name -- n ) M: abstract-c-type c-type-align align>> ; -M: string c-type-align c-type c-type-align ; +M: c-type-name c-type-align c-type c-type-align ; GENERIC: c-type-stack-align? ( name -- ? ) M: c-type c-type-stack-align? stack-align?>> ; -M: string c-type-stack-align? c-type c-type-stack-align? ; +M: c-type-name c-type-stack-align? c-type c-type-stack-align? ; : c-type-box ( n type -- ) [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi @@ -189,29 +207,29 @@ GENERIC: box-parameter ( n ctype -- ) M: c-type box-parameter c-type-box ; -M: string box-parameter c-type box-parameter ; +M: c-type-name box-parameter c-type box-parameter ; GENERIC: box-return ( ctype -- ) M: c-type box-return f swap c-type-box ; -M: string box-return c-type box-return ; +M: c-type-name box-return c-type box-return ; GENERIC: unbox-parameter ( n ctype -- ) M: c-type unbox-parameter c-type-unbox ; -M: string unbox-parameter c-type unbox-parameter ; +M: c-type-name unbox-parameter c-type unbox-parameter ; GENERIC: unbox-return ( ctype -- ) M: c-type unbox-return f swap c-type-unbox ; -M: string unbox-return c-type unbox-return ; +M: c-type-name unbox-return c-type unbox-return ; GENERIC: stack-size ( type -- size ) foldable -M: string stack-size c-type stack-size ; +M: c-type-name stack-size c-type stack-size ; M: c-type stack-size size>> cell align ; @@ -269,7 +287,15 @@ M: memory-stream stream-read \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi* ] [ ] make ; -: typedef ( old new -- ) c-types get set-at ; +GENERIC: typedef ( old new -- ) + +PREDICATE: typedef-word < c-type-word + "c-type" word-prop c-type-name? ; + +M: string typedef ( old new -- ) c-types get set-at ; +M: word typedef ( old new -- ) + [ name>> typedef ] + [ swap "c-type" set-word-prop ] 2bi ; TUPLE: long-long-type < c-type ; @@ -303,8 +329,8 @@ M: long-long-type box-return ( type -- ) : define-primitive-type ( type name -- ) [ typedef ] - [ define-deref ] - [ define-out ] + [ name>> define-deref ] + [ name>> define-out ] tri ; : malloc-file-contents ( path -- alien len ) @@ -313,17 +339,30 @@ M: long-long-type box-return ( type -- ) : if-void ( type true false -- ) pick "void" = [ drop nip call ] [ nip call ] if ; inline +SYMBOLS: + char uchar + short ushort + int uint + long ulong + longlong ulonglong + float double + void* bool ; + CONSTANT: primitive-types { - "char" "uchar" - "short" "ushort" - "int" "uint" - "long" "ulong" - "longlong" "ulonglong" - "float" "double" - "void*" "bool" + char uchar + short ushort + int uint + long ulong + longlong ulonglong + float double + void* bool } +SYMBOLS: + ptrdiff_t intptr_t size_t + char* uchar* ; + [ c-ptr >>class @@ -335,7 +374,7 @@ CONSTANT: primitive-types [ >c-ptr ] >>unboxer-quot "box_alien" >>boxer "alien_offset" >>unboxer - "void*" define-primitive-type + \ void* define-primitive-type integer >>class @@ -346,7 +385,7 @@ CONSTANT: primitive-types 8 >>align "box_signed_8" >>boxer "to_signed_8" >>unboxer - "longlong" define-primitive-type + \ longlong define-primitive-type integer >>class @@ -357,7 +396,7 @@ CONSTANT: primitive-types 8 >>align "box_unsigned_8" >>boxer "to_unsigned_8" >>unboxer - "ulonglong" define-primitive-type + \ ulonglong define-primitive-type integer >>class @@ -368,7 +407,7 @@ CONSTANT: primitive-types bootstrap-cell >>align "box_signed_cell" >>boxer "to_fixnum" >>unboxer - "long" define-primitive-type + \ long define-primitive-type integer >>class @@ -379,7 +418,7 @@ CONSTANT: primitive-types bootstrap-cell >>align "box_unsigned_cell" >>boxer "to_cell" >>unboxer - "ulong" define-primitive-type + \ ulong define-primitive-type integer >>class @@ -390,7 +429,7 @@ CONSTANT: primitive-types 4 >>align "box_signed_4" >>boxer "to_fixnum" >>unboxer - "int" define-primitive-type + \ int define-primitive-type integer >>class @@ -401,7 +440,7 @@ CONSTANT: primitive-types 4 >>align "box_unsigned_4" >>boxer "to_cell" >>unboxer - "uint" define-primitive-type + \ uint define-primitive-type fixnum >>class @@ -412,7 +451,7 @@ CONSTANT: primitive-types 2 >>align "box_signed_2" >>boxer "to_fixnum" >>unboxer - "short" define-primitive-type + \ short define-primitive-type fixnum >>class @@ -423,7 +462,7 @@ CONSTANT: primitive-types 2 >>align "box_unsigned_2" >>boxer "to_cell" >>unboxer - "ushort" define-primitive-type + \ ushort define-primitive-type fixnum >>class @@ -434,7 +473,7 @@ CONSTANT: primitive-types 1 >>align "box_signed_1" >>boxer "to_fixnum" >>unboxer - "char" define-primitive-type + \ char define-primitive-type fixnum >>class @@ -445,7 +484,7 @@ CONSTANT: primitive-types 1 >>align "box_unsigned_1" >>boxer "to_cell" >>unboxer - "uchar" define-primitive-type + \ uchar define-primitive-type [ alien-unsigned-1 c-bool> ] >>getter @@ -454,7 +493,7 @@ CONSTANT: primitive-types 1 >>align "box_boolean" >>boxer "to_boolean" >>unboxer - "bool" define-primitive-type + \ bool define-primitive-type float >>class @@ -467,7 +506,7 @@ CONSTANT: primitive-types "to_float" >>unboxer float-rep >>rep [ >float ] >>unboxer-quot - "float" define-primitive-type + \ float define-primitive-type float >>class @@ -480,10 +519,10 @@ CONSTANT: primitive-types "to_double" >>unboxer double-rep >>rep [ >float ] >>unboxer-quot - "double" define-primitive-type + \ double define-primitive-type - "long" "ptrdiff_t" typedef - "long" "intptr_t" typedef - "ulong" "size_t" typedef + \ long \ ptrdiff_t typedef + \ long \ intptr_t typedef + \ ulong \ size_t typedef ] with-compilation-unit diff --git a/basis/alien/parser/parser.factor b/basis/alien/parser/parser.factor index 19ab08c03c..f855378890 100644 --- a/basis/alien/parser/parser.factor +++ b/basis/alien/parser/parser.factor @@ -5,12 +5,18 @@ parser sequences splitting words fry locals lexer namespaces summary math ; IN: alien.parser +: scan-c-type ( -- c-type ) + scan dup "{" = + [ drop \ } parse-until >array ] + [ parse-c-type ] if ; + : normalize-c-arg ( type name -- type' name' ) [ length ] [ [ CHAR: * = ] trim-head [ length - CHAR: * append ] keep - ] bi ; + ] bi + [ parse-c-type ] dip ; : parse-arglist ( parameters return -- types effect ) [ diff --git a/basis/alien/prettyprint/prettyprint.factor b/basis/alien/prettyprint/prettyprint.factor index 0ffd5023a7..6201f1e245 100644 --- a/basis/alien/prettyprint/prettyprint.factor +++ b/basis/alien/prettyprint/prettyprint.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel combinators alien alien.strings alien.syntax -math.parser prettyprint.backend prettyprint.custom -prettyprint.sections ; +USING: kernel combinators alien alien.strings alien.c-types +alien.syntax math.parser prettyprint.backend prettyprint.custom +prettyprint.sections definitions see see.private ; IN: alien.prettyprint M: alien pprint* @@ -13,3 +13,13 @@ M: alien pprint* } cond ; M: dll pprint* dll-path dup "DLL\" " "\"" pprint-string ; + +M: c-type-word definer drop \ C-TYPE: f ; +M: c-type-word definition drop f ; + +M: typedef-word see-class* + ; diff --git a/basis/alien/structs/structs.factor b/basis/alien/structs/structs.factor index a80adf5137..1558748164 100755 --- a/basis/alien/structs/structs.factor +++ b/basis/alien/structs/structs.factor @@ -13,7 +13,7 @@ M: struct-type c-type ; M: struct-type c-type-stack-align? drop f ; : if-value-struct ( ctype true false -- ) - [ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline + [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline M: struct-type unbox-parameter [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ; diff --git a/basis/alien/syntax/syntax.factor b/basis/alien/syntax/syntax.factor index e8206c6968..040c6b0787 100644 --- a/basis/alien/syntax/syntax.factor +++ b/basis/alien/syntax/syntax.factor @@ -19,18 +19,21 @@ SYNTAX: FUNCTION: (FUNCTION:) define-declared ; SYNTAX: TYPEDEF: - scan scan typedef ; + scan-c-type CREATE typedef ; SYNTAX: C-STRUCT: - scan current-vocab parse-definition define-struct ; deprecated + CREATE current-vocab parse-definition define-struct ; deprecated SYNTAX: C-UNION: - scan parse-definition define-union ; deprecated + CREATE parse-definition define-union ; deprecated SYNTAX: C-ENUM: ";" parse-tokens [ [ create-in ] dip define-constant ] each-index ; +SYNTAX: C-TYPE: + "Primitive C type definition not supported" throw ; + ERROR: no-such-symbol name library ; : address-of ( name library -- value ) From b03eaf3c32885517c23c691353c3bbb2c7a245a1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 15 Sep 2009 15:42:46 -0500 Subject: [PATCH 061/345] math: minor doc fixes --- basis/math/functions/functions-docs.factor | 1 + basis/math/libm/libm-docs.factor | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/basis/math/functions/functions-docs.factor b/basis/math/functions/functions-docs.factor index d61ad9a14a..fb392191d4 100644 --- a/basis/math/functions/functions-docs.factor +++ b/basis/math/functions/functions-docs.factor @@ -48,6 +48,7 @@ ARTICLE: "power-functions" "Powers and logarithms" { $subsection exp } { $subsection cis } { $subsection log } +"Other logarithms:" { $subsection log1+ } { $subsection log10 } "Raising a number to a power:" diff --git a/basis/math/libm/libm-docs.factor b/basis/math/libm/libm-docs.factor index abbb6f1289..64f6026f0b 100644 --- a/basis/math/libm/libm-docs.factor +++ b/basis/math/libm/libm-docs.factor @@ -6,7 +6,7 @@ ARTICLE: "math.libm" "C standard library math functions" { $warning "These functions are unsafe. The compiler special-cases them to operate on floats only. They can be called directly, however there is little reason to do so, since they only implement real-valued functions, and in some cases place restrictions on the domain:" { $example "USE: math.functions" "2.0 acos ." "C{ 0.0 1.316957896924817 }" } -{ $unchecked-example "USE: math.libm" "2 facos ." "0/0." } } +{ $unchecked-example "USE: math.libm" "2.0 facos ." "0/0." } } "Trigonometric functions:" { $subsection fcos } { $subsection fsin } @@ -20,6 +20,7 @@ ARTICLE: "math.libm" "C standard library math functions" "Exponentials and logarithms:" { $subsection fexp } { $subsection flog } +{ $subsection flog10 } "Powers:" { $subsection fpow } { $subsection fsqrt } ; @@ -66,6 +67,10 @@ HELP: flog { $values { "x" real } { "y" real } } { $description "Calls the natural logarithm function from the C standard library. User code should call " { $link log } " instead." } ; +HELP: flog10 +{ $values { "x" real } { "y" real } } +{ $description "Calls the base 10 logarithm function from the C standard library. User code should call " { $link log10 } " instead." } ; + HELP: fpow { $values { "x" real } { "y" real } { "z" real } } { $description "Calls the power function (" { $snippet "z=x^y" } ") from the C standard library. User code should call " { $link ^ } " instead." } ; From 35b76b83afd7f353d22a9ac7dcbe86caca9e276f Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 16:08:42 -0500 Subject: [PATCH 062/345] convert compiler cpu backends to use c-type words --- basis/alien/c-types/c-types.factor | 34 ++++++++++++---------- basis/alien/prettyprint/prettyprint.factor | 12 ++++++-- basis/cpu/ppc/ppc.factor | 2 +- basis/cpu/x86/64/unix/unix.factor | 9 +++--- basis/cpu/x86/64/winnt/winnt.factor | 8 ++--- basis/cpu/x86/x86.factor | 1 + 6 files changed, 38 insertions(+), 28 deletions(-) diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index 2d53e01f0f..553ff26443 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -5,9 +5,19 @@ namespaces make parser sequences strings words splitting math.parser cpu.architecture alien alien.accessors alien.strings quotations layouts system compiler.units io io.files io.encodings.binary io.streams.memory accessors combinators effects continuations fry -classes vocabs vocabs.loader vocabs.parser ; +classes vocabs vocabs.loader vocabs.parser words.symbol ; +QUALIFIED: math IN: alien.c-types +SYMBOLS: + char uchar + short ushort + int uint + long ulong + longlong ulonglong + float double + void* bool ; + DEFER: DEFER: *char @@ -78,7 +88,7 @@ M: string resolve-pointer-type { { [ CHAR: ] over member? ] [ parse-array-type ] } { [ dup search c-type-word? ] [ parse-c-type-name resolve-typedef ] } - { [ dup c-types get at ] [ dup c-types get at resolve-typedef ] } + { [ dup c-types get at ] [ c-types get at resolve-typedef ] } { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] } [ no-c-type ] } cond ; @@ -294,8 +304,9 @@ PREDICATE: typedef-word < c-type-word M: string typedef ( old new -- ) c-types get set-at ; M: word typedef ( old new -- ) + [ nip define-symbol ] [ name>> typedef ] - [ swap "c-type" set-word-prop ] 2bi ; + [ swap "c-type" set-word-prop ] 2tri ; TUPLE: long-long-type < c-type ; @@ -339,15 +350,6 @@ M: long-long-type box-return ( type -- ) : if-void ( type true false -- ) pick "void" = [ drop nip call ] [ nip call ] if ; inline -SYMBOLS: - char uchar - short ushort - int uint - long ulong - longlong ulonglong - float double - void* bool ; - CONSTANT: primitive-types { char uchar @@ -496,8 +498,8 @@ SYMBOLS: \ bool define-primitive-type - float >>class - float >>boxed-class + math:float >>class + math:float >>boxed-class [ alien-float ] >>getter [ [ >float ] 2dip set-alien-float ] >>setter 4 >>size @@ -509,8 +511,8 @@ SYMBOLS: \ float define-primitive-type - float >>class - float >>boxed-class + math:float >>class + math:float >>boxed-class [ alien-double ] >>getter [ [ >float ] 2dip set-alien-double ] >>setter 8 >>size diff --git a/basis/alien/prettyprint/prettyprint.factor b/basis/alien/prettyprint/prettyprint.factor index 6201f1e245..54bb3812a4 100644 --- a/basis/alien/prettyprint/prettyprint.factor +++ b/basis/alien/prettyprint/prettyprint.factor @@ -1,8 +1,9 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel combinators alien alien.strings alien.c-types -alien.syntax math.parser prettyprint.backend prettyprint.custom -prettyprint.sections definitions see see.private ; +alien.syntax arrays math.parser prettyprint.backend +prettyprint.custom prettyprint.sections definitions see see.private +strings words ; IN: alien.prettyprint M: alien pprint* @@ -17,9 +18,14 @@ M: dll pprint* dll-path dup "DLL\" " "\"" pprint-string ; M: c-type-word definer drop \ C-TYPE: f ; M: c-type-word definition drop f ; +GENERIC: pprint-c-type ( c-type -- ) +M: word pprint-c-type pprint-word ; +M: string pprint-c-type text ; +M: array pprint-c-type pprint* ; + M: typedef-word see-class* ; diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index 9c829bc390..f881ff5f91 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -770,5 +770,5 @@ USE: vocabs.loader 4 >>align "box_boolean" >>boxer "to_boolean" >>unboxer - "bool" define-primitive-type + bool define-primitive-type ] with-compilation-unit diff --git a/basis/cpu/x86/64/unix/unix.factor b/basis/cpu/x86/64/unix/unix.factor index e06c026d39..1088f20175 100644 --- a/basis/cpu/x86/64/unix/unix.factor +++ b/basis/cpu/x86/64/unix/unix.factor @@ -14,9 +14,10 @@ M: float-regs param-regs M: x86.64 reserved-area-size 0 ; -! The ABI for passing structs by value is pretty messed up -<< "void*" c-type clone "__stack_value" define-primitive-type -stack-params "__stack_value" c-type (>>rep) >> +SYMBOL: (stack-value) +! The ABI for passing structs by value is pretty great +<< void* c-type clone \ (stack-value) define-primitive-type +stack-params \ (stack-value) c-type (>>rep) >> : struct-types&offset ( struct-type -- pairs ) fields>> [ @@ -36,7 +37,7 @@ stack-params "__stack_value" c-type (>>rep) >> : flatten-large-struct ( c-type -- seq ) heap-size cell align - cell /i "__stack_value" c-type ; + cell /i \ (stack-value) c-type ; M: struct-type flatten-value-type ( type -- seq ) dup heap-size 16 > [ diff --git a/basis/cpu/x86/64/winnt/winnt.factor b/basis/cpu/x86/64/winnt/winnt.factor index d9f83612e6..bbe943e06b 100644 --- a/basis/cpu/x86/64/winnt/winnt.factor +++ b/basis/cpu/x86/64/winnt/winnt.factor @@ -25,8 +25,8 @@ M: x86.64 dummy-fp-params? t ; M: x86.64 temp-reg RAX ; << -"longlong" "ptrdiff_t" typedef -"longlong" "intptr_t" typedef -"int" c-type "long" define-primitive-type -"uint" c-type "ulong" define-primitive-type +longlong ptrdiff_t typedef +longlong intptr_t typedef +int c-type long define-primitive-type +uint c-type ulong define-primitive-type >> diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 27b6667c05..04b5308836 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -12,6 +12,7 @@ compiler.cfg.comparisons compiler.cfg.stack-frame compiler.codegen compiler.codegen.fixup ; +FROM: math => float ; IN: cpu.x86 << enable-fixnum-log2 >> From 9382c674372f55be58cd64ce903c87c3d08d2d4d Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 15 Sep 2009 16:24:58 -0500 Subject: [PATCH 063/345] alien.syntax: improve C-ENUM: docs --- basis/alien/syntax/syntax-docs.factor | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/basis/alien/syntax/syntax-docs.factor b/basis/alien/syntax/syntax-docs.factor index c9190f68c0..e56c83a154 100644 --- a/basis/alien/syntax/syntax-docs.factor +++ b/basis/alien/syntax/syntax-docs.factor @@ -73,10 +73,12 @@ HELP: C-ENUM: { $syntax "C-ENUM: words... ;" } { $values { "words" "a sequence of word names" } } { $description "Creates a sequence of word definitions in the current vocabulary. Each word pushes an integer according to its index in the enumeration definition. The first word pushes 0." } -{ $notes "This word emulates a C-style " { $snippet "enum" } " in Factor. While this feature can be used for any purpose, using integer constants is discouraged unless it is for interfacing with C libraries. Factor code should use symbolic constants instead." } +{ $notes "This word emulates a C-style " { $snippet "enum" } " in Factor. While this feature can be used for any purpose, using integer constants is discouraged unless it is for interfacing with C libraries. Factor code should use " { $link "words.symbol" } " or " { $link "singletons" } " instead." } { $examples - "The following two lines are equivalent:" - { $code "C-ENUM: red green blue ;" ": red 0 ; : green 1 ; : blue 2 ;" } + "Here is an example enumeration definition:" + { $code "C-ENUM: red green blue ;" } + "It is equivalent to the following series of definitions:" + { $code "CONSTANT: red 0" "CONSTANT: green 1" "CONSTANT: blue 2" } } ; HELP: &: From 172219e93105f0e7bffa8c70334cc7209b816b0c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 15 Sep 2009 14:30:20 -0700 Subject: [PATCH 064/345] compiler.tree.propagation: fix unary-op type functions with complex number inputs --- .../compiler/tree/propagation/known-words/known-words.factor | 5 ++++- basis/compiler/tree/propagation/propagation-tests.factor | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/basis/compiler/tree/propagation/known-words/known-words.factor b/basis/compiler/tree/propagation/known-words/known-words.factor index 63d2df543d..621b8d082b 100644 --- a/basis/compiler/tree/propagation/known-words/known-words.factor +++ b/basis/compiler/tree/propagation/known-words/known-words.factor @@ -81,7 +81,10 @@ IN: compiler.tree.propagation.known-words class>> dup null-class? [ drop null ] [ math-closure ] if ; : unary-op-interval ( info quot -- newinterval ) - [ interval>> ] dip call ; inline + [ + dup class>> real classes-intersect? + [ interval>> ] [ drop full-interval ] if + ] dip call ; inline : unary-op ( word interval-quot post-proc-quot -- ) '[ diff --git a/basis/compiler/tree/propagation/propagation-tests.factor b/basis/compiler/tree/propagation/propagation-tests.factor index ec5fbd95cd..0c220542ca 100644 --- a/basis/compiler/tree/propagation/propagation-tests.factor +++ b/basis/compiler/tree/propagation/propagation-tests.factor @@ -186,6 +186,10 @@ IN: compiler.tree.propagation.tests [ t ] [ [ { complex } declare absq ] final-info first interval>> [0,inf] = ] unit-test +[ t ] [ [ { float float } declare rect> C{ 0.0 0.0 } + absq ] final-info first interval>> [0,inf] = ] unit-test + +[ V{ float } ] [ [ { float float } declare rect> C{ 0.0 0.0 } + absq ] final-classes ] unit-test + [ t ] [ [ [ - absq ] [ + ] 2map-reduce ] final-info first interval>> [0,inf] = ] unit-test [ t ] [ [ { double-array double-array } declare [ - absq ] [ + ] 2map-reduce ] final-info first interval>> [0,inf] = ] unit-test From 096c7e7a47ffc41b6bf5c309120f334cfdad7972 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 15 Sep 2009 14:48:56 -0700 Subject: [PATCH 065/345] help.html: don't strip out vocabs without roots since that gets rid of private vocabs --- basis/help/html/html-tests.factor | 8 +++++++- basis/help/html/html.factor | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/basis/help/html/html-tests.factor b/basis/help/html/html-tests.factor index 90ff6c110f..b4e6103868 100644 --- a/basis/help/html/html-tests.factor +++ b/basis/help/html/html-tests.factor @@ -1,6 +1,12 @@ -USING: help.html tools.test help.topics kernel ; +USING: help.html tools.test help.topics kernel sequences vocabs ; IN: help.html.tests [ ] [ "xml" >link help>html drop ] unit-test [ "article-foobar.html" ] [ "foobar" >link topic>filename ] unit-test + +[ t ] [ all-vocabs-really [ vocab-spec? ] all? ] unit-test + +[ t ] [ all-vocabs-really [ vocab-name "sequences.private" = ] any? ] unit-test + +[ f ] [ all-vocabs-really [ vocab-name "scratchpad" = ] any? ] unit-test diff --git a/basis/help/html/html.factor b/basis/help/html/html.factor index e8cc7e04c5..948b52a345 100644 --- a/basis/help/html/html.factor +++ b/basis/help/html/html.factor @@ -73,7 +73,8 @@ M: topic url-of topic>filename ; dup topic>filename utf8 [ help>html write-xml ] with-file-writer ; : all-vocabs-really ( -- seq ) - all-vocabs-recursive >hashtable f over delete-at no-roots remove-redundant-prefixes ; + all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes + [ vocab-name "scratchpad" = not ] filter ; : all-topics ( -- topics ) [ From 4d16c569f0002574bb9deb467726f2e27229a62c Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 17:38:49 -0500 Subject: [PATCH 066/345] struct classes now make their own C type without help from alien.structs. remove alien.structs dependencies from everywhere outside of alien and compiler, and have the FFI handle both alien.structs and classes.struct c-types --- basis/alien/arrays/arrays.factor | 13 +-- basis/alien/c-types/c-types.factor | 18 ++++ basis/alien/complex/complex.factor | 2 +- basis/alien/complex/functor/functor.factor | 2 +- basis/alien/fortran/fortran-docs.factor | 9 +- basis/alien/fortran/fortran-tests.factor | 33 +------ basis/alien/fortran/fortran.factor | 10 +- basis/alien/structs/structs.factor | 4 +- basis/classes/struct/struct-tests.factor | 17 ++-- basis/classes/struct/struct.factor | 107 ++++++++++++--------- basis/compiler/alien/alien.factor | 2 +- basis/compiler/cfg/builder/builder.factor | 4 +- basis/compiler/codegen/codegen.factor | 9 +- basis/cpu/x86/64/64.factor | 2 +- basis/cpu/x86/64/unix/unix.factor | 11 ++- extra/gpu/render/render.factor | 2 +- 16 files changed, 126 insertions(+), 119 deletions(-) diff --git a/basis/alien/arrays/arrays.factor b/basis/alien/arrays/arrays.factor index 64827ec139..a69f7609b1 100755 --- a/basis/alien/arrays/arrays.factor +++ b/basis/alien/arrays/arrays.factor @@ -1,11 +1,11 @@ ! Copyright (C) 2008, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.strings alien.c-types alien.accessors alien.structs +USING: alien alien.strings alien.c-types alien.accessors arrays words sequences math kernel namespaces fry libc cpu.architecture io.encodings.utf8 accessors ; IN: alien.arrays -UNION: value-type array struct-type ; +INSTANCE: array value-type M: array c-type ; @@ -40,15 +40,6 @@ M: array c-type-boxer-quot M: array c-type-unboxer-quot drop [ >c-ptr ] ; -M: value-type c-type-rep drop int-rep ; - -M: value-type c-type-getter - drop [ swap ] ; - -M: value-type c-type-setter ( type -- quot ) - [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri - '[ @ swap @ _ memcpy ] ; - PREDICATE: string-type < pair first2 [ "char*" = ] [ word? ] bi* and ; diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index b177ab35d4..35a9627d50 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -71,6 +71,13 @@ M: string c-type ( name -- type ) ] ?if ] if ; +GENERIC: c-struct? ( type -- ? ) + +M: object c-struct? + drop f ; +M: string c-struct? + dup "void" = [ drop f ] [ c-type c-struct? ] if ; + ! These words being foldable means that words need to be ! recompiled if a C type is redefined. Even so, folding the ! size facilitates some optimizations. @@ -215,6 +222,17 @@ M: string stack-size c-type stack-size ; M: c-type stack-size size>> cell align ; +MIXIN: value-type + +M: value-type c-type-rep drop int-rep ; + +M: value-type c-type-getter + drop [ swap ] ; + +M: value-type c-type-setter ( type -- quot ) + [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri + '[ @ swap @ _ memcpy ] ; + GENERIC: byte-length ( seq -- n ) flushable M: byte-array byte-length length ; inline diff --git a/basis/alien/complex/complex.factor b/basis/alien/complex/complex.factor index b0229358d1..65c4095e25 100644 --- a/basis/alien/complex/complex.factor +++ b/basis/alien/complex/complex.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types alien.structs alien.complex.functor accessors +USING: alien.c-types alien.complex.functor accessors sequences kernel ; IN: alien.complex diff --git a/basis/alien/complex/functor/functor.factor b/basis/alien/complex/functor/functor.factor index b1f9c2be85..1faa64be61 100644 --- a/basis/alien/complex/functor/functor.factor +++ b/basis/alien/complex/functor/functor.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien alien.structs alien.c-types classes.struct math +USING: accessors alien alien.c-types classes.struct math math.functions sequences arrays kernel functors vocabs.parser namespaces quotations ; IN: alien.complex.functor diff --git a/basis/alien/fortran/fortran-docs.factor b/basis/alien/fortran/fortran-docs.factor index 8027020c75..7778500bf1 100644 --- a/basis/alien/fortran/fortran-docs.factor +++ b/basis/alien/fortran/fortran-docs.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Joe Groff ! See http://factorcode.org/license.txt for BSD license. -USING: help.markup help.syntax kernel quotations sequences strings words.symbol ; +USING: help.markup help.syntax kernel quotations sequences strings words.symbol classes.struct ; QUALIFIED-WITH: alien.syntax c IN: alien.fortran @@ -25,7 +25,7 @@ ARTICLE: "alien.fortran-types" "Fortran types" { { $snippet "DOUBLE-COMPLEX" } " specifies a double-precision floating-point complex value. The alias " { $snippet "COMPLEX*16" } " is also recognized." } { { $snippet "CHARACTER(n)" } " specifies a character string of length " { $snippet "n" } ". The Fortran 77 syntax " { $snippet "CHARACTER*n" } " is also recognized." } { "Fortran arrays can be specified by suffixing a comma-separated set of dimensions in parentheses, e.g. " { $snippet "REAL(2,3,4)" } ". Arrays of unspecified length can be specified using " { $snippet "*" } " as a dimension. Arrays are passed in as flat " { $link "specialized-arrays" } "." } - { "Fortran records defined by " { $link POSTPONE: RECORD: } " and C structs defined by " { $link POSTPONE: c:C-STRUCT: } " are also supported as parameter and return types." } + { "Struct classes defined by " { $link POSTPONE: STRUCT: } " are also supported as parameter and return types." } } "When declaring the parameters of Fortran functions, an output argument can be specified by prefixing an exclamation point to the type name. This will cause the function word to leave the final value of the parameter on the stack." ; @@ -42,10 +42,6 @@ HELP: LIBRARY: { $values { "name" "a logical library name" } } { $description "Sets the logical library for subsequent " { $link POSTPONE: FUNCTION: } " and " { $link POSTPONE: SUBROUTINE: } " definitions. The given library name must have been opened with a previous call to " { $link add-fortran-library } "." } ; -HELP: RECORD: -{ $syntax "RECORD: NAME { \"TYPE\" \"SLOT\" } ... ;" } -{ $description "Defines a Fortran record type with the given slots. The record is defined as the corresponding C struct and can be used as a type for subsequent Fortran or C function declarations." } ; - HELP: add-fortran-library { $values { "name" string } { "soname" string } { "fortran-abi" symbol } } { $description "Opens the shared library in the file specified by " { $snippet "soname" } " under the logical name " { $snippet "name" } " so that it may be used in subsequent " { $link POSTPONE: LIBRARY: } " and " { $link fortran-invoke } " calls. Functions and subroutines from the library will be defined using the specified " { $snippet "fortran-abi" } ", which must be one of the supported " { $link "alien.fortran-abis" } "." } @@ -66,7 +62,6 @@ ARTICLE: "alien.fortran" "Fortran FFI" { $subsection POSTPONE: LIBRARY: } { $subsection POSTPONE: FUNCTION: } { $subsection POSTPONE: SUBROUTINE: } -{ $subsection POSTPONE: RECORD: } { $subsection fortran-invoke } ; diff --git a/basis/alien/fortran/fortran-tests.factor b/basis/alien/fortran/fortran-tests.factor index 177d1077c2..9d893b95c4 100644 --- a/basis/alien/fortran/fortran-tests.factor +++ b/basis/alien/fortran/fortran-tests.factor @@ -1,6 +1,6 @@ ! (c) 2009 Joe Groff, see BSD license USING: accessors alien alien.c-types alien.complex -alien.fortran alien.fortran.private alien.strings alien.structs +alien.fortran alien.fortran.private alien.strings classes.struct arrays assocs byte-arrays combinators fry generalizations io.encodings.ascii kernel macros macros.expander namespaces sequences shuffle tools.test ; @@ -8,10 +8,10 @@ IN: alien.fortran.tests << intel-unix-abi "(alien.fortran-tests)" (add-fortran-library) >> LIBRARY: (alien.fortran-tests) -RECORD: FORTRAN_TEST_RECORD - { "INTEGER" "FOO" } - { "REAL(2)" "BAR" } - { "CHARACTER*4" "BAS" } ; +STRUCT: FORTRAN_TEST_RECORD + { FOO int } + { BAR double[2] } + { BAS char[4] } ; intel-unix-abi fortran-abi [ @@ -168,29 +168,6 @@ intel-unix-abi fortran-abi [ [ "complex" { "character*17" "character" "integer" } fortran-sig>c-sig ] unit-test - ! fortran-record>c-struct - - [ { - { "double" "ex" } - { "float" "wye" } - { "int" "zee" } - { "char[20]" "woo" } - } ] [ - { - { "DOUBLE-PRECISION" "EX" } - { "REAL" "WYE" } - { "INTEGER" "ZEE" } - { "CHARACTER(20)" "WOO" } - } fortran-record>c-struct - ] unit-test - - ! RECORD: - - [ 16 ] [ "fortran_test_record" heap-size ] unit-test - [ 0 ] [ "foo" "fortran_test_record" offset-of ] unit-test - [ 4 ] [ "bar" "fortran_test_record" offset-of ] unit-test - [ 12 ] [ "bas" "fortran_test_record" offset-of ] unit-test - ! (fortran-invoke) [ [ diff --git a/basis/alien/fortran/fortran.factor b/basis/alien/fortran/fortran.factor index 013c4d6f6a..52d69fd193 100644 --- a/basis/alien/fortran/fortran.factor +++ b/basis/alien/fortran/fortran.factor @@ -1,6 +1,6 @@ ! (c) 2009 Joe Groff, see BSD license USING: accessors alien alien.c-types alien.complex alien.parser -alien.strings alien.structs alien.syntax arrays ascii assocs +alien.strings alien.syntax arrays ascii assocs byte-arrays combinators combinators.short-circuit fry generalizations kernel lexer macros math math.parser namespaces parser sequences splitting stack-checker vectors vocabs.parser words locals @@ -415,14 +415,6 @@ PRIVATE> : fortran-sig>c-sig ( fortran-return fortran-args -- c-return c-args ) [ fortran-ret-type>c-type ] [ fortran-arg-types>c-types ] bi* append ; -: fortran-record>c-struct ( record -- struct ) - [ first2 [ fortran-type>c-type ] [ >lower ] bi* 2array ] map ; - -: define-fortran-record ( name vocab fields -- ) - [ >lower ] [ ] [ fortran-record>c-struct ] tri* define-struct ; - -SYNTAX: RECORD: scan current-vocab parse-definition define-fortran-record ; - : set-fortran-abi ( library -- ) library-fortran-abis get-global at fortran-abi set ; diff --git a/basis/alien/structs/structs.factor b/basis/alien/structs/structs.factor index a80adf5137..80837e9a01 100755 --- a/basis/alien/structs/structs.factor +++ b/basis/alien/structs/structs.factor @@ -8,6 +8,8 @@ IN: alien.structs TUPLE: struct-type < abstract-c-type fields return-in-registers? ; +INSTANCE: struct-type value-type + M: struct-type c-type ; M: struct-type c-type-stack-align? drop f ; @@ -33,7 +35,7 @@ M: struct-type box-return M: struct-type stack-size [ heap-size ] [ stack-size ] if-value-struct ; -: c-struct? ( type -- ? ) (c-type) struct-type? ; +M: struct-type c-struct? drop t ; : (define-struct) ( name size align fields class -- ) [ [ align ] keep ] 2dip new diff --git a/basis/classes/struct/struct-tests.factor b/basis/classes/struct/struct-tests.factor index 8508230bb2..62fce7f353 100755 --- a/basis/classes/struct/struct-tests.factor +++ b/basis/classes/struct/struct-tests.factor @@ -1,5 +1,5 @@ ! (c)Joe Groff bsd license -USING: accessors alien alien.c-types alien.structs.fields ascii +USING: accessors alien alien.c-types ascii assocs byte-arrays classes.struct classes.tuple.private combinators compiler.tree.debugger compiler.units destructors io.encodings.utf8 io.pathnames io.streams.string kernel libc @@ -196,41 +196,46 @@ UNION-STRUCT: struct-test-float-and-bits [ [ struct-test-float-and-bits see ] with-string-writer ] unit-test [ { - T{ field-spec + T{ struct-slot-spec { name "x" } { offset 0 } + { class fixnum } { type "char" } { reader x>> } { writer (>>x) } } - T{ field-spec + T{ struct-slot-spec { name "y" } { offset 4 } + { class $[ cell 8 = fixnum integer ? ] } { type "int" } { reader y>> } { writer (>>y) } } - T{ field-spec + T{ struct-slot-spec { name "z" } { offset 8 } { type "bool" } + { class boolean } { reader z>> } { writer (>>z) } } } ] [ "struct-test-foo" c-type fields>> ] unit-test [ { - T{ field-spec + T{ struct-slot-spec { name "f" } { offset 0 } { type "float" } + { class float } { reader f>> } { writer (>>f) } } - T{ field-spec + T{ struct-slot-spec { name "bits" } { offset 0 } { type "uint" } + { class $[ cell 8 = fixnum integer ? ] } { reader bits>> } { writer (>>bits) } } diff --git a/basis/classes/struct/struct.factor b/basis/classes/struct/struct.factor index 893bc5a257..1de221d2aa 100755 --- a/basis/classes/struct/struct.factor +++ b/basis/classes/struct/struct.factor @@ -1,14 +1,12 @@ ! (c)Joe Groff bsd license -USING: accessors alien alien.c-types alien.structs -alien.structs.fields arrays byte-arrays classes classes.parser -classes.tuple classes.tuple.parser classes.tuple.private -combinators combinators.short-circuit combinators.smart -definitions functors.backend fry generalizations generic.parser -kernel kernel.private lexer libc locals macros make math -math.order parser quotations sequences slots slots.private -specialized-arrays vectors words summary namespaces assocs -compiler.tree.propagation.transforms ; -FROM: slots => reader-word writer-word ; +USING: accessors alien alien.c-types arrays byte-arrays classes +classes.parser classes.tuple classes.tuple.parser +classes.tuple.private combinators combinators.short-circuit +combinators.smart cpu.architecture definitions functors.backend +fry generalizations generic.parser kernel kernel.private lexer +libc locals macros make math math.order parser quotations +sequences slots slots.private specialized-arrays vectors words +summary namespaces assocs ; IN: classes.struct SPECIALIZED-ARRAY: uchar @@ -22,7 +20,7 @@ TUPLE: struct { (underlying) c-ptr read-only } ; TUPLE: struct-slot-spec < slot-spec - c-type ; + type ; PREDICATE: struct-class < tuple-class superclass \ struct eq? ; @@ -86,11 +84,11 @@ MACRO: ( class -- quot: ( ... -- struct ) ) [ struct-slots [ initial>> ] map over length tail append ] keep ; : (reader-quot) ( slot -- quot ) - [ c-type>> c-type-getter-boxer ] + [ type>> c-type-getter-boxer ] [ offset>> [ >c-ptr ] swap suffix ] bi prepend ; : (writer-quot) ( slot -- quot ) - [ c-type>> c-setter ] + [ type>> c-setter ] [ offset>> [ >c-ptr ] swap suffix ] bi prepend ; : (boxer-quot) ( class -- quot ) @@ -117,6 +115,39 @@ M: struct-class writer-quot ! c-types +TUPLE: struct-c-type < abstract-c-type + fields + return-in-registers? ; + +INSTANCE: struct-c-type value-type + +M: struct-c-type c-type ; + +M: struct-c-type c-type-stack-align? drop f ; + +: if-value-struct ( ctype true false -- ) + [ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline + +M: struct-c-type unbox-parameter + [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ; + +M: struct-c-type box-parameter + [ %box-large-struct ] [ box-parameter ] if-value-struct ; + +: if-small-struct ( c-type true false -- ? ) + [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline + +M: struct-c-type unbox-return + [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ; + +M: struct-c-type box-return + [ %box-small-struct ] [ %box-large-struct ] if-small-struct ; + +M: struct-c-type stack-size + [ heap-size ] [ stack-size ] if-value-struct ; + +M: struct-c-type c-struct? drop t ; + struct [ ] 3sequence ] bi define-inline-method ; -: slot>field ( slot -- field ) - field-spec new swap { - [ name>> >>name ] - [ offset>> >>offset ] - [ c-type>> >>type ] - [ name>> reader-word >>reader ] - [ name>> writer-word >>writer ] +: c-type-for-class ( class -- c-type ) + struct-c-type new swap { + [ drop byte-array >>class ] + [ >>boxed-class ] + [ struct-slots >>fields ] + [ "struct-size" word-prop >>size ] + [ "struct-align" word-prop >>align ] + [ (unboxer-quot) >>unboxer-quot ] + [ (boxer-quot) >>boxer-quot ] } cleave ; - -: define-struct-for-class ( class -- ) - [ - { - [ name>> ] - [ "struct-size" word-prop ] - [ "struct-align" word-prop ] - [ struct-slots [ slot>field ] map ] - } cleave - struct-type (define-struct) - ] [ - { - [ name>> c-type ] - [ (unboxer-quot) >>unboxer-quot ] - [ (boxer-quot) >>boxer-quot ] - [ >>boxed-class ] - } cleave drop - ] bi ; - + : align-offset ( offset class -- offset' ) c-type-align align ; : struct-offsets ( slots -- size ) 0 [ - [ c-type>> align-offset ] keep - [ (>>offset) ] [ c-type>> heap-size + ] 2bi + [ type>> align-offset ] keep + [ (>>offset) ] [ type>> heap-size + ] 2bi ] reduce ; : union-struct-offsets ( slots -- size ) - [ 0 >>offset c-type>> heap-size ] [ max ] map-reduce ; + [ 0 >>offset type>> heap-size ] [ max ] map-reduce ; : struct-align ( slots -- align ) - [ c-type>> c-type-align ] [ max ] map-reduce ; + [ type>> c-type-align ] [ max ] map-reduce ; PRIVATE> M: struct-class c-type name>> c-type ; @@ -228,7 +243,7 @@ M: struct byte-length class "struct-size" word-prop ; foldable [ (struct-methods) ] tri ; : check-struct-slots ( slots -- ) - [ c-type>> c-type drop ] each ; + [ type>> c-type drop ] each ; : redefine-struct-tuple-class ( class -- ) [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ; @@ -244,7 +259,7 @@ M: struct byte-length class "struct-size" word-prop ; foldable [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri (struct-word-props) ] - [ drop define-struct-for-class ] 2tri ; inline + [ drop [ c-type-for-class ] [ name>> ] bi typedef ] 2tri ; inline PRIVATE> : define-struct-class ( class slots -- ) @@ -265,7 +280,7 @@ ERROR: invalid-struct-slot token ; : ( name c-type attributes -- slot-spec ) [ struct-slot-spec new ] 3dip [ >>name ] - [ [ >>c-type ] [ struct-slot-class >>class ] bi ] + [ [ >>type ] [ struct-slot-class >>class ] bi ] [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ; >rep) >> heap-size cell align cell /i "__stack_value" c-type ; -M: struct-type flatten-value-type ( type -- seq ) +: flatten-struct ( c-type -- seq ) dup heap-size 16 > [ flatten-large-struct ] [ flatten-small-struct ] if ; +M: alien.structs:struct-type flatten-value-type ( type -- seq ) + flatten-struct ; +M: classes.struct:struct-c-type flatten-value-type ( type -- seq ) + flatten-struct ; + M: x86.64 return-struct-in-registers? ( c-type -- ? ) heap-size 2 cells <= ; diff --git a/extra/gpu/render/render.factor b/extra/gpu/render/render.factor index c0dca56551..0ee9ab78c5 100644 --- a/extra/gpu/render/render.factor +++ b/extra/gpu/render/render.factor @@ -1,5 +1,5 @@ ! (c)2009 Joe Groff bsd license -USING: accessors alien alien.c-types alien.structs arrays +USING: accessors alien alien.c-types arrays assocs classes classes.mixin classes.parser classes.singleton classes.tuple classes.tuple.private combinators combinators.tuple destructors fry generic generic.parser gpu gpu.buffers gpu.framebuffers From 765aa5bc1df00207ca20b6995f7d0f54ba3ac8c3 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 15 Sep 2009 15:45:03 -0700 Subject: [PATCH 067/345] db: fix some typos in docs --- basis/db/db-docs.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/db/db-docs.factor b/basis/db/db-docs.factor index a8b03398c7..e73783fdfc 100644 --- a/basis/db/db-docs.factor +++ b/basis/db/db-docs.factor @@ -252,14 +252,14 @@ ARTICLE: "db-lowlevel-tutorial" "Low-level database tutorial" "Here's an example usage where we'll make a book table, insert some objects, and query them." $nl "First, let's set up a custom combinator for using our database. See " { $link "db-custom-database-combinators" } " for more details." { $code <" -USING: db.sqlite db io.files ; +USING: db.sqlite db io.files io.files.temp ; : with-book-db ( quot -- ) "book.db" temp-file swap with-db ; inline"> } "Now let's create the table manually:" { $code <" "create table books (id integer primary key, title text, author text, date_published timestamp, edition integer, cover_price double, condition text)" - [ sql-command ] with-book-db" "> } + [ sql-command ] with-book-db"> } "Time to insert some books:" { $code <" "insert into books From 1478785b188c2d53630f18611ffaa9e797936e4b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 15 Sep 2009 16:36:59 -0700 Subject: [PATCH 068/345] irc.client: fix suggested by ceninan --- extra/irc/client/client.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/irc/client/client.factor b/extra/irc/client/client.factor index ae48d3ac4e..3f1dba353c 100755 --- a/extra/irc/client/client.factor +++ b/extra/irc/client/client.factor @@ -9,7 +9,7 @@ IN: irc.client [ (connect-irc) (do-login) spawn-irc ] with-irc ; : attach-chat ( irc-chat irc-client -- ) [ (attach-chat) ] with-irc ; -: detach-chat ( irc-chat -- ) dup [ client>> remove-chat ] with-irc ; +: detach-chat ( irc-chat -- ) dup client>> [ remove-chat ] with-irc ; : speak ( message irc-chat -- ) dup client>> [ (speak) ] with-irc ; : hear ( irc-chat -- message ) in-messages>> mailbox-get ; : terminate-irc ( irc-client -- ) [ (terminate-irc) ] with-irc ; From cc680ae69834e5e6bfb0e8684f0d43ef3e6ecb3d Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 18:56:17 -0500 Subject: [PATCH 069/345] Revert "make struct classes print properly in the status bar" This reverts commit 4ee1a4f9e8590eb8846343dc5d30d9f5e3aaee26. --- .../struct/prettyprint/prettyprint.factor | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/basis/classes/struct/prettyprint/prettyprint.factor b/basis/classes/struct/prettyprint/prettyprint.factor index 8bda9dc5f9..e88834530c 100644 --- a/basis/classes/struct/prettyprint/prettyprint.factor +++ b/basis/classes/struct/prettyprint/prettyprint.factor @@ -1,13 +1,18 @@ ! (c)Joe Groff bsd license USING: accessors alien alien.c-types arrays assocs classes classes.struct combinators combinators.short-circuit continuations -definitions fry kernel libc make math math.parser mirrors -prettyprint.backend prettyprint.custom prettyprint.sections -see see.private sequences slots strings summary words ; +fry kernel libc make math math.parser mirrors prettyprint.backend +prettyprint.custom prettyprint.sections see.private sequences +slots strings summary words ; IN: classes.struct.prettyprint = + [ second offset>> 0 = \ UNION-STRUCT: \ STRUCT: ? ] + [ drop \ STRUCT: ] if ; + : struct>assoc ( struct -- assoc ) [ class struct-slots ] [ struct-slot-values ] bi zip ; @@ -34,14 +39,8 @@ IN: classes.struct.prettyprint PRIVATE> -M: struct-class definer - struct-slots dup length 2 >= - [ second offset>> 0 = \ UNION-STRUCT: \ STRUCT: ? ] - [ drop \ STRUCT: ] if - \ ; ; - M: struct-class see-class* - pprint-; block> ; From 3c541f736f3af3b1bc2c4b8b00fec93ca058a457 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 19:10:05 -0500 Subject: [PATCH 070/345] fix failing classes.struct tests --- .../struct/prettyprint/prettyprint.factor | 4 ++-- basis/classes/struct/struct-tests.factor | 23 ++++++++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/basis/classes/struct/prettyprint/prettyprint.factor b/basis/classes/struct/prettyprint/prettyprint.factor index e88834530c..2c969531e8 100644 --- a/basis/classes/struct/prettyprint/prettyprint.factor +++ b/basis/classes/struct/prettyprint/prettyprint.factor @@ -20,7 +20,7 @@ IN: classes.struct.prettyprint > text ] - [ c-type>> dup string? [ text ] [ pprint* ] if ] + [ type>> dup string? [ text ] [ pprint* ] if ] [ read-only>> [ \ read-only pprint-word ] when ] [ initial>> [ \ initial: pprint-word pprint* ] when* ] } cleave block> @@ -111,7 +111,7 @@ M: struct-mirror >alist ( mirror -- alist ) ] [ '[ _ struct>assoc - [ [ [ name>> ] [ c-type>> ] bi 2array ] dip ] assoc-map + [ [ [ name>> ] [ type>> ] bi 2array ] dip ] assoc-map ] [ drop { } ] recover ] bi append ; diff --git a/basis/classes/struct/struct-tests.factor b/basis/classes/struct/struct-tests.factor index 62fce7f353..bbbaf4f1d5 100755 --- a/basis/classes/struct/struct-tests.factor +++ b/basis/classes/struct/struct-tests.factor @@ -5,7 +5,7 @@ combinators compiler.tree.debugger compiler.units destructors io.encodings.utf8 io.pathnames io.streams.string kernel libc literals math mirrors multiline namespaces prettyprint prettyprint.config see sequences specialized-arrays system -tools.test parser lexer eval ; +tools.test parser lexer eval layouts ; SPECIALIZED-ARRAY: char SPECIALIZED-ARRAY: int SPECIALIZED-ARRAY: ushort @@ -199,26 +199,23 @@ UNION-STRUCT: struct-test-float-and-bits T{ struct-slot-spec { name "x" } { offset 0 } + { initial 0 } { class fixnum } { type "char" } - { reader x>> } - { writer (>>x) } } T{ struct-slot-spec { name "y" } { offset 4 } - { class $[ cell 8 = fixnum integer ? ] } + { initial 123 } + { class integer } { type "int" } - { reader y>> } - { writer (>>y) } } T{ struct-slot-spec { name "z" } { offset 8 } + { initial f } { type "bool" } - { class boolean } - { reader z>> } - { writer (>>z) } + { class object } } } ] [ "struct-test-foo" c-type fields>> ] unit-test @@ -228,16 +225,14 @@ UNION-STRUCT: struct-test-float-and-bits { offset 0 } { type "float" } { class float } - { reader f>> } - { writer (>>f) } + { initial 0.0 } } T{ struct-slot-spec { name "bits" } { offset 0 } { type "uint" } - { class $[ cell 8 = fixnum integer ? ] } - { reader bits>> } - { writer (>>bits) } + { class integer } + { initial 0 } } } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test From 3b4330fcf64b2b76fa08c646f3b5d6db6fd51166 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 21:43:18 -0500 Subject: [PATCH 071/345] get things to a point where they bootstrap again --- basis/alien/c-types/c-types.factor | 49 ++++++++----------- basis/alien/parser/parser.factor | 19 +++++-- basis/alien/syntax/syntax.factor | 4 +- basis/classes/struct/struct-tests.factor | 20 ++++---- basis/classes/struct/struct.factor | 27 ++-------- basis/compiler/codegen/codegen.factor | 2 +- basis/core-foundation/numbers/numbers.factor | 1 + basis/cpu/x86/features/features.factor | 4 +- basis/functors/functors-tests.factor | 15 +++--- basis/math/libm/libm.factor | 40 +++++++-------- .../specialized-arrays.factor | 28 +++++++---- basis/stack-checker/alien/alien.factor | 2 +- basis/windows/com/syntax/syntax.factor | 2 +- 13 files changed, 107 insertions(+), 106 deletions(-) diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index 71073ddc91..123abb5298 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -5,7 +5,7 @@ namespaces make parser sequences strings words splitting math.parser cpu.architecture alien alien.accessors alien.strings quotations layouts system compiler.units io io.files io.encodings.binary io.streams.memory accessors combinators effects continuations fry -classes vocabs vocabs.loader vocabs.parser words.symbol ; +classes vocabs vocabs.loader words.symbol ; QUALIFIED: math IN: alien.c-types @@ -16,7 +16,8 @@ SYMBOLS: long ulong longlong ulonglong float double - void* bool ; + void* bool + void ; DEFER: DEFER: *char @@ -55,56 +56,48 @@ PREDICATE: c-type-word < word UNION: c-type-name string c-type-word ; -: (c-type) ( name -- type/f ) - c-types get-global at dup [ - dup string? [ (c-type) ] when - ] when ; - ! C type protocol GENERIC: c-type ( name -- type ) foldable -: parse-c-type-name ( name -- word/string ) - [ search ] keep or ; - GENERIC: resolve-pointer-type ( name -- c-type ) M: word resolve-pointer-type dup "pointer-c-type" word-prop - [ ] [ drop void* ] ?if c-type ; + [ ] [ drop void* ] ?if ; M: string resolve-pointer-type c-types get at dup string? - [ "*" append ] [ drop void* ] if - c-type ; + [ "*" append ] [ drop void* ] if ; : resolve-typedef ( name -- type ) dup c-type-name? [ c-type ] when ; -: parse-array-type ( name -- array ) +: parse-array-type ( name -- dims type ) "[" split unclip - [ [ "]" ?tail drop string>number ] map ] dip - parse-c-type-name prefix ; - -: parse-c-type ( string -- array ) - { - { [ CHAR: ] over member? ] [ parse-array-type ] } - { [ dup search c-type-word? ] [ parse-c-type-name resolve-typedef ] } - { [ dup c-types get at ] [ c-types get at resolve-typedef ] } - { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] } - [ no-c-type ] - } cond ; + [ [ "]" ?tail drop string>number ] map ] dip ; M: string c-type ( name -- type ) - parse-c-type ; + CHAR: ] over member? [ + parse-array-type prefix + ] [ + dup c-types get at [ + resolve-typedef + ] [ + "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if + ] ?if + ] if ; M: word c-type "c-type" word-prop resolve-typedef ; +: void? ( c-type -- ? ) + { void "void" } member? ; + GENERIC: c-struct? ( type -- ? ) M: object c-struct? drop f ; M: string c-struct? - dup "void" = [ drop f ] [ c-type c-struct? ] if ; + dup void? [ drop f ] [ c-type c-struct? ] if ; ! These words being foldable means that words need to be ! recompiled if a C type is redefined. Even so, folding the @@ -366,7 +359,7 @@ M: long-long-type box-return ( type -- ) binary file-contents [ malloc-byte-array ] [ length ] bi ; : if-void ( type true false -- ) - pick "void" = [ drop nip call ] [ nip call ] if ; inline + pick void? [ drop nip call ] [ nip call ] if ; inline CONSTANT: primitive-types { diff --git a/basis/alien/parser/parser.factor b/basis/alien/parser/parser.factor index f855378890..bca7c93802 100644 --- a/basis/alien/parser/parser.factor +++ b/basis/alien/parser/parser.factor @@ -1,10 +1,23 @@ ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types arrays assocs effects grouping kernel -parser sequences splitting words fry locals lexer namespaces -summary math ; +USING: alien alien.c-types arrays assocs combinators effects +grouping kernel parser sequences splitting words fry locals +lexer namespaces summary math vocabs.parser ; IN: alien.parser +: parse-c-type-name ( name -- word/string ) + [ search ] keep or ; + +: parse-c-type ( string -- array ) + { + { [ dup "void" = ] [ drop void ] } + { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] } + { [ dup search c-type-word? ] [ parse-c-type-name ] } + { [ dup c-types get at ] [ ] } + { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] } + [ no-c-type ] + } cond ; + : scan-c-type ( -- c-type ) scan dup "{" = [ drop \ } parse-until >array ] diff --git a/basis/alien/syntax/syntax.factor b/basis/alien/syntax/syntax.factor index 040c6b0787..fac45176a3 100644 --- a/basis/alien/syntax/syntax.factor +++ b/basis/alien/syntax/syntax.factor @@ -22,10 +22,10 @@ SYNTAX: TYPEDEF: scan-c-type CREATE typedef ; SYNTAX: C-STRUCT: - CREATE current-vocab parse-definition define-struct ; deprecated + scan current-vocab parse-definition define-struct ; deprecated SYNTAX: C-UNION: - CREATE parse-definition define-union ; deprecated + scan parse-definition define-union ; deprecated SYNTAX: C-ENUM: ";" parse-tokens diff --git a/basis/classes/struct/struct-tests.factor b/basis/classes/struct/struct-tests.factor index bbbaf4f1d5..3be0be8ef1 100755 --- a/basis/classes/struct/struct-tests.factor +++ b/basis/classes/struct/struct-tests.factor @@ -6,6 +6,8 @@ io.encodings.utf8 io.pathnames io.streams.string kernel libc literals math mirrors multiline namespaces prettyprint prettyprint.config see sequences specialized-arrays system tools.test parser lexer eval layouts ; +FROM: math => float +QUALIFIED-WITH: alien.c-types c SPECIALIZED-ARRAY: char SPECIALIZED-ARRAY: int SPECIALIZED-ARRAY: ushort @@ -128,7 +130,7 @@ STRUCT: struct-test-bar ] unit-test UNION-STRUCT: struct-test-float-and-bits - { f float } + { f c:float } { bits uint } ; [ 1.0 ] [ struct-test-float-and-bits 1.0 float>bits >>bits f>> ] unit-test @@ -181,14 +183,14 @@ STRUCT: struct-test-string-ptr ] with-scope ] unit-test -[ <" USING: classes.struct ; +[ <" USING: alien.c-types classes.struct ; IN: classes.struct.tests STRUCT: struct-test-foo { x char initial: 0 } { y int initial: 123 } { z bool } ; "> ] [ [ struct-test-foo see ] with-string-writer ] unit-test -[ <" USING: classes.struct ; +[ <" USING: alien.c-types classes.struct ; IN: classes.struct.tests UNION-STRUCT: struct-test-float-and-bits { f float initial: 0.0 } { bits uint initial: 0 } ; @@ -201,20 +203,20 @@ UNION-STRUCT: struct-test-float-and-bits { offset 0 } { initial 0 } { class fixnum } - { type "char" } + { type char } } T{ struct-slot-spec { name "y" } { offset 4 } { initial 123 } { class integer } - { type "int" } + { type int } } T{ struct-slot-spec { name "z" } { offset 8 } { initial f } - { type "bool" } + { type bool } { class object } } } ] [ "struct-test-foo" c-type fields>> ] unit-test @@ -223,14 +225,14 @@ UNION-STRUCT: struct-test-float-and-bits T{ struct-slot-spec { name "f" } { offset 0 } - { type "float" } + { type c:float } { class float } { initial 0.0 } } T{ struct-slot-spec { name "bits" } { offset 0 } - { type "uint" } + { type uint } { class integer } { initial 0 } } @@ -277,7 +279,7 @@ STRUCT: struct-test-array-slots ] unit-test STRUCT: struct-test-optimization - { x { "int" 3 } } { y int } ; + { x { int 3 } } { y int } ; SPECIALIZED-ARRAY: struct-test-optimization diff --git a/basis/classes/struct/struct.factor b/basis/classes/struct/struct.factor index 1de221d2aa..a96a74d2ac 100755 --- a/basis/classes/struct/struct.factor +++ b/basis/classes/struct/struct.factor @@ -1,12 +1,12 @@ ! (c)Joe Groff bsd license -USING: accessors alien alien.c-types arrays byte-arrays classes -classes.parser classes.tuple classes.tuple.parser +USING: accessors alien alien.c-types alien.parser arrays +byte-arrays classes classes.parser classes.tuple classes.tuple.parser classes.tuple.private combinators combinators.short-circuit combinators.smart cpu.architecture definitions functors.backend fry generalizations generic.parser kernel kernel.private lexer libc locals macros make math math.order parser quotations sequences slots slots.private specialized-arrays vectors words -summary namespaces assocs ; +summary namespaces assocs vocabs.parser ; IN: classes.struct SPECIALIZED-ARRAY: uchar @@ -197,20 +197,6 @@ M: struct-c-type c-struct? drop t ; [ type>> c-type-align ] [ max ] map-reduce ; PRIVATE> -M: struct-class c-type name>> c-type ; - -M: struct-class c-type-align c-type c-type-align ; - -M: struct-class c-type-getter c-type c-type-getter ; - -M: struct-class c-type-setter c-type c-type-setter ; - -M: struct-class c-type-boxer-quot c-type c-type-boxer-quot ; - -M: struct-class c-type-unboxer-quot c-type c-type-boxer-quot ; - -M: struct-class heap-size c-type heap-size ; - M: struct byte-length class "struct-size" word-prop ; foldable ! class definition @@ -259,7 +245,7 @@ M: struct byte-length class "struct-size" word-prop ; foldable [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri (struct-word-props) ] - [ drop [ c-type-for-class ] [ name>> ] bi typedef ] 2tri ; inline + [ drop [ c-type-for-class ] keep typedef ] 2tri ; inline PRIVATE> : define-struct-class ( class slots -- ) @@ -284,9 +270,6 @@ ERROR: invalid-struct-slot token ; [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ; array ] when ; - : parse-struct-slot ( -- slot ) scan scan-c-type \ } parse-until ; @@ -317,7 +300,7 @@ SYNTAX: S@ array ] [ >string-param ] if ; + scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ; : parse-struct-slot` ( accum -- accum ) scan-string-param scan-c-type` \ } parse-until diff --git a/basis/compiler/codegen/codegen.factor b/basis/compiler/codegen/codegen.factor index 0456ff485f..ddf5aa0e02 100755 --- a/basis/compiler/codegen/codegen.factor +++ b/basis/compiler/codegen/codegen.factor @@ -456,7 +456,7 @@ TUPLE: callback-context ; : callback-return-quot ( ctype -- quot ) return>> { - { [ dup "void" = ] [ drop [ ] ] } + { [ dup void? ] [ drop [ ] ] } { [ dup large-struct? ] [ heap-size '[ _ memcpy ] ] } [ c-type c-type-unboxer-quot ] } cond ; diff --git a/basis/core-foundation/numbers/numbers.factor b/basis/core-foundation/numbers/numbers.factor index f01f522d61..ae061cb4eb 100644 --- a/basis/core-foundation/numbers/numbers.factor +++ b/basis/core-foundation/numbers/numbers.factor @@ -1,6 +1,7 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: alien.c-types alien.syntax kernel math core-foundation ; +FROM: math => float ; IN: core-foundation.numbers TYPEDEF: void* CFNumberRef diff --git a/basis/cpu/x86/features/features.factor b/basis/cpu/x86/features/features.factor index 02235bb62e..c5cf2d470a 100644 --- a/basis/cpu/x86/features/features.factor +++ b/basis/cpu/x86/features/features.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: system kernel math math.order math.parser namespaces -alien.syntax combinators locals init io cpu.x86 compiler -compiler.units accessors ; +alien.c-types alien.syntax combinators locals init io cpu.x86 +compiler compiler.units accessors ; IN: cpu.x86.features > ] when ; + : specialized-array-vocab ( c-type -- vocab ) "specialized-arrays.instances." prepend ; @@ -125,26 +133,26 @@ PRIVATE> ] ?if ; inline : define-array-vocab ( type -- vocab ) - underlying-type + underlying-type-name [ specialized-array-vocab ] [ '[ _ define-array ] ] bi generate-vocab ; -M: string require-c-array define-array-vocab drop ; +M: c-type-name require-c-array define-array-vocab drop ; ERROR: specialized-array-vocab-not-loaded c-type ; -M: string c-array-constructor - underlying-type +M: c-type-name c-array-constructor + underlying-type-name dup [ "<" "-array>" surround ] [ specialized-array-vocab ] bi lookup [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable -M: string c-(array)-constructor - underlying-type +M: c-type-name c-(array)-constructor + underlying-type-name dup [ "(" "-array)" surround ] [ specialized-array-vocab ] bi lookup [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable -M: string c-direct-array-constructor - underlying-type +M: c-type-name c-direct-array-constructor + underlying-type-name dup [ "" surround ] [ specialized-array-vocab ] bi lookup [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable diff --git a/basis/stack-checker/alien/alien.factor b/basis/stack-checker/alien/alien.factor index da559abd78..3d150adf91 100644 --- a/basis/stack-checker/alien/alien.factor +++ b/basis/stack-checker/alien/alien.factor @@ -19,7 +19,7 @@ TUPLE: alien-callback-params < alien-node-params quot xt ; : alien-stack ( params extra -- ) over parameters>> length + consume-d >>in-d - dup return>> "void" = 0 1 ? produce-d >>out-d + dup return>> void? 0 1 ? produce-d >>out-d drop ; : return-prep-quot ( node -- quot ) diff --git a/basis/windows/com/syntax/syntax.factor b/basis/windows/com/syntax/syntax.factor index 2100d6a215..3cf8b55e39 100755 --- a/basis/windows/com/syntax/syntax.factor +++ b/basis/windows/com/syntax/syntax.factor @@ -67,7 +67,7 @@ unless : (stack-effect-from-return-and-parameters) ( return parameters -- stack-effect ) swap [ [ second ] map ] - [ dup "void" = [ drop { } ] [ 1array ] if ] bi* + [ dup void? [ drop { } ] [ 1array ] if ] bi* ; : (define-word-for-function) ( function interface n -- ) From 26026ff6de7d5d1a0dd985a198c75dcc449d50f2 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 22:10:16 -0500 Subject: [PATCH 072/345] fix bug in pointer type parsing --- basis/alien/c-types/c-types.factor | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index 123abb5298..02ab2dcafa 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -79,11 +79,9 @@ M: string c-type ( name -- type ) CHAR: ] over member? [ parse-array-type prefix ] [ - dup c-types get at [ - resolve-typedef - ] [ + dup c-types get at [ ] [ "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if - ] ?if + ] ?if resolve-typedef ] if ; M: word c-type From b629391477ccaa8fb97a63bfea70ff35d8e67045 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 22:10:41 -0500 Subject: [PATCH 073/345] fix typedef prettyprinting --- basis/alien/prettyprint/prettyprint.factor | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/basis/alien/prettyprint/prettyprint.factor b/basis/alien/prettyprint/prettyprint.factor index 54bb3812a4..4b53f36c3b 100644 --- a/basis/alien/prettyprint/prettyprint.factor +++ b/basis/alien/prettyprint/prettyprint.factor @@ -17,15 +17,20 @@ M: dll pprint* dll-path dup "DLL\" " "\"" pprint-string ; M: c-type-word definer drop \ C-TYPE: f ; M: c-type-word definition drop f ; +M: typedef-word declarations. drop ; GENERIC: pprint-c-type ( c-type -- ) M: word pprint-c-type pprint-word ; M: string pprint-c-type text ; M: array pprint-c-type pprint* ; -M: typedef-word see-class* +M: typedef-word definer drop \ TYPEDEF: f ; + +M: typedef-word synopsis* ; + + From 2bbd29a5613d1887d3e7522faa07212f19a6734a Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 22:39:25 -0500 Subject: [PATCH 074/345] prettyprinting for FUNCTION: definitions --- basis/alien/parser/parser.factor | 10 +++++--- basis/alien/prettyprint/prettyprint.factor | 28 ++++++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/basis/alien/parser/parser.factor b/basis/alien/parser/parser.factor index bca7c93802..662139810e 100644 --- a/basis/alien/parser/parser.factor +++ b/basis/alien/parser/parser.factor @@ -1,8 +1,9 @@ ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types arrays assocs combinators effects -grouping kernel parser sequences splitting words fry locals -lexer namespaces summary math vocabs.parser ; +USING: accessors alien alien.c-types arrays assocs +combinators effects grouping kernel parser sequences +splitting words fry locals lexer namespaces summary +math vocabs.parser ; IN: alien.parser : parse-c-type-name ( name -- word/string ) @@ -55,3 +56,6 @@ IN: alien.parser : define-function ( return library function parameters -- ) make-function define-declared ; + +PREDICATE: alien-function-word < word + def>> [ length 5 = ] [ last \ alien-invoke eq? ] bi and ; diff --git a/basis/alien/prettyprint/prettyprint.factor b/basis/alien/prettyprint/prettyprint.factor index 4b53f36c3b..096a5547c5 100644 --- a/basis/alien/prettyprint/prettyprint.factor +++ b/basis/alien/prettyprint/prettyprint.factor @@ -1,9 +1,9 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel combinators alien alien.strings alien.c-types -alien.syntax arrays math.parser prettyprint.backend -prettyprint.custom prettyprint.sections definitions see see.private -strings words ; +USING: accessors kernel combinators alien alien.strings alien.c-types +alien.parser alien.syntax arrays assocs effects math.parser +prettyprint.backend prettyprint.custom prettyprint.sections +definitions see see.private sequences strings words ; IN: alien.prettyprint M: alien pprint* @@ -21,16 +21,30 @@ M: typedef-word declarations. drop ; GENERIC: pprint-c-type ( c-type -- ) M: word pprint-c-type pprint-word ; +M: wrapper pprint-c-type wrapped>> pprint-word ; M: string pprint-c-type text ; M: array pprint-c-type pprint* ; M: typedef-word definer drop \ TYPEDEF: f ; M: typedef-word synopsis* - ; + pprint-word ; +: pprint-function-arg ( type name -- ) + [ pprint-c-type ] [ text ] bi* ; +: pprint-function-args ( word -- ) + [ def>> fourth ] [ stack-effect in>> ] bi zip unclip-last + [ [ first2 "," append pprint-function-arg ] each ] dip + first2 pprint-function-arg ; + +M: alien-function-word definer + drop \ FUNCTION: \ ; ; +M: alien-function-word definition drop f ; +M: alien-function-word synopsis* + \ FUNCTION: pprint-word + [ def>> first pprint-c-type ] + [ pprint-word ] + [ ] tri ; From 93b12d3ef414041c9c62a01c469f6e0888816099 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 22:43:11 -0500 Subject: [PATCH 075/345] update classes.struct tests to use word c-types --- basis/classes/struct/struct-tests.factor | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/basis/classes/struct/struct-tests.factor b/basis/classes/struct/struct-tests.factor index 3be0be8ef1..e9e45487f9 100755 --- a/basis/classes/struct/struct-tests.factor +++ b/basis/classes/struct/struct-tests.factor @@ -6,7 +6,7 @@ io.encodings.utf8 io.pathnames io.streams.string kernel libc literals math mirrors multiline namespaces prettyprint prettyprint.config see sequences specialized-arrays system tools.test parser lexer eval layouts ; -FROM: math => float +FROM: math => float ; QUALIFIED-WITH: alien.c-types c SPECIALIZED-ARRAY: char SPECIALIZED-ARRAY: int @@ -48,9 +48,9 @@ STRUCT: struct-test-bar [ { { "underlying" B{ 98 0 0 98 127 0 0 127 0 0 0 0 } } - { { "x" "char" } 98 } - { { "y" "int" } HEX: 7F00007F } - { { "z" "bool" } f } + { { "x" char } 98 } + { { "y" int } HEX: 7F00007F } + { { "z" bool } f } } ] [ B{ 98 0 0 98 127 0 0 127 0 0 0 0 } struct-test-foo memory>struct make-mirror >alist From 6dc6886bd92c93b94c8ba74db0360a4afb612e5f Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 15 Sep 2009 22:58:07 -0500 Subject: [PATCH 076/345] typedefs share their original type's pointer definition --- basis/alien/c-types/c-types-tests.factor | 2 +- basis/alien/c-types/c-types.factor | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/basis/alien/c-types/c-types-tests.factor b/basis/alien/c-types/c-types-tests.factor index bfeff5f1de..792e7d416a 100644 --- a/basis/alien/c-types/c-types-tests.factor +++ b/basis/alien/c-types/c-types-tests.factor @@ -43,7 +43,7 @@ TYPEDEF: int* MyIntArray TYPEDEF: uchar* MyLPBYTE -[ t ] [ { "char*" utf8 } c-type "MyLPBYTE" c-type = ] unit-test +[ t ] [ { char* utf8 } c-type "MyLPBYTE" c-type = ] unit-test [ 0 B{ 1 2 3 4 } diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index 02ab2dcafa..7dc00333b8 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -65,8 +65,8 @@ M: word resolve-pointer-type dup "pointer-c-type" word-prop [ ] [ drop void* ] ?if ; M: string resolve-pointer-type - c-types get at dup string? - [ "*" append ] [ drop void* ] if ; + c-types get at dup c-type-name? + [ resolve-pointer-type ] [ drop void* ] if ; : resolve-typedef ( name -- type ) dup c-type-name? [ c-type ] when ; @@ -313,9 +313,17 @@ PREDICATE: typedef-word < c-type-word M: string typedef ( old new -- ) c-types get set-at ; M: word typedef ( old new -- ) - [ nip define-symbol ] - [ name>> typedef ] - [ swap "c-type" set-word-prop ] 2tri ; + { + [ nip define-symbol ] + [ name>> typedef ] + [ swap "c-type" set-word-prop ] + [ + swap dup word? [ + "pointer-c-type" word-prop + "pointer-c-type" set-word-prop + ] [ 2drop ] if + ] + } 2cleave ; TUPLE: long-long-type < c-type ; From 1b349f6be7284999786a94c3fc3fe6f916969b3b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:03 +0100 Subject: [PATCH 077/345] Added TOOLCHAIN_PREFIX var to makefile --- Makefile | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 18cb7d15c7..bfaa6ec7a3 100755 --- a/Makefile +++ b/Makefile @@ -164,17 +164,17 @@ macosx.app: factor Factor.app/Contents/MacOS/factor $(EXECUTABLE): $(DLL_OBJS) $(EXE_OBJS) - $(LINKER) $(ENGINE) $(DLL_OBJS) - $(CPP) $(LIBS) $(LIBPATH) -L. $(LINK_WITH_ENGINE) \ + $(TOOLCHAIN_PREFIX)$(LINKER) $(ENGINE) $(DLL_OBJS) + $(TOOLCHAIN_PREFIX)$(CPP) $(LIBS) $(LIBPATH) -L. $(LINK_WITH_ENGINE) \ $(CFLAGS) -o $@$(EXE_SUFFIX)$(EXE_EXTENSION) $(EXE_OBJS) $(CONSOLE_EXECUTABLE): $(DLL_OBJS) $(EXE_OBJS) - $(LINKER) $(ENGINE) $(DLL_OBJS) - $(CPP) $(LIBS) $(LIBPATH) -L. $(LINK_WITH_ENGINE) \ + $(TOOLCHAIN_PREFIX)$(LINKER) $(ENGINE) $(DLL_OBJS) + $(TOOLCHAIN_PREFIX)$(CPP) $(LIBS) $(LIBPATH) -L. $(LINK_WITH_ENGINE) \ $(CFLAGS) $(CFLAGS_CONSOLE) -o factor$(EXE_SUFFIX)$(CONSOLE_EXTENSION) $(EXE_OBJS) $(TEST_LIBRARY): vm/ffi_test.o - $(CC) $(LIBPATH) $(CFLAGS) $(FFI_TEST_CFLAGS) $(SHARED_FLAG) -o libfactor-ffi-test$(SHARED_DLL_EXTENSION) $(TEST_OBJS) + $(TOOLCHAIN_PREFIX)$(CC) $(LIBPATH) $(CFLAGS) $(FFI_TEST_CFLAGS) $(SHARED_FLAG) -o libfactor-ffi-test$(SHARED_DLL_EXTENSION) $(TEST_OBJS) clean: rm -f vm/*.o @@ -187,22 +187,22 @@ tags: etags vm/*.{cpp,hpp,mm,S,c} vm/resources.o: - $(WINDRES) vm/factor.rs vm/resources.o + $(TOOLCHAIN_PREFIX)$(WINDRES) vm/factor.rs vm/resources.o vm/ffi_test.o: vm/ffi_test.c - $(CC) -c $(CFLAGS) $(FFI_TEST_CFLAGS) -o $@ $< + $(TOOLCHAIN_PREFIX)$(CC) -c $(CFLAGS) $(FFI_TEST_CFLAGS) -o $@ $< .c.o: - $(CC) -c $(CFLAGS) -o $@ $< + $(TOOLCHAIN_PREFIX)$(CC) -c $(CFLAGS) -o $@ $< .cpp.o: - $(CPP) -c $(CFLAGS) -o $@ $< + $(TOOLCHAIN_PREFIX)$(CPP) -c $(CFLAGS) -o $@ $< .S.o: - $(CC) -x assembler-with-cpp -c $(CFLAGS) -o $@ $< + $(TOOLCHAIN_PREFIX)$(CC) -x assembler-with-cpp -c $(CFLAGS) -o $@ $< .mm.o: - $(CPP) -c $(CFLAGS) -o $@ $< + $(TOOLCHAIN_PREFIX)$(CPP) -c $(CFLAGS) -o $@ $< .PHONY: factor tags clean From caefc7aff9e19833dd130e0594a18a2ac2c3c505 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:04 +0100 Subject: [PATCH 078/345] Empty vm struct --- vm/master.hpp | 2 ++ vm/vm.hpp | 9 +++++++++ 2 files changed, 11 insertions(+) mode change 100644 => 100755 vm/master.hpp create mode 100644 vm/vm.hpp diff --git a/vm/master.hpp b/vm/master.hpp old mode 100644 new mode 100755 index 9d84c8b75c..c1b62d41ff --- a/vm/master.hpp +++ b/vm/master.hpp @@ -74,4 +74,6 @@ #include "factor.hpp" #include "utilities.hpp" +#include "vm.hpp" + #endif /* __FACTOR_MASTER_H__ */ diff --git a/vm/vm.hpp b/vm/vm.hpp new file mode 100644 index 0000000000..6c76cdddfa --- /dev/null +++ b/vm/vm.hpp @@ -0,0 +1,9 @@ +namespace factor +{ + +struct factorvm { +}; + +extern factorvm *vm; + +} From d8ea82d8e8cfe8967d095ee3c8dd65365d67e1c0 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:04 +0100 Subject: [PATCH 079/345] added stub PRIMITIVE_GETVM macro --- vm/primitives.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/primitives.hpp b/vm/primitives.hpp index c520a67cc5..f534a24791 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -5,5 +5,5 @@ extern "C" typedef void (*primitive_type)(); extern const primitive_type primitives[]; #define PRIMITIVE(name) extern "C" void primitive_##name() - +#define PRIMITIVE_GETVM() vm-> } From adf252945168021c41d22d91543de603134ee877 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:04 +0100 Subject: [PATCH 080/345] added vm singleton --- vm/factor.cpp | 3 +++ 1 file changed, 3 insertions(+) mode change 100644 => 100755 vm/factor.cpp diff --git a/vm/factor.cpp b/vm/factor.cpp old mode 100644 new mode 100755 index 33d8b73dfe..5dd88402bc --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -3,6 +3,8 @@ namespace factor { +factorvm *vm; + VM_C_API void default_parameters(vm_parameters *p) { p->image_path = NULL; @@ -95,6 +97,7 @@ static void do_stage1_init() VM_C_API void init_factor(vm_parameters *p) { + vm = new factorvm; /* Kilobytes */ p->ds_size = align_page(p->ds_size << 10); p->rs_size = align_page(p->rs_size << 10); From 88084a66ac79641a88509e80fccd277ca71b3e57 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:04 +0100 Subject: [PATCH 081/345] moved contexts functions into vm --- vm/contexts.cpp | 112 +++++++++++++++++++++++++++++++++++++++------- vm/primitives.hpp | 2 +- vm/vm.hpp | 20 +++++++++ 3 files changed, 117 insertions(+), 17 deletions(-) diff --git a/vm/contexts.cpp b/vm/contexts.cpp index b0a27ef18f..86156de3b5 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -8,27 +8,42 @@ namespace factor cell ds_size, rs_size; context *unused_contexts; -void reset_datastack() +void factorvm::reset_datastack() { ds = ds_bot - sizeof(cell); } -void reset_retainstack() +void reset_datastack() +{ + return vm->reset_datastack(); +} + +void factorvm::reset_retainstack() { rs = rs_bot - sizeof(cell); } +void reset_retainstack() +{ + return vm->reset_retainstack(); +} + static const cell stack_reserved = (64 * sizeof(cell)); -void fix_stacks() +void factorvm::fix_stacks() { if(ds + sizeof(cell) < ds_bot || ds + stack_reserved >= ds_top) reset_datastack(); if(rs + sizeof(cell) < rs_bot || rs + stack_reserved >= rs_top) reset_retainstack(); } +void fix_stacks() +{ + return vm->fix_stacks(); +} + /* called before entry into foreign C code. Note that ds and rs might be stored in registers, so callbacks must save and restore the correct values */ -void save_stacks() +void factorvm::save_stacks() { if(stack_chain) { @@ -37,7 +52,12 @@ void save_stacks() } } -context *alloc_context() +void save_stacks() +{ + return vm->save_stacks(); +} + +context *factorvm::alloc_context() { context *new_context; @@ -56,14 +76,24 @@ context *alloc_context() return new_context; } -void dealloc_context(context *old_context) +context *alloc_context() +{ + return vm->alloc_context(); +} + +void factorvm::dealloc_context(context *old_context) { old_context->next = unused_contexts; unused_contexts = old_context; } +void dealloc_context(context *old_context) +{ + return vm->dealloc_context(old_context); +} + /* called on entry into a compiled callback */ -void nest_stacks() +void factorvm::nest_stacks() { context *new_context = alloc_context(); @@ -94,8 +124,13 @@ void nest_stacks() reset_retainstack(); } +void nest_stacks() +{ + return vm->nest_stacks(); +} + /* called when leaving a compiled callback */ -void unnest_stacks() +void factorvm::unnest_stacks() { ds = stack_chain->datastack_save; rs = stack_chain->retainstack_save; @@ -109,8 +144,13 @@ void unnest_stacks() dealloc_context(old_stacks); } +void unnest_stacks() +{ + return vm->unnest_stacks(); +} + /* called on startup */ -void init_stacks(cell ds_size_, cell rs_size_) +void factorvm::init_stacks(cell ds_size_, cell rs_size_) { ds_size = ds_size_; rs_size = rs_size_; @@ -118,7 +158,12 @@ void init_stacks(cell ds_size_, cell rs_size_) unused_contexts = NULL; } -bool stack_to_array(cell bottom, cell top) +void init_stacks(cell ds_size_, cell rs_size_) +{ + return vm->init_stacks(ds_size_,rs_size_); +} + +bool factorvm::stack_to_array(cell bottom, cell top) { fixnum depth = (fixnum)(top - bottom + sizeof(cell)); @@ -133,38 +178,68 @@ bool stack_to_array(cell bottom, cell top) } } -PRIMITIVE(datastack) +bool stack_to_array(cell bottom, cell top) +{ + return vm->stack_to_array(bottom,top); +} + +inline void factorvm::vmprim_datastack() { if(!stack_to_array(ds_bot,ds)) general_error(ERROR_DS_UNDERFLOW,F,F,NULL); } -PRIMITIVE(retainstack) +PRIMITIVE(datastack) +{ + PRIMITIVE_GETVM()->vmprim_datastack(); +} + +inline void factorvm::vmprim_retainstack() { if(!stack_to_array(rs_bot,rs)) general_error(ERROR_RS_UNDERFLOW,F,F,NULL); } +PRIMITIVE(retainstack) +{ + PRIMITIVE_GETVM()->vmprim_retainstack(); +} + /* returns pointer to top of stack */ -cell array_to_stack(array *array, cell bottom) +cell factorvm::array_to_stack(array *array, cell bottom) { cell depth = array_capacity(array) * sizeof(cell); memcpy((void*)bottom,array + 1,depth); return bottom + depth - sizeof(cell); } -PRIMITIVE(set_datastack) +cell array_to_stack(array *array, cell bottom) +{ + return vm->array_to_stack(array,bottom); +} + +inline void factorvm::vmprim_set_datastack() { ds = array_to_stack(untag_check(dpop()),ds_bot); } -PRIMITIVE(set_retainstack) +PRIMITIVE(set_datastack) +{ + PRIMITIVE_GETVM()->vmprim_set_datastack(); +} + +inline void factorvm::vmprim_set_retainstack() { rs = array_to_stack(untag_check(dpop()),rs_bot); } +PRIMITIVE(set_retainstack) +{ + PRIMITIVE_GETVM()->vmprim_set_retainstack(); +} + /* Used to implement call( */ -PRIMITIVE(check_datastack) +inline void factorvm::vmprim_check_datastack() { fixnum out = to_fixnum(dpop()); fixnum in = to_fixnum(dpop()); @@ -189,4 +264,9 @@ PRIMITIVE(check_datastack) } } +PRIMITIVE(check_datastack) +{ + PRIMITIVE_GETVM()->vmprim_check_datastack(); +} + } diff --git a/vm/primitives.hpp b/vm/primitives.hpp index f534a24791..c7534ec1cc 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -5,5 +5,5 @@ extern "C" typedef void (*primitive_type)(); extern const primitive_type primitives[]; #define PRIMITIVE(name) extern "C" void primitive_##name() -#define PRIMITIVE_GETVM() vm-> +#define PRIMITIVE_GETVM() vm } diff --git a/vm/vm.hpp b/vm/vm.hpp index 6c76cdddfa..73bcffc764 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -2,6 +2,26 @@ namespace factor { struct factorvm { + + // contexts + void reset_datastack(); + void reset_retainstack(); + void fix_stacks(); + void save_stacks(); + context *alloc_context(); + void dealloc_context(context *old_context); + void nest_stacks(); + void unnest_stacks(); + void init_stacks(cell ds_size_, cell rs_size_); + bool stack_to_array(cell bottom, cell top); + cell array_to_stack(array *array, cell bottom); + inline void vmprim_datastack(); + inline void vmprim_retainstack(); + inline void vmprim_set_datastack(); + inline void vmprim_set_retainstack(); + inline void vmprim_check_datastack(); + // next method here: + }; extern factorvm *vm; From 110f9252458508f6b484b387c7c220106acd916f Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:04 +0100 Subject: [PATCH 082/345] move functions from run.cpp into vm --- vm/run.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 11 ++++++++++ 2 files changed, 65 insertions(+), 9 deletions(-) mode change 100644 => 100755 vm/run.cpp diff --git a/vm/run.cpp b/vm/run.cpp old mode 100644 new mode 100755 index c6a4bad695..0a14d4f017 --- a/vm/run.cpp +++ b/vm/run.cpp @@ -7,35 +7,60 @@ namespace factor cell T; -PRIMITIVE(getenv) +inline void factorvm::vmprim_getenv() { fixnum e = untag_fixnum(dpeek()); drepl(userenv[e]); } -PRIMITIVE(setenv) +PRIMITIVE(getenv) +{ + PRIMITIVE_GETVM()->vmprim_getenv(); +} + +inline void factorvm::vmprim_setenv() { fixnum e = untag_fixnum(dpop()); cell value = dpop(); userenv[e] = value; } -PRIMITIVE(exit) +PRIMITIVE(setenv) +{ + PRIMITIVE_GETVM()->vmprim_setenv(); +} + +inline void factorvm::vmprim_exit() { exit(to_fixnum(dpop())); } -PRIMITIVE(micros) +PRIMITIVE(exit) +{ + PRIMITIVE_GETVM()->vmprim_exit(); +} + +inline void factorvm::vmprim_micros() { box_unsigned_8(current_micros()); } -PRIMITIVE(sleep) +PRIMITIVE(micros) +{ + PRIMITIVE_GETVM()->vmprim_micros(); +} + +inline void factorvm::vmprim_sleep() { sleep_micros(to_cell(dpop())); } -PRIMITIVE(set_slot) +PRIMITIVE(sleep) +{ + PRIMITIVE_GETVM()->vmprim_sleep(); +} + +inline void factorvm::vmprim_set_slot() { fixnum slot = untag_fixnum(dpop()); object *obj = untag(dpop()); @@ -45,7 +70,12 @@ PRIMITIVE(set_slot) write_barrier(obj); } -PRIMITIVE(load_locals) +PRIMITIVE(set_slot) +{ + PRIMITIVE_GETVM()->vmprim_set_slot(); +} + +inline void factorvm::vmprim_load_locals() { fixnum count = untag_fixnum(dpop()); memcpy((cell *)(rs + sizeof(cell)),(cell *)(ds - sizeof(cell) * (count - 1)),sizeof(cell) * count); @@ -53,7 +83,12 @@ PRIMITIVE(load_locals) rs += sizeof(cell) * count; } -static cell clone_object(cell obj_) +PRIMITIVE(load_locals) +{ + PRIMITIVE_GETVM()->vmprim_load_locals(); +} + +cell factorvm::clone_object(cell obj_) { gc_root obj(obj_); @@ -68,9 +103,19 @@ static cell clone_object(cell obj_) } } -PRIMITIVE(clone) +cell clone_object(cell obj_) +{ + return vm->clone_object(obj_); +} + +inline void factorvm::vmprim_clone() { drepl(clone_object(dpeek())); } +PRIMITIVE(clone) +{ + PRIMITIVE_GETVM()->vmprim_clone(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 73bcffc764..7533d748b0 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -20,6 +20,17 @@ struct factorvm { inline void vmprim_set_datastack(); inline void vmprim_set_retainstack(); inline void vmprim_check_datastack(); + + // run + inline void vmprim_getenv(); + inline void vmprim_setenv(); + inline void vmprim_exit(); + inline void vmprim_micros(); + inline void vmprim_sleep(); + inline void vmprim_set_slot(); + inline void vmprim_load_locals(); + cell clone_object(cell obj_); + inline void vmprim_clone(); // next method here: }; From aa01f6b74828dced81e2eb0971038d70babe1261 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:04 +0100 Subject: [PATCH 083/345] moved profiling fns into vm --- vm/profiler.cpp | 28 ++++++++++++++++++++++++---- vm/vm.hpp | 6 ++++++ 2 files changed, 30 insertions(+), 4 deletions(-) mode change 100644 => 100755 vm/profiler.cpp diff --git a/vm/profiler.cpp b/vm/profiler.cpp old mode 100644 new mode 100755 index a3265e0ffa..7b8f04a3bd --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -5,13 +5,18 @@ namespace factor bool profiling_p; -void init_profiler() +void factorvm::init_profiler() { profiling_p = false; } +void init_profiler() +{ + return vm->init_profiler(); +} + /* Allocates memory */ -code_block *compile_profiling_stub(cell word_) +code_block *factorvm::compile_profiling_stub(cell word_) { gc_root word(word_); @@ -21,8 +26,13 @@ code_block *compile_profiling_stub(cell word_) return jit.to_code_block(); } +code_block *compile_profiling_stub(cell word_) +{ + return vm->compile_profiling_stub(word_); +} + /* Allocates memory */ -static void set_profiling(bool profiling) +void factorvm::set_profiling(bool profiling) { if(profiling == profiling_p) return; @@ -49,9 +59,19 @@ static void set_profiling(bool profiling) iterate_code_heap(relocate_code_block); } -PRIMITIVE(profiling) +void set_profiling(bool profiling) +{ + return vm->set_profiling(profiling); +} + +inline void factorvm::vmprim_profiling() { set_profiling(to_boolean(dpop())); } +PRIMITIVE(profiling) +{ + PRIMITIVE_GETVM()->vmprim_profiling(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 7533d748b0..c18ba90772 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -31,6 +31,12 @@ struct factorvm { inline void vmprim_load_locals(); cell clone_object(cell obj_); inline void vmprim_clone(); + + // profiler + void init_profiler(); + code_block *compile_profiling_stub(cell word_); + void set_profiling(bool profiling); + inline void vmprim_profiling(); // next method here: }; From 10901e7c372aa99b1a333ac914ffca7547f2b0ab Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:04 +0100 Subject: [PATCH 084/345] moved errors.cpp functions to vm --- vm/errors.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 20 +++++++++ 2 files changed, 117 insertions(+), 18 deletions(-) mode change 100644 => 100755 vm/errors.cpp diff --git a/vm/errors.cpp b/vm/errors.cpp old mode 100644 new mode 100755 index ebe6201f72..23833960e1 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -10,21 +10,31 @@ cell signal_fault_addr; unsigned int signal_fpu_status; stack_frame *signal_callstack_top; -void out_of_memory() +void factorvm::out_of_memory() { print_string("Out of memory\n\n"); dump_generations(); exit(1); } -void fatal_error(const char* msg, cell tagged) +void out_of_memory() +{ + return vm->out_of_memory(); +} + +void factorvm::fatal_error(const char* msg, cell tagged) { print_string("fatal_error: "); print_string(msg); print_string(": "); print_cell_hex(tagged); nl(); exit(1); } -void critical_error(const char* msg, cell tagged) +void fatal_error(const char* msg, cell tagged) +{ + return vm->fatal_error(msg,tagged); +} + +void factorvm::critical_error(const char* msg, cell tagged) { print_string("You have triggered a bug in Factor. Please report.\n"); print_string("critical_error: "); print_string(msg); @@ -32,7 +42,12 @@ void critical_error(const char* msg, cell tagged) factorbug(); } -void throw_error(cell error, stack_frame *callstack_top) +void critical_error(const char* msg, cell tagged) +{ + return vm->critical_error(msg,tagged); +} + +void factorvm::throw_error(cell error, stack_frame *callstack_top) { /* If the error handler is set, we rewind any C stack frames and pass the error to user-space. */ @@ -77,26 +92,45 @@ void throw_error(cell error, stack_frame *callstack_top) } } -void general_error(vm_error_type error, cell arg1, cell arg2, - stack_frame *callstack_top) +void throw_error(cell error, stack_frame *callstack_top) +{ + return vm->throw_error(error, callstack_top); +} + +void factorvm::general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top) { throw_error(allot_array_4(userenv[ERROR_ENV], tag_fixnum(error),arg1,arg2),callstack_top); } -void type_error(cell type, cell tagged) +void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top) +{ + return vm->general_error(error,arg1,arg2,callstack_top); +} + +void factorvm::type_error(cell type, cell tagged) { general_error(ERROR_TYPE,tag_fixnum(type),tagged,NULL); } -void not_implemented_error() +void type_error(cell type, cell tagged) +{ + return vm->type_error(type, tagged); +} + +void factorvm::not_implemented_error() { general_error(ERROR_NOT_IMPLEMENTED,F,F,NULL); } +void not_implemented_error() +{ + return vm->not_implemented_error(); +} + /* Test if 'fault' is in the guard page at the top or bottom (depending on offset being 0 or -1) of area+area_size */ -bool in_page(cell fault, cell area, cell area_size, int offset) +bool factorvm::in_page(cell fault, cell area, cell area_size, int offset) { int pagesize = getpagesize(); area += area_size; @@ -105,7 +139,12 @@ bool in_page(cell fault, cell area, cell area_size, int offset) return fault >= area && fault <= area + pagesize; } -void memory_protection_error(cell addr, stack_frame *native_stack) +bool in_page(cell fault, cell area, cell area_size, int offset) +{ + return vm->in_page(fault,area,area_size,offset); +} + +void factorvm::memory_protection_error(cell addr, stack_frame *native_stack) { if(in_page(addr, ds_bot, 0, -1)) general_error(ERROR_DS_UNDERFLOW,F,F,native_stack); @@ -121,45 +160,85 @@ void memory_protection_error(cell addr, stack_frame *native_stack) general_error(ERROR_MEMORY,allot_cell(addr),F,native_stack); } -void signal_error(int signal, stack_frame *native_stack) +void memory_protection_error(cell addr, stack_frame *native_stack) +{ + return vm->memory_protection_error(addr,native_stack); +} + +void factorvm::signal_error(int signal, stack_frame *native_stack) { general_error(ERROR_SIGNAL,tag_fixnum(signal),F,native_stack); } -void divide_by_zero_error() +void signal_error(int signal, stack_frame *native_stack) +{ + return vm->signal_error(signal, native_stack); +} + +void factorvm::divide_by_zero_error() { general_error(ERROR_DIVIDE_BY_ZERO,F,F,NULL); } -void fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top) +void divide_by_zero_error() +{ + return vm->divide_by_zero_error(); +} + +void factorvm::fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top) { general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),F,signal_callstack_top); } -PRIMITIVE(call_clear) +inline void factorvm::vmprim_call_clear() { throw_impl(dpop(),stack_chain->callstack_bottom); } +PRIMITIVE(call_clear) +{ + PRIMITIVE_GETVM()->vmprim_call_clear(); +} + /* For testing purposes */ -PRIMITIVE(unimplemented) +inline void factorvm::vmprim_unimplemented() { not_implemented_error(); } -void memory_signal_handler_impl() +PRIMITIVE(unimplemented) +{ + PRIMITIVE_GETVM()->vmprim_unimplemented(); +} + +void factorvm::memory_signal_handler_impl() { memory_protection_error(signal_fault_addr,signal_callstack_top); } -void misc_signal_handler_impl() +void memory_signal_handler_impl() +{ + return vm->memory_signal_handler_impl(); +} + +void factorvm::misc_signal_handler_impl() { signal_error(signal_number,signal_callstack_top); } -void fp_signal_handler_impl() +void misc_signal_handler_impl() +{ + vm->misc_signal_handler_impl(); +} + +void factorvm::fp_signal_handler_impl() { fp_trap_error(signal_fpu_status,signal_callstack_top); } +void fp_signal_handler_impl() +{ + vm->fp_signal_handler_impl(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index c18ba90772..dd34a211fa 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -37,6 +37,26 @@ struct factorvm { code_block *compile_profiling_stub(cell word_); void set_profiling(bool profiling); inline void vmprim_profiling(); + + // errors + void out_of_memory(); + void fatal_error(const char* msg, cell tagged); + void critical_error(const char* msg, cell tagged); + void throw_error(cell error, stack_frame *callstack_top); + + void not_implemented_error(); + bool in_page(cell fault, cell area, cell area_size, int offset); + void memory_protection_error(cell addr, stack_frame *native_stack); + void signal_error(int signal, stack_frame *native_stack); + void divide_by_zero_error(); + void fp_trap_error(stack_frame *signal_callstack_top); + inline void vmprim_call_clear(); + inline void vmprim_unimplemented(); + void memory_signal_handler_impl(); + void misc_signal_handler_impl(); + void fp_signal_handler_impl(); + void type_error(cell type, cell tagged); + void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top); // next method here: }; From 8426e2f877713316207892c9025a96fa84c7c954 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:05 +0100 Subject: [PATCH 085/345] Dev checkpoint --- vm/bignum.cpp | 40 ++++++++++++++++++++++++++++++---------- vm/vm.hpp | 8 +++++++- 2 files changed, 37 insertions(+), 11 deletions(-) mode change 100644 => 100755 vm/bignum.cpp diff --git a/vm/bignum.cpp b/vm/bignum.cpp old mode 100644 new mode 100755 index c487186da0..a3c3e73ca1 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -61,8 +61,7 @@ namespace factor /* Exports */ -int -bignum_equal_p(bignum * x, bignum * y) +int factorvm::bignum_equal_p(bignum * x, bignum * y) { return ((BIGNUM_ZERO_P (x)) @@ -74,8 +73,12 @@ bignum_equal_p(bignum * x, bignum * y) && (bignum_equal_p_unsigned (x, y)))); } -enum bignum_comparison -bignum_compare(bignum * x, bignum * y) +int bignum_equal_p(bignum * x, bignum * y) +{ + return vm->bignum_equal_p(x,y); +} + +enum bignum_comparison factorvm::bignum_compare(bignum * x, bignum * y) { return ((BIGNUM_ZERO_P (x)) @@ -97,9 +100,13 @@ bignum_compare(bignum * x, bignum * y) : (bignum_compare_unsigned (x, y)))); } +enum bignum_comparison bignum_compare(bignum * x, bignum * y) +{ + return vm->bignum_compare(x,y); +} + /* allocates memory */ -bignum * -bignum_add(bignum * x, bignum * y) +bignum *factorvm::bignum_add(bignum * x, bignum * y) { return ((BIGNUM_ZERO_P (x)) @@ -115,9 +122,13 @@ bignum_add(bignum * x, bignum * y) : (bignum_add_unsigned (x, y, 0))))); } +bignum *bignum_add(bignum * x, bignum * y) +{ + return vm->bignum_add(x,y); +} + /* allocates memory */ -bignum * -bignum_subtract(bignum * x, bignum * y) +bignum *factorvm::bignum_subtract(bignum * x, bignum * y) { return ((BIGNUM_ZERO_P (x)) @@ -135,9 +146,13 @@ bignum_subtract(bignum * x, bignum * y) : (bignum_subtract_unsigned (x, y)))))); } +bignum *bignum_subtract(bignum * x, bignum * y) +{ + return vm->bignum_subtract(x,y); +} + /* allocates memory */ -bignum * -bignum_multiply(bignum * x, bignum * y) +bignum *factorvm::bignum_multiply(bignum * x, bignum * y) { bignum_length_type x_length = (BIGNUM_LENGTH (x)); bignum_length_type y_length = (BIGNUM_LENGTH (y)); @@ -168,6 +183,11 @@ bignum_multiply(bignum * x, bignum * y) return (bignum_multiply_unsigned (x, y, negative_p)); } +bignum *bignum_multiply(bignum * x, bignum * y) +{ + return vm->bignum_multiply(x,y); +} + /* allocates memory */ void bignum_divide(bignum * numerator, bignum * denominator, diff --git a/vm/vm.hpp b/vm/vm.hpp index dd34a211fa..523e1f63e7 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -43,7 +43,6 @@ struct factorvm { void fatal_error(const char* msg, cell tagged); void critical_error(const char* msg, cell tagged); void throw_error(cell error, stack_frame *callstack_top); - void not_implemented_error(); bool in_page(cell fault, cell area, cell area_size, int offset); void memory_protection_error(cell addr, stack_frame *native_stack); @@ -57,6 +56,13 @@ struct factorvm { void fp_signal_handler_impl(); void type_error(cell type, cell tagged); void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top); + + // bignum + int bignum_equal_p(bignum * x, bignum * y); + enum bignum_comparison bignum_compare(bignum * x, bignum * y); + bignum *bignum_add(bignum * x, bignum * y); + bignum *bignum_subtract(bignum * x, bignum * y); + bignum *bignum_multiply(bignum * x, bignum * y); // next method here: }; From 149af514e654bd9f2ac05a0dd0386dbf3ef75e6f Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:05 +0100 Subject: [PATCH 086/345] Dev checkpoint --- vm/bignum.cpp | 119 +++++++++++++++++++++++++++++++++++--------------- vm/vm.hpp | 14 ++++++ 2 files changed, 98 insertions(+), 35 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index a3c3e73ca1..405aff4e5e 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -189,9 +189,7 @@ bignum *bignum_multiply(bignum * x, bignum * y) } /* allocates memory */ -void -bignum_divide(bignum * numerator, bignum * denominator, - bignum * * quotient, bignum * * remainder) +void factorvm::bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder) { if (BIGNUM_ZERO_P (denominator)) { @@ -261,9 +259,13 @@ bignum_divide(bignum * numerator, bignum * denominator, } } +void bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder) +{ + return vm->bignum_divide(numerator,denominator,quotient,remainder); +} + /* allocates memory */ -bignum * -bignum_quotient(bignum * numerator, bignum * denominator) +bignum *factorvm::bignum_quotient(bignum * numerator, bignum * denominator) { if (BIGNUM_ZERO_P (denominator)) { @@ -314,9 +316,13 @@ bignum_quotient(bignum * numerator, bignum * denominator) } } +bignum *bignum_quotient(bignum * numerator, bignum * denominator) +{ + return vm->bignum_quotient(numerator,denominator); +} + /* allocates memory */ -bignum * -bignum_remainder(bignum * numerator, bignum * denominator) +bignum *factorvm::bignum_remainder(bignum * numerator, bignum * denominator) { if (BIGNUM_ZERO_P (denominator)) { @@ -359,6 +365,11 @@ bignum_remainder(bignum * numerator, bignum * denominator) } } +bignum *bignum_remainder(bignum * numerator, bignum * denominator) +{ + return vm->bignum_remainder(numerator, denominator); +} + #define FOO_TO_BIGNUM(name,type,utype) \ bignum * name##_to_bignum(type n) \ { \ @@ -416,8 +427,7 @@ BIGNUM_TO_FOO(fixnum,fixnum,cell); BIGNUM_TO_FOO(long_long,s64,u64) BIGNUM_TO_FOO(ulong_long,u64,u64) -double -bignum_to_double(bignum * bignum) +double factorvm::bignum_to_double(bignum * bignum) { if (BIGNUM_ZERO_P (bignum)) return (0); @@ -431,6 +441,11 @@ bignum_to_double(bignum * bignum) } } +double bignum_to_double(bignum * bignum) +{ + return vm->bignum_to_double(bignum); +} + #define DTB_WRITE_DIGIT(factor) \ { \ significand *= (factor); \ @@ -442,8 +457,7 @@ bignum_to_double(bignum * bignum) /* allocates memory */ #define inf std::numeric_limits::infinity() -bignum * -double_to_bignum(double x) +bignum *factorvm::double_to_bignum(double x) { if (x == inf || x == -inf || x != x) return (BIGNUM_ZERO ()); int exponent; @@ -474,12 +488,16 @@ double_to_bignum(double x) } } +bignum *double_to_bignum(double x) +{ + return vm->double_to_bignum(x); +} + #undef DTB_WRITE_DIGIT /* Comparisons */ -int -bignum_equal_p_unsigned(bignum * x, bignum * y) +int factorvm::bignum_equal_p_unsigned(bignum * x, bignum * y) { bignum_length_type length = (BIGNUM_LENGTH (x)); if (length != (BIGNUM_LENGTH (y))) @@ -496,8 +514,12 @@ bignum_equal_p_unsigned(bignum * x, bignum * y) } } -enum bignum_comparison -bignum_compare_unsigned(bignum * x, bignum * y) +int bignum_equal_p_unsigned(bignum * x, bignum * y) +{ + return vm->bignum_equal_p_unsigned(x,y); +} + +enum bignum_comparison factorvm::bignum_compare_unsigned(bignum * x, bignum * y) { bignum_length_type x_length = (BIGNUM_LENGTH (x)); bignum_length_type y_length = (BIGNUM_LENGTH (y)); @@ -522,11 +544,15 @@ bignum_compare_unsigned(bignum * x, bignum * y) return (bignum_comparison_equal); } +enum bignum_comparison bignum_compare_unsigned(bignum * x, bignum * y) +{ + return vm->bignum_compare_unsigned(x,y); +} + /* Addition */ /* allocates memory */ -bignum * -bignum_add_unsigned(bignum * x, bignum * y, int negative_p) +bignum *factorvm::bignum_add_unsigned(bignum * x, bignum * y, int negative_p) { GC_BIGNUM(x); GC_BIGNUM(y); @@ -590,11 +616,15 @@ bignum_add_unsigned(bignum * x, bignum * y, int negative_p) } } +bignum *bignum_add_unsigned(bignum * x, bignum * y, int negative_p) +{ + return vm->bignum_add_unsigned(x,y,negative_p); +} + /* Subtraction */ /* allocates memory */ -bignum * -bignum_subtract_unsigned(bignum * x, bignum * y) +bignum *factorvm::bignum_subtract_unsigned(bignum * x, bignum * y) { GC_BIGNUM(x); GC_BIGNUM(y); @@ -665,6 +695,11 @@ bignum_subtract_unsigned(bignum * x, bignum * y) } } +bignum *bignum_subtract_unsigned(bignum * x, bignum * y) +{ + return vm->bignum_subtract_unsigned(x,y); +} + /* Multiplication Maximum value for product_low or product_high: ((R * R) + (R * (R - 2)) + (R - 1)) @@ -672,8 +707,7 @@ bignum_subtract_unsigned(bignum * x, bignum * y) where R == BIGNUM_RADIX_ROOT */ /* allocates memory */ -bignum * -bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) +bignum *factorvm::bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) { GC_BIGNUM(x); GC_BIGNUM(y); @@ -743,10 +777,13 @@ bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) } } +bignum *bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) +{ + return vm->bignum_multiply_unsigned(x,y,negative_p); +} + /* allocates memory */ -bignum * -bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y, - int negative_p) +bignum *factorvm::bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p) { GC_BIGNUM(x); @@ -760,8 +797,12 @@ bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y, return (bignum_trim (p)); } -void -bignum_destructive_add(bignum * bignum, bignum_digit_type n) +bignum *bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p) +{ + return vm->bignum_multiply_unsigned_small_factor(x,y,negative_p); +} + +void factorvm::bignum_destructive_add(bignum * bignum, bignum_digit_type n) { bignum_digit_type * scan = (BIGNUM_START_PTR (bignum)); bignum_digit_type digit; @@ -784,8 +825,12 @@ bignum_destructive_add(bignum * bignum, bignum_digit_type n) } } -void -bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) +void bignum_destructive_add(bignum * bignum, bignum_digit_type n) +{ + return vm->bignum_destructive_add(bignum,n); +} + +void factorvm::bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) { bignum_digit_type carry = 0; bignum_digit_type * scan = (BIGNUM_START_PTR (bignum)); @@ -814,6 +859,11 @@ bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) #undef product_high } +void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) +{ + return vm->bignum_destructive_scale_up(bignum,factor); +} + /* Division */ /* For help understanding this algorithm, see: @@ -822,13 +872,7 @@ bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) section 4.3.1, "Multiple-Precision Arithmetic". */ /* allocates memory */ -void -bignum_divide_unsigned_large_denominator(bignum * numerator, - bignum * denominator, - bignum * * quotient, - bignum * * remainder, - int q_negative_p, - int r_negative_p) +void factorvm::bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p) { GC_BIGNUM(numerator); GC_BIGNUM(denominator); @@ -885,6 +929,11 @@ bignum_divide_unsigned_large_denominator(bignum * numerator, return; } +void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p) +{ + return vm->bignum_divide_unsigned_large_denominator(numerator,denominator,quotient,remainder,q_negative_p,r_negative_p); +} + void bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) { diff --git a/vm/vm.hpp b/vm/vm.hpp index 523e1f63e7..c492477db2 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -63,6 +63,20 @@ struct factorvm { bignum *bignum_add(bignum * x, bignum * y); bignum *bignum_subtract(bignum * x, bignum * y); bignum *bignum_multiply(bignum * x, bignum * y); + void bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder); + bignum *bignum_quotient(bignum * numerator, bignum * denominator); + bignum *bignum_remainder(bignum * numerator, bignum * denominator); + double bignum_to_double(bignum * bignum); + bignum *double_to_bignum(double x); + int bignum_equal_p_unsigned(bignum * x, bignum * y); + enum bignum_comparison bignum_compare_unsigned(bignum * x, bignum * y); + bignum *bignum_add_unsigned(bignum * x, bignum * y, int negative_p); + bignum *bignum_subtract_unsigned(bignum * x, bignum * y); + bignum *bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p); + bignum *bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p); + void bignum_destructive_add(bignum * bignum, bignum_digit_type n); + void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor); + void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p); // next method here: }; From a6fc19f4b0f6a2962af71fd1d436defcf33f6376 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:05 +0100 Subject: [PATCH 087/345] Dev checkpoint --- vm/bignum.cpp | 41 ++++++++++++++++++++++++----------------- vm/vm.hpp | 4 ++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index 405aff4e5e..03fccf3d20 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -934,8 +934,7 @@ void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denom return vm->bignum_divide_unsigned_large_denominator(numerator,denominator,quotient,remainder,q_negative_p,r_negative_p); } -void -bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) +void factorvm::bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) { bignum_length_type u_length = (BIGNUM_LENGTH (u)); bignum_length_type v_length = (BIGNUM_LENGTH (v)); @@ -1009,11 +1008,12 @@ bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) #undef qj } -bignum_digit_type -bignum_divide_subtract(bignum_digit_type * v_start, - bignum_digit_type * v_end, - bignum_digit_type guess, - bignum_digit_type * u_start) +void bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) +{ + return vm->bignum_divide_unsigned_normalized(u,v,q); +} + +bignum_digit_type factorvm::bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, bignum_digit_type guess, bignum_digit_type * u_start) { bignum_digit_type * v_scan = v_start; bignum_digit_type * u_scan = u_start; @@ -1088,14 +1088,13 @@ bignum_divide_subtract(bignum_digit_type * v_start, return (guess - 1); } +bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, bignum_digit_type guess, bignum_digit_type * u_start) +{ + return vm->bignum_divide_subtract(v_start,v_end,guess,u_start); +} + /* allocates memory */ -void -bignum_divide_unsigned_medium_denominator(bignum * numerator, - bignum_digit_type denominator, - bignum * * quotient, - bignum * * remainder, - int q_negative_p, - int r_negative_p) +void factorvm::bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) { GC_BIGNUM(numerator); @@ -1153,9 +1152,12 @@ bignum_divide_unsigned_medium_denominator(bignum * numerator, return; } -void -bignum_destructive_normalization(bignum * source, bignum * target, - int shift_left) +void bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) +{ + vm->bignum_divide_unsigned_medium_denominator(numerator,denominator,quotient,remainder,q_negative_p,r_negative_p); +} + +void factorvm::bignum_destructive_normalization(bignum * source, bignum * target, int shift_left) { bignum_digit_type digit; bignum_digit_type * scan_source = (BIGNUM_START_PTR (source)); @@ -1178,6 +1180,11 @@ bignum_destructive_normalization(bignum * source, bignum * target, return; } +void bignum_destructive_normalization(bignum * source, bignum * target, int shift_left) +{ + return vm->bignum_destructive_normalization(source,target,shift_left); +} + void bignum_destructive_unnormalization(bignum * bignum, int shift_right) { diff --git a/vm/vm.hpp b/vm/vm.hpp index c492477db2..88cea40245 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -77,6 +77,10 @@ struct factorvm { void bignum_destructive_add(bignum * bignum, bignum_digit_type n); void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor); void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p); + void bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q); + bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, bignum_digit_type guess, bignum_digit_type * u_start); + void bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p); + void bignum_destructive_normalization(bignum * source, bignum * target, int shift_left); // next method here: }; From 1a87f3bb5f1b5e49a893c5dc98caccdaa1dda30d Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:05 +0100 Subject: [PATCH 088/345] moved bignum functions to vm --- vm/bignum.cpp | 236 +++++++++++++++++++++++++++++++++++--------------- vm/vm.hpp | 40 ++++++++- 2 files changed, 205 insertions(+), 71 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index 03fccf3d20..a5310d1c14 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -1185,8 +1185,7 @@ void bignum_destructive_normalization(bignum * source, bignum * target, int shif return vm->bignum_destructive_normalization(source,target,shift_left); } -void -bignum_destructive_unnormalization(bignum * bignum, int shift_right) +void factorvm::bignum_destructive_unnormalization(bignum * bignum, int shift_right) { bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); @@ -1204,6 +1203,11 @@ bignum_destructive_unnormalization(bignum * bignum, int shift_right) return; } +void bignum_destructive_unnormalization(bignum * bignum, int shift_right) +{ + return vm->bignum_destructive_unnormalization(bignum,shift_right); +} + /* This is a reduced version of the division algorithm, applied to the case of dividing two bignum digits by one bignum digit. It is assumed that the numerator, denominator are normalized. */ @@ -1232,10 +1236,7 @@ bignum_destructive_unnormalization(bignum * bignum, int shift_right) qn = (bignum_digit_divide_subtract (v1, v2, guess, (&u[j]))); \ } -bignum_digit_type -bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, - bignum_digit_type v, - bignum_digit_type * q) /* return value */ +bignum_digit_type factorvm::bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, bignum_digit_type v, bignum_digit_type * q) /* return value */ { bignum_digit_type guess; bignum_digit_type comparand; @@ -1271,6 +1272,11 @@ bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, return (HD_CONS ((u[2]), (u[3]))); } +bignum_digit_type bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, bignum_digit_type v, bignum_digit_type * q) /* return value */ +{ + return vm->bignum_digit_divide(uh,ul,v,q); +} + #undef BDD_STEP #define BDDS_MULSUB(vn, un, carry_in) \ @@ -1304,9 +1310,7 @@ bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, } \ } -bignum_digit_type -bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, - bignum_digit_type guess, bignum_digit_type * u) +bignum_digit_type factorvm::bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, bignum_digit_type guess, bignum_digit_type * u) { { bignum_digit_type product; @@ -1336,17 +1340,16 @@ bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, return (guess - 1); } +bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, bignum_digit_type guess, bignum_digit_type * u) +{ + return vm->bignum_digit_divide_subtract(v1,v2,guess,u); +} + #undef BDDS_MULSUB #undef BDDS_ADD /* allocates memory */ -void -bignum_divide_unsigned_small_denominator(bignum * numerator, - bignum_digit_type denominator, - bignum * * quotient, - bignum * * remainder, - int q_negative_p, - int r_negative_p) +void factorvm::bignum_divide_unsigned_small_denominator(bignum * numerator, bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) { GC_BIGNUM(numerator); @@ -1365,12 +1368,16 @@ bignum_divide_unsigned_small_denominator(bignum * numerator, return; } +void bignum_divide_unsigned_small_denominator(bignum * numerator, bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) +{ + return vm->bignum_divide_unsigned_small_denominator(numerator,denominator,quotient,remainder,q_negative_p,r_negative_p); +} + /* Given (denominator > 1), it is fairly easy to show that (quotient_high < BIGNUM_RADIX_ROOT), after which it is easy to see that all digits are < BIGNUM_RADIX. */ -bignum_digit_type -bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator) +bignum_digit_type factorvm::bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator) { bignum_digit_type numerator; bignum_digit_type remainder = 0; @@ -1392,10 +1399,13 @@ bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator) #undef quotient_high } +bignum_digit_type bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator) +{ + return vm->bignum_destructive_scale_down(bignum,denominator); +} + /* allocates memory */ -bignum * -bignum_remainder_unsigned_small_denominator( - bignum * n, bignum_digit_type d, int negative_p) +bignum * factorvm::bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p) { bignum_digit_type two_digits; bignum_digit_type * start = (BIGNUM_START_PTR (n)); @@ -1413,9 +1423,13 @@ bignum_remainder_unsigned_small_denominator( return (bignum_digit_to_bignum (r, negative_p)); } +bignum * bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p) +{ + return vm->bignum_remainder_unsigned_small_denominator(n,d,negative_p); +} + /* allocates memory */ -bignum * -bignum_digit_to_bignum(bignum_digit_type digit, int negative_p) +bignum *factorvm::bignum_digit_to_bignum(bignum_digit_type digit, int negative_p) { if (digit == 0) return (BIGNUM_ZERO ()); @@ -1427,9 +1441,13 @@ bignum_digit_to_bignum(bignum_digit_type digit, int negative_p) } } +bignum *bignum_digit_to_bignum(bignum_digit_type digit, int negative_p) +{ + return vm->bignum_digit_to_bignum(digit, negative_p); +} + /* allocates memory */ -bignum * -allot_bignum(bignum_length_type length, int negative_p) +bignum *factorvm::allot_bignum(bignum_length_type length, int negative_p) { BIGNUM_ASSERT ((length >= 0) || (length < BIGNUM_RADIX)); bignum * result = allot_array_internal(length + 1); @@ -1437,9 +1455,13 @@ allot_bignum(bignum_length_type length, int negative_p) return (result); } +bignum *allot_bignum(bignum_length_type length, int negative_p) +{ + return vm->allot_bignum(length,negative_p); +} + /* allocates memory */ -bignum * -allot_bignum_zeroed(bignum_length_type length, int negative_p) +bignum * factorvm::allot_bignum_zeroed(bignum_length_type length, int negative_p) { bignum * result = allot_bignum(length,negative_p); bignum_digit_type * scan = (BIGNUM_START_PTR (result)); @@ -1449,12 +1471,16 @@ allot_bignum_zeroed(bignum_length_type length, int negative_p) return (result); } +bignum * allot_bignum_zeroed(bignum_length_type length, int negative_p) +{ + return vm->allot_bignum_zeroed(length,negative_p); +} + #define BIGNUM_REDUCE_LENGTH(source, length) \ source = reallot_array(source,length + 1) /* allocates memory */ -bignum * -bignum_shorten_length(bignum * bignum, bignum_length_type length) +bignum *factorvm::bignum_shorten_length(bignum * bignum, bignum_length_type length) { bignum_length_type current_length = (BIGNUM_LENGTH (bignum)); BIGNUM_ASSERT ((length >= 0) || (length <= current_length)); @@ -1466,9 +1492,13 @@ bignum_shorten_length(bignum * bignum, bignum_length_type length) return (bignum); } +bignum *bignum_shorten_length(bignum * bignum, bignum_length_type length) +{ + return vm->bignum_shorten_length(bignum,length); +} + /* allocates memory */ -bignum * -bignum_trim(bignum * bignum) +bignum *factorvm::bignum_trim(bignum * bignum) { bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); bignum_digit_type * end = (start + (BIGNUM_LENGTH (bignum))); @@ -1485,11 +1515,15 @@ bignum_trim(bignum * bignum) return (bignum); } +bignum *bignum_trim(bignum * bignum) +{ + return vm->bignum_trim(bignum); +} + /* Copying */ /* allocates memory */ -bignum * -bignum_new_sign(bignum * x, int negative_p) +bignum *factorvm::bignum_new_sign(bignum * x, int negative_p) { GC_BIGNUM(x); bignum * result = (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); @@ -1498,9 +1532,13 @@ bignum_new_sign(bignum * x, int negative_p) return (result); } +bignum *bignum_new_sign(bignum * x, int negative_p) +{ + return vm->bignum_new_sign(x,negative_p); +} + /* allocates memory */ -bignum * -bignum_maybe_new_sign(bignum * x, int negative_p) +bignum *factorvm::bignum_maybe_new_sign(bignum * x, int negative_p) { if ((BIGNUM_NEGATIVE_P (x)) ? negative_p : (! negative_p)) return (x); @@ -1513,8 +1551,12 @@ bignum_maybe_new_sign(bignum * x, int negative_p) } } -void -bignum_destructive_copy(bignum * source, bignum * target) +bignum *bignum_maybe_new_sign(bignum * x, int negative_p) +{ + return vm->bignum_maybe_new_sign(x,negative_p); +} + +void factorvm::bignum_destructive_copy(bignum * source, bignum * target) { bignum_digit_type * scan_source = (BIGNUM_START_PTR (source)); bignum_digit_type * end_source = @@ -1525,20 +1567,28 @@ bignum_destructive_copy(bignum * source, bignum * target) return; } +void bignum_destructive_copy(bignum * source, bignum * target) +{ + return vm->bignum_destructive_copy(source,target); +} + /* * Added bitwise operations (and oddp). */ /* allocates memory */ -bignum * -bignum_bitwise_not(bignum * x) +bignum *factorvm::bignum_bitwise_not(bignum * x) { return bignum_subtract(BIGNUM_ONE(1), x); } +bignum *bignum_bitwise_not(bignum * x) +{ + return vm->bignum_bitwise_not(x); +} + /* allocates memory */ -bignum * -bignum_arithmetic_shift(bignum * arg1, fixnum n) +bignum *factorvm::bignum_arithmetic_shift(bignum * arg1, fixnum n) { if (BIGNUM_NEGATIVE_P(arg1) && n < 0) return bignum_bitwise_not(bignum_magnitude_ash(bignum_bitwise_not(arg1), n)); @@ -1546,13 +1596,17 @@ bignum_arithmetic_shift(bignum * arg1, fixnum n) return bignum_magnitude_ash(arg1, n); } +bignum *bignum_arithmetic_shift(bignum * arg1, fixnum n) +{ + return vm->bignum_arithmetic_shift(arg1,n); +} + #define AND_OP 0 #define IOR_OP 1 #define XOR_OP 2 /* allocates memory */ -bignum * -bignum_bitwise_and(bignum * arg1, bignum * arg2) +bignum *factorvm::bignum_bitwise_and(bignum * arg1, bignum * arg2) { return( (BIGNUM_NEGATIVE_P (arg1)) @@ -1565,9 +1619,13 @@ bignum_bitwise_and(bignum * arg1, bignum * arg2) ); } +bignum *bignum_bitwise_and(bignum * arg1, bignum * arg2) +{ + return vm->bignum_bitwise_and(arg1,arg2); +} + /* allocates memory */ -bignum * -bignum_bitwise_ior(bignum * arg1, bignum * arg2) +bignum *factorvm::bignum_bitwise_ior(bignum * arg1, bignum * arg2) { return( (BIGNUM_NEGATIVE_P (arg1)) @@ -1580,9 +1638,13 @@ bignum_bitwise_ior(bignum * arg1, bignum * arg2) ); } +bignum *bignum_bitwise_ior(bignum * arg1, bignum * arg2) +{ + return vm->bignum_bitwise_ior(arg1,arg2); +} + /* allocates memory */ -bignum * -bignum_bitwise_xor(bignum * arg1, bignum * arg2) +bignum *factorvm::bignum_bitwise_xor(bignum * arg1, bignum * arg2) { return( (BIGNUM_NEGATIVE_P (arg1)) @@ -1595,11 +1657,15 @@ bignum_bitwise_xor(bignum * arg1, bignum * arg2) ); } +bignum *bignum_bitwise_xor(bignum * arg1, bignum * arg2) +{ + return vm->bignum_bitwise_xor(arg1,arg2); +} + /* allocates memory */ /* ash for the magnitude */ /* assume arg1 is a big number, n is a long */ -bignum * -bignum_magnitude_ash(bignum * arg1, fixnum n) +bignum *factorvm::bignum_magnitude_ash(bignum * arg1, fixnum n) { GC_BIGNUM(arg1); @@ -1659,9 +1725,13 @@ bignum_magnitude_ash(bignum * arg1, fixnum n) return (bignum_trim (result)); } +bignum *bignum_magnitude_ash(bignum * arg1, fixnum n) +{ + return vm->bignum_magnitude_ash(arg1,n); +} + /* allocates memory */ -bignum * -bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) +bignum *factorvm::bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) { GC_BIGNUM(arg1); GC_BIGNUM(arg2); @@ -1694,9 +1764,13 @@ bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) return bignum_trim(result); } +bignum *bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) +{ + return vm->bignum_pospos_bitwise_op(op,arg1,arg2); +} + /* allocates memory */ -bignum * -bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) +bignum *factorvm::bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) { GC_BIGNUM(arg1); GC_BIGNUM(arg2); @@ -1747,9 +1821,13 @@ bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) return bignum_trim(result); } +bignum *bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) +{ + return vm->bignum_posneg_bitwise_op(op,arg1,arg2); +} + /* allocates memory */ -bignum * -bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) +bignum *factorvm::bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) { GC_BIGNUM(arg1); GC_BIGNUM(arg2); @@ -1808,8 +1886,12 @@ bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) return bignum_trim(result); } -void -bignum_negate_magnitude(bignum * arg) +bignum *bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) +{ + return vm->bignum_negneg_bitwise_op(op,arg1,arg2); +} + +void factorvm::bignum_negate_magnitude(bignum * arg) { bignum_digit_type *scan; bignum_digit_type *end; @@ -1836,9 +1918,13 @@ bignum_negate_magnitude(bignum * arg) } } +void bignum_negate_magnitude(bignum * arg) +{ + return vm->bignum_negate_magnitude(arg); +} + /* Allocates memory */ -bignum * -bignum_integer_length(bignum * x) +bignum *factorvm::bignum_integer_length(bignum * x) { GC_BIGNUM(x); @@ -1858,17 +1944,25 @@ bignum_integer_length(bignum * x) return (bignum_trim (result)); } +bignum *bignum_integer_length(bignum * x) +{ + return vm->bignum_integer_length(x); +} + /* Allocates memory */ -int -bignum_logbitp(int shift, bignum * arg) +int factorvm::bignum_logbitp(int shift, bignum * arg) { return((BIGNUM_NEGATIVE_P (arg)) ? !bignum_unsigned_logbitp (shift, bignum_bitwise_not (arg)) : bignum_unsigned_logbitp (shift,arg)); } -int -bignum_unsigned_logbitp(int shift, bignum * bignum) +int bignum_logbitp(int shift, bignum * arg) +{ + return vm->bignum_logbitp(shift,arg); +} + +int factorvm::bignum_unsigned_logbitp(int shift, bignum * bignum) { bignum_length_type len = (BIGNUM_LENGTH (bignum)); int index = shift / BIGNUM_DIGIT_LENGTH; @@ -1880,12 +1974,13 @@ bignum_unsigned_logbitp(int shift, bignum * bignum) return (digit & mask) ? 1 : 0; } +int bignum_unsigned_logbitp(int shift, bignum * bignum) +{ + return vm->bignum_unsigned_logbitp(shift,bignum); +} + /* Allocates memory */ -bignum * -digit_stream_to_bignum(unsigned int n_digits, - unsigned int (*producer)(unsigned int), - unsigned int radix, - int negative_p) +bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p) { BIGNUM_ASSERT ((radix > 1) && (radix <= BIGNUM_RADIX_ROOT)); if (n_digits == 0) @@ -1921,4 +2016,9 @@ digit_stream_to_bignum(unsigned int n_digits, } } +bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p) +{ + return vm->digit_stream_to_bignum(n_digits,producer,radix,negative_p); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 88cea40245..17a62fb0af 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -76,11 +76,45 @@ struct factorvm { bignum *bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p); void bignum_destructive_add(bignum * bignum, bignum_digit_type n); void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor); - void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p); + void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, + bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p); void bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q); - bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, bignum_digit_type guess, bignum_digit_type * u_start); - void bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p); + bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, + bignum_digit_type guess, bignum_digit_type * u_start); + void bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, + bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p); void bignum_destructive_normalization(bignum * source, bignum * target, int shift_left); + void bignum_destructive_unnormalization(bignum * bignum, int shift_right); + bignum_digit_type bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, + bignum_digit_type v, bignum_digit_type * q) /* return value */; + bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, + bignum_digit_type guess, bignum_digit_type * u); + void bignum_divide_unsigned_small_denominator(bignum * numerator, bignum_digit_type denominator, + bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p); + bignum_digit_type bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator); + bignum * bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p); + bignum *bignum_digit_to_bignum(bignum_digit_type digit, int negative_p); + bignum *allot_bignum(bignum_length_type length, int negative_p); + bignum * allot_bignum_zeroed(bignum_length_type length, int negative_p); + bignum *bignum_shorten_length(bignum * bignum, bignum_length_type length); + bignum *bignum_trim(bignum * bignum); + bignum *bignum_new_sign(bignum * x, int negative_p); + bignum *bignum_maybe_new_sign(bignum * x, int negative_p); + void bignum_destructive_copy(bignum * source, bignum * target); + bignum *bignum_bitwise_not(bignum * x); + bignum *bignum_arithmetic_shift(bignum * arg1, fixnum n); + bignum *bignum_bitwise_and(bignum * arg1, bignum * arg2); + bignum *bignum_bitwise_ior(bignum * arg1, bignum * arg2); + bignum *bignum_bitwise_xor(bignum * arg1, bignum * arg2); + bignum *bignum_magnitude_ash(bignum * arg1, fixnum n); + bignum *bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2); + bignum *bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2); + bignum *bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2); + void bignum_negate_magnitude(bignum * arg); + bignum *bignum_integer_length(bignum * x); + int bignum_logbitp(int shift, bignum * arg); + int bignum_unsigned_logbitp(int shift, bignum * bignum); + bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p); // next method here: }; From 9f6f7adaba0e0e944d0d082f9f1a7287004071c8 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:05 +0100 Subject: [PATCH 089/345] moved data_heap fns to vm struct --- vm/data_heap.cpp | 182 ++++++++++++++++++++++++++++++++++++++--------- vm/vm.hpp | 27 +++++++ 2 files changed, 177 insertions(+), 32 deletions(-) mode change 100644 => 100755 vm/data_heap.cpp diff --git a/vm/data_heap.cpp b/vm/data_heap.cpp old mode 100644 new mode 100755 index 5c1c8079c7..9069621e52 --- a/vm/data_heap.cpp +++ b/vm/data_heap.cpp @@ -16,7 +16,7 @@ bool gc_off; data_heap *data; -cell init_zone(zone *z, cell size, cell start) +cell factorvm::init_zone(zone *z, cell size, cell start) { z->size = size; z->start = z->here = start; @@ -24,7 +24,12 @@ cell init_zone(zone *z, cell size, cell start) return z->end; } -void init_card_decks() +cell init_zone(zone *z, cell size, cell start) +{ + return vm->init_zone(z,size,start); +} + +void factorvm::init_card_decks() { cell start = align(data->seg->start,deck_size); allot_markers_offset = (cell)data->allot_markers - (start >> card_bits); @@ -32,10 +37,12 @@ void init_card_decks() decks_offset = (cell)data->decks - (start >> deck_bits); } -data_heap *alloc_data_heap(cell gens, - cell young_size, - cell aging_size, - cell tenured_size) +void init_card_decks() +{ + return vm->init_card_decks(); +} + +data_heap *factorvm::alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size) { young_size = align(young_size,deck_size); aging_size = align(aging_size,deck_size); @@ -99,7 +106,12 @@ data_heap *alloc_data_heap(cell gens, return data; } -data_heap *grow_data_heap(data_heap *data, cell requested_bytes) +data_heap *alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size) +{ + return vm->alloc_data_heap(gens,young_size,aging_size,tenured_size); +} + +data_heap *factorvm::grow_data_heap(data_heap *data, cell requested_bytes) { cell new_tenured_size = (data->tenured_size * 2) + requested_bytes; @@ -109,7 +121,12 @@ data_heap *grow_data_heap(data_heap *data, cell requested_bytes) new_tenured_size); } -void dealloc_data_heap(data_heap *data) +data_heap *grow_data_heap(data_heap *data, cell requested_bytes) +{ + return vm->grow_data_heap(data,requested_bytes); +} + +void factorvm::dealloc_data_heap(data_heap *data) { dealloc_segment(data->seg); free(data->generations); @@ -120,7 +137,12 @@ void dealloc_data_heap(data_heap *data) free(data); } -void clear_cards(cell from, cell to) +void dealloc_data_heap(data_heap *data) +{ + return vm->dealloc_data_heap(data); +} + +void factorvm::clear_cards(cell from, cell to) { /* NOTE: reverse order due to heap layout. */ card *first_card = addr_to_card(data->generations[to].start); @@ -128,7 +150,12 @@ void clear_cards(cell from, cell to) memset(first_card,0,last_card - first_card); } -void clear_decks(cell from, cell to) +void clear_cards(cell from, cell to) +{ + return vm->clear_cards(from,to); +} + +void factorvm::clear_decks(cell from, cell to) { /* NOTE: reverse order due to heap layout. */ card_deck *first_deck = addr_to_deck(data->generations[to].start); @@ -136,7 +163,12 @@ void clear_decks(cell from, cell to) memset(first_deck,0,last_deck - first_deck); } -void clear_allot_markers(cell from, cell to) +void clear_decks(cell from, cell to) +{ + return vm->clear_decks(from,to); +} + +void factorvm::clear_allot_markers(cell from, cell to) { /* NOTE: reverse order due to heap layout. */ card *first_card = addr_to_allot_marker((object *)data->generations[to].start); @@ -144,7 +176,12 @@ void clear_allot_markers(cell from, cell to) memset(first_card,invalid_allot_marker,last_card - first_card); } -void reset_generation(cell i) +void clear_allot_markers(cell from, cell to) +{ + return vm->clear_allot_markers(from,to); +} + +void factorvm::reset_generation(cell i) { zone *z = (i == data->nursery() ? &nursery : &data->generations[i]); @@ -153,9 +190,14 @@ void reset_generation(cell i) memset((void*)z->start,69,z->size); } +void reset_generation(cell i) +{ + return vm->reset_generation(i); +} + /* After garbage collection, any generations which are now empty need to have their allocation pointers and cards reset. */ -void reset_generations(cell from, cell to) +void factorvm::reset_generations(cell from, cell to) { cell i; for(i = from; i <= to; i++) @@ -166,7 +208,12 @@ void reset_generations(cell from, cell to) clear_allot_markers(from,to); } -void set_data_heap(data_heap *data_) +void reset_generations(cell from, cell to) +{ + return vm->reset_generations(from,to); +} + +void factorvm::set_data_heap(data_heap *data_) { data = data_; nursery = data->generations[data->nursery()]; @@ -176,17 +223,23 @@ void set_data_heap(data_heap *data_) clear_allot_markers(data->nursery(),data->tenured()); } -void init_data_heap(cell gens, - cell young_size, - cell aging_size, - cell tenured_size, - bool secure_gc_) +void set_data_heap(data_heap *data_) +{ + return vm->set_data_heap(data_); +} + +void factorvm::init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size,bool secure_gc_) { set_data_heap(alloc_data_heap(gens,young_size,aging_size,tenured_size)); secure_gc = secure_gc_; init_data_gc(); } +void init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size,bool secure_gc_) +{ + return vm->init_data_heap(gens,young_size,aging_size,tenured_size,secure_gc_); +} + /* Size of the object pointed to by a tagged pointer */ cell object_size(cell tagged) { @@ -197,13 +250,18 @@ cell object_size(cell tagged) } /* Size of the object pointed to by an untagged pointer */ -cell untagged_object_size(object *pointer) +cell factorvm::untagged_object_size(object *pointer) { return align8(unaligned_object_size(pointer)); } +cell untagged_object_size(object *pointer) +{ + return vm->untagged_object_size(pointer); +} + /* Size of the data area of an object pointed to by an untagged pointer */ -cell unaligned_object_size(object *pointer) +cell factorvm::unaligned_object_size(object *pointer) { switch(pointer->h.hi_tag()) { @@ -237,15 +295,25 @@ cell unaligned_object_size(object *pointer) } } -PRIMITIVE(size) +cell unaligned_object_size(object *pointer) +{ + return vm->unaligned_object_size(pointer); +} + +inline void factorvm::vmprim_size() { box_unsigned_cell(object_size(dpop())); } +PRIMITIVE(size) +{ + PRIMITIVE_GETVM()->vmprim_size(); +} + /* The number of cells from the start of the object which should be scanned by the GC. Some types have a binary payload at the end (string, word, DLL) which we ignore. */ -cell binary_payload_start(object *pointer) +cell factorvm::binary_payload_start(object *pointer) { switch(pointer->h.hi_tag()) { @@ -279,8 +347,13 @@ cell binary_payload_start(object *pointer) } } +cell binary_payload_start(object *pointer) +{ + return vm->binary_payload_start(pointer); +} + /* Push memory usage statistics in data heap */ -PRIMITIVE(data_room) +inline void factorvm::vmprim_data_room() { dpush(tag_fixnum((data->cards_end - data->cards) >> 10)); dpush(tag_fixnum((data->decks_end - data->decks) >> 10)); @@ -299,28 +372,48 @@ PRIMITIVE(data_room) dpush(a.elements.value()); } +PRIMITIVE(data_room) +{ + PRIMITIVE_GETVM()->vmprim_data_room(); +} + /* A heap walk allows useful things to be done, like finding all references to an object for debugging purposes. */ cell heap_scan_ptr; /* Disables GC and activates next-object ( -- obj ) primitive */ -void begin_scan() +void factorvm::begin_scan() { heap_scan_ptr = data->generations[data->tenured()].start; gc_off = true; } -void end_scan() +void begin_scan() +{ + return vm->begin_scan(); +} + +void factorvm::end_scan() { gc_off = false; } -PRIMITIVE(begin_scan) +void end_scan() +{ + return vm->end_scan(); +} + +inline void factorvm::vmprim_begin_scan() { begin_scan(); } -cell next_object() +PRIMITIVE(begin_scan) +{ + PRIMITIVE_GETVM()->vmprim_begin_scan(); +} + +cell factorvm::next_object() { if(!gc_off) general_error(ERROR_HEAP_SCAN,F,F,NULL); @@ -333,19 +426,34 @@ cell next_object() return tag_dynamic(obj); } +cell next_object() +{ + return vm->next_object(); +} + /* Push object at heap scan cursor and advance; pushes f when done */ -PRIMITIVE(next_object) +inline void factorvm::vmprim_next_object() { dpush(next_object()); } +PRIMITIVE(next_object) +{ + PRIMITIVE_GETVM()->vmprim_next_object(); +} + /* Re-enables GC */ -PRIMITIVE(end_scan) +inline void factorvm::vmprim_end_scan() { gc_off = false; } -template void each_object(T &functor) +PRIMITIVE(end_scan) +{ + PRIMITIVE_GETVM()->vmprim_end_scan(); +} + +template void factorvm::each_object(T &functor) { begin_scan(); cell obj; @@ -354,6 +462,11 @@ template void each_object(T &functor) end_scan(); } +template void each_object(T &functor) +{ + return vm->each_object(functor); +} + namespace { @@ -371,7 +484,7 @@ struct word_accumulator { } -cell find_all_words() +cell factorvm::find_all_words() { word_counter counter; each_object(counter); @@ -381,4 +494,9 @@ cell find_all_words() return accum.words.elements.value(); } +cell find_all_words() +{ + return vm->find_all_words(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 17a62fb0af..8ec5c7d094 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -115,6 +115,33 @@ struct factorvm { int bignum_logbitp(int shift, bignum * arg); int bignum_unsigned_logbitp(int shift, bignum * bignum); bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p); + + //data_heap + cell init_zone(zone *z, cell size, cell start); + void init_card_decks(); + data_heap *alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size); + data_heap *grow_data_heap(data_heap *data, cell requested_bytes); + void dealloc_data_heap(data_heap *data); + void clear_cards(cell from, cell to); + void clear_decks(cell from, cell to); + void clear_allot_markers(cell from, cell to); + void reset_generation(cell i); + void reset_generations(cell from, cell to); + void set_data_heap(data_heap *data_); + void init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size,bool secure_gc_); + cell untagged_object_size(object *pointer); + cell unaligned_object_size(object *pointer); + inline void vmprim_size(); + cell binary_payload_start(object *pointer); + inline void vmprim_data_room(); + void begin_scan(); + void end_scan(); + inline void vmprim_begin_scan(); + cell next_object(); + inline void vmprim_next_object(); + inline void vmprim_end_scan(); + template void each_object(T &functor); + cell find_all_words(); // next method here: }; From 64c2d813061566e4226b8a13313ccf38def63d26 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:05 +0100 Subject: [PATCH 090/345] moved data_gc fns over to vm struct --- vm/data_gc.cpp | 205 +++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 32 ++++++++ 2 files changed, 206 insertions(+), 31 deletions(-) mode change 100644 => 100755 vm/data_gc.cpp diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp old mode 100644 new mode 100755 index 458a437e37..ea7100fc02 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -33,15 +33,20 @@ cell last_code_heap_scan; bool growing_data_heap; data_heap *old_data_heap; -void init_data_gc() +void factorvm::init_data_gc() { performing_gc = false; last_code_heap_scan = data->nursery(); collecting_aging_again = false; } +void init_data_gc() +{ + return vm->init_data_gc(); +} + /* Given a pointer to oldspace, copy it to newspace */ -static object *copy_untagged_object_impl(object *pointer, cell size) +object *factorvm::copy_untagged_object_impl(object *pointer, cell size) { if(newspace->here + size >= newspace->end) longjmp(gc_jmp,1); @@ -55,14 +60,24 @@ static object *copy_untagged_object_impl(object *pointer, cell size) return newpointer; } -static object *copy_object_impl(object *untagged) +object *copy_untagged_object_impl(object *pointer, cell size) +{ + return vm->copy_untagged_object_impl(pointer,size); +} + +object *factorvm::copy_object_impl(object *untagged) { object *newpointer = copy_untagged_object_impl(untagged,untagged_object_size(untagged)); untagged->h.forward_to(newpointer); return newpointer; } -static bool should_copy_p(object *untagged) +object *copy_object_impl(object *untagged) +{ + return vm->copy_object_impl(untagged); +} + +bool factorvm::should_copy_p(object *untagged) { if(in_zone(newspace,untagged)) return false; @@ -79,8 +94,13 @@ static bool should_copy_p(object *untagged) } } +bool should_copy_p(object *untagged) +{ + return vm->should_copy_p(untagged); +} + /* Follow a chain of forwarding pointers */ -static object *resolve_forwarding(object *untagged) +object *factorvm::resolve_forwarding(object *untagged) { check_data_pointer(untagged); @@ -98,7 +118,12 @@ static object *resolve_forwarding(object *untagged) } } -template static T *copy_untagged_object(T *untagged) +object *resolve_forwarding(object *untagged) +{ + return vm->resolve_forwarding(untagged); +} + +template T *factorvm::copy_untagged_object(T *untagged) { check_data_pointer(untagged); @@ -113,12 +138,22 @@ template static T *copy_untagged_object(T *untagged) return untagged; } -static cell copy_object(cell pointer) +template T *copy_untagged_object(T *untagged) +{ + return vm->copy_untagged_object(untagged); +} + +cell factorvm::copy_object(cell pointer) { return RETAG(copy_untagged_object(untag(pointer)),TAG(pointer)); } -void copy_handle(cell *handle) +cell copy_object(cell pointer) +{ + return vm->copy_object(pointer); +} + +void factorvm::copy_handle(cell *handle) { cell pointer = *handle; @@ -131,8 +166,13 @@ void copy_handle(cell *handle) } } +void copy_handle(cell *handle) +{ + return vm->copy_handle(handle); +} + /* Scan all the objects in the card */ -static void copy_card(card *ptr, cell gen, cell here) +void factorvm::copy_card(card *ptr, cell gen, cell here) { cell card_scan = card_to_addr(ptr) + card_offset(ptr); cell card_end = card_to_addr(ptr + 1); @@ -145,7 +185,12 @@ static void copy_card(card *ptr, cell gen, cell here) cards_scanned++; } -static void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask) +void copy_card(card *ptr, cell gen, cell here) +{ + return vm->copy_card(ptr,gen,here); +} + +void factorvm::copy_card_deck(card_deck *deck, cell gen, card mask, card unmask) { card *first_card = deck_to_card(deck); card *last_card = deck_to_card(deck + 1); @@ -176,8 +221,13 @@ static void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask) decks_scanned++; } +void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask) +{ + return vm->copy_card_deck(deck,gen,mask,unmask); +} + /* Copy all newspace objects referenced from marked cards to the destination */ -static void copy_gen_cards(cell gen) +void factorvm::copy_gen_cards(cell gen) { card_deck *first_deck = addr_to_deck(data->generations[gen].start); card_deck *last_deck = addr_to_deck(data->generations[gen].end); @@ -242,9 +292,14 @@ static void copy_gen_cards(cell gen) } } +void copy_gen_cards(cell gen) +{ + return vm->copy_gen_cards(gen); +} + /* Scan cards in all generations older than the one being collected, copying old->new references */ -static void copy_cards() +void factorvm::copy_cards() { u64 start = current_micros(); @@ -255,8 +310,13 @@ static void copy_cards() card_scan_time += (current_micros() - start); } +void copy_cards() +{ + return vm->copy_cards(); +} + /* Copy all tagged pointers in a range of memory */ -static void copy_stack_elements(segment *region, cell top) +void factorvm::copy_stack_elements(segment *region, cell top) { cell ptr = region->start; @@ -264,7 +324,12 @@ static void copy_stack_elements(segment *region, cell top) copy_handle((cell*)ptr); } -static void copy_registered_locals() +void copy_stack_elements(segment *region, cell top) +{ + return vm->copy_stack_elements(region,top); +} + +void factorvm::copy_registered_locals() { std::vector::const_iterator iter = gc_locals.begin(); std::vector::const_iterator end = gc_locals.end(); @@ -273,7 +338,12 @@ static void copy_registered_locals() copy_handle((cell *)(*iter)); } -static void copy_registered_bignums() +void copy_registered_locals() +{ + return vm->copy_registered_locals(); +} + +void factorvm::copy_registered_bignums() { std::vector::const_iterator iter = gc_bignums.begin(); std::vector::const_iterator end = gc_bignums.end(); @@ -295,9 +365,14 @@ static void copy_registered_bignums() } } +void copy_registered_bignums() +{ + return vm->copy_registered_bignums(); +} + /* Copy roots over at the start of GC, namely various constants, stacks, the user environment and extra roots registered by local_roots.hpp */ -static void copy_roots() +void factorvm::copy_roots() { copy_handle(&T); copy_handle(&bignum_zero); @@ -331,7 +406,12 @@ static void copy_roots() copy_handle(&userenv[i]); } -static cell copy_next_from_nursery(cell scan) +void copy_roots() +{ + return vm->copy_roots(); +} + +cell factorvm::copy_next_from_nursery(cell scan) { cell *obj = (cell *)scan; cell *end = (cell *)(scan + binary_payload_start((object *)scan)); @@ -359,7 +439,12 @@ static cell copy_next_from_nursery(cell scan) return scan + untagged_object_size((object *)scan); } -static cell copy_next_from_aging(cell scan) +cell copy_next_from_nursery(cell scan) +{ + return vm->copy_next_from_nursery(scan); +} + +cell factorvm::copy_next_from_aging(cell scan) { cell *obj = (cell *)scan; cell *end = (cell *)(scan + binary_payload_start((object *)scan)); @@ -391,7 +476,12 @@ static cell copy_next_from_aging(cell scan) return scan + untagged_object_size((object *)scan); } -static cell copy_next_from_tenured(cell scan) +cell copy_next_from_aging(cell scan) +{ + return vm->copy_next_from_aging(scan); +} + +cell factorvm::copy_next_from_tenured(cell scan) { cell *obj = (cell *)scan; cell *end = (cell *)(scan + binary_payload_start((object *)scan)); @@ -421,7 +511,12 @@ static cell copy_next_from_tenured(cell scan) return scan + untagged_object_size((object *)scan); } -void copy_reachable_objects(cell scan, cell *end) +cell copy_next_from_tenured(cell scan) +{ + return vm->copy_next_from_tenured(scan); +} + +void factorvm::copy_reachable_objects(cell scan, cell *end) { if(collecting_gen == data->nursery()) { @@ -440,8 +535,13 @@ void copy_reachable_objects(cell scan, cell *end) } } +void copy_reachable_objects(cell scan, cell *end) +{ + return vm->copy_reachable_objects(scan,end); +} + /* Prepare to start copying reachable objects into an unused zone */ -static void begin_gc(cell requested_bytes) +void factorvm::begin_gc(cell requested_bytes) { if(growing_data_heap) { @@ -474,7 +574,12 @@ static void begin_gc(cell requested_bytes) } } -static void end_gc(cell gc_elapsed) +void begin_gc(cell requested_bytes) +{ + return vm->begin_gc(requested_bytes); +} + +void factorvm::end_gc(cell gc_elapsed) { gc_stats *s = &stats[collecting_gen]; @@ -512,12 +617,15 @@ static void end_gc(cell gc_elapsed) collecting_aging_again = false; } +void end_gc(cell gc_elapsed) +{ + return vm->end_gc(gc_elapsed); +} + /* Collect gen and all younger generations. If growing_data_heap_ is true, we must grow the data heap to such a size that an allocation of requested_bytes won't fail */ -void garbage_collection(cell gen, - bool growing_data_heap_, - cell requested_bytes) +void factorvm::garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes) { if(gc_off) { @@ -595,17 +703,32 @@ void garbage_collection(cell gen, performing_gc = false; } -void gc() +void garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes) +{ + return vm->garbage_collection(gen,growing_data_heap_,requested_bytes); +} + +void factorvm::gc() { garbage_collection(data->tenured(),false,0); } -PRIMITIVE(gc) +void gc() +{ + return vm->gc(); +} + +inline void factorvm::vmprim_gc() { gc(); } -PRIMITIVE(gc_stats) +PRIMITIVE(gc) +{ + PRIMITIVE_GETVM()->vmprim_gc(); +} + +inline void factorvm::vmprim_gc_stats() { growable_array result; @@ -635,7 +758,12 @@ PRIMITIVE(gc_stats) dpush(result.elements.value()); } -void clear_gc_stats() +PRIMITIVE(gc_stats) +{ + PRIMITIVE_GETVM()->vmprim_gc_stats(); +} + +void factorvm::clear_gc_stats() { for(cell i = 0; i < max_gen_count; i++) memset(&stats[i],0,sizeof(gc_stats)); @@ -646,6 +774,11 @@ void clear_gc_stats() code_heap_scans = 0; } +void clear_gc_stats() +{ + return vm->clear_gc_stats(); +} + PRIMITIVE(clear_gc_stats) { clear_gc_stats(); @@ -653,7 +786,7 @@ PRIMITIVE(clear_gc_stats) /* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this to coalesce equal but distinct quotations and wrappers. */ -PRIMITIVE(become) +inline void factorvm::vmprim_become() { array *new_objects = untag_check(dpop()); array *old_objects = untag_check(dpop()); @@ -682,7 +815,12 @@ PRIMITIVE(become) compile_all_words(); } -VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size) +PRIMITIVE(become) +{ + PRIMITIVE_GETVM()->vmprim_become(); +} + +VM_ASM_API void factorvm::inline_gc(cell *gc_roots_base, cell gc_roots_size) { for(cell i = 0; i < gc_roots_size; i++) gc_locals.push_back((cell)&gc_roots_base[i]); @@ -693,4 +831,9 @@ VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size) gc_locals.pop_back(); } +VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size) +{ + return vm->inline_gc(gc_roots_base,gc_roots_size); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 8ec5c7d094..5750d434c6 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -142,8 +142,40 @@ struct factorvm { inline void vmprim_end_scan(); template void each_object(T &functor); cell find_all_words(); + + //data_gc + void init_data_gc(); + object *copy_untagged_object_impl(object *pointer, cell size); + object *copy_object_impl(object *untagged); + bool should_copy_p(object *untagged); + object *resolve_forwarding(object *untagged); + template T *copy_untagged_object(T *untagged); + cell copy_object(cell pointer); + void copy_handle(cell *handle); + void copy_card(card *ptr, cell gen, cell here); + void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask); + void copy_gen_cards(cell gen); + void copy_cards(); + void copy_stack_elements(segment *region, cell top); + void copy_registered_locals(); + void copy_registered_bignums(); + void copy_roots(); + cell copy_next_from_nursery(cell scan); + cell copy_next_from_aging(cell scan); + cell copy_next_from_tenured(cell scan); + void copy_reachable_objects(cell scan, cell *end); + void begin_gc(cell requested_bytes); + void end_gc(cell gc_elapsed); + void garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes); + void gc(); + inline void vmprim_gc(); + inline void vmprim_gc_stats(); + void clear_gc_stats(); + inline void vmprim_become(); + void inline_gc(cell *gc_roots_base, cell gc_roots_size); // next method here: + }; extern factorvm *vm; From 13e0ae6d97388b332801ee452d8e831c45a2e211 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:06 +0100 Subject: [PATCH 091/345] moved debug functions into vm struct --- vm/debug.cpp | 158 +++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 24 ++++++++ 2 files changed, 158 insertions(+), 24 deletions(-) mode change 100644 => 100755 vm/debug.cpp diff --git a/vm/debug.cpp b/vm/debug.cpp old mode 100644 new mode 100755 index 5f78afb9db..870c817a2b --- a/vm/debug.cpp +++ b/vm/debug.cpp @@ -6,14 +6,19 @@ namespace factor static bool fep_disabled; static bool full_output; -void print_chars(string* str) +void factorvm::print_chars(string* str) { cell i; for(i = 0; i < string_capacity(str); i++) putchar(string_nth(str,i)); } -void print_word(word* word, cell nesting) +void print_chars(string* str) +{ + return vm->print_chars(str); +} + +void factorvm::print_word(word* word, cell nesting) { if(tagged(word->vocabulary).type_p(STRING_TYPE)) { @@ -31,14 +36,24 @@ void print_word(word* word, cell nesting) } } -void print_factor_string(string* str) +void print_word(word* word, cell nesting) +{ + return vm->print_word(word,nesting); +} + +void factorvm::print_factor_string(string* str) { putchar('"'); print_chars(str); putchar('"'); } -void print_array(array* array, cell nesting) +void print_factor_string(string* str) +{ + return vm->print_factor_string(str); +} + +void factorvm::print_array(array* array, cell nesting) { cell length = array_capacity(array); cell i; @@ -62,7 +77,12 @@ void print_array(array* array, cell nesting) print_string("..."); } -void print_tuple(tuple *tuple, cell nesting) +void print_array(array* array, cell nesting) +{ + return vm->print_array(array,nesting); +} + +void factorvm::print_tuple(tuple *tuple, cell nesting) { tuple_layout *layout = untag(tuple->layout); cell length = to_fixnum(layout->size); @@ -91,7 +111,12 @@ void print_tuple(tuple *tuple, cell nesting) print_string("..."); } -void print_nested_obj(cell obj, fixnum nesting) +void print_tuple(tuple *tuple, cell nesting) +{ + return vm->print_tuple(tuple,nesting); +} + +void factorvm::print_nested_obj(cell obj, fixnum nesting) { if(nesting <= 0 && !full_output) { @@ -141,12 +166,22 @@ void print_nested_obj(cell obj, fixnum nesting) } } -void print_obj(cell obj) +void print_nested_obj(cell obj, fixnum nesting) +{ + return vm->print_nested_obj(obj,nesting); +} + +void factorvm::print_obj(cell obj) { print_nested_obj(obj,10); } -void print_objects(cell *start, cell *end) +void print_obj(cell obj) +{ + return vm->print_obj(obj); +} + +void factorvm::print_objects(cell *start, cell *end) { for(; start <= end; start++) { @@ -155,19 +190,34 @@ void print_objects(cell *start, cell *end) } } -void print_datastack() +void print_objects(cell *start, cell *end) +{ + return vm->print_objects(start,end); +} + +void factorvm::print_datastack() { print_string("==== DATA STACK:\n"); print_objects((cell *)ds_bot,(cell *)ds); } -void print_retainstack() +void print_datastack() +{ + return vm->print_datastack(); +} + +void factorvm::print_retainstack() { print_string("==== RETAIN STACK:\n"); print_objects((cell *)rs_bot,(cell *)rs); } -void print_stack_frame(stack_frame *frame) +void print_retainstack() +{ + return vm->print_retainstack(); +} + +void factorvm::print_stack_frame(stack_frame *frame) { print_obj(frame_executing(frame)); print_string("\n"); @@ -184,15 +234,25 @@ void print_stack_frame(stack_frame *frame) print_string("\n"); } -void print_callstack() +void print_stack_frame(stack_frame *frame) +{ + return vm->print_stack_frame(frame); +} + +void factorvm::print_callstack() { print_string("==== CALL STACK:\n"); cell bottom = (cell)stack_chain->callstack_bottom; cell top = (cell)stack_chain->callstack_top; - iterate_callstack(top,bottom,print_stack_frame); + iterate_callstack(top,bottom,factor::print_stack_frame); } -void dump_cell(cell x) +void print_callstack() +{ + return vm->print_callstack(); +} + +void factorvm::dump_cell(cell x) { print_cell_hex_pad(x); print_string(": "); x = *(cell *)x; @@ -200,7 +260,12 @@ void dump_cell(cell x) nl(); } -void dump_memory(cell from, cell to) +void dump_cell(cell x) +{ + return vm->dump_cell(x); +} + +void factorvm::dump_memory(cell from, cell to) { from = UNTAG(from); @@ -208,14 +273,24 @@ void dump_memory(cell from, cell to) dump_cell(from); } -void dump_zone(zone *z) +void dump_memory(cell from, cell to) +{ + return vm->dump_memory(from,to); +} + +void factorvm::dump_zone(zone *z) { print_string("Start="); print_cell(z->start); print_string(", size="); print_cell(z->size); print_string(", here="); print_cell(z->here - z->start); nl(); } -void dump_generations() +void dump_zone(zone *z) +{ + return vm->dump_zone(z); +} + +void factorvm::dump_generations() { cell i; @@ -241,7 +316,12 @@ void dump_generations() nl(); } -void dump_objects(cell type) +void dump_generations() +{ + return vm->dump_generations(); +} + +void factorvm::dump_objects(cell type) { gc(); begin_scan(); @@ -261,10 +341,15 @@ void dump_objects(cell type) end_scan(); } +void dump_objects(cell type) +{ + return vm->dump_objects(type); +} + cell look_for; cell obj; -void find_data_references_step(cell *scan) +void factorvm::find_data_references_step(cell *scan) { if(look_for == *scan) { @@ -275,20 +360,30 @@ void find_data_references_step(cell *scan) } } -void find_data_references(cell look_for_) +void find_data_references_step(cell *scan) +{ + return vm->find_data_references_step(scan); +} + +void factorvm::find_data_references(cell look_for_) { look_for = look_for_; begin_scan(); while((obj = next_object()) != F) - do_slots(UNTAG(obj),find_data_references_step); + do_slots(UNTAG(obj),factor::find_data_references_step); end_scan(); } +void find_data_references(cell look_for_) +{ + return vm->find_data_references(look_for_); +} + /* Dump all code blocks for debugging */ -void dump_code_heap() +void factorvm::dump_code_heap() { cell reloc_size = 0, literal_size = 0; @@ -328,7 +423,12 @@ void dump_code_heap() print_cell(literal_size); print_string(" bytes of literal data\n"); } -void factorbug() +void dump_code_heap() +{ + return vm->dump_code_heap(); +} + +void factorvm::factorbug() { if(fep_disabled) { @@ -472,11 +572,21 @@ void factorbug() } } -PRIMITIVE(die) +void factorbug() +{ + return vm->factorbug(); +} + +inline void factorvm::vmprim_die() { print_string("The die word was called by the library. Unless you called it yourself,\n"); print_string("you have triggered a bug in Factor. Please report.\n"); factorbug(); } +PRIMITIVE(die) +{ + PRIMITIVE_GETVM()->vmprim_die(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 5750d434c6..6a4b85012a 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -173,6 +173,30 @@ struct factorvm { void clear_gc_stats(); inline void vmprim_become(); void inline_gc(cell *gc_roots_base, cell gc_roots_size); + + //debug + void print_chars(string* str); + void print_word(word* word, cell nesting); + void print_factor_string(string* str); + void print_array(array* array, cell nesting); + void print_tuple(tuple *tuple, cell nesting); + void print_nested_obj(cell obj, fixnum nesting); + void print_obj(cell obj); + void print_objects(cell *start, cell *end); + void print_datastack(); + void print_retainstack(); + void print_stack_frame(stack_frame *frame); + void print_callstack(); + void dump_cell(cell x); + void dump_memory(cell from, cell to); + void dump_zone(zone *z); + void dump_generations(); + void dump_objects(cell type); + void find_data_references_step(cell *scan); + void find_data_references(cell look_for_); + void dump_code_heap(); + void factorbug(); + inline void vmprim_die(); // next method here: From 72098c5f6a2a3011e9d496018cf18a7a7967c45e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:06 +0100 Subject: [PATCH 092/345] moved arrays fns into vm --- vm/arrays.cpp | 42 ++++++++++++++++++++++++++++++++++++------ vm/vm.hpp | 8 ++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/vm/arrays.cpp b/vm/arrays.cpp index f9a3f211d0..236f2d9c54 100644 --- a/vm/arrays.cpp +++ b/vm/arrays.cpp @@ -4,7 +4,7 @@ namespace factor { /* make a new array with an initial element */ -array *allot_array(cell capacity, cell fill_) +array *factorvm::allot_array(cell capacity, cell fill_) { gc_root fill(fill_); gc_root new_array(allot_array_internal(capacity)); @@ -23,15 +23,25 @@ array *allot_array(cell capacity, cell fill_) return new_array.untagged(); } +array *allot_array(cell capacity, cell fill_) +{ + return vm->allot_array(capacity,fill_); +} + /* push a new array on the stack */ -PRIMITIVE(array) +inline void factorvm::vmprim_array() { cell initial = dpop(); cell size = unbox_array_size(); dpush(tag(allot_array(size,initial))); } -cell allot_array_1(cell obj_) +PRIMITIVE(array) +{ + PRIMITIVE_GETVM()->vmprim_array(); +} + +cell factorvm::allot_array_1(cell obj_) { gc_root obj(obj_); gc_root a(allot_array_internal(1)); @@ -39,7 +49,12 @@ cell allot_array_1(cell obj_) return a.value(); } -cell allot_array_2(cell v1_, cell v2_) +cell allot_array_1(cell obj_) +{ + return vm->allot_array_1(obj_); +} + +cell factorvm::allot_array_2(cell v1_, cell v2_) { gc_root v1(v1_); gc_root v2(v2_); @@ -49,7 +64,12 @@ cell allot_array_2(cell v1_, cell v2_) return a.value(); } -cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) +cell allot_array_2(cell v1_, cell v2_) +{ + return vm->allot_array_2(v1_,v2_); +} + +cell factorvm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) { gc_root v1(v1_); gc_root v2(v2_); @@ -63,13 +83,23 @@ cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) return a.value(); } -PRIMITIVE(resize_array) +cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) +{ + return vm->allot_array_4(v1_,v2_,v3_,v4_); +} + +inline void factorvm::vmprim_resize_array() { array* a = untag_check(dpop()); cell capacity = unbox_array_size(); dpush(tag(reallot_array(a,capacity))); } +PRIMITIVE(resize_array) +{ + PRIMITIVE_GETVM()->vmprim_resize_array(); +} + void growable_array::add(cell elt_) { gc_root elt(elt_); diff --git a/vm/vm.hpp b/vm/vm.hpp index 6a4b85012a..65fea6f9f2 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -197,6 +197,14 @@ struct factorvm { void dump_code_heap(); void factorbug(); inline void vmprim_die(); + + //arrays + array *allot_array(cell capacity, cell fill_); + inline void vmprim_array(); + cell allot_array_1(cell obj_); + cell allot_array_2(cell v1_, cell v2_); + cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_); + inline void vmprim_resize_array(); // next method here: From 0f2a89cfbd50e6fb4c3fa8728b79d5d14209d165 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:06 +0100 Subject: [PATCH 093/345] moved strings fns to vm --- vm/strings.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 16 +++++++++ 2 files changed, 100 insertions(+), 14 deletions(-) diff --git a/vm/strings.cpp b/vm/strings.cpp index c70d9dfb6d..7e0506a519 100644 --- a/vm/strings.cpp +++ b/vm/strings.cpp @@ -3,7 +3,7 @@ namespace factor { -cell string_nth(string* str, cell index) +cell factorvm::string_nth(string* str, cell index) { /* If high bit is set, the most significant 16 bits of the char come from the aux vector. The least significant bit of the @@ -22,12 +22,22 @@ cell string_nth(string* str, cell index) } } -void set_string_nth_fast(string *str, cell index, cell ch) +cell string_nth(string* str, cell index) +{ + return vm->string_nth(str,index); +} + +void factorvm::set_string_nth_fast(string *str, cell index, cell ch) { str->data()[index] = ch; } -void set_string_nth_slow(string *str_, cell index, cell ch) +void set_string_nth_fast(string *str, cell index, cell ch) +{ + return vm->set_string_nth_fast(str,index,ch); +} + +void factorvm::set_string_nth_slow(string *str_, cell index, cell ch) { gc_root str(str_); @@ -54,8 +64,13 @@ void set_string_nth_slow(string *str_, cell index, cell ch) aux->data()[index] = ((ch >> 7) ^ 1); } +void set_string_nth_slow(string *str_, cell index, cell ch) +{ + return vm->set_string_nth_slow(str_,index,ch); +} + /* allocates memory */ -void set_string_nth(string *str, cell index, cell ch) +void factorvm::set_string_nth(string *str, cell index, cell ch) { if(ch <= 0x7f) set_string_nth_fast(str,index,ch); @@ -63,8 +78,13 @@ void set_string_nth(string *str, cell index, cell ch) set_string_nth_slow(str,index,ch); } +void set_string_nth(string *str, cell index, cell ch) +{ + return vm->set_string_nth(str,index,ch); +} + /* Allocates memory */ -string *allot_string_internal(cell capacity) +string *factorvm::allot_string_internal(cell capacity) { string *str = allot(string_size(capacity)); @@ -75,8 +95,13 @@ string *allot_string_internal(cell capacity) return str; } +string *allot_string_internal(cell capacity) +{ + return vm->allot_string_internal(capacity); +} + /* Allocates memory */ -void fill_string(string *str_, cell start, cell capacity, cell fill) +void factorvm::fill_string(string *str_, cell start, cell capacity, cell fill) { gc_root str(str_); @@ -91,29 +116,49 @@ void fill_string(string *str_, cell start, cell capacity, cell fill) } } +void fill_string(string *str_, cell start, cell capacity, cell fill) +{ + return vm->fill_string(str_,start,capacity,fill); +} + /* Allocates memory */ -string *allot_string(cell capacity, cell fill) +string *factorvm::allot_string(cell capacity, cell fill) { gc_root str(allot_string_internal(capacity)); fill_string(str.untagged(),0,capacity,fill); return str.untagged(); } -PRIMITIVE(string) +string *allot_string(cell capacity, cell fill) +{ + return vm->allot_string(capacity,fill); +} + +inline void factorvm::vmprim_string() { cell initial = to_cell(dpop()); cell length = unbox_array_size(); dpush(tag(allot_string(length,initial))); } -static bool reallot_string_in_place_p(string *str, cell capacity) +PRIMITIVE(string) +{ + PRIMITIVE_GETVM()->vmprim_string(); +} + +bool factorvm::reallot_string_in_place_p(string *str, cell capacity) { return in_zone(&nursery,str) && (str->aux == F || in_zone(&nursery,untag(str->aux))) && capacity <= string_capacity(str); } -string* reallot_string(string *str_, cell capacity) +bool reallot_string_in_place_p(string *str, cell capacity) +{ + return vm->reallot_string_in_place_p(str,capacity); +} + +string* factorvm::reallot_string(string *str_, cell capacity) { gc_root str(str_); @@ -155,21 +200,36 @@ string* reallot_string(string *str_, cell capacity) } } -PRIMITIVE(resize_string) +string* reallot_string(string *str_, cell capacity) +{ + return vm->reallot_string(str_,capacity); +} + +inline void factorvm::vmprim_resize_string() { string* str = untag_check(dpop()); cell capacity = unbox_array_size(); dpush(tag(reallot_string(str,capacity))); } -PRIMITIVE(string_nth) +PRIMITIVE(resize_string) +{ + PRIMITIVE_GETVM()->vmprim_resize_string(); +} + +inline void factorvm::vmprim_string_nth() { string *str = untag(dpop()); cell index = untag_fixnum(dpop()); dpush(tag_fixnum(string_nth(str,index))); } -PRIMITIVE(set_string_nth_fast) +PRIMITIVE(string_nth) +{ + PRIMITIVE_GETVM()->vmprim_string_nth(); +} + +inline void factorvm::vmprim_set_string_nth_fast() { string *str = untag(dpop()); cell index = untag_fixnum(dpop()); @@ -177,7 +237,12 @@ PRIMITIVE(set_string_nth_fast) set_string_nth_fast(str,index,value); } -PRIMITIVE(set_string_nth_slow) +PRIMITIVE(set_string_nth_fast) +{ + PRIMITIVE_GETVM()->vmprim_set_string_nth_fast(); +} + +inline void factorvm::vmprim_set_string_nth_slow() { string *str = untag(dpop()); cell index = untag_fixnum(dpop()); @@ -185,4 +250,9 @@ PRIMITIVE(set_string_nth_slow) set_string_nth_slow(str,index,value); } +PRIMITIVE(set_string_nth_slow) +{ + PRIMITIVE_GETVM()->vmprim_set_string_nth_slow(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 65fea6f9f2..087e1ebe95 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -205,6 +205,22 @@ struct factorvm { cell allot_array_2(cell v1_, cell v2_); cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_); inline void vmprim_resize_array(); + + //strings + cell string_nth(string* str, cell index); + void set_string_nth_fast(string *str, cell index, cell ch); + void set_string_nth_slow(string *str_, cell index, cell ch); + void set_string_nth(string *str, cell index, cell ch); + string *allot_string_internal(cell capacity); + void fill_string(string *str_, cell start, cell capacity, cell fill); + string *allot_string(cell capacity, cell fill); + inline void vmprim_string(); + bool reallot_string_in_place_p(string *str, cell capacity); + string* reallot_string(string *str_, cell capacity); + inline void vmprim_resize_string(); + inline void vmprim_string_nth(); + inline void vmprim_set_string_nth_fast(); + inline void vmprim_set_string_nth_slow(); // next method here: From 25d0bb756f575b31971c6d06151dfacc5e681695 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:06 +0100 Subject: [PATCH 094/345] added boolean fns to vm --- vm/booleans.cpp | 14 ++++++++++++-- vm/vm.hpp | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/vm/booleans.cpp b/vm/booleans.cpp index 8407e10099..6a1bb79298 100644 --- a/vm/booleans.cpp +++ b/vm/booleans.cpp @@ -3,14 +3,24 @@ namespace factor { -VM_C_API void box_boolean(bool value) +void factorvm::box_boolean(bool value) { dpush(value ? T : F); } -VM_C_API bool to_boolean(cell value) +VM_C_API void box_boolean(bool value) +{ + return vm->box_boolean(value); +} + +bool factorvm::to_boolean(cell value) { return value != F; } +VM_C_API bool to_boolean(cell value) +{ + return vm->to_boolean(value); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 087e1ebe95..fbdfe125d3 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -221,6 +221,10 @@ struct factorvm { inline void vmprim_string_nth(); inline void vmprim_set_string_nth_fast(); inline void vmprim_set_string_nth_slow(); + + //booleans + void box_boolean(bool value); + bool to_boolean(cell value); // next method here: From fa46b90197f363c370bbce768c680664ff0f7a2d Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:06 +0100 Subject: [PATCH 095/345] added byte_arrays fns to vm --- vm/byte_arrays.cpp | 28 ++++++++++++++++++++++++---- vm/vm.hpp | 6 ++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/vm/byte_arrays.cpp b/vm/byte_arrays.cpp index 2eda3f33c4..495e5211a7 100644 --- a/vm/byte_arrays.cpp +++ b/vm/byte_arrays.cpp @@ -3,32 +3,52 @@ namespace factor { -byte_array *allot_byte_array(cell size) +byte_array *factorvm::allot_byte_array(cell size) { byte_array *array = allot_array_internal(size); memset(array + 1,0,size); return array; } -PRIMITIVE(byte_array) +byte_array *allot_byte_array(cell size) +{ + return vm->allot_byte_array(size); +} + +inline void factorvm::vmprim_byte_array() { cell size = unbox_array_size(); dpush(tag(allot_byte_array(size))); } -PRIMITIVE(uninitialized_byte_array) +PRIMITIVE(byte_array) +{ + PRIMITIVE_GETVM()->vmprim_byte_array(); +} + +inline void factorvm::vmprim_uninitialized_byte_array() { cell size = unbox_array_size(); dpush(tag(allot_array_internal(size))); } -PRIMITIVE(resize_byte_array) +PRIMITIVE(uninitialized_byte_array) +{ + PRIMITIVE_GETVM()->vmprim_uninitialized_byte_array(); +} + +inline void factorvm::vmprim_resize_byte_array() { byte_array *array = untag_check(dpop()); cell capacity = unbox_array_size(); dpush(tag(reallot_array(array,capacity))); } +PRIMITIVE(resize_byte_array) +{ + PRIMITIVE_GETVM()->vmprim_resize_byte_array(); +} + void growable_byte_array::append_bytes(void *elts, cell len) { cell new_size = count + len; diff --git a/vm/vm.hpp b/vm/vm.hpp index fbdfe125d3..8e8d8bb9b8 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -225,6 +225,12 @@ struct factorvm { //booleans void box_boolean(bool value); bool to_boolean(cell value); + + //byte arrays + byte_array *allot_byte_array(cell size); + inline void vmprim_byte_array(); + inline void vmprim_uninitialized_byte_array(); + inline void vmprim_resize_byte_array(); // next method here: From dbbc9bb2b149c37052e12610efc2fabd7c8a2ae8 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:06 +0100 Subject: [PATCH 096/345] added tuples fns to vm --- vm/tuples.cpp | 21 ++++++++++++++++++--- vm/vm.hpp | 5 +++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/vm/tuples.cpp b/vm/tuples.cpp index d7e22bb807..c38832ac0b 100644 --- a/vm/tuples.cpp +++ b/vm/tuples.cpp @@ -4,7 +4,7 @@ namespace factor { /* push a new tuple on the stack */ -tuple *allot_tuple(cell layout_) +tuple *factorvm::allot_tuple(cell layout_) { gc_root layout(layout_); gc_root t(allot(tuple_size(layout.untagged()))); @@ -12,7 +12,12 @@ tuple *allot_tuple(cell layout_) return t.untagged(); } -PRIMITIVE(tuple) +tuple *allot_tuple(cell layout_) +{ + return vm->allot_tuple(layout_); +} + +inline void factorvm::vmprim_tuple() { gc_root layout(dpop()); tuple *t = allot_tuple(layout.value()); @@ -23,8 +28,13 @@ PRIMITIVE(tuple) dpush(tag(t)); } +PRIMITIVE(tuple) +{ + PRIMITIVE_GETVM()->vmprim_tuple(); +} + /* push a new tuple on the stack, filling its slots from the stack */ -PRIMITIVE(tuple_boa) +inline void factorvm::vmprim_tuple_boa() { gc_root layout(dpop()); gc_root t(allot_tuple(layout.value())); @@ -34,4 +44,9 @@ PRIMITIVE(tuple_boa) dpush(t.value()); } +PRIMITIVE(tuple_boa) +{ + PRIMITIVE_GETVM()->vmprim_tuple_boa(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 8e8d8bb9b8..5ddfd5cd4a 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -231,6 +231,11 @@ struct factorvm { inline void vmprim_byte_array(); inline void vmprim_uninitialized_byte_array(); inline void vmprim_resize_byte_array(); + + //tuples + tuple *allot_tuple(cell layout_); + inline void vmprim_tuple(); + inline void vmprim_tuple_boa(); // next method here: From 4f4c53c8228568655ce297feb971ee8d1685e24c Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:07 +0100 Subject: [PATCH 097/345] moved words functions to vm --- vm/vm.hpp | 8 ++++++++ vm/words.cpp | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/vm/vm.hpp b/vm/vm.hpp index 5ddfd5cd4a..c1dc417f83 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -236,6 +236,14 @@ struct factorvm { tuple *allot_tuple(cell layout_); inline void vmprim_tuple(); inline void vmprim_tuple_boa(); + + //words + word *allot_word(cell vocab_, cell name_); + inline void vmprim_word(); + inline void vmprim_word_xt(); + void update_word_xt(cell w_); + inline void vmprim_optimized_p(); + inline void vmprim_wrapper(); // next method here: diff --git a/vm/words.cpp b/vm/words.cpp index fa090c9cea..644db46896 100644 --- a/vm/words.cpp +++ b/vm/words.cpp @@ -3,7 +3,7 @@ namespace factor { -word *allot_word(cell vocab_, cell name_) +word *factorvm::allot_word(cell vocab_, cell name_) { gc_root vocab(vocab_); gc_root name(name_); @@ -31,16 +31,26 @@ word *allot_word(cell vocab_, cell name_) return new_word.untagged(); } +word *allot_word(cell vocab_, cell name_) +{ + return vm->allot_word(vocab_,name_); +} + /* ( name vocabulary -- word ) */ -PRIMITIVE(word) +inline void factorvm::vmprim_word() { cell vocab = dpop(); cell name = dpop(); dpush(tag(allot_word(vocab,name))); } +PRIMITIVE(word) +{ + PRIMITIVE_GETVM()->vmprim_word(); +} + /* word-xt ( word -- start end ) */ -PRIMITIVE(word_xt) +inline void factorvm::vmprim_word_xt() { word *w = untag_check(dpop()); code_block *code = (profiling_p ? w->profiling : w->code); @@ -48,8 +58,13 @@ PRIMITIVE(word_xt) dpush(allot_cell((cell)code + code->size)); } +PRIMITIVE(word_xt) +{ + PRIMITIVE_GETVM()->vmprim_word_xt(); +} + /* Allocates memory */ -void update_word_xt(cell w_) +void factorvm::update_word_xt(cell w_) { gc_root w(w_); @@ -64,16 +79,31 @@ void update_word_xt(cell w_) w->xt = w->code->xt(); } -PRIMITIVE(optimized_p) +void update_word_xt(cell w_) +{ + return vm->update_word_xt(w_); +} + +inline void factorvm::vmprim_optimized_p() { drepl(tag_boolean(word_optimized_p(untag_check(dpeek())))); } -PRIMITIVE(wrapper) +PRIMITIVE(optimized_p) +{ + PRIMITIVE_GETVM()->vmprim_optimized_p(); +} + +inline void factorvm::vmprim_wrapper() { wrapper *new_wrapper = allot(sizeof(wrapper)); new_wrapper->object = dpeek(); drepl(tag(new_wrapper)); } +PRIMITIVE(wrapper) +{ + PRIMITIVE_GETVM()->vmprim_wrapper(); +} + } From 552b9ecd81215100934802895d7423386c33598f Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:07 +0100 Subject: [PATCH 098/345] Dev checkpoint --- vm/math.cpp | 212 ++++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 32 ++++++++ 2 files changed, 213 insertions(+), 31 deletions(-) mode change 100644 => 100755 vm/math.cpp diff --git a/vm/math.cpp b/vm/math.cpp old mode 100644 new mode 100755 index b16557b8b7..30b32de7ed --- a/vm/math.cpp +++ b/vm/math.cpp @@ -7,19 +7,29 @@ cell bignum_zero; cell bignum_pos_one; cell bignum_neg_one; -PRIMITIVE(bignum_to_fixnum) +inline void factorvm::vmprim_bignum_to_fixnum() { drepl(tag_fixnum(bignum_to_fixnum(untag(dpeek())))); } -PRIMITIVE(float_to_fixnum) +PRIMITIVE(bignum_to_fixnum) +{ + PRIMITIVE_GETVM()->vmprim_bignum_to_fixnum(); +} + +inline void factorvm::vmprim_float_to_fixnum() { drepl(tag_fixnum(float_to_fixnum(dpeek()))); } +PRIMITIVE(float_to_fixnum) +{ + PRIMITIVE_GETVM()->vmprim_float_to_fixnum(); +} + /* Division can only overflow when we are dividing the most negative fixnum by -1. */ -PRIMITIVE(fixnum_divint) +inline void factorvm::vmprim_fixnum_divint() { fixnum y = untag_fixnum(dpop()); \ fixnum x = untag_fixnum(dpeek()); @@ -30,7 +40,12 @@ PRIMITIVE(fixnum_divint) drepl(tag_fixnum(result)); } -PRIMITIVE(fixnum_divmod) +PRIMITIVE(fixnum_divint) +{ + PRIMITIVE_GETVM()->vmprim_fixnum_divint(); +} + +inline void factorvm::vmprim_fixnum_divmod() { cell y = ((cell *)ds)[0]; cell x = ((cell *)ds)[-1]; @@ -46,26 +61,46 @@ PRIMITIVE(fixnum_divmod) } } +PRIMITIVE(fixnum_divmod) +{ + PRIMITIVE_GETVM()->vmprim_fixnum_divmod(); +} + /* * If we're shifting right by n bits, we won't overflow as long as none of the * high WORD_SIZE-TAG_BITS-n bits are set. */ -static inline fixnum sign_mask(fixnum x) +inline fixnum factorvm::sign_mask(fixnum x) { return x >> (WORD_SIZE - 1); } -static inline fixnum branchless_max(fixnum x, fixnum y) +inline fixnum sign_mask(fixnum x) +{ + return vm->sign_mask(x); +} + +inline fixnum factorvm::branchless_max(fixnum x, fixnum y) { return (x - ((x - y) & sign_mask(x - y))); } -static inline fixnum branchless_abs(fixnum x) +inline fixnum branchless_max(fixnum x, fixnum y) +{ + return vm->branchless_max(x,y); +} + +inline fixnum factorvm::branchless_abs(fixnum x) { return (x ^ sign_mask(x)) - sign_mask(x); } -PRIMITIVE(fixnum_shift) +inline fixnum branchless_abs(fixnum x) +{ + return vm->branchless_abs(x); +} + +inline void factorvm::vmprim_fixnum_shift() { fixnum y = untag_fixnum(dpop()); fixnum x = untag_fixnum(dpeek()); @@ -92,51 +127,91 @@ PRIMITIVE(fixnum_shift) fixnum_to_bignum(x),y))); } -PRIMITIVE(fixnum_to_bignum) +PRIMITIVE(fixnum_shift) +{ + PRIMITIVE_GETVM()->vmprim_fixnum_shift(); +} + +inline void factorvm::vmprim_fixnum_to_bignum() { drepl(tag(fixnum_to_bignum(untag_fixnum(dpeek())))); } -PRIMITIVE(float_to_bignum) +PRIMITIVE(fixnum_to_bignum) +{ + PRIMITIVE_GETVM()->vmprim_fixnum_to_bignum(); +} + +inline void factorvm::vmprim_float_to_bignum() { drepl(tag(float_to_bignum(dpeek()))); } +PRIMITIVE(float_to_bignum) +{ + PRIMITIVE_GETVM()->vmprim_float_to_bignum(); +} + #define POP_BIGNUMS(x,y) \ bignum * y = untag(dpop()); \ bignum * x = untag(dpop()); -PRIMITIVE(bignum_eq) +inline void factorvm::vmprim_bignum_eq() { POP_BIGNUMS(x,y); box_boolean(bignum_equal_p(x,y)); } -PRIMITIVE(bignum_add) +PRIMITIVE(bignum_eq) +{ + PRIMITIVE_GETVM()->vmprim_bignum_eq(); +} + +inline void factorvm::vmprim_bignum_add() { POP_BIGNUMS(x,y); dpush(tag(bignum_add(x,y))); } -PRIMITIVE(bignum_subtract) +PRIMITIVE(bignum_add) +{ + PRIMITIVE_GETVM()->vmprim_bignum_add(); +} + +inline void factorvm::vmprim_bignum_subtract() { POP_BIGNUMS(x,y); dpush(tag(bignum_subtract(x,y))); } -PRIMITIVE(bignum_multiply) +PRIMITIVE(bignum_subtract) +{ + PRIMITIVE_GETVM()->vmprim_bignum_subtract(); +} + +inline void factorvm::vmprim_bignum_multiply() { POP_BIGNUMS(x,y); dpush(tag(bignum_multiply(x,y))); } -PRIMITIVE(bignum_divint) +PRIMITIVE(bignum_multiply) +{ + PRIMITIVE_GETVM()->vmprim_bignum_multiply(); +} + +inline void factorvm::vmprim_bignum_divint() { POP_BIGNUMS(x,y); dpush(tag(bignum_quotient(x,y))); } -PRIMITIVE(bignum_divmod) +PRIMITIVE(bignum_divint) +{ + PRIMITIVE_GETVM()->vmprim_bignum_divint(); +} + +inline void factorvm::vmprim_bignum_divmod() { bignum *q, *r; POP_BIGNUMS(x,y); @@ -145,91 +220,166 @@ PRIMITIVE(bignum_divmod) dpush(tag(r)); } -PRIMITIVE(bignum_mod) +PRIMITIVE(bignum_divmod) +{ + PRIMITIVE_GETVM()->vmprim_bignum_divmod(); +} + +inline void factorvm::vmprim_bignum_mod() { POP_BIGNUMS(x,y); dpush(tag(bignum_remainder(x,y))); } -PRIMITIVE(bignum_and) +PRIMITIVE(bignum_mod) +{ + PRIMITIVE_GETVM()->vmprim_bignum_mod(); +} + +inline void factorvm::vmprim_bignum_and() { POP_BIGNUMS(x,y); dpush(tag(bignum_bitwise_and(x,y))); } -PRIMITIVE(bignum_or) +PRIMITIVE(bignum_and) +{ + PRIMITIVE_GETVM()->vmprim_bignum_and(); +} + +inline void factorvm::vmprim_bignum_or() { POP_BIGNUMS(x,y); dpush(tag(bignum_bitwise_ior(x,y))); } -PRIMITIVE(bignum_xor) +PRIMITIVE(bignum_or) +{ + PRIMITIVE_GETVM()->vmprim_bignum_or(); +} + +inline void factorvm::vmprim_bignum_xor() { POP_BIGNUMS(x,y); dpush(tag(bignum_bitwise_xor(x,y))); } -PRIMITIVE(bignum_shift) +PRIMITIVE(bignum_xor) +{ + PRIMITIVE_GETVM()->vmprim_bignum_xor(); +} + +inline void factorvm::vmprim_bignum_shift() { fixnum y = untag_fixnum(dpop()); bignum* x = untag(dpop()); dpush(tag(bignum_arithmetic_shift(x,y))); } -PRIMITIVE(bignum_less) +PRIMITIVE(bignum_shift) +{ + PRIMITIVE_GETVM()->vmprim_bignum_shift(); +} + +inline void factorvm::vmprim_bignum_less() { POP_BIGNUMS(x,y); box_boolean(bignum_compare(x,y) == bignum_comparison_less); } -PRIMITIVE(bignum_lesseq) +PRIMITIVE(bignum_less) +{ + PRIMITIVE_GETVM()->vmprim_bignum_less(); +} + +inline void factorvm::vmprim_bignum_lesseq() { POP_BIGNUMS(x,y); box_boolean(bignum_compare(x,y) != bignum_comparison_greater); } -PRIMITIVE(bignum_greater) +PRIMITIVE(bignum_lesseq) +{ + PRIMITIVE_GETVM()->vmprim_bignum_lesseq(); +} + +inline void factorvm::vmprim_bignum_greater() { POP_BIGNUMS(x,y); box_boolean(bignum_compare(x,y) == bignum_comparison_greater); } -PRIMITIVE(bignum_greatereq) +PRIMITIVE(bignum_greater) +{ + PRIMITIVE_GETVM()->vmprim_bignum_greater(); +} + +inline void factorvm::vmprim_bignum_greatereq() { POP_BIGNUMS(x,y); box_boolean(bignum_compare(x,y) != bignum_comparison_less); } -PRIMITIVE(bignum_not) +PRIMITIVE(bignum_greatereq) +{ + PRIMITIVE_GETVM()->vmprim_bignum_greatereq(); +} + +inline void factorvm::vmprim_bignum_not() { drepl(tag(bignum_bitwise_not(untag(dpeek())))); } -PRIMITIVE(bignum_bitp) +PRIMITIVE(bignum_not) +{ + PRIMITIVE_GETVM()->vmprim_bignum_not(); +} + +inline void factorvm::vmprim_bignum_bitp() { fixnum bit = to_fixnum(dpop()); bignum *x = untag(dpop()); box_boolean(bignum_logbitp(bit,x)); } -PRIMITIVE(bignum_log2) +PRIMITIVE(bignum_bitp) +{ + PRIMITIVE_GETVM()->vmprim_bignum_bitp(); +} + +inline void factorvm::vmprim_bignum_log2() { drepl(tag(bignum_integer_length(untag(dpeek())))); } -unsigned int bignum_producer(unsigned int digit) +PRIMITIVE(bignum_log2) +{ + PRIMITIVE_GETVM()->vmprim_bignum_log2(); +} + +unsigned int factorvm::bignum_producer(unsigned int digit) { unsigned char *ptr = (unsigned char *)alien_offset(dpeek()); return *(ptr + digit); } -PRIMITIVE(byte_array_to_bignum) +unsigned int bignum_producer(unsigned int digit) +{ + return vm->bignum_producer(digit); +} + +inline void factorvm::vmprim_byte_array_to_bignum() { cell n_digits = array_capacity(untag_check(dpeek())); - bignum * result = digit_stream_to_bignum(n_digits,bignum_producer,0x100,0); + bignum * result = factor::digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0); drepl(tag(result)); } +PRIMITIVE(byte_array_to_bignum) +{ + PRIMITIVE_GETVM()->vmprim_byte_array_to_bignum(); +} + cell unbox_array_size() { switch(tagged(dpeek()).type()) diff --git a/vm/vm.hpp b/vm/vm.hpp index c1dc417f83..3521978cae 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -244,6 +244,38 @@ struct factorvm { void update_word_xt(cell w_); inline void vmprim_optimized_p(); inline void vmprim_wrapper(); + + //math + inline void vmprim_bignum_to_fixnum(); + inline void vmprim_float_to_fixnum(); + inline void vmprim_fixnum_divint(); + inline void vmprim_fixnum_divmod(); + inline fixnum sign_mask(fixnum x); + inline fixnum branchless_max(fixnum x, fixnum y); + inline fixnum branchless_abs(fixnum x); + inline void vmprim_fixnum_shift(); + inline void vmprim_fixnum_to_bignum(); + inline void vmprim_float_to_bignum(); + inline void vmprim_bignum_eq(); + inline void vmprim_bignum_add(); + inline void vmprim_bignum_subtract(); + inline void vmprim_bignum_multiply(); + inline void vmprim_bignum_divint(); + inline void vmprim_bignum_divmod(); + inline void vmprim_bignum_mod(); + inline void vmprim_bignum_and(); + inline void vmprim_bignum_or(); + inline void vmprim_bignum_xor(); + inline void vmprim_bignum_shift(); + inline void vmprim_bignum_less(); + inline void vmprim_bignum_lesseq(); + inline void vmprim_bignum_greater(); + inline void vmprim_bignum_greatereq(); + inline void vmprim_bignum_not(); + inline void vmprim_bignum_bitp(); + inline void vmprim_bignum_log2(); + unsigned int bignum_producer(unsigned int digit); + inline void vmprim_byte_array_to_bignum(); // next method here: From 10e5dc9b3ce850feeb5f4d92a2e4a8375f3d117b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:07 +0100 Subject: [PATCH 099/345] moved math functions to vm --- vm/math.cpp | 274 +++++++++++++++++++++++++++++++++++++++++++++------- vm/vm.hpp | 42 ++++++++ 2 files changed, 279 insertions(+), 37 deletions(-) diff --git a/vm/math.cpp b/vm/math.cpp index 30b32de7ed..ac04e906a2 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -380,7 +380,7 @@ PRIMITIVE(byte_array_to_bignum) PRIMITIVE_GETVM()->vmprim_byte_array_to_bignum(); } -cell unbox_array_size() +cell factorvm::unbox_array_size() { switch(tagged(dpeek()).type()) { @@ -413,17 +413,32 @@ cell unbox_array_size() return 0; /* can't happen */ } -PRIMITIVE(fixnum_to_float) +cell unbox_array_size() +{ + return vm->unbox_array_size(); +} + +inline void factorvm::vmprim_fixnum_to_float() { drepl(allot_float(fixnum_to_float(dpeek()))); } -PRIMITIVE(bignum_to_float) +PRIMITIVE(fixnum_to_float) +{ + PRIMITIVE_GETVM()->vmprim_fixnum_to_float(); +} + +inline void factorvm::vmprim_bignum_to_float() { drepl(allot_float(bignum_to_float(dpeek()))); } -PRIMITIVE(str_to_float) +PRIMITIVE(bignum_to_float) +{ + PRIMITIVE_GETVM()->vmprim_bignum_to_float(); +} + +inline void factorvm::vmprim_str_to_float() { byte_array *bytes = untag_check(dpeek()); cell capacity = array_capacity(bytes); @@ -437,98 +452,178 @@ PRIMITIVE(str_to_float) drepl(F); } -PRIMITIVE(float_to_str) +PRIMITIVE(str_to_float) +{ + PRIMITIVE_GETVM()->vmprim_str_to_float(); +} + +inline void factorvm::vmprim_float_to_str() { byte_array *array = allot_byte_array(33); snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop())); dpush(tag(array)); } +PRIMITIVE(float_to_str) +{ + PRIMITIVE_GETVM()->vmprim_float_to_str(); +} + #define POP_FLOATS(x,y) \ double y = untag_float(dpop()); \ double x = untag_float(dpop()); -PRIMITIVE(float_eq) +inline void factorvm::vmprim_float_eq() { POP_FLOATS(x,y); box_boolean(x == y); } -PRIMITIVE(float_add) +PRIMITIVE(float_eq) +{ + PRIMITIVE_GETVM()->vmprim_float_eq(); +} + +inline void factorvm::vmprim_float_add() { POP_FLOATS(x,y); box_double(x + y); } -PRIMITIVE(float_subtract) +PRIMITIVE(float_add) +{ + PRIMITIVE_GETVM()->vmprim_float_add(); +} + +inline void factorvm::vmprim_float_subtract() { POP_FLOATS(x,y); box_double(x - y); } -PRIMITIVE(float_multiply) +PRIMITIVE(float_subtract) +{ + PRIMITIVE_GETVM()->vmprim_float_subtract(); +} + +inline void factorvm::vmprim_float_multiply() { POP_FLOATS(x,y); box_double(x * y); } -PRIMITIVE(float_divfloat) +PRIMITIVE(float_multiply) +{ + PRIMITIVE_GETVM()->vmprim_float_multiply(); +} + +inline void factorvm::vmprim_float_divfloat() { POP_FLOATS(x,y); box_double(x / y); } -PRIMITIVE(float_mod) +PRIMITIVE(float_divfloat) +{ + PRIMITIVE_GETVM()->vmprim_float_divfloat(); +} + +inline void factorvm::vmprim_float_mod() { POP_FLOATS(x,y); box_double(fmod(x,y)); } -PRIMITIVE(float_less) +PRIMITIVE(float_mod) +{ + PRIMITIVE_GETVM()->vmprim_float_mod(); +} + +inline void factorvm::vmprim_float_less() { POP_FLOATS(x,y); box_boolean(x < y); } -PRIMITIVE(float_lesseq) +PRIMITIVE(float_less) +{ + PRIMITIVE_GETVM()->vmprim_float_less(); +} + +inline void factorvm::vmprim_float_lesseq() { POP_FLOATS(x,y); box_boolean(x <= y); } -PRIMITIVE(float_greater) +PRIMITIVE(float_lesseq) +{ + PRIMITIVE_GETVM()->vmprim_float_lesseq(); +} + +inline void factorvm::vmprim_float_greater() { POP_FLOATS(x,y); box_boolean(x > y); } -PRIMITIVE(float_greatereq) +PRIMITIVE(float_greater) +{ + PRIMITIVE_GETVM()->vmprim_float_greater(); +} + +inline void factorvm::vmprim_float_greatereq() { POP_FLOATS(x,y); box_boolean(x >= y); } -PRIMITIVE(float_bits) +PRIMITIVE(float_greatereq) +{ + PRIMITIVE_GETVM()->vmprim_float_greatereq(); +} + +inline void factorvm::vmprim_float_bits() { box_unsigned_4(float_bits(untag_float_check(dpop()))); } -PRIMITIVE(bits_float) +PRIMITIVE(float_bits) +{ + PRIMITIVE_GETVM()->vmprim_float_bits(); +} + +inline void factorvm::vmprim_bits_float() { box_float(bits_float(to_cell(dpop()))); } -PRIMITIVE(double_bits) +PRIMITIVE(bits_float) +{ + PRIMITIVE_GETVM()->vmprim_bits_float(); +} + +inline void factorvm::vmprim_double_bits() { box_unsigned_8(double_bits(untag_float_check(dpop()))); } -PRIMITIVE(bits_double) +PRIMITIVE(double_bits) +{ + PRIMITIVE_GETVM()->vmprim_double_bits(); +} + +inline void factorvm::vmprim_bits_double() { box_double(bits_double(to_unsigned_8(dpop()))); } -VM_C_API fixnum to_fixnum(cell tagged) +PRIMITIVE(bits_double) +{ + PRIMITIVE_GETVM()->vmprim_bits_double(); +} + +fixnum factorvm::to_fixnum(cell tagged) { switch(TAG(tagged)) { @@ -542,52 +637,102 @@ VM_C_API fixnum to_fixnum(cell tagged) } } -VM_C_API cell to_cell(cell tagged) +VM_C_API fixnum to_fixnum(cell tagged) +{ + return vm->to_fixnum(tagged); +} + +cell factorvm::to_cell(cell tagged) { return (cell)to_fixnum(tagged); } +VM_C_API cell to_cell(cell tagged) +{ + return vm->to_cell(tagged); +} + +void factorvm::box_signed_1(s8 n) +{ + dpush(tag_fixnum(n)); +} + VM_C_API void box_signed_1(s8 n) +{ + return vm->box_signed_1(n); +} + +void factorvm::box_unsigned_1(u8 n) { dpush(tag_fixnum(n)); } VM_C_API void box_unsigned_1(u8 n) +{ + return vm->box_unsigned_1(n); +} + +void factorvm::box_signed_2(s16 n) { dpush(tag_fixnum(n)); } VM_C_API void box_signed_2(s16 n) +{ + return vm->box_signed_2(n); +} + +void factorvm::box_unsigned_2(u16 n) { dpush(tag_fixnum(n)); } VM_C_API void box_unsigned_2(u16 n) { - dpush(tag_fixnum(n)); + return vm->box_unsigned_2(n); } -VM_C_API void box_signed_4(s32 n) +void factorvm::box_signed_4(s32 n) { dpush(allot_integer(n)); } -VM_C_API void box_unsigned_4(u32 n) +VM_C_API void box_signed_4(s32 n) +{ + return vm->box_signed_4(n); +} + +void factorvm::box_unsigned_4(u32 n) { dpush(allot_cell(n)); } -VM_C_API void box_signed_cell(fixnum integer) +VM_C_API void box_unsigned_4(u32 n) +{ + return vm->box_unsigned_4(n); +} + +void factorvm::box_signed_cell(fixnum integer) { dpush(allot_integer(integer)); } -VM_C_API void box_unsigned_cell(cell cell) +VM_C_API void box_signed_cell(fixnum integer) +{ + return vm->box_signed_cell(integer); +} + +void factorvm::box_unsigned_cell(cell cell) { dpush(allot_cell(cell)); } -VM_C_API void box_signed_8(s64 n) +VM_C_API void box_unsigned_cell(cell cell) +{ + return vm->box_unsigned_cell(cell); +} + +void factorvm::box_signed_8(s64 n) { if(n < fixnum_min || n > fixnum_max) dpush(tag(long_long_to_bignum(n))); @@ -595,7 +740,12 @@ VM_C_API void box_signed_8(s64 n) dpush(tag_fixnum(n)); } -VM_C_API s64 to_signed_8(cell obj) +VM_C_API void box_signed_8(s64 n) +{ + return vm->box_signed_8(n); +} + +s64 factorvm::to_signed_8(cell obj) { switch(tagged(obj).type()) { @@ -609,7 +759,12 @@ VM_C_API s64 to_signed_8(cell obj) } } -VM_C_API void box_unsigned_8(u64 n) +VM_C_API s64 to_signed_8(cell obj) +{ + return vm->to_signed_8(obj); +} + +void factorvm::box_unsigned_8(u64 n) { if(n > (u64)fixnum_max) dpush(tag(ulong_long_to_bignum(n))); @@ -617,7 +772,12 @@ VM_C_API void box_unsigned_8(u64 n) dpush(tag_fixnum(n)); } -VM_C_API u64 to_unsigned_8(cell obj) +VM_C_API void box_unsigned_8(u64 n) +{ + return vm->box_unsigned_8(n); +} + +u64 factorvm::to_unsigned_8(cell obj) { switch(tagged(obj).type()) { @@ -631,41 +791,76 @@ VM_C_API u64 to_unsigned_8(cell obj) } } -VM_C_API void box_float(float flo) +VM_C_API u64 to_unsigned_8(cell obj) +{ + return vm->to_unsigned_8(obj); +} + +void factorvm::box_float(float flo) { dpush(allot_float(flo)); } +VM_C_API void box_float(float flo) +{ + return vm->box_float(flo); +} + +float factorvm::to_float(cell value) +{ + return untag_float_check(value); +} + VM_C_API float to_float(cell value) { - return untag_float_check(value); + return vm->to_float(value); } -VM_C_API void box_double(double flo) +void factorvm::box_double(double flo) { dpush(allot_float(flo)); } -VM_C_API double to_double(cell value) +VM_C_API void box_double(double flo) +{ + return vm->box_double(flo); +} + +double factorvm::to_double(cell value) { return untag_float_check(value); } +VM_C_API double to_double(cell value) +{ + return vm->to_double(value); +} + /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On overflow, they call these functions. */ -VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y) +void factorvm::overflow_fixnum_add(fixnum x, fixnum y) { drepl(tag(fixnum_to_bignum( untag_fixnum(x) + untag_fixnum(y)))); } -VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y) +VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y) +{ + return vm->overflow_fixnum_add(x,y); +} + +void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) { drepl(tag(fixnum_to_bignum( untag_fixnum(x) - untag_fixnum(y)))); } -VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y) +VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y) +{ + return vm->overflow_fixnum_subtract(x,y); +} + +void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) { bignum *bx = fixnum_to_bignum(x); GC_BIGNUM(bx); @@ -674,4 +869,9 @@ VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y) drepl(tag(bignum_multiply(bx,by))); } +VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y) +{ + return vm->overflow_fixnum_multiply(x,y); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 3521978cae..7df9386083 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -276,9 +276,51 @@ struct factorvm { inline void vmprim_bignum_log2(); unsigned int bignum_producer(unsigned int digit); inline void vmprim_byte_array_to_bignum(); + cell unbox_array_size(); + inline void vmprim_fixnum_to_float(); + inline void vmprim_bignum_to_float(); + inline void vmprim_str_to_float(); + inline void vmprim_float_to_str(); + inline void vmprim_float_eq(); + inline void vmprim_float_add(); + inline void vmprim_float_subtract(); + inline void vmprim_float_multiply(); + inline void vmprim_float_divfloat(); + inline void vmprim_float_mod(); + inline void vmprim_float_less(); + inline void vmprim_float_lesseq(); + inline void vmprim_float_greater(); + inline void vmprim_float_greatereq(); + inline void vmprim_float_bits(); + inline void vmprim_bits_float(); + inline void vmprim_double_bits(); + inline void vmprim_bits_double(); + fixnum to_fixnum(cell tagged); + cell to_cell(cell tagged); + void box_signed_1(s8 n); + void box_unsigned_1(u8 n); + void box_signed_2(s16 n); + void box_unsigned_2(u16 n); + void box_signed_4(s32 n); + void box_unsigned_4(u32 n); + void box_signed_cell(fixnum integer); + void box_unsigned_cell(cell cell); + void box_signed_8(s64 n); + s64 to_signed_8(cell obj); + void box_unsigned_8(u64 n); + u64 to_unsigned_8(cell obj); + void box_float(float flo); + float to_float(cell value); + void box_double(double flo); + double to_double(cell value); + void overflow_fixnum_add(fixnum x, fixnum y); + void overflow_fixnum_subtract(fixnum x, fixnum y); + void overflow_fixnum_multiply(fixnum x, fixnum y); // next method here: + + }; extern factorvm *vm; From 062c56f94b020429d730697b4206e5a9fa8b7863 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:07 +0100 Subject: [PATCH 100/345] moved io functions to vm --- vm/io.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 14 ++++++++++ 2 files changed, 86 insertions(+), 12 deletions(-) mode change 100644 => 100755 vm/io.cpp diff --git a/vm/io.cpp b/vm/io.cpp old mode 100644 new mode 100755 index 5bb5834691..570a9a2633 --- a/vm/io.cpp +++ b/vm/io.cpp @@ -14,14 +14,19 @@ The Factor library provides platform-specific code for Unix and Windows with many more capabilities so these words are not usually used in normal operation. */ -void init_c_io() +void factorvm::init_c_io() { userenv[STDIN_ENV] = allot_alien(F,(cell)stdin); userenv[STDOUT_ENV] = allot_alien(F,(cell)stdout); userenv[STDERR_ENV] = allot_alien(F,(cell)stderr); } -void io_error() +void init_c_io() +{ + return vm->init_c_io(); +} + +void factorvm::io_error() { #ifndef WINCE if(errno == EINTR) @@ -31,7 +36,12 @@ void io_error() general_error(ERROR_IO,tag_fixnum(errno),F,NULL); } -PRIMITIVE(fopen) +void io_error() +{ + return vm->io_error(); +} + +inline void factorvm::vmprim_fopen() { gc_root mode(dpop()); gc_root path(dpop()); @@ -52,7 +62,12 @@ PRIMITIVE(fopen) } } -PRIMITIVE(fgetc) +PRIMITIVE(fopen) +{ + PRIMITIVE_GETVM()->vmprim_fopen(); +} + +inline void factorvm::vmprim_fgetc() { FILE *file = (FILE *)unbox_alien(); @@ -77,7 +92,12 @@ PRIMITIVE(fgetc) } } -PRIMITIVE(fread) +PRIMITIVE(fgetc) +{ + PRIMITIVE_GETVM()->vmprim_fgetc(); +} + +inline void factorvm::vmprim_fread() { FILE *file = (FILE *)unbox_alien(); fixnum size = unbox_array_size(); @@ -117,7 +137,12 @@ PRIMITIVE(fread) } } -PRIMITIVE(fputc) +PRIMITIVE(fread) +{ + PRIMITIVE_GETVM()->vmprim_fread(); +} + +inline void factorvm::vmprim_fputc() { FILE *file = (FILE *)unbox_alien(); fixnum ch = to_fixnum(dpop()); @@ -135,7 +160,12 @@ PRIMITIVE(fputc) } } -PRIMITIVE(fwrite) +PRIMITIVE(fputc) +{ + PRIMITIVE_GETVM()->vmprim_fputc(); +} + +inline void factorvm::vmprim_fwrite() { FILE *file = (FILE *)unbox_alien(); byte_array *text = untag_check(dpop()); @@ -164,7 +194,12 @@ PRIMITIVE(fwrite) } } -PRIMITIVE(fseek) +PRIMITIVE(fwrite) +{ + PRIMITIVE_GETVM()->vmprim_fwrite(); +} + +inline void factorvm::vmprim_fseek() { int whence = to_fixnum(dpop()); FILE *file = (FILE *)unbox_alien(); @@ -189,7 +224,12 @@ PRIMITIVE(fseek) } } -PRIMITIVE(fflush) +PRIMITIVE(fseek) +{ + PRIMITIVE_GETVM()->vmprim_fseek(); +} + +inline void factorvm::vmprim_fflush() { FILE *file = (FILE *)unbox_alien(); for(;;) @@ -201,7 +241,12 @@ PRIMITIVE(fflush) } } -PRIMITIVE(fclose) +PRIMITIVE(fflush) +{ + PRIMITIVE_GETVM()->vmprim_fflush(); +} + +inline void factorvm::vmprim_fclose() { FILE *file = (FILE *)unbox_alien(); for(;;) @@ -213,17 +258,32 @@ PRIMITIVE(fclose) } } +PRIMITIVE(fclose) +{ + PRIMITIVE_GETVM()->vmprim_fclose(); +} + /* This function is used by FFI I/O. Accessing the errno global directly is not portable, since on some libc's errno is not a global but a funky macro that reads thread-local storage. */ -VM_C_API int err_no() +int factorvm::err_no() { return errno; } -VM_C_API void clear_err_no() +VM_C_API int err_no() +{ + return vm->err_no(); +} + +void factorvm::clear_err_no() { errno = 0; } +VM_C_API void clear_err_no() +{ + return vm->clear_err_no(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 7df9386083..372908b697 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -316,6 +316,20 @@ struct factorvm { void overflow_fixnum_add(fixnum x, fixnum y); void overflow_fixnum_subtract(fixnum x, fixnum y); void overflow_fixnum_multiply(fixnum x, fixnum y); + + //io + void init_c_io(); + void io_error(); + inline void vmprim_fopen(); + inline void vmprim_fgetc(); + inline void vmprim_fread(); + inline void vmprim_fputc(); + inline void vmprim_fwrite(); + inline void vmprim_fseek(); + inline void vmprim_fflush(); + inline void vmprim_fclose(); + int err_no(); + void clear_err_no(); // next method here: From fdabc9a5d83beedde377f9c9de4d1811eea53117 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:07 +0100 Subject: [PATCH 101/345] moved code_gc functions to vm --- vm/code_gc.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++------- vm/vm.hpp | 18 ++++++++ 2 files changed, 114 insertions(+), 16 deletions(-) mode change 100644 => 100755 vm/code_gc.cpp diff --git a/vm/code_gc.cpp b/vm/code_gc.cpp old mode 100644 new mode 100755 index 4710a1baa0..d229fcd3bf --- a/vm/code_gc.cpp +++ b/vm/code_gc.cpp @@ -3,15 +3,20 @@ namespace factor { -static void clear_free_list(heap *heap) +void factorvm::clear_free_list(heap *heap) { memset(&heap->free,0,sizeof(heap_free_list)); } +void clear_free_list(heap *heap) +{ + return vm->clear_free_list(heap); +} + /* This malloc-style heap code is reasonably generic. Maybe in the future, it will be used for the data heap too, if we ever get incremental mark/sweep/compact GC. */ -void new_heap(heap *heap, cell size) +void factorvm::new_heap(heap *heap, cell size) { heap->seg = alloc_segment(align_page(size)); if(!heap->seg) @@ -20,7 +25,12 @@ void new_heap(heap *heap, cell size) clear_free_list(heap); } -static void add_to_free_list(heap *heap, free_heap_block *block) +void new_heap(heap *heap, cell size) +{ + return vm->new_heap(heap,size); +} + +void factorvm::add_to_free_list(heap *heap, free_heap_block *block) { if(block->size < free_list_count * block_size_increment) { @@ -35,11 +45,16 @@ static void add_to_free_list(heap *heap, free_heap_block *block) } } +void add_to_free_list(heap *heap, free_heap_block *block) +{ + return vm->add_to_free_list(heap,block); +} + /* Called after reading the code heap from the image file, and after code GC. In the former case, we must add a large free block from compiling.base + size to compiling.limit. */ -void build_free_list(heap *heap, cell size) +void factorvm::build_free_list(heap *heap, cell size) { heap_block *prev = NULL; @@ -91,13 +106,23 @@ void build_free_list(heap *heap, cell size) } -static void assert_free_block(free_heap_block *block) +void build_free_list(heap *heap, cell size) +{ + return vm->build_free_list(heap,size); +} + +void factorvm::assert_free_block(free_heap_block *block) { if(block->status != B_FREE) critical_error("Invalid block in free list",(cell)block); } + +void assert_free_block(free_heap_block *block) +{ + return vm->assert_free_block(block); +} -static free_heap_block *find_free_block(heap *heap, cell size) +free_heap_block *factorvm::find_free_block(heap *heap, cell size) { cell attempt = size; @@ -137,7 +162,12 @@ static free_heap_block *find_free_block(heap *heap, cell size) return NULL; } -static free_heap_block *split_free_block(heap *heap, free_heap_block *block, cell size) +free_heap_block *find_free_block(heap *heap, cell size) +{ + return vm->find_free_block(heap,size); +} + +free_heap_block *factorvm::split_free_block(heap *heap, free_heap_block *block, cell size) { if(block->size != size ) { @@ -153,8 +183,13 @@ static free_heap_block *split_free_block(heap *heap, free_heap_block *block, cel return block; } +free_heap_block *split_free_block(heap *heap, free_heap_block *block, cell size) +{ + return vm->split_free_block(heap,block,size); +} + /* Allocate a block of memory from the mark and sweep GC heap */ -heap_block *heap_allot(heap *heap, cell size) +heap_block *factorvm::heap_allot(heap *heap, cell size) { size = (size + block_size_increment - 1) & ~(block_size_increment - 1); @@ -170,14 +205,24 @@ heap_block *heap_allot(heap *heap, cell size) return NULL; } +heap_block *heap_allot(heap *heap, cell size) +{ + return vm->heap_allot(heap,size); +} + /* Deallocates a block manually */ -void heap_free(heap *heap, heap_block *block) +void factorvm::heap_free(heap *heap, heap_block *block) { block->status = B_FREE; add_to_free_list(heap,(free_heap_block *)block); } -void mark_block(heap_block *block) +void heap_free(heap *heap, heap_block *block) +{ + return vm->heap_free(heap,block); +} + +void factorvm::mark_block(heap_block *block) { /* If already marked, do nothing */ switch(block->status) @@ -193,9 +238,14 @@ void mark_block(heap_block *block) } } +void mark_block(heap_block *block) +{ + return vm->mark_block(block); +} + /* If in the middle of code GC, we have to grow the heap, data GC restarts from scratch, so we have to unmark any marked blocks. */ -void unmark_marked(heap *heap) +void factorvm::unmark_marked(heap *heap) { heap_block *scan = first_block(heap); @@ -208,9 +258,14 @@ void unmark_marked(heap *heap) } } +void unmark_marked(heap *heap) +{ + return vm->unmark_marked(heap); +} + /* After code GC, all referenced code blocks have status set to B_MARKED, so any which are allocated and not marked can be reclaimed. */ -void free_unmarked(heap *heap, heap_iterator iter) +void factorvm::free_unmarked(heap *heap, heap_iterator iter) { clear_free_list(heap); @@ -257,8 +312,13 @@ void free_unmarked(heap *heap, heap_iterator iter) add_to_free_list(heap,(free_heap_block *)prev); } +void free_unmarked(heap *heap, heap_iterator iter) +{ + return vm->free_unmarked(heap,iter); +} + /* Compute total sum of sizes of free blocks, and size of largest free block */ -void heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free) +void factorvm::heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free) { *used = 0; *total_free = 0; @@ -286,8 +346,13 @@ void heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free) } } +void heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free) +{ + return vm->heap_usage(heap,used,total_free,max_free); +} + /* The size of the heap, not including the last block if it's free */ -cell heap_size(heap *heap) +cell factorvm::heap_size(heap *heap) { heap_block *scan = first_block(heap); @@ -302,8 +367,13 @@ cell heap_size(heap *heap) return heap->seg->size; } +cell heap_size(heap *heap) +{ + return vm->heap_size(heap); +} + /* Compute where each block is going to go, after compaction */ -cell compute_heap_forwarding(heap *heap, unordered_map &forwarding) +cell factorvm::compute_heap_forwarding(heap *heap, unordered_map &forwarding) { heap_block *scan = first_block(heap); char *address = (char *)first_block(heap); @@ -324,7 +394,12 @@ cell compute_heap_forwarding(heap *heap, unordered_map &for return (cell)address - heap->seg->start; } -void compact_heap(heap *heap, unordered_map &forwarding) +cell compute_heap_forwarding(heap *heap, unordered_map &forwarding) +{ + return vm->compute_heap_forwarding(heap,forwarding); +} + +void factorvm::compact_heap(heap *heap, unordered_map &forwarding) { heap_block *scan = first_block(heap); @@ -338,4 +413,9 @@ void compact_heap(heap *heap, unordered_map &forwarding) } } +void compact_heap(heap *heap, unordered_map &forwarding) +{ + return vm->compact_heap(heap,forwarding); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 372908b697..8166adb556 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -330,6 +330,24 @@ struct factorvm { inline void vmprim_fclose(); int err_no(); void clear_err_no(); + + //code_gc + void clear_free_list(heap *heap); + void new_heap(heap *heap, cell size); + void add_to_free_list(heap *heap, free_heap_block *block); + void build_free_list(heap *heap, cell size); + void assert_free_block(free_heap_block *block); + free_heap_block *find_free_block(heap *heap, cell size); + free_heap_block *split_free_block(heap *heap, free_heap_block *block, cell size); + heap_block *heap_allot(heap *heap, cell size); + void heap_free(heap *heap, heap_block *block); + void mark_block(heap_block *block); + void unmark_marked(heap *heap); + void free_unmarked(heap *heap, heap_iterator iter); + void heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free); + cell heap_size(heap *heap); + cell compute_heap_forwarding(heap *heap, unordered_map &forwarding); + void compact_heap(heap *heap, unordered_map &forwarding); // next method here: From 0097e76a82fdf18af50cfe99e46b73093536b2eb Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:07 +0100 Subject: [PATCH 102/345] moved code_block functions to vm --- vm/code_block.cpp | 247 +++++++++++++++++++++++++++++++++++++--------- vm/vm.hpp | 34 +++++++ 2 files changed, 235 insertions(+), 46 deletions(-) mode change 100644 => 100755 vm/code_block.cpp diff --git a/vm/code_block.cpp b/vm/code_block.cpp old mode 100644 new mode 100755 index aaf8e25866..ff569328be --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -3,27 +3,47 @@ namespace factor { -static relocation_type relocation_type_of(relocation_entry r) +relocation_type factorvm::relocation_type_of(relocation_entry r) { return (relocation_type)((r & 0xf0000000) >> 28); } -static relocation_class relocation_class_of(relocation_entry r) +relocation_type relocation_type_of(relocation_entry r) +{ + return vm->relocation_type_of(r); +} + +relocation_class factorvm::relocation_class_of(relocation_entry r) { return (relocation_class)((r & 0x0f000000) >> 24); } -static cell relocation_offset_of(relocation_entry r) +relocation_class relocation_class_of(relocation_entry r) +{ + return vm->relocation_class_of(r); +} + +cell factorvm::relocation_offset_of(relocation_entry r) { return (r & 0x00ffffff); } -void flush_icache_for(code_block *block) +cell relocation_offset_of(relocation_entry r) +{ + return vm->relocation_offset_of(r); +} + +void factorvm::flush_icache_for(code_block *block) { flush_icache((cell)block,block->size); } -static int number_of_parameters(relocation_type type) +void flush_icache_for(code_block *block) +{ + return vm->flush_icache_for(block); +} + +int factorvm::number_of_parameters(relocation_type type) { switch(type) { @@ -47,7 +67,12 @@ static int number_of_parameters(relocation_type type) } } -void *object_xt(cell obj) +int number_of_parameters(relocation_type type) +{ + return vm->number_of_parameters(type); +} + +void *factorvm::object_xt(cell obj) { switch(tagged(obj).type()) { @@ -61,7 +86,12 @@ void *object_xt(cell obj) } } -static void *xt_pic(word *w, cell tagged_quot) +void *object_xt(cell obj) +{ + return vm->object_xt(obj); +} + +void *factorvm::xt_pic(word *w, cell tagged_quot) { if(tagged_quot == F || max_pic_size == 0) return w->xt; @@ -75,25 +105,45 @@ static void *xt_pic(word *w, cell tagged_quot) } } -void *word_xt_pic(word *w) +void *xt_pic(word *w, cell tagged_quot) +{ + return vm->xt_pic(w,tagged_quot); +} + +void *factorvm::word_xt_pic(word *w) { return xt_pic(w,w->pic_def); } -void *word_xt_pic_tail(word *w) +void *word_xt_pic(word *w) +{ + return vm->word_xt_pic(w); +} + +void *factorvm::word_xt_pic_tail(word *w) { return xt_pic(w,w->pic_tail_def); } +void *word_xt_pic_tail(word *w) +{ + return vm->word_xt_pic_tail(w); +} + /* References to undefined symbols are patched up to call this function on image load */ -void undefined_symbol() +void factorvm::undefined_symbol() { general_error(ERROR_UNDEFINED_SYMBOL,F,F,NULL); } +void undefined_symbol() +{ + return vm->undefined_symbol(); +} + /* Look up an external library symbol referenced by a compiled code block */ -void *get_rel_symbol(array *literals, cell index) +void *factorvm::get_rel_symbol(array *literals, cell index) { cell symbol = array_nth(literals,index); cell library = array_nth(literals,index + 1); @@ -101,7 +151,7 @@ void *get_rel_symbol(array *literals, cell index) dll *d = (library == F ? NULL : untag(library)); if(d != NULL && !d->dll) - return (void *)undefined_symbol; + return (void *)factor::undefined_symbol; switch(tagged(symbol).type()) { @@ -114,7 +164,7 @@ void *get_rel_symbol(array *literals, cell index) return sym; else { - return (void *)undefined_symbol; + return (void *)factor::undefined_symbol; } } case ARRAY_TYPE: @@ -129,15 +179,20 @@ void *get_rel_symbol(array *literals, cell index) if(sym) return sym; } - return (void *)undefined_symbol; + return (void *)factor::undefined_symbol; } default: critical_error("Bad symbol specifier",symbol); - return (void *)undefined_symbol; + return (void *)factor::undefined_symbol; } } -cell compute_relocation(relocation_entry rel, cell index, code_block *compiled) +void *get_rel_symbol(array *literals, cell index) +{ + return vm->get_rel_symbol(literals,index); +} + +cell factorvm::compute_relocation(relocation_entry rel, cell index, code_block *compiled) { array *literals = untag(compiled->literals); cell offset = relocation_offset_of(rel) + (cell)compiled->xt(); @@ -179,7 +234,12 @@ cell compute_relocation(relocation_entry rel, cell index, code_block *compiled) #undef ARG } -void iterate_relocations(code_block *compiled, relocation_iterator iter) +cell compute_relocation(relocation_entry rel, cell index, code_block *compiled) +{ + return vm->compute_relocation(rel,index,compiled); +} + +void factorvm::iterate_relocations(code_block *compiled, relocation_iterator iter) { if(compiled->relocation != F) { @@ -197,15 +257,25 @@ void iterate_relocations(code_block *compiled, relocation_iterator iter) } } +void iterate_relocations(code_block *compiled, relocation_iterator iter) +{ + return vm->iterate_relocations(compiled,iter); +} + /* Store a 32-bit value into a PowerPC LIS/ORI sequence */ -static void store_address_2_2(cell *ptr, cell value) +void factorvm::store_address_2_2(cell *ptr, cell value) { ptr[-1] = ((ptr[-1] & ~0xffff) | ((value >> 16) & 0xffff)); ptr[ 0] = ((ptr[ 0] & ~0xffff) | (value & 0xffff)); } +void store_address_2_2(cell *ptr, cell value) +{ + return vm->store_address_2_2(ptr,value); +} + /* Store a value into a bitfield of a PowerPC instruction */ -static void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift) +void factorvm::store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift) { /* This is unaccurate but good enough */ fixnum test = (fixnum)mask >> 1; @@ -215,8 +285,13 @@ static void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shif *ptr = ((*ptr & ~mask) | ((value >> shift) & mask)); } +void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift) +{ + return vm->store_address_masked(ptr,value,mask,shift); +} + /* Perform a fixup on a code block */ -void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value) +void factorvm::store_address_in_code_block(cell klass, cell offset, fixnum absolute_value) { fixnum relative_value = absolute_value - offset; @@ -261,7 +336,12 @@ void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value) } } -void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled) +void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value) +{ + return vm->store_address_in_code_block(klass,offset,absolute_value); +} + +void factorvm::update_literal_references_step(relocation_entry rel, cell index, code_block *compiled) { if(relocation_type_of(rel) == RT_IMMEDIATE) { @@ -272,19 +352,29 @@ void update_literal_references_step(relocation_entry rel, cell index, code_block } } +void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled) +{ + return vm->update_literal_references_step(rel,index,compiled); +} + /* Update pointers to literals from compiled code. */ -void update_literal_references(code_block *compiled) +void factorvm::update_literal_references(code_block *compiled) { if(!compiled->needs_fixup) { - iterate_relocations(compiled,update_literal_references_step); + iterate_relocations(compiled,factor::update_literal_references_step); flush_icache_for(compiled); } } +void update_literal_references(code_block *compiled) +{ + return vm->update_literal_references(compiled); +} + /* Copy all literals referenced from a code block to newspace. Only for aging and nursery collections */ -void copy_literal_references(code_block *compiled) +void factorvm::copy_literal_references(code_block *compiled) { if(collecting_gen >= compiled->last_scan) { @@ -307,8 +397,13 @@ void copy_literal_references(code_block *compiled) } } +void copy_literal_references(code_block *compiled) +{ + return vm->copy_literal_references(compiled); +} + /* Compute an address to store at a relocation */ -void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled) +void factorvm::relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled) { #ifdef FACTOR_DEBUG tagged(compiled->literals).untag_check(); @@ -320,18 +415,28 @@ void relocate_code_block_step(relocation_entry rel, cell index, code_block *comp compute_relocation(rel,index,compiled)); } -void update_word_references_step(relocation_entry rel, cell index, code_block *compiled) +void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled) +{ + return vm->relocate_code_block_step(rel,index,compiled); +} + +void factorvm::update_word_references_step(relocation_entry rel, cell index, code_block *compiled) { relocation_type type = relocation_type_of(rel); if(type == RT_XT || type == RT_XT_PIC || type == RT_XT_PIC_TAIL) relocate_code_block_step(rel,index,compiled); } +void update_word_references_step(relocation_entry rel, cell index, code_block *compiled) +{ + return vm->update_word_references_step(rel,index,compiled); +} + /* Relocate new code blocks completely; updating references to literals, dlsyms, and words. For all other words in the code heap, we only need to update references to other words, without worrying about literals or dlsyms. */ -void update_word_references(code_block *compiled) +void factorvm::update_word_references(code_block *compiled) { if(compiled->needs_fixup) relocate_code_block(compiled); @@ -346,30 +451,45 @@ void update_word_references(code_block *compiled) heap_free(&code,compiled); else { - iterate_relocations(compiled,update_word_references_step); + iterate_relocations(compiled,factor::update_word_references_step); flush_icache_for(compiled); } } -void update_literal_and_word_references(code_block *compiled) +void update_word_references(code_block *compiled) +{ + return vm->update_word_references(compiled); +} + +void factorvm::update_literal_and_word_references(code_block *compiled) { update_literal_references(compiled); update_word_references(compiled); } -static void check_code_address(cell address) +void update_literal_and_word_references(code_block *compiled) +{ + return vm->update_literal_and_word_references(compiled); +} + +void factorvm::check_code_address(cell address) { #ifdef FACTOR_DEBUG assert(address >= code.seg->start && address < code.seg->end); #endif } +void check_code_address(cell address) +{ + return vm->check_code_address(address); +} + /* Update references to words. This is done after a new code block is added to the heap. */ /* Mark all literals referenced from a word XT. Only for tenured collections */ -void mark_code_block(code_block *compiled) +void factorvm::mark_code_block(code_block *compiled) { check_code_address((cell)compiled); @@ -379,24 +499,39 @@ void mark_code_block(code_block *compiled) copy_handle(&compiled->relocation); } -void mark_stack_frame_step(stack_frame *frame) +void mark_code_block(code_block *compiled) +{ + return vm->mark_code_block(compiled); +} + +void factorvm::mark_stack_frame_step(stack_frame *frame) { mark_code_block(frame_code(frame)); } +void mark_stack_frame_step(stack_frame *frame) +{ + return vm->mark_stack_frame_step(frame); +} + /* Mark code blocks executing in currently active stack frames. */ -void mark_active_blocks(context *stacks) +void factorvm::mark_active_blocks(context *stacks) { if(collecting_gen == data->tenured()) { cell top = (cell)stacks->callstack_top; cell bottom = (cell)stacks->callstack_bottom; - iterate_callstack(top,bottom,mark_stack_frame_step); + iterate_callstack(top,bottom,factor::mark_stack_frame_step); } } -void mark_object_code_block(object *object) +void mark_active_blocks(context *stacks) +{ + return vm->mark_active_blocks(stacks); +} + +void factorvm::mark_object_code_block(object *object) { switch(object->h.hi_tag()) { @@ -419,23 +554,33 @@ void mark_object_code_block(object *object) case CALLSTACK_TYPE: { callstack *stack = (callstack *)object; - iterate_callstack_object(stack,mark_stack_frame_step); + iterate_callstack_object(stack,factor::mark_stack_frame_step); break; } } } +void mark_object_code_block(object *object) +{ + return vm->mark_object_code_block(object); +} + /* Perform all fixups on a code block */ -void relocate_code_block(code_block *compiled) +void factorvm::relocate_code_block(code_block *compiled) { compiled->last_scan = data->nursery(); compiled->needs_fixup = false; - iterate_relocations(compiled,relocate_code_block_step); + iterate_relocations(compiled,factor::relocate_code_block_step); flush_icache_for(compiled); } +void relocate_code_block(code_block *compiled) +{ + return vm->relocate_code_block(compiled); +} + /* Fixup labels. This is done at compile time, not image load time */ -void fixup_labels(array *labels, code_block *compiled) +void factorvm::fixup_labels(array *labels, code_block *compiled) { cell i; cell size = array_capacity(labels); @@ -452,8 +597,13 @@ void fixup_labels(array *labels, code_block *compiled) } } +void fixup_labels(array *labels, code_block *compiled) +{ + return vm->fixup_labels(labels,compiled); +} + /* Might GC */ -code_block *allot_code_block(cell size) +code_block *factorvm::allot_code_block(cell size) { heap_block *block = heap_allot(&code,size + sizeof(code_block)); @@ -480,13 +630,13 @@ code_block *allot_code_block(cell size) return (code_block *)block; } +code_block *allot_code_block(cell size) +{ + return vm->allot_code_block(size); +} + /* Might GC */ -code_block *add_code_block( - cell type, - cell code_, - cell labels_, - cell relocation_, - cell literals_) +code_block *factorvm::add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_) { gc_root code(code_); gc_root labels(labels_); @@ -522,4 +672,9 @@ code_block *add_code_block( return compiled; } +code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_) +{ + return vm->add_code_block(type,code_,labels_,relocation_,literals_); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 8166adb556..e829417d0b 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -348,6 +348,40 @@ struct factorvm { cell heap_size(heap *heap); cell compute_heap_forwarding(heap *heap, unordered_map &forwarding); void compact_heap(heap *heap, unordered_map &forwarding); + + //code_block + relocation_type relocation_type_of(relocation_entry r); + relocation_class relocation_class_of(relocation_entry r); + cell relocation_offset_of(relocation_entry r); + void flush_icache_for(code_block *block); + int number_of_parameters(relocation_type type); + void *object_xt(cell obj); + void *xt_pic(word *w, cell tagged_quot); + void *word_xt_pic(word *w); + void *word_xt_pic_tail(word *w); + void undefined_symbol(); + void *get_rel_symbol(array *literals, cell index); + cell compute_relocation(relocation_entry rel, cell index, code_block *compiled); + void iterate_relocations(code_block *compiled, relocation_iterator iter); + void store_address_2_2(cell *ptr, cell value); + void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift); + void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value); + void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled); + void update_literal_references(code_block *compiled); + void copy_literal_references(code_block *compiled); + void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled); + void update_word_references_step(relocation_entry rel, cell index, code_block *compiled); + void update_word_references(code_block *compiled); + void update_literal_and_word_references(code_block *compiled); + void check_code_address(cell address); + void mark_code_block(code_block *compiled); + void mark_stack_frame_step(stack_frame *frame); + void mark_active_blocks(context *stacks); + void mark_object_code_block(object *object); + void relocate_code_block(code_block *compiled); + void fixup_labels(array *labels, code_block *compiled); + code_block *allot_code_block(cell size); + code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_); // next method here: From ee07c0b4e50a6ae7b8f2988a64aa220898c3d3ca Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:08 +0100 Subject: [PATCH 103/345] moved code_heap functions to vm --- vm/code_heap.cpp | 95 ++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 15 ++++++++ 2 files changed, 95 insertions(+), 15 deletions(-) mode change 100644 => 100755 vm/code_heap.cpp diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp old mode 100644 new mode 100755 index 2d2e975fb4..53eb011a9d --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -6,18 +6,28 @@ namespace factor heap code; /* Allocate a code heap during startup */ -void init_code_heap(cell size) +void factorvm::init_code_heap(cell size) { new_heap(&code,size); } -bool in_code_heap_p(cell ptr) +void init_code_heap(cell size) +{ + return vm->init_code_heap(size); +} + +bool factorvm::in_code_heap_p(cell ptr) { return (ptr >= code.seg->start && ptr <= code.seg->end); } +bool in_code_heap_p(cell ptr) +{ + return vm->in_code_heap_p(ptr); +} + /* Compile a word definition with the non-optimizing compiler. Allocates memory */ -void jit_compile_word(cell word_, cell def_, bool relocate) +void factorvm::jit_compile_word(cell word_, cell def_, bool relocate) { gc_root word(word_); gc_root def(def_); @@ -30,8 +40,13 @@ void jit_compile_word(cell word_, cell def_, bool relocate) if(word->pic_tail_def != F) jit_compile(word->pic_tail_def,relocate); } +void jit_compile_word(cell word_, cell def_, bool relocate) +{ + return vm->jit_compile_word(word_,def_,relocate); +} + /* Apply a function to every code block */ -void iterate_code_heap(code_heap_iterator iter) +void factorvm::iterate_code_heap(code_heap_iterator iter) { heap_block *scan = first_block(&code); @@ -43,21 +58,36 @@ void iterate_code_heap(code_heap_iterator iter) } } +void iterate_code_heap(code_heap_iterator iter) +{ + return vm->iterate_code_heap(iter); +} + /* Copy literals referenced from all code blocks to newspace. Only for aging and nursery collections */ +void factorvm::copy_code_heap_roots() +{ + iterate_code_heap(factor::copy_literal_references); +} + void copy_code_heap_roots() { - iterate_code_heap(copy_literal_references); + return vm->copy_code_heap_roots(); } /* Update pointers to words referenced from all code blocks. Only after defining a new word. */ -void update_code_heap_words() +void factorvm::update_code_heap_words() { - iterate_code_heap(update_word_references); + iterate_code_heap(factor::update_word_references); } -PRIMITIVE(modify_code_heap) +void update_code_heap_words() +{ + return vm->update_code_heap_words(); +} + +inline void factorvm::vmprim_modify_code_heap() { gc_root alist(dpop()); @@ -108,8 +138,13 @@ PRIMITIVE(modify_code_heap) update_code_heap_words(); } +PRIMITIVE(modify_code_heap) +{ + PRIMITIVE_GETVM()->vmprim_modify_code_heap(); +} + /* Push the free space and total size of the code heap */ -PRIMITIVE(code_room) +inline void factorvm::vmprim_code_room() { cell used, total_free, max_free; heap_usage(&code,&used,&total_free,&max_free); @@ -119,14 +154,24 @@ PRIMITIVE(code_room) dpush(tag_fixnum(max_free / 1024)); } +PRIMITIVE(code_room) +{ + PRIMITIVE_GETVM()->vmprim_code_room(); +} + static unordered_map forwarding; -code_block *forward_xt(code_block *compiled) +code_block *factorvm::forward_xt(code_block *compiled) { return (code_block *)forwarding[compiled]; } -void forward_frame_xt(stack_frame *frame) +code_block *forward_xt(code_block *compiled) +{ + return vm->forward_xt(compiled); +} + +void factorvm::forward_frame_xt(stack_frame *frame) { cell offset = (cell)FRAME_RETURN_ADDRESS(frame) - (cell)frame_code(frame); code_block *forwarded = forward_xt(frame_code(frame)); @@ -134,7 +179,12 @@ void forward_frame_xt(stack_frame *frame) FRAME_RETURN_ADDRESS(frame) = (void *)((cell)forwarded + offset); } -void forward_object_xts() +void forward_frame_xt(stack_frame *frame) +{ + return vm->forward_frame_xt(frame); +} + +void factorvm::forward_object_xts() { begin_scan(); @@ -165,7 +215,7 @@ void forward_object_xts() case CALLSTACK_TYPE: { callstack *stack = untag(obj); - iterate_callstack_object(stack,forward_frame_xt); + iterate_callstack_object(stack,factor::forward_frame_xt); } break; default: @@ -176,8 +226,13 @@ void forward_object_xts() end_scan(); } +void forward_object_xts() +{ + return vm->forward_object_xts(); +} + /* Set the XT fields now that the heap has been compacted */ -void fixup_object_xts() +void factorvm::fixup_object_xts() { begin_scan(); @@ -205,11 +260,16 @@ void fixup_object_xts() end_scan(); } +void fixup_object_xts() +{ + return vm->fixup_object_xts(); +} + /* Move all free space to the end of the code heap. This is not very efficient, since it makes several passes over the code and data heaps, but we only ever do this before saving a deployed image and exiting, so performaance is not critical here */ -void compact_code_heap() +void factorvm::compact_code_heap() { /* Free all unreachable code blocks */ gc(); @@ -231,4 +291,9 @@ void compact_code_heap() build_free_list(&code,size); } +void compact_code_heap() +{ + return vm->compact_code_heap(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index e829417d0b..0395876137 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -382,6 +382,21 @@ struct factorvm { void fixup_labels(array *labels, code_block *compiled); code_block *allot_code_block(cell size); code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_); + + //code_heap + void init_code_heap(cell size); + bool in_code_heap_p(cell ptr); + void jit_compile_word(cell word_, cell def_, bool relocate); + void iterate_code_heap(code_heap_iterator iter); + void copy_code_heap_roots(); + void update_code_heap_words(); + inline void vmprim_modify_code_heap(); + inline void vmprim_code_room(); + code_block *forward_xt(code_block *compiled); + void forward_frame_xt(stack_frame *frame); + void forward_object_xts(); + void fixup_object_xts(); + void compact_code_heap(); // next method here: From 1bba717b368857a0f8eaced0a6f019c54f72f294 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:08 +0100 Subject: [PATCH 104/345] moved image functions to vm --- vm/image.cpp | 132 +++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 20 ++++++++ 2 files changed, 131 insertions(+), 21 deletions(-) diff --git a/vm/image.cpp b/vm/image.cpp index de9de1acf1..7724e28f72 100644 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -4,7 +4,7 @@ namespace factor { /* Certain special objects in the image are known to the runtime */ -static void init_objects(image_header *h) +void factorvm::init_objects(image_header *h) { memcpy(userenv,h->userenv,sizeof(userenv)); @@ -14,9 +14,14 @@ static void init_objects(image_header *h) bignum_neg_one = h->bignum_neg_one; } +void init_objects(image_header *h) +{ + return vm->init_objects(h); +} + cell data_relocation_base; -static void load_data_heap(FILE *file, image_header *h, vm_parameters *p) +void factorvm::load_data_heap(FILE *file, image_header *h, vm_parameters *p) { cell good_size = h->data_size + (1 << 20); @@ -49,9 +54,14 @@ static void load_data_heap(FILE *file, image_header *h, vm_parameters *p) data_relocation_base = h->data_relocation_base; } +void load_data_heap(FILE *file, image_header *h, vm_parameters *p) +{ + return vm->load_data_heap(file,h,p); +} + cell code_relocation_base; -static void load_code_heap(FILE *file, image_header *h, vm_parameters *p) +void factorvm::load_code_heap(FILE *file, image_header *h, vm_parameters *p) { if(h->code_size > p->code_size) fatal_error("Code heap too small to fit image",h->code_size); @@ -76,8 +86,13 @@ static void load_code_heap(FILE *file, image_header *h, vm_parameters *p) build_free_list(&code,h->code_size); } +void load_code_heap(FILE *file, image_header *h, vm_parameters *p) +{ + return vm->load_code_heap(file,h,p); +} + /* Save the current image to disk */ -bool save_image(const vm_char *filename) +bool factorvm::save_image(const vm_char *filename) { FILE* file; image_header h; @@ -122,7 +137,12 @@ bool save_image(const vm_char *filename) return ok; } -PRIMITIVE(save_image) +bool save_image(const vm_char *filename) +{ + return vm->save_image(filename); +} + +inline void factorvm::vmprim_save_image() { /* do a full GC to push everything into tenured space */ gc(); @@ -132,8 +152,13 @@ PRIMITIVE(save_image) save_image((vm_char *)(path.untagged() + 1)); } -PRIMITIVE(save_image_and_exit) -{ +PRIMITIVE(save_image) +{ + PRIMITIVE_GETVM()->vmprim_save_image(); +} + +inline void factorvm::vmprim_save_image_and_exit() +{ /* We unbox this before doing anything else. This is the only point where we might throw an error, so we have to throw an error here since later steps destroy the current image. */ @@ -158,7 +183,12 @@ PRIMITIVE(save_image_and_exit) exit(1); } -static void data_fixup(cell *cell) +PRIMITIVE(save_image_and_exit) +{ + PRIMITIVE_GETVM()->vmprim_save_image_and_exit(); +} + +void factorvm::data_fixup(cell *cell) { if(immediate_p(*cell)) return; @@ -167,14 +197,24 @@ static void data_fixup(cell *cell) *cell += (tenured->start - data_relocation_base); } -template void code_fixup(T **handle) +void data_fixup(cell *cell) +{ + return vm->data_fixup(cell); +} + +template void factorvm::code_fixup(T **handle) { T *ptr = *handle; T *new_ptr = (T *)(((cell)ptr) + (code.seg->start - code_relocation_base)); *handle = new_ptr; } -static void fixup_word(word *word) +template void code_fixup(T **handle) +{ + return vm->code_fixup(handle); +} + +void factorvm::fixup_word(word *word) { if(word->code) code_fixup(&word->code); @@ -183,7 +223,12 @@ static void fixup_word(word *word) code_fixup(&word->xt); } -static void fixup_quotation(quotation *quot) +void fixup_word(word *word) +{ + return vm->fixup_word(word); +} + +void factorvm::fixup_quotation(quotation *quot) { if(quot->code) { @@ -194,24 +239,44 @@ static void fixup_quotation(quotation *quot) quot->xt = (void *)lazy_jit_compile; } -static void fixup_alien(alien *d) +void fixup_quotation(quotation *quot) +{ + return vm->fixup_quotation(quot); +} + +void factorvm::fixup_alien(alien *d) { d->expired = T; } -static void fixup_stack_frame(stack_frame *frame) +void fixup_alien(alien *d) +{ + return vm->fixup_alien(d); +} + +void factorvm::fixup_stack_frame(stack_frame *frame) { code_fixup(&frame->xt); code_fixup(&FRAME_RETURN_ADDRESS(frame)); } -static void fixup_callstack_object(callstack *stack) +void fixup_stack_frame(stack_frame *frame) { - iterate_callstack_object(stack,fixup_stack_frame); + return vm->fixup_stack_frame(frame); +} + +void factorvm::fixup_callstack_object(callstack *stack) +{ + iterate_callstack_object(stack,factor::fixup_stack_frame); +} + +void fixup_callstack_object(callstack *stack) +{ + return vm->fixup_callstack_object(stack); } /* Initialize an object in a newly-loaded image */ -static void relocate_object(object *object) +void factorvm::relocate_object(object *object) { cell hi_tag = object->h.hi_tag(); @@ -231,7 +296,7 @@ static void relocate_object(object *object) } else { - do_slots((cell)object,data_fixup); + do_slots((cell)object,factor::data_fixup); switch(hi_tag) { @@ -254,9 +319,14 @@ static void relocate_object(object *object) } } +void relocate_object(object *object) +{ + return vm->relocate_object(object); +} + /* Since the image might have been saved with a different base address than where it is loaded, we need to fix up pointers in the image. */ -void relocate_data() +void factorvm::relocate_data() { cell relocating; @@ -281,7 +351,12 @@ void relocate_data() } } -static void fixup_code_block(code_block *compiled) +void relocate_data() +{ + return vm->relocate_data(); +} + +void factorvm::fixup_code_block(code_block *compiled) { /* relocate literal table data */ data_fixup(&compiled->relocation); @@ -290,14 +365,24 @@ static void fixup_code_block(code_block *compiled) relocate_code_block(compiled); } +void fixup_code_block(code_block *compiled) +{ + return vm->fixup_code_block(compiled); +} + +void factorvm::relocate_code() +{ + iterate_code_heap(factor::fixup_code_block); +} + void relocate_code() { - iterate_code_heap(fixup_code_block); + return vm->relocate_code(); } /* Read an image file from disk, only done once during startup */ /* This function also initializes the data and code heaps */ -void load_image(vm_parameters *p) +void factorvm::load_image(vm_parameters *p) { FILE *file = OPEN_READ(p->image_path); if(file == NULL) @@ -331,4 +416,9 @@ void load_image(vm_parameters *p) userenv[IMAGE_ENV] = allot_alien(F,(cell)p->image_path); } +void load_image(vm_parameters *p) +{ + return vm->load_image(p); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 0395876137..21effc10c3 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -397,6 +397,26 @@ struct factorvm { void forward_object_xts(); void fixup_object_xts(); void compact_code_heap(); + + //image + void init_objects(image_header *h); + void load_data_heap(FILE *file, image_header *h, vm_parameters *p); + void load_code_heap(FILE *file, image_header *h, vm_parameters *p); + bool save_image(const vm_char *filename); + inline void vmprim_save_image(); + inline void vmprim_save_image_and_exit(); + void data_fixup(cell *cell); + template void code_fixup(T **handle); + void fixup_word(word *word); + void fixup_quotation(quotation *quot); + void fixup_alien(alien *d); + void fixup_stack_frame(stack_frame *frame); + void fixup_callstack_object(callstack *stack); + void relocate_object(object *object); + void relocate_data(); + void fixup_code_block(code_block *compiled); + void relocate_code(); + void load_image(vm_parameters *p); // next method here: From 28620619e93465e0b7248e1a498874d519e5699b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:08 +0100 Subject: [PATCH 105/345] moved alien functions to vm --- vm/alien.cpp | 112 +++++++++++++++++++++++++++++++++++------ vm/callstack.cpp | 126 ++++++++++++++++++++++++++++++++++++++++------- vm/data_gc.cpp | 2 +- vm/profiler.cpp | 2 +- vm/vm.hpp | 38 ++++++++++++++ 5 files changed, 244 insertions(+), 36 deletions(-) mode change 100644 => 100755 vm/alien.cpp mode change 100644 => 100755 vm/callstack.cpp diff --git a/vm/alien.cpp b/vm/alien.cpp old mode 100644 new mode 100755 index 13764a8e50..3a99d89afb --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -5,7 +5,7 @@ namespace factor /* gets the address of an object representing a C pointer, with the intention of storing the pointer across code which may potentially GC. */ -char *pinned_alien_offset(cell obj) +char *factorvm::pinned_alien_offset(cell obj) { switch(tagged(obj).type()) { @@ -24,8 +24,13 @@ char *pinned_alien_offset(cell obj) } } +char *pinned_alien_offset(cell obj) +{ + return vm->pinned_alien_offset(obj); +} + /* make an alien */ -cell allot_alien(cell delegate_, cell displacement) +cell factorvm::allot_alien(cell delegate_, cell displacement) { gc_root delegate(delegate_); gc_root new_alien(allot(sizeof(alien))); @@ -45,8 +50,13 @@ cell allot_alien(cell delegate_, cell displacement) return new_alien.value(); } +cell allot_alien(cell delegate_, cell displacement) +{ + return vm->allot_alien(delegate_,displacement); +} + /* make an alien pointing at an offset of another alien */ -PRIMITIVE(displaced_alien) +inline void factorvm::vmprim_displaced_alien() { cell alien = dpop(); cell displacement = to_cell(dpop()); @@ -69,20 +79,35 @@ PRIMITIVE(displaced_alien) } } +PRIMITIVE(displaced_alien) +{ + PRIMITIVE_GETVM()->vmprim_displaced_alien(); +} + /* address of an object representing a C pointer. Explicitly throw an error if the object is a byte array, as a sanity check. */ -PRIMITIVE(alien_address) +inline void factorvm::vmprim_alien_address() { box_unsigned_cell((cell)pinned_alien_offset(dpop())); } +PRIMITIVE(alien_address) +{ + PRIMITIVE_GETVM()->vmprim_alien_address(); +} + /* pop ( alien n ) from datastack, return alien's address plus n */ -static void *alien_pointer() +void *factorvm::alien_pointer() { fixnum offset = to_fixnum(dpop()); return unbox_alien() + offset; } +void *alien_pointer() +{ + return vm->alien_pointer(); +} + /* define words to read/write values at an alien address */ #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \ PRIMITIVE(alien_##name) \ @@ -111,7 +136,7 @@ DEFINE_ALIEN_ACCESSOR(double,double,box_double,to_double) DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset) /* open a native library and push a handle */ -PRIMITIVE(dlopen) +inline void factorvm::vmprim_dlopen() { gc_root path(dpop()); path.untag_check(); @@ -121,8 +146,13 @@ PRIMITIVE(dlopen) dpush(library.value()); } +PRIMITIVE(dlopen) +{ + PRIMITIVE_GETVM()->vmprim_dlopen(); +} + /* look up a symbol in a native library */ -PRIMITIVE(dlsym) +inline void factorvm::vmprim_dlsym() { gc_root library(dpop()); gc_root name(dpop()); @@ -143,15 +173,25 @@ PRIMITIVE(dlsym) } } +PRIMITIVE(dlsym) +{ + PRIMITIVE_GETVM()->vmprim_dlsym(); +} + /* close a native library handle */ -PRIMITIVE(dlclose) +inline void factorvm::vmprim_dlclose() { dll *d = untag_check(dpop()); if(d->dll != NULL) ffi_dlclose(d); } -PRIMITIVE(dll_validp) +PRIMITIVE(dlclose) +{ + PRIMITIVE_GETVM()->vmprim_dlclose(); +} + +inline void factorvm::vmprim_dll_validp() { cell library = dpop(); if(library == F) @@ -160,8 +200,13 @@ PRIMITIVE(dll_validp) dpush(untag_check(library)->dll == NULL ? F : T); } +PRIMITIVE(dll_validp) +{ + PRIMITIVE_GETVM()->vmprim_dll_validp(); +} + /* gets the address of an object representing a C pointer */ -VM_C_API char *alien_offset(cell obj) +char *factorvm::alien_offset(cell obj) { switch(tagged(obj).type()) { @@ -182,14 +227,24 @@ VM_C_API char *alien_offset(cell obj) } } +VM_C_API char *alien_offset(cell obj) +{ + return vm->alien_offset(obj); +} + /* pop an object representing a C pointer */ -VM_C_API char *unbox_alien() +char *factorvm::unbox_alien() { return alien_offset(dpop()); } +VM_C_API char *unbox_alien() +{ + return vm->unbox_alien(); +} + /* make an alien and push */ -VM_C_API void box_alien(void *ptr) +void factorvm::box_alien(void *ptr) { if(ptr == NULL) dpush(F); @@ -197,22 +252,37 @@ VM_C_API void box_alien(void *ptr) dpush(allot_alien(F,(cell)ptr)); } +VM_C_API void box_alien(void *ptr) +{ + return vm->box_alien(ptr); +} + /* for FFI calls passing structs by value */ -VM_C_API void to_value_struct(cell src, void *dest, cell size) +void factorvm::to_value_struct(cell src, void *dest, cell size) { memcpy(dest,alien_offset(src),size); } +VM_C_API void to_value_struct(cell src, void *dest, cell size) +{ + return vm->to_value_struct(src,dest,size); +} + /* for FFI callbacks receiving structs by value */ -VM_C_API void box_value_struct(void *src, cell size) +void factorvm::box_value_struct(void *src, cell size) { byte_array *bytes = allot_byte_array(size); memcpy(bytes->data(),src,size); dpush(tag(bytes)); } +VM_C_API void box_value_struct(void *src, cell size) +{ + return vm->box_value_struct(src,size); +} + /* On some x86 OSes, structs <= 8 bytes are returned in registers. */ -VM_C_API void box_small_struct(cell x, cell y, cell size) +void factorvm::box_small_struct(cell x, cell y, cell size) { cell data[2]; data[0] = x; @@ -220,8 +290,13 @@ VM_C_API void box_small_struct(cell x, cell y, cell size) box_value_struct(data,size); } +VM_C_API void box_small_struct(cell x, cell y, cell size) +{ + return vm->box_small_struct(x,y,size); +} + /* On OS X/PPC, complex numbers are returned in registers. */ -VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) +void factorvm::box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) { cell data[4]; data[0] = x1; @@ -231,4 +306,9 @@ VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) box_value_struct(data,size); } +VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) +{ + return vm->box_medium_struct(x1, x2, x3, x4, size); +} + } diff --git a/vm/callstack.cpp b/vm/callstack.cpp old mode 100644 new mode 100755 index 39988ae976..e82314b8d2 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -3,7 +3,7 @@ namespace factor { -static void check_frame(stack_frame *frame) +void factorvm::check_frame(stack_frame *frame) { #ifdef FACTOR_DEBUG check_code_pointer((cell)frame->xt); @@ -11,14 +11,24 @@ static void check_frame(stack_frame *frame) #endif } -callstack *allot_callstack(cell size) +void check_frame(stack_frame *frame) +{ + return vm->check_frame(frame); +} + +callstack *factorvm::allot_callstack(cell size) { callstack *stack = allot(callstack_size(size)); stack->length = tag_fixnum(size); return stack; } -stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom) +callstack *allot_callstack(cell size) +{ + return vm->allot_callstack(size); +} + +stack_frame *factorvm::fix_callstack_top(stack_frame *top, stack_frame *bottom) { stack_frame *frame = bottom - 1; @@ -28,6 +38,11 @@ stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom) return frame + 1; } +stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom) +{ + return vm->fix_callstack_top(top,bottom); +} + /* We ignore the topmost frame, the one calling 'callstack', so that set-callstack doesn't get stuck in an infinite loop. @@ -35,7 +50,7 @@ This means that if 'callstack' is called in tail position, we will have popped a necessary frame... however this word is only called by continuation implementation, and user code shouldn't be calling it at all, so we leave it as it is for now. */ -stack_frame *capture_start() +stack_frame *factorvm::capture_start() { stack_frame *frame = stack_chain->callstack_bottom - 1; while(frame >= stack_chain->callstack_top @@ -46,7 +61,12 @@ stack_frame *capture_start() return frame + 1; } -PRIMITIVE(callstack) +stack_frame *capture_start() +{ + return vm->capture_start(); +} + +inline void factorvm::vmprim_callstack() { stack_frame *top = capture_start(); stack_frame *bottom = stack_chain->callstack_bottom; @@ -60,7 +80,12 @@ PRIMITIVE(callstack) dpush(tag(stack)); } -PRIMITIVE(set_callstack) +PRIMITIVE(callstack) +{ + PRIMITIVE_GETVM()->vmprim_callstack(); +} + +inline void factorvm::vmprim_set_callstack() { callstack *stack = untag_check(dpop()); @@ -73,18 +98,33 @@ PRIMITIVE(set_callstack) critical_error("Bug in set_callstack()",0); } -code_block *frame_code(stack_frame *frame) +PRIMITIVE(set_callstack) +{ + PRIMITIVE_GETVM()->vmprim_set_callstack(); +} + +code_block *factorvm::frame_code(stack_frame *frame) { check_frame(frame); return (code_block *)frame->xt - 1; } -cell frame_type(stack_frame *frame) +code_block *frame_code(stack_frame *frame) +{ + return vm->frame_code(frame); +} + +cell factorvm::frame_type(stack_frame *frame) { return frame_code(frame)->type; } -cell frame_executing(stack_frame *frame) +cell frame_type(stack_frame *frame) +{ + return vm->frame_type(frame); +} + +cell factorvm::frame_executing(stack_frame *frame) { code_block *compiled = frame_code(frame); if(compiled->literals == F || !stack_traces_p()) @@ -98,14 +138,24 @@ cell frame_executing(stack_frame *frame) } } -stack_frame *frame_successor(stack_frame *frame) +cell frame_executing(stack_frame *frame) +{ + return vm->frame_executing(frame); +} + +stack_frame *factorvm::frame_successor(stack_frame *frame) { check_frame(frame); return (stack_frame *)((cell)frame - frame->size); } +stack_frame *frame_successor(stack_frame *frame) +{ + return vm->frame_successor(frame); +} + /* Allocates memory */ -cell frame_scan(stack_frame *frame) +cell factorvm::frame_scan(stack_frame *frame) { switch(frame_type(frame)) { @@ -131,6 +181,11 @@ cell frame_scan(stack_frame *frame) } } +cell frame_scan(stack_frame *frame) +{ + return vm->frame_scan(frame); +} + namespace { @@ -149,7 +204,7 @@ struct stack_frame_accumulator { } -PRIMITIVE(callstack_to_array) +inline void factorvm::vmprim_callstack_to_array() { gc_root callstack(dpop()); @@ -160,7 +215,12 @@ PRIMITIVE(callstack_to_array) dpush(accum.frames.elements.value()); } -stack_frame *innermost_stack_frame(callstack *stack) +PRIMITIVE(callstack_to_array) +{ + PRIMITIVE_GETVM()->vmprim_callstack_to_array(); +} + +stack_frame *factorvm::innermost_stack_frame(callstack *stack) { stack_frame *top = stack->top(); stack_frame *bottom = stack->bottom(); @@ -172,26 +232,46 @@ stack_frame *innermost_stack_frame(callstack *stack) return frame; } -stack_frame *innermost_stack_frame_quot(callstack *callstack) +stack_frame *innermost_stack_frame(callstack *stack) +{ + return vm->innermost_stack_frame(stack); +} + +stack_frame *factorvm::innermost_stack_frame_quot(callstack *callstack) { stack_frame *inner = innermost_stack_frame(callstack); tagged(frame_executing(inner)).untag_check(); return inner; } +stack_frame *innermost_stack_frame_quot(callstack *callstack) +{ + return vm->innermost_stack_frame_quot(callstack); +} + /* Some primitives implementing a limited form of callstack mutation. Used by the single stepper. */ -PRIMITIVE(innermost_stack_frame_executing) +inline void factorvm::vmprim_innermost_stack_frame_executing() { dpush(frame_executing(innermost_stack_frame(untag_check(dpop())))); } -PRIMITIVE(innermost_stack_frame_scan) +PRIMITIVE(innermost_stack_frame_executing) +{ + PRIMITIVE_GETVM()->vmprim_innermost_stack_frame_executing(); +} + +inline void factorvm::vmprim_innermost_stack_frame_scan() { dpush(frame_scan(innermost_stack_frame_quot(untag_check(dpop())))); } -PRIMITIVE(set_innermost_stack_frame_quot) +PRIMITIVE(innermost_stack_frame_scan) +{ + PRIMITIVE_GETVM()->vmprim_innermost_stack_frame_scan(); +} + +inline void factorvm::vmprim_set_innermost_stack_frame_quot() { gc_root callstack(dpop()); gc_root quot(dpop()); @@ -207,10 +287,20 @@ PRIMITIVE(set_innermost_stack_frame_quot) FRAME_RETURN_ADDRESS(inner) = (char *)quot->xt + offset; } +PRIMITIVE(set_innermost_stack_frame_quot) +{ + PRIMITIVE_GETVM()->vmprim_set_innermost_stack_frame_quot(); +} + /* called before entry into Factor code. */ -VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom) +void factorvm::save_callstack_bottom(stack_frame *callstack_bottom) { stack_chain->callstack_bottom = callstack_bottom; } +VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom) +{ + return vm->save_callstack_bottom(callstack_bottom); +} + } diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index ea7100fc02..35d6812290 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -686,7 +686,7 @@ void factorvm::garbage_collection(cell gen,bool growing_data_heap_,cell requeste code_heap_scans++; if(collecting_gen == data->tenured()) - free_unmarked(&code,(heap_iterator)update_literal_and_word_references); + free_unmarked(&code,(heap_iterator)factor::update_literal_and_word_references); else copy_code_heap_roots(); diff --git a/vm/profiler.cpp b/vm/profiler.cpp index 7b8f04a3bd..054fe01537 100755 --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -56,7 +56,7 @@ void factorvm::set_profiling(bool profiling) } /* Update XTs in code heap */ - iterate_code_heap(relocate_code_block); + iterate_code_heap(factor::relocate_code_block); } void set_profiling(bool profiling) diff --git a/vm/vm.hpp b/vm/vm.hpp index 21effc10c3..063627b9b1 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -417,6 +417,44 @@ struct factorvm { void fixup_code_block(code_block *compiled); void relocate_code(); void load_image(vm_parameters *p); + + //callstack + void check_frame(stack_frame *frame); + callstack *allot_callstack(cell size); + stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom); + stack_frame *capture_start(); + inline void vmprim_callstack(); + inline void vmprim_set_callstack(); + code_block *frame_code(stack_frame *frame); + cell frame_type(stack_frame *frame); + cell frame_executing(stack_frame *frame); + stack_frame *frame_successor(stack_frame *frame); + cell frame_scan(stack_frame *frame); + inline void vmprim_callstack_to_array(); + stack_frame *innermost_stack_frame(callstack *stack); + stack_frame *innermost_stack_frame_quot(callstack *callstack); + inline void vmprim_innermost_stack_frame_executing(); + inline void vmprim_innermost_stack_frame_scan(); + inline void vmprim_set_innermost_stack_frame_quot(); + void save_callstack_bottom(stack_frame *callstack_bottom); + + //alien + char *pinned_alien_offset(cell obj); + cell allot_alien(cell delegate_, cell displacement); + inline void vmprim_displaced_alien(); + inline void vmprim_alien_address(); + void *alien_pointer(); + inline void vmprim_dlopen(); + inline void vmprim_dlsym(); + inline void vmprim_dlclose(); + inline void vmprim_dll_validp(); + char *alien_offset(cell obj); + char *unbox_alien(); + void box_alien(void *ptr); + void to_value_struct(cell src, void *dest, cell size); + void box_value_struct(void *src, cell size); + void box_small_struct(cell x, cell y, cell size); + void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size); // next method here: From 2eca2ddeafde5e3715d6d70d1a3f9bb7e5728d51 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:08 +0100 Subject: [PATCH 106/345] moved quotations functions to vm --- vm/quotations.cpp | 65 +++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 11 ++++++++ 2 files changed, 66 insertions(+), 10 deletions(-) mode change 100644 => 100755 vm/quotations.cpp diff --git a/vm/quotations.cpp b/vm/quotations.cpp old mode 100644 new mode 100755 index e96af39766..a093e0e9df --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -265,7 +265,7 @@ void quotation_jit::iterate_quotation() } } -void set_quot_xt(quotation *quot, code_block *code) +void factorvm::set_quot_xt(quotation *quot, code_block *code) { if(code->type != QUOTATION_TYPE) critical_error("Bad param to set_quot_xt",(cell)code); @@ -274,8 +274,13 @@ void set_quot_xt(quotation *quot, code_block *code) quot->xt = code->xt(); } +void set_quot_xt(quotation *quot, code_block *code) +{ + return vm->set_quot_xt(quot,code); +} + /* Allocates memory */ -void jit_compile(cell quot_, bool relocating) +void factorvm::jit_compile(cell quot_, bool relocating) { gc_root quot(quot_); if(quot->code) return; @@ -289,13 +294,23 @@ void jit_compile(cell quot_, bool relocating) if(relocating) relocate_code_block(compiled); } -PRIMITIVE(jit_compile) +void jit_compile(cell quot_, bool relocating) +{ + return vm->jit_compile(quot_,relocating); +} + +inline void factorvm::vmprim_jit_compile() { jit_compile(dpop(),true); } +PRIMITIVE(jit_compile) +{ + PRIMITIVE_GETVM()->vmprim_jit_compile(); +} + /* push a new quotation on the stack */ -PRIMITIVE(array_to_quotation) +inline void factorvm::vmprim_array_to_quotation() { quotation *quot = allot(sizeof(quotation)); quot->array = dpeek(); @@ -306,13 +321,23 @@ PRIMITIVE(array_to_quotation) drepl(tag(quot)); } -PRIMITIVE(quotation_xt) +PRIMITIVE(array_to_quotation) +{ + PRIMITIVE_GETVM()->vmprim_array_to_quotation(); +} + +inline void factorvm::vmprim_quotation_xt() { quotation *quot = untag_check(dpeek()); drepl(allot_cell((cell)quot->xt)); } -void compile_all_words() +PRIMITIVE(quotation_xt) +{ + PRIMITIVE_GETVM()->vmprim_quotation_xt(); +} + +void factorvm::compile_all_words() { gc_root words(find_all_words()); @@ -329,11 +354,16 @@ void compile_all_words() } - iterate_code_heap(relocate_code_block); + iterate_code_heap(factor::relocate_code_block); +} + +void compile_all_words() +{ + return vm->compile_all_words(); } /* Allocates memory */ -fixnum quot_code_offset_to_scan(cell quot_, cell offset) +fixnum factorvm::quot_code_offset_to_scan(cell quot_, cell offset) { gc_root quot(quot_); gc_root array(quot->array); @@ -345,7 +375,12 @@ fixnum quot_code_offset_to_scan(cell quot_, cell offset) return compiler.get_position(); } -VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack) +fixnum quot_code_offset_to_scan(cell quot_, cell offset) +{ + return vm->quot_code_offset_to_scan(quot_,offset); +} + +cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) { gc_root quot(quot_); stack_chain->callstack_top = stack; @@ -353,11 +388,21 @@ VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack) return quot.value(); } -PRIMITIVE(quot_compiled_p) +VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack) +{ + return vm->lazy_jit_compile_impl(quot_,stack); +} + +inline void factorvm::vmprim_quot_compiled_p() { tagged quot(dpop()); quot.untag_check(); dpush(tag_boolean(quot->code != NULL)); } +PRIMITIVE(quot_compiled_p) +{ + PRIMITIVE_GETVM()->vmprim_quot_compiled_p(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 063627b9b1..bd3a01ac8f 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -455,6 +455,17 @@ struct factorvm { void box_value_struct(void *src, cell size); void box_small_struct(cell x, cell y, cell size); void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size); + + //quotations + inline void vmprim_jit_compile(); + inline void vmprim_array_to_quotation(); + inline void vmprim_quotation_xt(); + void set_quot_xt(quotation *quot, code_block *code); + void jit_compile(cell quot_, bool relocating); + void compile_all_words(); + fixnum quot_code_offset_to_scan(cell quot_, cell offset); + cell lazy_jit_compile_impl(cell quot_, stack_frame *stack); + inline void vmprim_quot_compiled_p(); // next method here: From c018372cd3ffc7104daa34c03ee8ab536b91eb25 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:08 +0100 Subject: [PATCH 107/345] moved dispatch functions to vm --- vm/dispatch.cpp | 105 +++++++++++++++++++++++++++++++++++++++++------- vm/vm.hpp | 17 ++++++++ 2 files changed, 107 insertions(+), 15 deletions(-) mode change 100644 => 100755 vm/dispatch.cpp diff --git a/vm/dispatch.cpp b/vm/dispatch.cpp old mode 100644 new mode 100755 index 4a1411733e..fbfc0f79cf --- a/vm/dispatch.cpp +++ b/vm/dispatch.cpp @@ -6,7 +6,7 @@ namespace factor cell megamorphic_cache_hits; cell megamorphic_cache_misses; -static cell search_lookup_alist(cell table, cell klass) +cell factorvm::search_lookup_alist(cell table, cell klass) { array *elements = untag(table); fixnum index = array_capacity(elements) - 2; @@ -21,7 +21,12 @@ static cell search_lookup_alist(cell table, cell klass) return F; } -static cell search_lookup_hash(cell table, cell klass, cell hashcode) +cell search_lookup_alist(cell table, cell klass) +{ + return vm->search_lookup_alist(table,klass); +} + +cell factorvm::search_lookup_hash(cell table, cell klass, cell hashcode) { array *buckets = untag(table); cell bucket = array_nth(buckets,hashcode & (array_capacity(buckets) - 1)); @@ -31,19 +36,34 @@ static cell search_lookup_hash(cell table, cell klass, cell hashcode) return search_lookup_alist(bucket,klass); } -static cell nth_superclass(tuple_layout *layout, fixnum echelon) +cell search_lookup_hash(cell table, cell klass, cell hashcode) +{ + return vm->search_lookup_hash(table,klass,hashcode); +} + +cell factorvm::nth_superclass(tuple_layout *layout, fixnum echelon) { cell *ptr = (cell *)(layout + 1); return ptr[echelon * 2]; } -static cell nth_hashcode(tuple_layout *layout, fixnum echelon) +cell nth_superclass(tuple_layout *layout, fixnum echelon) +{ + return vm->nth_superclass(layout,echelon); +} + +cell factorvm::nth_hashcode(tuple_layout *layout, fixnum echelon) { cell *ptr = (cell *)(layout + 1); return ptr[echelon * 2 + 1]; } -static cell lookup_tuple_method(cell obj, cell methods) +cell nth_hashcode(tuple_layout *layout, fixnum echelon) +{ + return vm->nth_hashcode(layout,echelon); +} + +cell factorvm::lookup_tuple_method(cell obj, cell methods) { tuple_layout *layout = untag(untag(obj)->layout); @@ -75,7 +95,12 @@ static cell lookup_tuple_method(cell obj, cell methods) return F; } -static cell lookup_hi_tag_method(cell obj, cell methods) +cell lookup_tuple_method(cell obj, cell methods) +{ + return vm->lookup_tuple_method(obj,methods); +} + +cell factorvm::lookup_hi_tag_method(cell obj, cell methods) { array *hi_tag_methods = untag(methods); cell tag = untag(obj)->h.hi_tag() - HEADER_TYPE; @@ -85,7 +110,12 @@ static cell lookup_hi_tag_method(cell obj, cell methods) return array_nth(hi_tag_methods,tag); } -static cell lookup_hairy_method(cell obj, cell methods) +cell lookup_hi_tag_method(cell obj, cell methods) +{ + return vm->lookup_hi_tag_method(obj,methods); +} + +cell factorvm::lookup_hairy_method(cell obj, cell methods) { cell method = array_nth(untag(methods),TAG(obj)); if(tagged(method).type_p(WORD_TYPE)) @@ -107,7 +137,12 @@ static cell lookup_hairy_method(cell obj, cell methods) } } -cell lookup_method(cell obj, cell methods) +cell lookup_hairy_method(cell obj, cell methods) +{ + return vm->lookup_hairy_method(obj,methods); +} + +cell factorvm::lookup_method(cell obj, cell methods) { cell tag = TAG(obj); if(tag == TUPLE_TYPE || tag == OBJECT_TYPE) @@ -116,14 +151,24 @@ cell lookup_method(cell obj, cell methods) return array_nth(untag(methods),TAG(obj)); } -PRIMITIVE(lookup_method) +cell lookup_method(cell obj, cell methods) +{ + return vm->lookup_method(obj,methods); +} + +inline void factorvm::vmprim_lookup_method() { cell methods = dpop(); cell obj = dpop(); dpush(lookup_method(obj,methods)); } -cell object_class(cell obj) +PRIMITIVE(lookup_method) +{ + PRIMITIVE_GETVM()->vmprim_lookup_method(); +} + +cell factorvm::object_class(cell obj) { switch(TAG(obj)) { @@ -136,13 +181,23 @@ cell object_class(cell obj) } } -static cell method_cache_hashcode(cell klass, array *array) +cell object_class(cell obj) +{ + return vm->object_class(obj); +} + +cell factorvm::method_cache_hashcode(cell klass, array *array) { cell capacity = (array_capacity(array) >> 1) - 1; return ((klass >> TAG_BITS) & capacity) << 1; } -static void update_method_cache(cell cache, cell klass, cell method) +cell method_cache_hashcode(cell klass, array *array) +{ + return vm->method_cache_hashcode(klass,array); +} + +void factorvm::update_method_cache(cell cache, cell klass, cell method) { array *cache_elements = untag(cache); cell hashcode = method_cache_hashcode(klass,cache_elements); @@ -150,7 +205,12 @@ static void update_method_cache(cell cache, cell klass, cell method) set_array_nth(cache_elements,hashcode + 1,method); } -PRIMITIVE(mega_cache_miss) +void update_method_cache(cell cache, cell klass, cell method) +{ + return vm->update_method_cache(cache,klass,method); +} + +inline void factorvm::vmprim_mega_cache_miss() { megamorphic_cache_misses++; @@ -167,12 +227,22 @@ PRIMITIVE(mega_cache_miss) dpush(method); } -PRIMITIVE(reset_dispatch_stats) +PRIMITIVE(mega_cache_miss) +{ + PRIMITIVE_GETVM()->vmprim_mega_cache_miss(); +} + +inline void factorvm::vmprim_reset_dispatch_stats() { megamorphic_cache_hits = megamorphic_cache_misses = 0; } -PRIMITIVE(dispatch_stats) +PRIMITIVE(reset_dispatch_stats) +{ + PRIMITIVE_GETVM()->vmprim_reset_dispatch_stats(); +} + +inline void factorvm::vmprim_dispatch_stats() { growable_array stats; stats.add(allot_cell(megamorphic_cache_hits)); @@ -181,6 +251,11 @@ PRIMITIVE(dispatch_stats) dpush(stats.elements.value()); } +PRIMITIVE(dispatch_stats) +{ + PRIMITIVE_GETVM()->vmprim_dispatch_stats(); +} + void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_) { gc_root methods(methods_); diff --git a/vm/vm.hpp b/vm/vm.hpp index bd3a01ac8f..9e3e802b4f 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -466,6 +466,23 @@ struct factorvm { fixnum quot_code_offset_to_scan(cell quot_, cell offset); cell lazy_jit_compile_impl(cell quot_, stack_frame *stack); inline void vmprim_quot_compiled_p(); + + //dispatch + cell search_lookup_alist(cell table, cell klass); + cell search_lookup_hash(cell table, cell klass, cell hashcode); + cell nth_superclass(tuple_layout *layout, fixnum echelon); + cell nth_hashcode(tuple_layout *layout, fixnum echelon); + cell lookup_tuple_method(cell obj, cell methods); + cell lookup_hi_tag_method(cell obj, cell methods); + cell lookup_hairy_method(cell obj, cell methods); + cell lookup_method(cell obj, cell methods); + inline void vmprim_lookup_method(); + cell object_class(cell obj); + cell method_cache_hashcode(cell klass, array *array); + void update_method_cache(cell cache, cell klass, cell method); + inline void vmprim_mega_cache_miss(); + inline void vmprim_reset_dispatch_stats(); + inline void vmprim_dispatch_stats(); // next method here: From 5980165829bdd3e790e887f99007262f785f957d Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:08 +0100 Subject: [PATCH 108/345] moved inline_cache functions to vm --- vm/inline_cache.cpp | 88 ++++++++++++++++++++++++++++++++++++--------- vm/vm.hpp | 14 ++++++++ 2 files changed, 86 insertions(+), 16 deletions(-) mode change 100644 => 100755 vm/inline_cache.cpp diff --git a/vm/inline_cache.cpp b/vm/inline_cache.cpp old mode 100644 new mode 100755 index e9e098de70..05a977a67d --- a/vm/inline_cache.cpp +++ b/vm/inline_cache.cpp @@ -12,12 +12,17 @@ cell pic_to_mega_transitions; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ cell pic_counts[4]; -void init_inline_caching(int max_size) +void factorvm::init_inline_caching(int max_size) { max_pic_size = max_size; } -void deallocate_inline_cache(cell return_address) +void init_inline_caching(int max_size) +{ + return vm->init_inline_caching(max_size); +} + +void factorvm::deallocate_inline_cache(cell return_address) { /* Find the call target. */ void *old_xt = get_call_target(return_address); @@ -36,9 +41,14 @@ void deallocate_inline_cache(cell return_address) heap_free(&code,old_block); } +void deallocate_inline_cache(cell return_address) +{ + return vm->deallocate_inline_cache(return_address); +} + /* Figure out what kind of type check the PIC needs based on the methods it contains */ -static cell determine_inline_cache_type(array *cache_entries) +cell factorvm::determine_inline_cache_type(array *cache_entries) { bool seen_hi_tag = false, seen_tuple = false; @@ -75,11 +85,21 @@ static cell determine_inline_cache_type(array *cache_entries) return 0; } -static void update_pic_count(cell type) +cell determine_inline_cache_type(array *cache_entries) +{ + return vm->determine_inline_cache_type(cache_entries); +} + +void factorvm::update_pic_count(cell type) { pic_counts[type - PIC_TAG]++; } +void update_pic_count(cell type) +{ + return vm->update_pic_count(type); +} + struct inline_cache_jit : public jit { fixnum index; @@ -147,11 +167,7 @@ void inline_cache_jit::compile_inline_cache(fixnum index, word_special(userenv[tail_call_p ? PIC_MISS_TAIL_WORD : PIC_MISS_WORD]); } -static code_block *compile_inline_cache(fixnum index, - cell generic_word_, - cell methods_, - cell cache_entries_, - bool tail_call_p) +code_block *factorvm::compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p) { gc_root generic_word(generic_word_); gc_root methods(methods_); @@ -168,19 +184,34 @@ static code_block *compile_inline_cache(fixnum index, return code; } +code_block *compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p) +{ + return vm->compile_inline_cache(index,generic_word_,methods_,cache_entries_,tail_call_p); +} + /* A generic word's definition performs general method lookup. Allocates memory */ -static void *megamorphic_call_stub(cell generic_word) +void *factorvm::megamorphic_call_stub(cell generic_word) { return untag(generic_word)->xt; } -static cell inline_cache_size(cell cache_entries) +void *megamorphic_call_stub(cell generic_word) +{ + return vm->megamorphic_call_stub(generic_word); +} + +cell factorvm::inline_cache_size(cell cache_entries) { return array_capacity(untag_check(cache_entries)) / 2; } +cell inline_cache_size(cell cache_entries) +{ + return vm->inline_cache_size(cache_entries); +} + /* Allocates memory */ -static cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_) +cell factorvm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_) { gc_root cache_entries(cache_entries_); gc_root klass(klass_); @@ -193,7 +224,12 @@ static cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method return new_cache_entries.value(); } -static void update_pic_transitions(cell pic_size) +cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_) +{ + return vm->add_inline_cache_entry(cache_entries_,klass_,method_); +} + +void factorvm::update_pic_transitions(cell pic_size) { if(pic_size == max_pic_size) pic_to_mega_transitions++; @@ -203,9 +239,14 @@ static void update_pic_transitions(cell pic_size) ic_to_pic_transitions++; } +void update_pic_transitions(cell pic_size) +{ + return vm->update_pic_transitions(pic_size); +} + /* The cache_entries parameter is either f (on cold call site) or an array (on cache miss). Called from assembly with the actual return address */ -void *inline_cache_miss(cell return_address) +void *factorvm::inline_cache_miss(cell return_address) { check_code_pointer(return_address); @@ -257,14 +298,24 @@ void *inline_cache_miss(cell return_address) return xt; } -PRIMITIVE(reset_inline_cache_stats) +void *inline_cache_miss(cell return_address) +{ + return vm->inline_cache_miss(return_address); +} + +inline void factorvm::vmprim_reset_inline_cache_stats() { cold_call_to_ic_transitions = ic_to_pic_transitions = pic_to_mega_transitions = 0; cell i; for(i = 0; i < 4; i++) pic_counts[i] = 0; } -PRIMITIVE(inline_cache_stats) +PRIMITIVE(reset_inline_cache_stats) +{ + PRIMITIVE_GETVM()->vmprim_reset_inline_cache_stats(); +} + +inline void factorvm::vmprim_inline_cache_stats() { growable_array stats; stats.add(allot_cell(cold_call_to_ic_transitions)); @@ -277,4 +328,9 @@ PRIMITIVE(inline_cache_stats) dpush(stats.elements.value()); } +PRIMITIVE(inline_cache_stats) +{ + PRIMITIVE_GETVM()->vmprim_inline_cache_stats(); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 9e3e802b4f..7b23f07ba8 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -483,6 +483,20 @@ struct factorvm { inline void vmprim_mega_cache_miss(); inline void vmprim_reset_dispatch_stats(); inline void vmprim_dispatch_stats(); + + //inline cache + void init_inline_caching(int max_size); + void deallocate_inline_cache(cell return_address); + cell determine_inline_cache_type(array *cache_entries); + void update_pic_count(cell type); + code_block *compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p); + void *megamorphic_call_stub(cell generic_word); + cell inline_cache_size(cell cache_entries); + cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_); + void update_pic_transitions(cell pic_size); + void *inline_cache_miss(cell return_address); + inline void vmprim_reset_inline_cache_stats(); + inline void vmprim_inline_cache_stats(); // next method here: From f88eaa0df3577e72210ee285363c6dc7c89cb9d2 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:09 +0100 Subject: [PATCH 109/345] moved factor.cpp functions to vm --- vm/factor.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++-------- vm/vm.hpp | 15 +++++++++ 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 5dd88402bc..6a7180ccd7 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -5,7 +5,7 @@ namespace factor factorvm *vm; -VM_C_API void default_parameters(vm_parameters *p) +void factorvm::default_parameters(vm_parameters *p) { p->image_path = NULL; @@ -45,7 +45,12 @@ VM_C_API void default_parameters(vm_parameters *p) p->stack_traces = true; } -static bool factor_arg(const vm_char* str, const vm_char* arg, cell* value) +VM_C_API void default_parameters(vm_parameters *p) +{ + return vm->default_parameters(p); +} + +bool factorvm::factor_arg(const vm_char* str, const vm_char* arg, cell* value) { int val; if(SSCANF(str,arg,&val) > 0) @@ -57,7 +62,12 @@ static bool factor_arg(const vm_char* str, const vm_char* arg, cell* value) return false; } -VM_C_API void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv) +bool factor_arg(const vm_char* str, const vm_char* arg, cell* value) +{ + return vm->factor_arg(str,arg,value); +} + +void factorvm::init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv) { default_parameters(p); p->executable_path = argv[0]; @@ -82,8 +92,13 @@ VM_C_API void init_parameters_from_args(vm_parameters *p, int argc, vm_char **ar } } +VM_C_API void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv) +{ + return vm->init_parameters_from_args(p,argc,argv); +} + /* Do some initialization that we do once only */ -static void do_stage1_init() +void factorvm::do_stage1_init() { print_string("*** Stage 2 early init... "); fflush(stdout); @@ -95,7 +110,12 @@ static void do_stage1_init() fflush(stdout); } -VM_C_API void init_factor(vm_parameters *p) +void do_stage1_init() +{ + return vm->do_stage1_init(); +} + +void factorvm::init_factor(vm_parameters *p) { vm = new factorvm; /* Kilobytes */ @@ -152,8 +172,13 @@ VM_C_API void init_factor(vm_parameters *p) } } +VM_C_API void init_factor(vm_parameters *p) +{ + return vm->init_factor(p); +} + /* May allocate memory */ -VM_C_API void pass_args_to_factor(int argc, vm_char **argv) +void factorvm::pass_args_to_factor(int argc, vm_char **argv) { growable_array args; int i; @@ -165,7 +190,12 @@ VM_C_API void pass_args_to_factor(int argc, vm_char **argv) userenv[ARGS_ENV] = args.elements.value(); } -static void start_factor(vm_parameters *p) +VM_C_API void pass_args_to_factor(int argc, vm_char **argv) +{ + return vm->pass_args_to_factor(argc,argv); +} + +void factorvm::start_factor(vm_parameters *p) { if(p->fep) factorbug(); @@ -174,13 +204,23 @@ static void start_factor(vm_parameters *p) unnest_stacks(); } -VM_C_API void start_embedded_factor(vm_parameters *p) +void start_factor(vm_parameters *p) +{ + return vm->start_factor(p); +} + +void factorvm::start_embedded_factor(vm_parameters *p) { userenv[EMBEDDED_ENV] = T; start_factor(p); } -VM_C_API void start_standalone_factor(int argc, vm_char **argv) +VM_C_API void start_embedded_factor(vm_parameters *p) +{ + return vm->start_embedded_factor(p); +} + +void factorvm::start_standalone_factor(int argc, vm_char **argv) { vm_parameters p; default_parameters(&p); @@ -190,27 +230,52 @@ VM_C_API void start_standalone_factor(int argc, vm_char **argv) start_factor(&p); } -VM_C_API char *factor_eval_string(char *string) +VM_C_API void start_standalone_factor(int argc, vm_char **argv) +{ + return vm->start_standalone_factor(argc,argv); +} + +char *factorvm::factor_eval_string(char *string) { char *(*callback)(char *) = (char *(*)(char *))alien_offset(userenv[EVAL_CALLBACK_ENV]); return callback(string); } -VM_C_API void factor_eval_free(char *result) +VM_C_API char *factor_eval_string(char *string) +{ + return vm->factor_eval_string(string); +} + +void factorvm::factor_eval_free(char *result) { free(result); } -VM_C_API void factor_yield() +VM_C_API void factor_eval_free(char *result) +{ + return vm->factor_eval_free(result); +} + +void factorvm::factor_yield() { void (*callback)() = (void (*)())alien_offset(userenv[YIELD_CALLBACK_ENV]); callback(); } -VM_C_API void factor_sleep(long us) +VM_C_API void factor_yield() +{ + return vm->factor_yield(); +} + +void factorvm::factor_sleep(long us) { void (*callback)(long) = (void (*)(long))alien_offset(userenv[SLEEP_CALLBACK_ENV]); callback(us); } +VM_C_API void factor_sleep(long us) +{ + return vm->factor_sleep(us); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 7b23f07ba8..2c4cf21144 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -497,6 +497,21 @@ struct factorvm { void *inline_cache_miss(cell return_address); inline void vmprim_reset_inline_cache_stats(); inline void vmprim_inline_cache_stats(); + + //factor + void default_parameters(vm_parameters *p); + bool factor_arg(const vm_char* str, const vm_char* arg, cell* value); + void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv); + void do_stage1_init(); + void init_factor(vm_parameters *p); + void pass_args_to_factor(int argc, vm_char **argv); + void start_factor(vm_parameters *p); + void start_embedded_factor(vm_parameters *p); + void start_standalone_factor(int argc, vm_char **argv); + char *factor_eval_string(char *string); + void factor_eval_free(char *result); + void factor_yield(); + void factor_sleep(long us); // next method here: From a826496a7185e5ad594c9aa60fcbca6d667e4228 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:09 +0100 Subject: [PATCH 110/345] moved utilities.cpp functions to vm --- vm/utilities.cpp | 63 +++++++++++++++++++++++++++++++++++++++++------- vm/vm.hpp | 11 +++++++++ 2 files changed, 65 insertions(+), 9 deletions(-) mode change 100644 => 100755 vm/utilities.cpp diff --git a/vm/utilities.cpp b/vm/utilities.cpp old mode 100644 new mode 100755 index 37fe28948e..ce17e8f526 --- a/vm/utilities.cpp +++ b/vm/utilities.cpp @@ -4,57 +4,102 @@ namespace factor { /* If memory allocation fails, bail out */ -void *safe_malloc(size_t size) +void *factorvm::safe_malloc(size_t size) { void *ptr = malloc(size); if(!ptr) fatal_error("Out of memory in safe_malloc", 0); return ptr; } -vm_char *safe_strdup(const vm_char *str) +void *safe_malloc(size_t size) +{ + return vm->safe_malloc(size); +} + +vm_char *factorvm::safe_strdup(const vm_char *str) { vm_char *ptr = STRDUP(str); if(!ptr) fatal_error("Out of memory in safe_strdup", 0); return ptr; } +vm_char *safe_strdup(const vm_char *str) +{ + return vm->safe_strdup(str); +} + /* We don't use printf directly, because format directives are not portable. Instead we define the common cases here. */ -void nl() +void factorvm::nl() { fputs("\n",stdout); } -void print_string(const char *str) +void nl() +{ + return vm->nl(); +} + +void factorvm::print_string(const char *str) { fputs(str,stdout); } -void print_cell(cell x) +void print_string(const char *str) +{ + return vm->print_string(str); +} + +void factorvm::print_cell(cell x) { printf(CELL_FORMAT,x); } -void print_cell_hex(cell x) +void print_cell(cell x) +{ + return vm->print_cell(x); +} + +void factorvm::print_cell_hex(cell x) { printf(CELL_HEX_FORMAT,x); } -void print_cell_hex_pad(cell x) +void print_cell_hex(cell x) +{ + return vm->print_cell_hex(x); +} + +void factorvm::print_cell_hex_pad(cell x) { printf(CELL_HEX_PAD_FORMAT,x); } -void print_fixnum(fixnum x) +void print_cell_hex_pad(cell x) +{ + return vm->print_cell_hex_pad(x); +} + +void factorvm::print_fixnum(fixnum x) { printf(FIXNUM_FORMAT,x); } -cell read_cell_hex() +void print_fixnum(fixnum x) +{ + return vm->print_fixnum(x); +} + +cell factorvm::read_cell_hex() { cell cell; if(scanf(CELL_HEX_FORMAT,&cell) < 0) exit(1); return cell; +} + +cell read_cell_hex() +{ + return vm->read_cell_hex(); }; } diff --git a/vm/vm.hpp b/vm/vm.hpp index 2c4cf21144..bae1b61c09 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -512,6 +512,17 @@ struct factorvm { void factor_eval_free(char *result); void factor_yield(); void factor_sleep(long us); + + //utilities + void *safe_malloc(size_t size); + vm_char *safe_strdup(const vm_char *str); + void nl(); + void print_string(const char *str); + void print_cell(cell x); + void print_cell_hex(cell x); + void print_cell_hex_pad(cell x); + void print_fixnum(fixnum x); + cell read_cell_hex(); // next method here: From 8fea98ad7a9170a50120c5b444ebdf627788b49d Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:09 +0100 Subject: [PATCH 111/345] factorvm initialised globally --- vm/factor.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 6a7180ccd7..a75f8da8c2 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -3,7 +3,7 @@ namespace factor { -factorvm *vm; +factorvm *vm = new factorvm; void factorvm::default_parameters(vm_parameters *p) { @@ -117,7 +117,6 @@ void do_stage1_init() void factorvm::init_factor(vm_parameters *p) { - vm = new factorvm; /* Kilobytes */ p->ds_size = align_page(p->ds_size << 10); p->rs_size = align_page(p->rs_size << 10); From 386dafe7479b33472302c3ebae99761faaf2a902 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:09 +0100 Subject: [PATCH 112/345] moved local roots state to vm, shuffled includes around --- vm/code_gc.hpp | 2 +- vm/code_heap.hpp | 2 +- vm/jit.hpp | 1 + vm/local_roots.cpp | 5 ----- vm/local_roots.hpp | 25 +++++++++++-------------- vm/master.hpp | 5 +++-- vm/stacks.hpp | 8 ++++---- vm/vm.hpp | 17 +++++++++++++++++ 8 files changed, 38 insertions(+), 27 deletions(-) mode change 100644 => 100755 vm/code_gc.hpp mode change 100644 => 100755 vm/code_heap.hpp diff --git a/vm/code_gc.hpp b/vm/code_gc.hpp old mode 100644 new mode 100755 index 1cfafb69c2..ed59cc5919 --- a/vm/code_gc.hpp +++ b/vm/code_gc.hpp @@ -14,7 +14,7 @@ struct heap { heap_free_list free; }; -typedef void (*heap_iterator)(heap_block *compiled); +//typedef void (*heap_iterator)(heap_block *compiled); void new_heap(heap *h, cell size); void build_free_list(heap *h, cell size); diff --git a/vm/code_heap.hpp b/vm/code_heap.hpp old mode 100644 new mode 100755 index 6f139a4728..a77a89b827 --- a/vm/code_heap.hpp +++ b/vm/code_heap.hpp @@ -10,7 +10,7 @@ bool in_code_heap_p(cell ptr); void jit_compile_word(cell word, cell def, bool relocate); -typedef void (*code_heap_iterator)(code_block *compiled); +//typedef void (*code_heap_iterator)(code_block *compiled); void iterate_code_heap(code_heap_iterator iter); diff --git a/vm/jit.hpp b/vm/jit.hpp index 50b40eca30..69ab0f9595 100644 --- a/vm/jit.hpp +++ b/vm/jit.hpp @@ -10,6 +10,7 @@ struct jit { bool computing_offset_p; fixnum position; cell offset; + //factorvm *vm; jit(cell jit_type, cell owner); void compute_position(cell offset); diff --git a/vm/local_roots.cpp b/vm/local_roots.cpp index 7e1b2da76a..71baee6deb 100644 --- a/vm/local_roots.cpp +++ b/vm/local_roots.cpp @@ -2,9 +2,4 @@ namespace factor { - -std::vector gc_locals; - -std::vector gc_bignums; - } diff --git a/vm/local_roots.hpp b/vm/local_roots.hpp index d67622fc0a..ac9b322d5f 100644 --- a/vm/local_roots.hpp +++ b/vm/local_roots.hpp @@ -1,16 +1,14 @@ namespace factor { -/* If a runtime function needs to call another function which potentially -allocates memory, it must wrap any local variable references to Factor -objects in gc_root instances */ -extern std::vector gc_locals; +struct factorvm; template struct gc_root : public tagged { - void push() { check_tagged_pointer(tagged::value()); gc_locals.push_back((cell)this); } + void push() { check_tagged_pointer(tagged::value()); vm->gc_locals.push_back((cell)this); } + //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } explicit gc_root(cell value_) : tagged(value_) { push(); } explicit gc_root(T *value_) : tagged(value_) { push(); } @@ -19,30 +17,29 @@ struct gc_root : public tagged ~gc_root() { #ifdef FACTOR_DEBUG - assert(gc_locals.back() == (cell)this); + assert(vm->gc_locals.back() == (cell)this); #endif - gc_locals.pop_back(); + vm->gc_locals.pop_back(); } }; /* A similar hack for the bignum implementation */ -extern std::vector gc_bignums; - struct gc_bignum { bignum **addr; - - gc_bignum(bignum **addr_) : addr(addr_) { + factorvm *myvm; + //gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { + gc_bignum(bignum **addr_) : addr(addr_), myvm(vm) { if(*addr_) check_data_pointer(*addr_); - gc_bignums.push_back((cell)addr); + vm->gc_bignums.push_back((cell)addr); } ~gc_bignum() { #ifdef FACTOR_DEBUG - assert(gc_bignums.back() == (cell)addr); + assert(vm->gc_bignums.back() == (cell)addr); #endif - gc_bignums.pop_back(); + vm->gc_bignums.pop_back(); } }; diff --git a/vm/master.hpp b/vm/master.hpp index c1b62d41ff..2f9b19d12b 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -46,6 +46,8 @@ #include "errors.hpp" #include "bignumint.hpp" #include "bignum.hpp" +#include "code_block.hpp" +#include "vm.hpp" #include "data_heap.hpp" #include "write_barrier.hpp" #include "data_gc.hpp" @@ -62,7 +64,6 @@ #include "float_bits.hpp" #include "io.hpp" #include "code_gc.hpp" -#include "code_block.hpp" #include "code_heap.hpp" #include "image.hpp" #include "callstack.hpp" @@ -74,6 +75,6 @@ #include "factor.hpp" #include "utilities.hpp" -#include "vm.hpp" + #endif /* __FACTOR_MASTER_H__ */ diff --git a/vm/stacks.hpp b/vm/stacks.hpp index bc1aac8154..4906d107bc 100644 --- a/vm/stacks.hpp +++ b/vm/stacks.hpp @@ -2,15 +2,15 @@ namespace factor { #define DEFPUSHPOP(prefix,ptr) \ - inline static cell prefix##peek() { return *(cell *)ptr; } \ - inline static void prefix##repl(cell tagged) { *(cell *)ptr = tagged; } \ - inline static cell prefix##pop() \ + inline cell prefix##peek() { return *(cell *)ptr; } \ + inline void prefix##repl(cell tagged) { *(cell *)ptr = tagged; } \ + inline cell prefix##pop() \ { \ cell value = prefix##peek(); \ ptr -= sizeof(cell); \ return value; \ } \ - inline static void prefix##push(cell tagged) \ + inline void prefix##push(cell tagged) \ { \ ptr += sizeof(cell); \ prefix##repl(tagged); \ diff --git a/vm/vm.hpp b/vm/vm.hpp index bae1b61c09..2326f23148 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -1,6 +1,19 @@ namespace factor { +struct heap; +struct data_heap; +struct data; +struct zone; +struct vm_parameters; +struct image_header; + +typedef u8 card; +typedef u8 card_deck; + +typedef void (*heap_iterator)(heap_block *compiled); +typedef void (*code_heap_iterator)(code_block *compiled); + struct factorvm { // contexts @@ -174,6 +187,10 @@ struct factorvm { inline void vmprim_become(); void inline_gc(cell *gc_roots_base, cell gc_roots_size); + // local roots + std::vector gc_locals; + std::vector gc_bignums; + //debug void print_chars(string* str); void print_word(word* word, cell nesting); From 54b3c1ea8801cc65964486b6b841715c040100c1 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:09 +0100 Subject: [PATCH 113/345] added vm member to jit classes --- vm/inline_cache.cpp | 4 ++-- vm/jit.cpp | 5 +++-- vm/jit.hpp | 4 ++-- vm/profiler.cpp | 2 +- vm/quotations.cpp | 4 ++-- vm/quotations.hpp | 4 ++-- 6 files changed, 12 insertions(+), 11 deletions(-) mode change 100644 => 100755 vm/quotations.hpp diff --git a/vm/inline_cache.cpp b/vm/inline_cache.cpp index 05a977a67d..9e26f75ad9 100755 --- a/vm/inline_cache.cpp +++ b/vm/inline_cache.cpp @@ -103,7 +103,7 @@ void update_pic_count(cell type) struct inline_cache_jit : public jit { fixnum index; - inline_cache_jit(cell generic_word_) : jit(PIC_TYPE,generic_word_) {}; + inline_cache_jit(cell generic_word_,factorvm *vm) : jit(PIC_TYPE,generic_word_,vm) {}; void emit_check(cell klass); void compile_inline_cache(fixnum index, @@ -173,7 +173,7 @@ code_block *factorvm::compile_inline_cache(fixnum index,cell generic_word_,cell gc_root methods(methods_); gc_root cache_entries(cache_entries_); - inline_cache_jit jit(generic_word.value()); + inline_cache_jit jit(generic_word.value(),this); jit.compile_inline_cache(index, generic_word.value(), methods.value(), diff --git a/vm/jit.cpp b/vm/jit.cpp index a3f222a953..104231f532 100644 --- a/vm/jit.cpp +++ b/vm/jit.cpp @@ -10,7 +10,7 @@ namespace factor - polymorphic inline caches (inline_cache.cpp) */ /* Allocates memory */ -jit::jit(cell type_, cell owner_) +jit::jit(cell type_, cell owner_, factorvm *vm) : type(type_), owner(owner_), code(), @@ -18,7 +18,8 @@ jit::jit(cell type_, cell owner_) literals(), computing_offset_p(false), position(0), - offset(0) + offset(0), + myvm(vm) { if(stack_traces_p()) literal(owner.value()); } diff --git a/vm/jit.hpp b/vm/jit.hpp index 69ab0f9595..0e3db90d38 100644 --- a/vm/jit.hpp +++ b/vm/jit.hpp @@ -10,9 +10,9 @@ struct jit { bool computing_offset_p; fixnum position; cell offset; - //factorvm *vm; + factorvm *myvm; - jit(cell jit_type, cell owner); + jit(cell jit_type, cell owner, factorvm *vm); void compute_position(cell offset); void emit_relocation(cell code_template); diff --git a/vm/profiler.cpp b/vm/profiler.cpp index 054fe01537..de5da244a9 100755 --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -20,7 +20,7 @@ code_block *factorvm::compile_profiling_stub(cell word_) { gc_root word(word_); - jit jit(WORD_TYPE,word.value()); + jit jit(WORD_TYPE,word.value(),this); jit.emit_with(userenv[JIT_PROFILING],word.value()); return jit.to_code_block(); diff --git a/vm/quotations.cpp b/vm/quotations.cpp index a093e0e9df..4a1ad4d371 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -285,7 +285,7 @@ void factorvm::jit_compile(cell quot_, bool relocating) gc_root quot(quot_); if(quot->code) return; - quotation_jit compiler(quot.value(),true,relocating); + quotation_jit compiler(quot.value(),true,relocating,this); compiler.iterate_quotation(); code_block *compiled = compiler.to_code_block(); @@ -368,7 +368,7 @@ fixnum factorvm::quot_code_offset_to_scan(cell quot_, cell offset) gc_root quot(quot_); gc_root array(quot->array); - quotation_jit compiler(quot.value(),false,false); + quotation_jit compiler(quot.value(),false,false,this); compiler.compute_position(offset); compiler.iterate_quotation(); diff --git a/vm/quotations.hpp b/vm/quotations.hpp old mode 100644 new mode 100755 index c1a2a92bd1..b70be4306e --- a/vm/quotations.hpp +++ b/vm/quotations.hpp @@ -5,8 +5,8 @@ struct quotation_jit : public jit { gc_root elements; bool compiling, relocate; - quotation_jit(cell quot, bool compiling_, bool relocate_) - : jit(QUOTATION_TYPE,quot), + quotation_jit(cell quot, bool compiling_, bool relocate_, factorvm *vm) + : jit(QUOTATION_TYPE,quot,vm), elements(owner.as().untagged()->array), compiling(compiling_), relocate(relocate_) {}; From a2f14b5a6d782525d6386e4b2391043ebba80182 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:09 +0100 Subject: [PATCH 114/345] added vm member to gc_root and growable arrays --- vm/alien.cpp | 12 ++++++------ vm/arrays.cpp | 26 +++++++++++++------------- vm/arrays.hpp | 3 +-- vm/byte_arrays.cpp | 2 +- vm/byte_arrays.hpp | 2 +- vm/callstack.cpp | 14 ++++++++------ vm/callstack.hpp | 2 +- vm/code_block.cpp | 8 ++++---- vm/code_heap.cpp | 12 ++++++------ vm/data_gc.cpp | 2 +- vm/data_heap.cpp | 6 +++--- vm/dispatch.cpp | 6 +++--- vm/factor.cpp | 2 +- vm/generic_arrays.hpp | 2 +- vm/image.cpp | 4 ++-- vm/inline_cache.cpp | 32 ++++++++++++++++---------------- vm/io.cpp | 6 +++--- vm/jit.cpp | 18 +++++++++--------- vm/jit.hpp | 4 ++-- vm/local_roots.hpp | 20 +++++++++++--------- vm/profiler.cpp | 4 ++-- vm/quotations.cpp | 14 +++++++------- vm/quotations.hpp | 4 ++-- vm/run.cpp | 2 +- vm/strings.cpp | 10 +++++----- vm/tuples.cpp | 10 +++++----- vm/words.cpp | 8 ++++---- 27 files changed, 119 insertions(+), 116 deletions(-) mode change 100644 => 100755 vm/arrays.hpp mode change 100644 => 100755 vm/byte_arrays.hpp mode change 100644 => 100755 vm/callstack.hpp mode change 100644 => 100755 vm/image.cpp diff --git a/vm/alien.cpp b/vm/alien.cpp index 3a99d89afb..0419e3cec3 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -32,8 +32,8 @@ char *pinned_alien_offset(cell obj) /* make an alien */ cell factorvm::allot_alien(cell delegate_, cell displacement) { - gc_root delegate(delegate_); - gc_root new_alien(allot(sizeof(alien))); + gc_root delegate(delegate_,this); + gc_root new_alien(allot(sizeof(alien)),this); if(delegate.type_p(ALIEN_TYPE)) { @@ -138,9 +138,9 @@ DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset) /* open a native library and push a handle */ inline void factorvm::vmprim_dlopen() { - gc_root path(dpop()); + gc_root path(dpop(),this); path.untag_check(); - gc_root library(allot(sizeof(dll))); + gc_root library(allot(sizeof(dll)),this); library->path = path.value(); ffi_dlopen(library.untagged()); dpush(library.value()); @@ -154,8 +154,8 @@ PRIMITIVE(dlopen) /* look up a symbol in a native library */ inline void factorvm::vmprim_dlsym() { - gc_root library(dpop()); - gc_root name(dpop()); + gc_root library(dpop(),this); + gc_root name(dpop(),this); name.untag_check(); symbol_char *sym = name->data(); diff --git a/vm/arrays.cpp b/vm/arrays.cpp index 236f2d9c54..6d09a11c7d 100644 --- a/vm/arrays.cpp +++ b/vm/arrays.cpp @@ -6,8 +6,8 @@ namespace factor /* make a new array with an initial element */ array *factorvm::allot_array(cell capacity, cell fill_) { - gc_root fill(fill_); - gc_root new_array(allot_array_internal(capacity)); + gc_root fill(fill_,this); + gc_root new_array(allot_array_internal(capacity),this); if(fill.value() == tag_fixnum(0)) memset(new_array->data(),'\0',capacity * sizeof(cell)); @@ -43,8 +43,8 @@ PRIMITIVE(array) cell factorvm::allot_array_1(cell obj_) { - gc_root obj(obj_); - gc_root a(allot_array_internal(1)); + gc_root obj(obj_,this); + gc_root a(allot_array_internal(1),this); set_array_nth(a.untagged(),0,obj.value()); return a.value(); } @@ -56,9 +56,9 @@ cell allot_array_1(cell obj_) cell factorvm::allot_array_2(cell v1_, cell v2_) { - gc_root v1(v1_); - gc_root v2(v2_); - gc_root a(allot_array_internal(2)); + gc_root v1(v1_,this); + gc_root v2(v2_,this); + gc_root a(allot_array_internal(2),this); set_array_nth(a.untagged(),0,v1.value()); set_array_nth(a.untagged(),1,v2.value()); return a.value(); @@ -71,11 +71,11 @@ cell allot_array_2(cell v1_, cell v2_) cell factorvm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) { - gc_root v1(v1_); - gc_root v2(v2_); - gc_root v3(v3_); - gc_root v4(v4_); - gc_root a(allot_array_internal(4)); + gc_root v1(v1_,this); + gc_root v2(v2_,this); + gc_root v3(v3_,this); + gc_root v4(v4_,this); + gc_root a(allot_array_internal(4),this); set_array_nth(a.untagged(),0,v1.value()); set_array_nth(a.untagged(),1,v2.value()); set_array_nth(a.untagged(),2,v3.value()); @@ -102,7 +102,7 @@ PRIMITIVE(resize_array) void growable_array::add(cell elt_) { - gc_root elt(elt_); + gc_root elt(elt_,elements.myvm); if(count == array_capacity(elements.untagged())) elements = reallot_array(elements.untagged(),count * 2); diff --git a/vm/arrays.hpp b/vm/arrays.hpp old mode 100644 new mode 100755 index 06e6ed6e4d..ab4ad61a71 --- a/vm/arrays.hpp +++ b/vm/arrays.hpp @@ -1,6 +1,5 @@ namespace factor { - inline static cell array_nth(array *array, cell slot) { #ifdef FACTOR_DEBUG @@ -34,7 +33,7 @@ struct growable_array { cell count; gc_root elements; - growable_array(cell capacity = 10) : count(0), elements(allot_array(capacity,F)) {} + growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(allot_array(capacity,F),myvm) {} void add(cell elt); void trim(); diff --git a/vm/byte_arrays.cpp b/vm/byte_arrays.cpp index 495e5211a7..39ca778147 100644 --- a/vm/byte_arrays.cpp +++ b/vm/byte_arrays.cpp @@ -63,7 +63,7 @@ void growable_byte_array::append_bytes(void *elts, cell len) void growable_byte_array::append_byte_array(cell byte_array_) { - gc_root byte_array(byte_array_); + gc_root byte_array(byte_array_,elements.myvm); cell len = array_capacity(byte_array.untagged()); cell new_size = count + len; diff --git a/vm/byte_arrays.hpp b/vm/byte_arrays.hpp old mode 100644 new mode 100755 index 6de8ee4e9f..68b706be7b --- a/vm/byte_arrays.hpp +++ b/vm/byte_arrays.hpp @@ -11,7 +11,7 @@ struct growable_byte_array { cell count; gc_root elements; - growable_byte_array(cell capacity = 40) : count(0), elements(allot_byte_array(capacity)) { } + growable_byte_array(factorvm *vm,cell capacity = 40) : count(0), elements(allot_byte_array(capacity),vm) { } void append_bytes(void *elts, cell len); void append_byte_array(cell elts); diff --git a/vm/callstack.cpp b/vm/callstack.cpp index e82314b8d2..e4f03abca9 100755 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -192,10 +192,12 @@ namespace struct stack_frame_accumulator { growable_array frames; + stack_frame_accumulator(factorvm *vm) : frames(vm) {} + void operator()(stack_frame *frame) { - gc_root executing(frame_executing(frame)); - gc_root scan(frame_scan(frame)); + gc_root executing(frame_executing(frame),frames.elements.myvm); + gc_root scan(frame_scan(frame),frames.elements.myvm); frames.add(executing.value()); frames.add(scan.value()); @@ -206,9 +208,9 @@ struct stack_frame_accumulator { inline void factorvm::vmprim_callstack_to_array() { - gc_root callstack(dpop()); + gc_root callstack(dpop(),this); - stack_frame_accumulator accum; + stack_frame_accumulator accum(this); iterate_callstack_object(callstack.untagged(),accum); accum.frames.trim(); @@ -273,8 +275,8 @@ PRIMITIVE(innermost_stack_frame_scan) inline void factorvm::vmprim_set_innermost_stack_frame_quot() { - gc_root callstack(dpop()); - gc_root quot(dpop()); + gc_root callstack(dpop(),this); + gc_root quot(dpop(),this); callstack.untag_check(); quot.untag_check(); diff --git a/vm/callstack.hpp b/vm/callstack.hpp old mode 100644 new mode 100755 index a3cc058e2b..ea62f6da4b --- a/vm/callstack.hpp +++ b/vm/callstack.hpp @@ -37,7 +37,7 @@ template void iterate_callstack(cell top, cell bottom, T &iterator) keep the callstack in a GC root and use relative offsets */ template void iterate_callstack_object(callstack *stack_, T &iterator) { - gc_root stack(stack_); + gc_root stack(stack_,vm); fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); while(frame_offset >= 0) diff --git a/vm/code_block.cpp b/vm/code_block.cpp index ff569328be..d5aa7581c2 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -638,10 +638,10 @@ code_block *allot_code_block(cell size) /* Might GC */ code_block *factorvm::add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_) { - gc_root code(code_); - gc_root labels(labels_); - gc_root relocation(relocation_); - gc_root literals(literals_); + gc_root code(code_,this); + gc_root labels(labels_,this); + gc_root relocation(relocation_,this); + gc_root literals(literals_,this); cell code_length = align8(array_capacity(code.untagged())); code_block *compiled = allot_code_block(code_length); diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index 53eb011a9d..943ee8d7c0 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -29,8 +29,8 @@ bool in_code_heap_p(cell ptr) /* Compile a word definition with the non-optimizing compiler. Allocates memory */ void factorvm::jit_compile_word(cell word_, cell def_, bool relocate) { - gc_root word(word_); - gc_root def(def_); + gc_root word(word_,this); + gc_root def(def_,this); jit_compile(def.value(),relocate); @@ -89,7 +89,7 @@ void update_code_heap_words() inline void factorvm::vmprim_modify_code_heap() { - gc_root alist(dpop()); + gc_root alist(dpop(),this); cell count = array_capacity(alist.untagged()); @@ -99,10 +99,10 @@ inline void factorvm::vmprim_modify_code_heap() cell i; for(i = 0; i < count; i++) { - gc_root pair(array_nth(alist.untagged(),i)); + gc_root pair(array_nth(alist.untagged(),i),this); - gc_root word(array_nth(pair.untagged(),0)); - gc_root data(array_nth(pair.untagged(),1)); + gc_root word(array_nth(pair.untagged(),0),this); + gc_root data(array_nth(pair.untagged(),1),this); switch(data.type()) { diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index 35d6812290..ea7098912e 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -730,7 +730,7 @@ PRIMITIVE(gc) inline void factorvm::vmprim_gc_stats() { - growable_array result; + growable_array result(this); cell i; u64 total_gc_time = 0; diff --git a/vm/data_heap.cpp b/vm/data_heap.cpp index 9069621e52..377cd6e943 100755 --- a/vm/data_heap.cpp +++ b/vm/data_heap.cpp @@ -358,7 +358,7 @@ inline void factorvm::vmprim_data_room() dpush(tag_fixnum((data->cards_end - data->cards) >> 10)); dpush(tag_fixnum((data->decks_end - data->decks) >> 10)); - growable_array a; + growable_array a(this); cell gen; for(gen = 0; gen < data->gen_count; gen++) @@ -478,7 +478,7 @@ struct word_counter { struct word_accumulator { growable_array words; - word_accumulator(int count) : words(count) {} + word_accumulator(int count,factorvm *vm) : words(vm,count) {} void operator()(tagged obj) { if(obj.type_p(WORD_TYPE)) words.add(obj.value()); } }; @@ -488,7 +488,7 @@ cell factorvm::find_all_words() { word_counter counter; each_object(counter); - word_accumulator accum(counter.count); + word_accumulator accum(counter.count,this); each_object(accum); accum.words.trim(); return accum.words.elements.value(); diff --git a/vm/dispatch.cpp b/vm/dispatch.cpp index fbfc0f79cf..1721efe181 100755 --- a/vm/dispatch.cpp +++ b/vm/dispatch.cpp @@ -244,7 +244,7 @@ PRIMITIVE(reset_dispatch_stats) inline void factorvm::vmprim_dispatch_stats() { - growable_array stats; + growable_array stats(this); stats.add(allot_cell(megamorphic_cache_hits)); stats.add(allot_cell(megamorphic_cache_misses)); stats.trim(); @@ -258,8 +258,8 @@ PRIMITIVE(dispatch_stats) void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_) { - gc_root methods(methods_); - gc_root cache(cache_); + gc_root methods(methods_,myvm); + gc_root cache(cache_,myvm); /* Generate machine code to determine the object's class. */ emit_class_lookup(index,PIC_HI_TAG_TUPLE); diff --git a/vm/factor.cpp b/vm/factor.cpp index a75f8da8c2..55d7abcc49 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -179,7 +179,7 @@ VM_C_API void init_factor(vm_parameters *p) /* May allocate memory */ void factorvm::pass_args_to_factor(int argc, vm_char **argv) { - growable_array args; + growable_array args(this); int i; for(i = 1; i < argc; i++) diff --git a/vm/generic_arrays.hpp b/vm/generic_arrays.hpp index 26c8149a10..6d85e4569a 100644 --- a/vm/generic_arrays.hpp +++ b/vm/generic_arrays.hpp @@ -33,7 +33,7 @@ template bool reallot_array_in_place_p(T *array, cell capacity) template T *reallot_array(T *array_, cell capacity) { - gc_root array(array_); + gc_root array(array_,vm); if(reallot_array_in_place_p(array.untagged(),capacity)) { diff --git a/vm/image.cpp b/vm/image.cpp old mode 100644 new mode 100755 index 7724e28f72..24988b24b5 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -147,7 +147,7 @@ inline void factorvm::vmprim_save_image() /* do a full GC to push everything into tenured space */ gc(); - gc_root path(dpop()); + gc_root path(dpop(),this); path.untag_check(); save_image((vm_char *)(path.untagged() + 1)); } @@ -162,7 +162,7 @@ inline void factorvm::vmprim_save_image_and_exit() /* We unbox this before doing anything else. This is the only point where we might throw an error, so we have to throw an error here since later steps destroy the current image. */ - gc_root path(dpop()); + gc_root path(dpop(),this); path.untag_check(); /* strip out userenv data which is set on startup anyway */ diff --git a/vm/inline_cache.cpp b/vm/inline_cache.cpp index 9e26f75ad9..b6a90b1ff4 100755 --- a/vm/inline_cache.cpp +++ b/vm/inline_cache.cpp @@ -132,9 +132,9 @@ void inline_cache_jit::compile_inline_cache(fixnum index, cell cache_entries_, bool tail_call_p) { - gc_root generic_word(generic_word_); - gc_root methods(methods_); - gc_root cache_entries(cache_entries_); + gc_root generic_word(generic_word_,myvm); + gc_root methods(methods_,myvm); + gc_root cache_entries(cache_entries_,myvm); cell inline_cache_type = determine_inline_cache_type(cache_entries.untagged()); update_pic_count(inline_cache_type); @@ -169,9 +169,9 @@ void inline_cache_jit::compile_inline_cache(fixnum index, code_block *factorvm::compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p) { - gc_root generic_word(generic_word_); - gc_root methods(methods_); - gc_root cache_entries(cache_entries_); + gc_root generic_word(generic_word_,this); + gc_root methods(methods_,this); + gc_root cache_entries(cache_entries_,this); inline_cache_jit jit(generic_word.value(),this); jit.compile_inline_cache(index, @@ -213,12 +213,12 @@ cell inline_cache_size(cell cache_entries) /* Allocates memory */ cell factorvm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_) { - gc_root cache_entries(cache_entries_); - gc_root klass(klass_); - gc_root method(method_); + gc_root cache_entries(cache_entries_,this); + gc_root klass(klass_,this); + gc_root method(method_,this); cell pic_size = array_capacity(cache_entries.untagged()); - gc_root new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2)); + gc_root new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2),this); set_array_nth(new_cache_entries.untagged(),pic_size,klass.value()); set_array_nth(new_cache_entries.untagged(),pic_size + 1,method.value()); return new_cache_entries.value(); @@ -255,11 +255,11 @@ void *factorvm::inline_cache_miss(cell return_address) instead of leaving dead PICs around until the next GC. */ deallocate_inline_cache(return_address); - gc_root cache_entries(dpop()); + gc_root cache_entries(dpop(),this); fixnum index = untag_fixnum(dpop()); - gc_root methods(dpop()); - gc_root generic_word(dpop()); - gc_root object(((cell *)ds)[-index]); + gc_root methods(dpop(),this); + gc_root generic_word(dpop(),this); + gc_root object(((cell *)ds)[-index],this); void *xt; @@ -277,7 +277,7 @@ void *factorvm::inline_cache_miss(cell return_address) gc_root new_cache_entries(add_inline_cache_entry( cache_entries.value(), klass, - method)); + method),this); xt = compile_inline_cache(index, generic_word.value(), methods.value(), @@ -317,7 +317,7 @@ PRIMITIVE(reset_inline_cache_stats) inline void factorvm::vmprim_inline_cache_stats() { - growable_array stats; + growable_array stats(this); stats.add(allot_cell(cold_call_to_ic_transitions)); stats.add(allot_cell(ic_to_pic_transitions)); stats.add(allot_cell(pic_to_mega_transitions)); diff --git a/vm/io.cpp b/vm/io.cpp index 570a9a2633..93ae005b6a 100755 --- a/vm/io.cpp +++ b/vm/io.cpp @@ -43,8 +43,8 @@ void io_error() inline void factorvm::vmprim_fopen() { - gc_root mode(dpop()); - gc_root path(dpop()); + gc_root mode(dpop(),this); + gc_root path(dpop(),this); mode.untag_check(); path.untag_check(); @@ -108,7 +108,7 @@ inline void factorvm::vmprim_fread() return; } - gc_root buf(allot_array_internal(size)); + gc_root buf(allot_array_internal(size),this); for(;;) { diff --git a/vm/jit.cpp b/vm/jit.cpp index 104231f532..9757e54dc4 100644 --- a/vm/jit.cpp +++ b/vm/jit.cpp @@ -12,10 +12,10 @@ namespace factor /* Allocates memory */ jit::jit(cell type_, cell owner_, factorvm *vm) : type(type_), - owner(owner_), - code(), - relocation(), - literals(), + owner(owner_,vm), + code(vm), + relocation(vm), + literals(vm), computing_offset_p(false), position(0), offset(0), @@ -26,7 +26,7 @@ jit::jit(cell type_, cell owner_, factorvm *vm) void jit::emit_relocation(cell code_template_) { - gc_root code_template(code_template_); + gc_root code_template(code_template_,myvm); cell capacity = array_capacity(code_template.untagged()); for(cell i = 1; i < capacity; i += 3) { @@ -45,11 +45,11 @@ void jit::emit_relocation(cell code_template_) /* Allocates memory */ void jit::emit(cell code_template_) { - gc_root code_template(code_template_); + gc_root code_template(code_template_,myvm); emit_relocation(code_template.value()); - gc_root insns(array_nth(code_template.untagged(),0)); + gc_root insns(array_nth(code_template.untagged(),0),myvm); if(computing_offset_p) { @@ -73,8 +73,8 @@ void jit::emit(cell code_template_) } void jit::emit_with(cell code_template_, cell argument_) { - gc_root code_template(code_template_); - gc_root argument(argument_); + gc_root code_template(code_template_,myvm); + gc_root argument(argument_,myvm); literal(argument.value()); emit(code_template.value()); } diff --git a/vm/jit.hpp b/vm/jit.hpp index 0e3db90d38..0fa145cded 100644 --- a/vm/jit.hpp +++ b/vm/jit.hpp @@ -40,8 +40,8 @@ struct jit { } void emit_subprimitive(cell word_) { - gc_root word(word_); - gc_root code_template(word->subprimitive); + gc_root word(word_,myvm); + gc_root code_template(word->subprimitive,myvm); if(array_capacity(code_template.untagged()) > 1) literal(T); emit(code_template.value()); } diff --git a/vm/local_roots.hpp b/vm/local_roots.hpp index ac9b322d5f..e0b33a4d1d 100644 --- a/vm/local_roots.hpp +++ b/vm/local_roots.hpp @@ -3,17 +3,19 @@ namespace factor struct factorvm; -template -struct gc_root : public tagged +template +struct gc_root : public tagged { - void push() { check_tagged_pointer(tagged::value()); vm->gc_locals.push_back((cell)this); } - - //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } - explicit gc_root(cell value_) : tagged(value_) { push(); } - explicit gc_root(T *value_) : tagged(value_) { push(); } + factorvm *myvm; - const gc_root& operator=(const T *x) { tagged::operator=(x); return *this; } - const gc_root& operator=(const cell &x) { tagged::operator=(x); return *this; } + void push() { check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } + + //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } + explicit gc_root(cell value_,factorvm *vm) : tagged(value_),myvm(vm) { push(); } + explicit gc_root(TYPE *value_, factorvm *vm) : tagged(value_),myvm(vm) { push(); } + + const gc_root& operator=(const TYPE *x) { tagged::operator=(x); return *this; } + const gc_root& operator=(const cell &x) { tagged::operator=(x); return *this; } ~gc_root() { #ifdef FACTOR_DEBUG diff --git a/vm/profiler.cpp b/vm/profiler.cpp index de5da244a9..e87dd947fd 100755 --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -18,7 +18,7 @@ void init_profiler() /* Allocates memory */ code_block *factorvm::compile_profiling_stub(cell word_) { - gc_root word(word_); + gc_root word(word_,this); jit jit(WORD_TYPE,word.value(),this); jit.emit_with(userenv[JIT_PROFILING],word.value()); @@ -43,7 +43,7 @@ void factorvm::set_profiling(bool profiling) and allocate profiling blocks if necessary */ gc(); - gc_root words(find_all_words()); + gc_root words(find_all_words(),this); cell i; cell length = array_capacity(words.untagged()); diff --git a/vm/quotations.cpp b/vm/quotations.cpp index 4a1ad4d371..0237651e17 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -125,7 +125,7 @@ void quotation_jit::iterate_quotation() { set_position(i); - gc_root obj(array_nth(elements.untagged(),i)); + gc_root obj(array_nth(elements.untagged(),i),myvm); switch(obj.type()) { @@ -282,7 +282,7 @@ void set_quot_xt(quotation *quot, code_block *code) /* Allocates memory */ void factorvm::jit_compile(cell quot_, bool relocating) { - gc_root quot(quot_); + gc_root quot(quot_,this); if(quot->code) return; quotation_jit compiler(quot.value(),true,relocating,this); @@ -339,13 +339,13 @@ PRIMITIVE(quotation_xt) void factorvm::compile_all_words() { - gc_root words(find_all_words()); + gc_root words(find_all_words(),this); cell i; cell length = array_capacity(words.untagged()); for(i = 0; i < length; i++) { - gc_root word(array_nth(words.untagged(),i)); + gc_root word(array_nth(words.untagged(),i),this); if(!word->code || !word_optimized_p(word.untagged())) jit_compile_word(word.value(),word->def,false); @@ -365,8 +365,8 @@ void compile_all_words() /* Allocates memory */ fixnum factorvm::quot_code_offset_to_scan(cell quot_, cell offset) { - gc_root quot(quot_); - gc_root array(quot->array); + gc_root quot(quot_,this); + gc_root array(quot->array,this); quotation_jit compiler(quot.value(),false,false,this); compiler.compute_position(offset); @@ -382,7 +382,7 @@ fixnum quot_code_offset_to_scan(cell quot_, cell offset) cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) { - gc_root quot(quot_); + gc_root quot(quot_,this); stack_chain->callstack_top = stack; jit_compile(quot.value(),true); return quot.value(); diff --git a/vm/quotations.hpp b/vm/quotations.hpp index b70be4306e..dad41917e0 100755 --- a/vm/quotations.hpp +++ b/vm/quotations.hpp @@ -7,9 +7,9 @@ struct quotation_jit : public jit { quotation_jit(cell quot, bool compiling_, bool relocate_, factorvm *vm) : jit(QUOTATION_TYPE,quot,vm), - elements(owner.as().untagged()->array), + elements(owner.as().untagged()->array,vm), compiling(compiling_), - relocate(relocate_) {}; + relocate(relocate_){}; void emit_mega_cache_lookup(cell methods, fixnum index, cell cache); bool primitive_call_p(cell i); diff --git a/vm/run.cpp b/vm/run.cpp index 0a14d4f017..e0ad1abb12 100755 --- a/vm/run.cpp +++ b/vm/run.cpp @@ -90,7 +90,7 @@ PRIMITIVE(load_locals) cell factorvm::clone_object(cell obj_) { - gc_root obj(obj_); + gc_root obj(obj_,this); if(immediate_p(obj.value())) return obj.value(); diff --git a/vm/strings.cpp b/vm/strings.cpp index 7e0506a519..a8962ee921 100644 --- a/vm/strings.cpp +++ b/vm/strings.cpp @@ -39,7 +39,7 @@ void set_string_nth_fast(string *str, cell index, cell ch) void factorvm::set_string_nth_slow(string *str_, cell index, cell ch) { - gc_root str(str_); + gc_root str(str_,this); byte_array *aux; @@ -103,7 +103,7 @@ string *allot_string_internal(cell capacity) /* Allocates memory */ void factorvm::fill_string(string *str_, cell start, cell capacity, cell fill) { - gc_root str(str_); + gc_root str(str_,this); if(fill <= 0x7f) memset(&str->data()[start],fill,capacity - start); @@ -124,7 +124,7 @@ void fill_string(string *str_, cell start, cell capacity, cell fill) /* Allocates memory */ string *factorvm::allot_string(cell capacity, cell fill) { - gc_root str(allot_string_internal(capacity)); + gc_root str(allot_string_internal(capacity),this); fill_string(str.untagged(),0,capacity,fill); return str.untagged(); } @@ -160,7 +160,7 @@ bool reallot_string_in_place_p(string *str, cell capacity) string* factorvm::reallot_string(string *str_, cell capacity) { - gc_root str(str_); + gc_root str(str_,this); if(reallot_string_in_place_p(str.untagged(),capacity)) { @@ -180,7 +180,7 @@ string* factorvm::reallot_string(string *str_, cell capacity) if(capacity < to_copy) to_copy = capacity; - gc_root new_str(allot_string_internal(capacity)); + gc_root new_str(allot_string_internal(capacity),this); memcpy(new_str->data(),str->data(),to_copy); diff --git a/vm/tuples.cpp b/vm/tuples.cpp index c38832ac0b..5c4c6e17b0 100644 --- a/vm/tuples.cpp +++ b/vm/tuples.cpp @@ -6,8 +6,8 @@ namespace factor /* push a new tuple on the stack */ tuple *factorvm::allot_tuple(cell layout_) { - gc_root layout(layout_); - gc_root t(allot(tuple_size(layout.untagged()))); + gc_root layout(layout_,this); + gc_root t(allot(tuple_size(layout.untagged())),this); t->layout = layout.value(); return t.untagged(); } @@ -19,7 +19,7 @@ tuple *allot_tuple(cell layout_) inline void factorvm::vmprim_tuple() { - gc_root layout(dpop()); + gc_root layout(dpop(),this); tuple *t = allot_tuple(layout.value()); fixnum i; for(i = tuple_size(layout.untagged()) - 1; i >= 0; i--) @@ -36,8 +36,8 @@ PRIMITIVE(tuple) /* push a new tuple on the stack, filling its slots from the stack */ inline void factorvm::vmprim_tuple_boa() { - gc_root layout(dpop()); - gc_root t(allot_tuple(layout.value())); + gc_root layout(dpop(),this); + gc_root t(allot_tuple(layout.value()),this); cell size = untag_fixnum(layout.untagged()->size) * sizeof(cell); memcpy(t->data(),(cell *)(ds - (size - sizeof(cell))),size); ds -= size; diff --git a/vm/words.cpp b/vm/words.cpp index 644db46896..68b6afd9d0 100644 --- a/vm/words.cpp +++ b/vm/words.cpp @@ -5,10 +5,10 @@ namespace factor word *factorvm::allot_word(cell vocab_, cell name_) { - gc_root vocab(vocab_); - gc_root name(name_); + gc_root vocab(vocab_,this); + gc_root name(name_,this); - gc_root new_word(allot(sizeof(word))); + gc_root new_word(allot(sizeof(word)),this); new_word->hashcode = tag_fixnum((rand() << 16) ^ rand()); new_word->vocabulary = vocab.value(); @@ -66,7 +66,7 @@ PRIMITIVE(word_xt) /* Allocates memory */ void factorvm::update_word_xt(cell w_) { - gc_root w(w_); + gc_root w(w_,this); if(profiling_p) { From e678f6a68158d62af2a14a011708ad8dbeeb992f Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:10 +0100 Subject: [PATCH 115/345] added vm member to gc_bignum --- vm/bignum.cpp | 34 +++++++++++++++++----------------- vm/local_roots.hpp | 15 +++++++-------- vm/math.cpp | 4 ++-- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index a5310d1c14..62d9952364 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -554,7 +554,7 @@ enum bignum_comparison bignum_compare_unsigned(bignum * x, bignum * y) /* allocates memory */ bignum *factorvm::bignum_add_unsigned(bignum * x, bignum * y, int negative_p) { - GC_BIGNUM(x); GC_BIGNUM(y); + GC_BIGNUM(x,this); GC_BIGNUM(y,this); if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) { @@ -626,7 +626,7 @@ bignum *bignum_add_unsigned(bignum * x, bignum * y, int negative_p) /* allocates memory */ bignum *factorvm::bignum_subtract_unsigned(bignum * x, bignum * y) { - GC_BIGNUM(x); GC_BIGNUM(y); + GC_BIGNUM(x,this); GC_BIGNUM(y,this); int negative_p = 0; switch (bignum_compare_unsigned (x, y)) @@ -709,7 +709,7 @@ bignum *bignum_subtract_unsigned(bignum * x, bignum * y) /* allocates memory */ bignum *factorvm::bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) { - GC_BIGNUM(x); GC_BIGNUM(y); + GC_BIGNUM(x,this); GC_BIGNUM(y,this); if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) { @@ -785,7 +785,7 @@ bignum *bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) /* allocates memory */ bignum *factorvm::bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p) { - GC_BIGNUM(x); + GC_BIGNUM(x,this); bignum_length_type length_x = (BIGNUM_LENGTH (x)); @@ -874,7 +874,7 @@ void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) /* allocates memory */ void factorvm::bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p) { - GC_BIGNUM(numerator); GC_BIGNUM(denominator); + GC_BIGNUM(numerator,this); GC_BIGNUM(denominator,this); bignum_length_type length_n = ((BIGNUM_LENGTH (numerator)) + 1); bignum_length_type length_d = (BIGNUM_LENGTH (denominator)); @@ -883,10 +883,10 @@ void factorvm::bignum_divide_unsigned_large_denominator(bignum * numerator, bign ((quotient != ((bignum * *) 0)) ? (allot_bignum ((length_n - length_d), q_negative_p)) : BIGNUM_OUT_OF_BAND); - GC_BIGNUM(q); + GC_BIGNUM(q,this); bignum * u = (allot_bignum (length_n, r_negative_p)); - GC_BIGNUM(u); + GC_BIGNUM(u,this); int shift = 0; BIGNUM_ASSERT (length_d > 1); @@ -1096,12 +1096,12 @@ bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_dig /* allocates memory */ void factorvm::bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) { - GC_BIGNUM(numerator); + GC_BIGNUM(numerator,this); bignum_length_type length_n = (BIGNUM_LENGTH (numerator)); bignum_length_type length_q; bignum * q = NULL; - GC_BIGNUM(q); + GC_BIGNUM(q,this); int shift = 0; /* Because `bignum_digit_divide' requires a normalized denominator. */ @@ -1351,10 +1351,10 @@ bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digi /* allocates memory */ void factorvm::bignum_divide_unsigned_small_denominator(bignum * numerator, bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) { - GC_BIGNUM(numerator); + GC_BIGNUM(numerator,this); bignum * q = (bignum_new_sign (numerator, q_negative_p)); - GC_BIGNUM(q); + GC_BIGNUM(q,this); bignum_digit_type r = (bignum_destructive_scale_down (q, denominator)); @@ -1525,7 +1525,7 @@ bignum *bignum_trim(bignum * bignum) /* allocates memory */ bignum *factorvm::bignum_new_sign(bignum * x, int negative_p) { - GC_BIGNUM(x); + GC_BIGNUM(x,this); bignum * result = (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); bignum_destructive_copy (x, result); @@ -1667,7 +1667,7 @@ bignum *bignum_bitwise_xor(bignum * arg1, bignum * arg2) /* assume arg1 is a big number, n is a long */ bignum *factorvm::bignum_magnitude_ash(bignum * arg1, fixnum n) { - GC_BIGNUM(arg1); + GC_BIGNUM(arg1,this); bignum * result = NULL; bignum_digit_type *scan1; @@ -1733,7 +1733,7 @@ bignum *bignum_magnitude_ash(bignum * arg1, fixnum n) /* allocates memory */ bignum *factorvm::bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) { - GC_BIGNUM(arg1); GC_BIGNUM(arg2); + GC_BIGNUM(arg1,this); GC_BIGNUM(arg2,this); bignum * result; bignum_length_type max_length; @@ -1772,7 +1772,7 @@ bignum *bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) /* allocates memory */ bignum *factorvm::bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) { - GC_BIGNUM(arg1); GC_BIGNUM(arg2); + GC_BIGNUM(arg1,this); GC_BIGNUM(arg2,this); bignum * result; bignum_length_type max_length; @@ -1829,7 +1829,7 @@ bignum *bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) /* allocates memory */ bignum *factorvm::bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) { - GC_BIGNUM(arg1); GC_BIGNUM(arg2); + GC_BIGNUM(arg1,this); GC_BIGNUM(arg2,this); bignum * result; bignum_length_type max_length; @@ -1926,7 +1926,7 @@ void bignum_negate_magnitude(bignum * arg) /* Allocates memory */ bignum *factorvm::bignum_integer_length(bignum * x) { - GC_BIGNUM(x); + GC_BIGNUM(x,this); bignum_length_type index = ((BIGNUM_LENGTH (x)) - 1); bignum_digit_type digit = (BIGNUM_REF (x, index)); diff --git a/vm/local_roots.hpp b/vm/local_roots.hpp index e0b33a4d1d..26d083be40 100644 --- a/vm/local_roots.hpp +++ b/vm/local_roots.hpp @@ -19,9 +19,9 @@ struct gc_root : public tagged ~gc_root() { #ifdef FACTOR_DEBUG - assert(vm->gc_locals.back() == (cell)this); + assert(myvm->gc_locals.back() == (cell)this); #endif - vm->gc_locals.pop_back(); + myvm->gc_locals.pop_back(); } }; @@ -30,21 +30,20 @@ struct gc_bignum { bignum **addr; factorvm *myvm; - //gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { - gc_bignum(bignum **addr_) : addr(addr_), myvm(vm) { + gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { if(*addr_) check_data_pointer(*addr_); - vm->gc_bignums.push_back((cell)addr); + myvm->gc_bignums.push_back((cell)addr); } ~gc_bignum() { #ifdef FACTOR_DEBUG - assert(vm->gc_bignums.back() == (cell)addr); + assert(myvm->gc_bignums.back() == (cell)addr); #endif - vm->gc_bignums.pop_back(); + myvm->gc_bignums.pop_back(); } }; -#define GC_BIGNUM(x) gc_bignum x##__gc_root(&x) +#define GC_BIGNUM(x,vm) gc_bignum x##__gc_root(&x,vm) } diff --git a/vm/math.cpp b/vm/math.cpp index ac04e906a2..f273cede0e 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -863,9 +863,9 @@ VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y) void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) { bignum *bx = fixnum_to_bignum(x); - GC_BIGNUM(bx); + GC_BIGNUM(bx,this); bignum *by = fixnum_to_bignum(y); - GC_BIGNUM(by); + GC_BIGNUM(by,this); drepl(tag(bignum_multiply(bx,by))); } From 1b6415599879d1dead5175c4b9767289a7bcf502 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:10 +0100 Subject: [PATCH 116/345] moved reallot_array into vm --- vm/arrays.cpp | 8 +++++--- vm/byte_arrays.cpp | 11 ++++++----- vm/generic_arrays.hpp | 12 ++++++------ vm/vm.hpp | 4 ++++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/vm/arrays.cpp b/vm/arrays.cpp index 6d09a11c7d..8d964c4d21 100644 --- a/vm/arrays.cpp +++ b/vm/arrays.cpp @@ -102,16 +102,18 @@ PRIMITIVE(resize_array) void growable_array::add(cell elt_) { - gc_root elt(elt_,elements.myvm); + factorvm* myvm = elements.myvm; + gc_root elt(elt_,myvm); if(count == array_capacity(elements.untagged())) - elements = reallot_array(elements.untagged(),count * 2); + elements = myvm->reallot_array(elements.untagged(),count * 2); set_array_nth(elements.untagged(),count++,elt.value()); } void growable_array::trim() { - elements = reallot_array(elements.untagged(),count); + factorvm *myvm = elements.myvm; + elements = myvm->reallot_array(elements.untagged(),count); } } diff --git a/vm/byte_arrays.cpp b/vm/byte_arrays.cpp index 39ca778147..0f4591a89c 100644 --- a/vm/byte_arrays.cpp +++ b/vm/byte_arrays.cpp @@ -52,9 +52,9 @@ PRIMITIVE(resize_byte_array) void growable_byte_array::append_bytes(void *elts, cell len) { cell new_size = count + len; - + factorvm *myvm = elements.myvm; if(new_size >= array_capacity(elements.untagged())) - elements = reallot_array(elements.untagged(),new_size * 2); + elements = myvm->reallot_array(elements.untagged(),new_size * 2); memcpy(&elements->data()[count],elts,len); @@ -67,9 +67,9 @@ void growable_byte_array::append_byte_array(cell byte_array_) cell len = array_capacity(byte_array.untagged()); cell new_size = count + len; - + factorvm *myvm = elements.myvm; if(new_size >= array_capacity(elements.untagged())) - elements = reallot_array(elements.untagged(),new_size * 2); + elements = myvm->reallot_array(elements.untagged(),new_size * 2); memcpy(&elements->data()[count],byte_array->data(),len); @@ -78,7 +78,8 @@ void growable_byte_array::append_byte_array(cell byte_array_) void growable_byte_array::trim() { - elements = reallot_array(elements.untagged(),count); + factorvm *myvm = elements.myvm; + elements = myvm->reallot_array(elements.untagged(),count); } } diff --git a/vm/generic_arrays.hpp b/vm/generic_arrays.hpp index 6d85e4569a..3c997708e5 100644 --- a/vm/generic_arrays.hpp +++ b/vm/generic_arrays.hpp @@ -31,9 +31,9 @@ template bool reallot_array_in_place_p(T *array, cell capacity) return in_zone(&nursery,array) && capacity <= array_capacity(array); } -template T *reallot_array(T *array_, cell capacity) +template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) { - gc_root array(array_,vm); + gc_root array(array_,this); if(reallot_array_in_place_p(array.untagged(),capacity)) { @@ -46,11 +46,11 @@ template T *reallot_array(T *array_, cell capacity) if(capacity < to_copy) to_copy = capacity; - T *new_array = allot_array_internal(capacity); + TYPE *new_array = allot_array_internal(capacity); - memcpy(new_array + 1,array.untagged() + 1,to_copy * T::element_size); - memset((char *)(new_array + 1) + to_copy * T::element_size, - 0,(capacity - to_copy) * T::element_size); + memcpy(new_array + 1,array.untagged() + 1,to_copy * TYPE::element_size); + memset((char *)(new_array + 1) + to_copy * TYPE::element_size, + 0,(capacity - to_copy) * TYPE::element_size); return new_array; } diff --git a/vm/vm.hpp b/vm/vm.hpp index 2326f23148..b0dc17d4e8 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -191,6 +191,10 @@ struct factorvm { std::vector gc_locals; std::vector gc_bignums; + // generic arrays + + template TYPE *reallot_array(TYPE *array_, cell capacity); + //debug void print_chars(string* str); void print_word(word* word, cell nesting); From be3a9f7f663d2ccee6eb5df0c68e45b1148d7f34 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:10 +0100 Subject: [PATCH 117/345] moved data_gc templates and inline functions to vm --- vm/data_gc.hpp | 44 +++++++++++++++++++++++++++++++++++++------- vm/vm.hpp | 9 ++++++++- 2 files changed, 45 insertions(+), 8 deletions(-) mode change 100644 => 100755 vm/data_gc.hpp diff --git a/vm/data_gc.hpp b/vm/data_gc.hpp old mode 100644 new mode 100755 index 334ad5a2bb..995d539d5d --- a/vm/data_gc.hpp +++ b/vm/data_gc.hpp @@ -22,7 +22,7 @@ void init_data_gc(); void gc(); -inline static bool collecting_accumulation_gen_p() +inline bool factorvm::collecting_accumulation_gen_p() { return ((data->have_aging_p() && collecting_gen == data->aging() @@ -30,6 +30,11 @@ inline static bool collecting_accumulation_gen_p() || collecting_gen == data->tenured()); } +inline bool collecting_accumulation_gen_p() +{ + return vm->collecting_accumulation_gen_p(); +} + void copy_handle(cell *handle); void garbage_collection(volatile cell gen, @@ -41,7 +46,7 @@ allocation (which does not call GC because of possible roots in volatile registers) does not run out of memory */ static const cell allot_buffer_zone = 1024; -inline static object *allot_zone(zone *z, cell a) +inline object *factorvm::allot_zone(zone *z, cell a) { cell h = z->here; z->here = h + align8(a); @@ -50,11 +55,16 @@ inline static object *allot_zone(zone *z, cell a) return obj; } +inline object *allot_zone(zone *z, cell a) +{ + return vm->allot_zone(z,a); +} + /* * It is up to the caller to fill in the object's fields in a meaningful * fashion! */ -inline static object *allot_object(header header, cell size) +inline object *factorvm::allot_object(header header, cell size) { #ifdef GC_DEBUG if(!gc_off) @@ -105,9 +115,19 @@ inline static object *allot_object(header header, cell size) return obj; } -template T *allot(cell size) +inline object *allot_object(header header, cell size) { - return (T *)allot_object(header(T::type_number),size); + return vm->allot_object(header,size); +} + +template TYPE *factorvm::allot(cell size) +{ + return (TYPE *)allot_object(header(TYPE::type_number),size); +} + +template TYPE *allot(cell size) +{ + return vm->allot(size); } void copy_reachable_objects(cell scan, cell *end); @@ -120,7 +140,7 @@ PRIMITIVE(become); extern bool growing_data_heap; -inline static void check_data_pointer(object *pointer) +inline void factorvm::check_data_pointer(object *pointer) { #ifdef FACTOR_DEBUG if(!growing_data_heap) @@ -131,7 +151,12 @@ inline static void check_data_pointer(object *pointer) #endif } -inline static void check_tagged_pointer(cell tagged) +inline void check_data_pointer(object *pointer) +{ + return vm->check_data_pointer(pointer); +} + +inline void factorvm::check_tagged_pointer(cell tagged) { #ifdef FACTOR_DEBUG if(!immediate_p(tagged)) @@ -143,6 +168,11 @@ inline static void check_tagged_pointer(cell tagged) #endif } +inline void check_tagged_pointer(cell tagged) +{ + return vm->check_tagged_pointer(tagged); +} + VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size); } diff --git a/vm/vm.hpp b/vm/vm.hpp index b0dc17d4e8..bcbf2fe6bf 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -186,6 +186,13 @@ struct factorvm { void clear_gc_stats(); inline void vmprim_become(); void inline_gc(cell *gc_roots_base, cell gc_roots_size); + inline bool collecting_accumulation_gen_p(); + inline object *allot_zone(zone *z, cell a); + inline object *allot_object(header header, cell size); + template TYPE *allot(cell size); + inline void check_data_pointer(object *pointer); + inline void check_tagged_pointer(cell tagged); + // next method here: // local roots std::vector gc_locals; @@ -544,7 +551,7 @@ struct factorvm { void print_cell_hex_pad(cell x); void print_fixnum(fixnum x); cell read_cell_hex(); - // next method here: + From b1189dc4f19160c7a87b55a8ce29393cbd605e80 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:10 +0100 Subject: [PATCH 118/345] moved write_barrier functions to vm --- vm/code_block.hpp | 2 +- vm/vm.hpp | 15 +++++++++-- vm/write_barrier.hpp | 63 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 68 insertions(+), 12 deletions(-) mode change 100644 => 100755 vm/write_barrier.hpp diff --git a/vm/code_block.hpp b/vm/code_block.hpp index d46cd9e885..3497ff33ba 100644 --- a/vm/code_block.hpp +++ b/vm/code_block.hpp @@ -86,7 +86,7 @@ void mark_object_code_block(object *scan); void relocate_code_block(code_block *relocating); -inline static bool stack_traces_p() +inline bool stack_traces_p() { return userenv[STACK_TRACES_ENV] != F; } diff --git a/vm/vm.hpp b/vm/vm.hpp index bcbf2fe6bf..ffdf42aff9 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -155,6 +155,19 @@ struct factorvm { inline void vmprim_end_scan(); template void each_object(T &functor); cell find_all_words(); + + //write barrier + inline card *addr_to_card(cell a); + inline cell card_to_addr(card *c); + inline cell card_offset(card *c); + inline card_deck *addr_to_deck(cell a); + inline cell deck_to_addr(card_deck *c); + inline card *deck_to_card(card_deck *d); + inline card *addr_to_allot_marker(object *a); + inline void write_barrier(object *obj); + inline void allot_barrier(object *address); + // next method here: + //data_gc void init_data_gc(); @@ -192,14 +205,12 @@ struct factorvm { template TYPE *allot(cell size); inline void check_data_pointer(object *pointer); inline void check_tagged_pointer(cell tagged); - // next method here: // local roots std::vector gc_locals; std::vector gc_bignums; // generic arrays - template TYPE *reallot_array(TYPE *array_, cell capacity); //debug diff --git a/vm/write_barrier.hpp b/vm/write_barrier.hpp old mode 100644 new mode 100755 index 0006581034..e874600b24 --- a/vm/write_barrier.hpp +++ b/vm/write_barrier.hpp @@ -22,65 +22,110 @@ static const cell card_bits = 8; static const cell card_size = (1<> card_bits) + cards_offset); } -inline static cell card_to_addr(card *c) +inline card *addr_to_card(cell a) +{ + return vm->addr_to_card(a); +} + +inline cell factorvm::card_to_addr(card *c) { return ((cell)c - cards_offset) << card_bits; } -inline static cell card_offset(card *c) +inline cell card_to_addr(card *c) +{ + return vm->card_to_addr(c); +} + +inline cell factorvm::card_offset(card *c) { return *(c - (cell)data->cards + (cell)data->allot_markers); } +inline cell card_offset(card *c) +{ + return vm->card_offset(c); +} + typedef u8 card_deck; static const cell deck_bits = (card_bits + 10); static const cell deck_size = (1<> deck_bits) + decks_offset); } -inline static cell deck_to_addr(card_deck *c) +inline card_deck *addr_to_deck(cell a) +{ + return vm->addr_to_deck(a); +} + +inline cell factorvm::deck_to_addr(card_deck *c) { return ((cell)c - decks_offset) << deck_bits; } -inline static card *deck_to_card(card_deck *d) +inline cell deck_to_addr(card_deck *c) +{ + return vm->deck_to_addr(c); +} + +inline card *factorvm::deck_to_card(card_deck *d) { return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset); } +inline card *deck_to_card(card_deck *d) +{ + return vm->deck_to_card(d); +} + static const cell invalid_allot_marker = 0xff; extern cell allot_markers_offset; -inline static card *addr_to_allot_marker(object *a) +inline card *factorvm::addr_to_allot_marker(object *a) { return (card *)(((cell)a >> card_bits) + allot_markers_offset); } +inline card *addr_to_allot_marker(object *a) +{ + return vm->addr_to_allot_marker(a); +} + /* the write barrier must be called any time we are potentially storing a pointer from an older generation to a younger one */ -inline static void write_barrier(object *obj) +inline void factorvm::write_barrier(object *obj) { *addr_to_card((cell)obj) = card_mark_mask; *addr_to_deck((cell)obj) = card_mark_mask; } +inline void write_barrier(object *obj) +{ + return vm->write_barrier(obj); +} + /* we need to remember the first object allocated in the card */ -inline static void allot_barrier(object *address) +inline void factorvm::allot_barrier(object *address) { card *ptr = addr_to_allot_marker(address); if(*ptr == invalid_allot_marker) *ptr = ((cell)address & addr_card_mask); } +inline void allot_barrier(object *address) +{ + return vm->allot_barrier(address); +} + } From 625380c25c2abff24d93e3fa458ad3b8c320a0e7 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:10 +0100 Subject: [PATCH 119/345] moved generic_array.hpp functions to vm --- vm/generic_arrays.hpp | 14 ++++++++++++-- vm/vm.hpp | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/vm/generic_arrays.hpp b/vm/generic_arrays.hpp index 3c997708e5..e134b1a2ad 100644 --- a/vm/generic_arrays.hpp +++ b/vm/generic_arrays.hpp @@ -19,18 +19,28 @@ template cell array_size(T *array) return array_size(array_capacity(array)); } -template T *allot_array_internal(cell capacity) +template T *factorvm::allot_array_internal(cell capacity) { T *array = allot(array_size(capacity)); array->capacity = tag_fixnum(capacity); return array; } -template bool reallot_array_in_place_p(T *array, cell capacity) +template T *allot_array_internal(cell capacity) +{ + return vm->allot_array_internal(capacity); +} + +template bool factorvm::reallot_array_in_place_p(T *array, cell capacity) { return in_zone(&nursery,array) && capacity <= array_capacity(array); } +template bool reallot_array_in_place_p(T *array, cell capacity) +{ + return vm->reallot_array_in_place_p(array,capacity); +} + template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) { gc_root array(array_,this); diff --git a/vm/vm.hpp b/vm/vm.hpp index ffdf42aff9..7c657ff9e4 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -166,7 +166,6 @@ struct factorvm { inline card *addr_to_allot_marker(object *a); inline void write_barrier(object *obj); inline void allot_barrier(object *address); - // next method here: //data_gc @@ -211,7 +210,10 @@ struct factorvm { std::vector gc_bignums; // generic arrays + template T *allot_array_internal(cell capacity); + template bool reallot_array_in_place_p(T *array, cell capacity); template TYPE *reallot_array(TYPE *array_, cell capacity); + // next method here: //debug void print_chars(string* str); From 33ecaa5010631ac0c834e0996c22d05ebdbb070b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:10 +0100 Subject: [PATCH 120/345] moved arrays.hpp functions to vm --- vm/arrays.hpp | 10 ++++++++-- vm/vm.hpp | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/vm/arrays.hpp b/vm/arrays.hpp index ab4ad61a71..eda30c52fb 100755 --- a/vm/arrays.hpp +++ b/vm/arrays.hpp @@ -1,6 +1,7 @@ namespace factor { -inline static cell array_nth(array *array, cell slot) + +inline cell array_nth(array *array, cell slot) { #ifdef FACTOR_DEBUG assert(slot < array_capacity(array)); @@ -9,7 +10,7 @@ inline static cell array_nth(array *array, cell slot) return array->data()[slot]; } -inline static void set_array_nth(array *array, cell slot, cell value) +inline void factorvm::set_array_nth(array *array, cell slot, cell value) { #ifdef FACTOR_DEBUG assert(slot < array_capacity(array)); @@ -20,6 +21,11 @@ inline static void set_array_nth(array *array, cell slot, cell value) write_barrier(array); } +inline void set_array_nth(array *array, cell slot, cell value) +{ + return vm->set_array_nth(array,slot,value); +} + array *allot_array(cell capacity, cell fill); cell allot_array_1(cell obj); diff --git a/vm/vm.hpp b/vm/vm.hpp index 7c657ff9e4..0389817bb9 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -213,7 +213,6 @@ struct factorvm { template T *allot_array_internal(cell capacity); template bool reallot_array_in_place_p(T *array, cell capacity); template TYPE *reallot_array(TYPE *array_, cell capacity); - // next method here: //debug void print_chars(string* str); @@ -246,6 +245,8 @@ struct factorvm { cell allot_array_2(cell v1_, cell v2_); cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_); inline void vmprim_resize_array(); + inline void set_array_nth(array *array, cell slot, cell value); + // next method here: //strings cell string_nth(string* str, cell index); From ae5c0fbfb299f678c667ba75af75d64915326f89 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:11 +0100 Subject: [PATCH 121/345] moved math.hpp functions to vm --- vm/math.hpp | 37 +++++++++++++++++++++++++++++++------ vm/vm.hpp | 7 ++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/vm/math.hpp b/vm/math.hpp index 7828aa3e6c..cb4f1b1101 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -42,7 +42,7 @@ PRIMITIVE(bignum_bitp); PRIMITIVE(bignum_log2); PRIMITIVE(byte_array_to_bignum); -inline static cell allot_integer(fixnum x) +inline cell factorvm::allot_integer(fixnum x) { if(x < fixnum_min || x > fixnum_max) return tag(fixnum_to_bignum(x)); @@ -50,7 +50,12 @@ inline static cell allot_integer(fixnum x) return tag_fixnum(x); } -inline static cell allot_cell(cell x) +inline cell allot_integer(fixnum x) +{ + return vm->allot_integer(x); +} + +inline cell factorvm::allot_cell(cell x) { if(x > (cell)fixnum_max) return tag(cell_to_bignum(x)); @@ -58,6 +63,11 @@ inline static cell allot_cell(cell x) return tag_fixnum(x); } +inline cell allot_cell(cell x) +{ + return vm->allot_cell(x); +} + cell unbox_array_size(); inline static double untag_float(cell tagged) @@ -70,33 +80,48 @@ inline static double untag_float_check(cell tagged) return untag_check(tagged)->n; } -inline static cell allot_float(double n) +inline cell factorvm::allot_float(double n) { boxed_float *flo = allot(sizeof(boxed_float)); flo->n = n; return tag(flo); } +inline cell allot_float(double n) +{ + return vm->allot_float(n); +} + inline static fixnum float_to_fixnum(cell tagged) { return (fixnum)untag_float(tagged); } -inline static bignum *float_to_bignum(cell tagged) +inline bignum *factorvm::float_to_bignum(cell tagged) { return double_to_bignum(untag_float(tagged)); } -inline static double fixnum_to_float(cell tagged) +inline bignum *float_to_bignum(cell tagged) +{ + return vm->float_to_bignum(tagged); +} + +inline double fixnum_to_float(cell tagged) { return (double)untag_fixnum(tagged); } -inline static double bignum_to_float(cell tagged) +inline double factorvm::bignum_to_float(cell tagged) { return bignum_to_double(untag(tagged)); } +inline double bignum_to_float(cell tagged) +{ + return vm->bignum_to_float(tagged); +} + PRIMITIVE(fixnum_to_float); PRIMITIVE(bignum_to_float); PRIMITIVE(str_to_float); diff --git a/vm/vm.hpp b/vm/vm.hpp index 0389817bb9..5db239626d 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -246,7 +246,6 @@ struct factorvm { cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_); inline void vmprim_resize_array(); inline void set_array_nth(array *array, cell slot, cell value); - // next method here: //strings cell string_nth(string* str, cell index); @@ -358,6 +357,12 @@ struct factorvm { void overflow_fixnum_add(fixnum x, fixnum y); void overflow_fixnum_subtract(fixnum x, fixnum y); void overflow_fixnum_multiply(fixnum x, fixnum y); + inline cell allot_integer(fixnum x); + inline cell allot_cell(cell x); + inline cell allot_float(double n); + inline bignum *float_to_bignum(cell tagged); + inline double bignum_to_float(cell tagged); + // next method here: //io void init_c_io(); From a6c3c1e7d2877d541900afd9a83a9d421380a381 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:11 +0100 Subject: [PATCH 122/345] moved callstack.hpp functions to vm --- vm/callstack.hpp | 7 ++++++- vm/code_heap.hpp | 2 -- vm/vm.hpp | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/vm/callstack.hpp b/vm/callstack.hpp index ea62f6da4b..7e7e5cc20b 100755 --- a/vm/callstack.hpp +++ b/vm/callstack.hpp @@ -35,7 +35,7 @@ template void iterate_callstack(cell top, cell bottom, T &iterator) /* This is a little tricky. The iterator may allocate memory, so we keep the callstack in a GC root and use relative offsets */ -template void iterate_callstack_object(callstack *stack_, T &iterator) +template void factorvm::iterate_callstack_object(callstack *stack_, T &iterator) { gc_root stack(stack_,vm); fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); @@ -48,4 +48,9 @@ template void iterate_callstack_object(callstack *stack_, T &iterato } } +template void iterate_callstack_object(callstack *stack_, T &iterator) +{ + return vm->iterate_callstack_object(stack_,iterator); +} + } diff --git a/vm/code_heap.hpp b/vm/code_heap.hpp index a77a89b827..20d2b974d3 100755 --- a/vm/code_heap.hpp +++ b/vm/code_heap.hpp @@ -10,8 +10,6 @@ bool in_code_heap_p(cell ptr); void jit_compile_word(cell word, cell def, bool relocate); -//typedef void (*code_heap_iterator)(code_block *compiled); - void iterate_code_heap(code_heap_iterator iter); void copy_code_heap_roots(); diff --git a/vm/vm.hpp b/vm/vm.hpp index 5db239626d..2c52ffcf5d 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -466,6 +466,7 @@ struct factorvm { void load_image(vm_parameters *p); //callstack + template void iterate_callstack_object(callstack *stack_, T &iterator); void check_frame(stack_frame *frame); callstack *allot_callstack(cell size); stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom); From 31905b68a7fbcb5b1d460be19969d85ff04b14a8 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:11 +0100 Subject: [PATCH 123/345] moved write_barrier inline function impls to vm.hpp --- vm/master.hpp | 2 +- vm/vm.hpp | 103 ++++++++++++++++++++++++++++++++++++++++++- vm/write_barrier.hpp | 97 ---------------------------------------- 3 files changed, 103 insertions(+), 99 deletions(-) diff --git a/vm/master.hpp b/vm/master.hpp index 2f9b19d12b..0b3bf7b474 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -47,9 +47,9 @@ #include "bignumint.hpp" #include "bignum.hpp" #include "code_block.hpp" -#include "vm.hpp" #include "data_heap.hpp" #include "write_barrier.hpp" +#include "vm.hpp" #include "data_gc.hpp" #include "local_roots.hpp" #include "generic_arrays.hpp" diff --git a/vm/vm.hpp b/vm/vm.hpp index 2c52ffcf5d..cfb18d2036 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -362,7 +362,6 @@ struct factorvm { inline cell allot_float(double n); inline bignum *float_to_bignum(cell tagged); inline double bignum_to_float(cell tagged); - // next method here: //io void init_c_io(); @@ -580,4 +579,106 @@ struct factorvm { extern factorvm *vm; + +// write_barrier.hpp + +inline card *factorvm::addr_to_card(cell a) +{ + return (card*)(((cell)(a) >> card_bits) + cards_offset); +} + +inline card *addr_to_card(cell a) +{ + return vm->addr_to_card(a); +} + +inline cell factorvm::card_to_addr(card *c) +{ + return ((cell)c - cards_offset) << card_bits; +} + +inline cell card_to_addr(card *c) +{ + return vm->card_to_addr(c); +} + +inline cell factorvm::card_offset(card *c) +{ + return *(c - (cell)data->cards + (cell)data->allot_markers); +} + +inline cell card_offset(card *c) +{ + return vm->card_offset(c); +} + +inline card_deck *factorvm::addr_to_deck(cell a) +{ + return (card_deck *)(((cell)a >> deck_bits) + decks_offset); +} + +inline card_deck *addr_to_deck(cell a) +{ + return vm->addr_to_deck(a); +} + +inline cell factorvm::deck_to_addr(card_deck *c) +{ + return ((cell)c - decks_offset) << deck_bits; +} + +inline cell deck_to_addr(card_deck *c) +{ + return vm->deck_to_addr(c); +} + +inline card *factorvm::deck_to_card(card_deck *d) +{ + return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset); +} + +inline card *deck_to_card(card_deck *d) +{ + return vm->deck_to_card(d); +} + +inline card *factorvm::addr_to_allot_marker(object *a) +{ + return (card *)(((cell)a >> card_bits) + allot_markers_offset); +} + +inline card *addr_to_allot_marker(object *a) +{ + return vm->addr_to_allot_marker(a); +} + +/* the write barrier must be called any time we are potentially storing a +pointer from an older generation to a younger one */ +inline void factorvm::write_barrier(object *obj) +{ + *addr_to_card((cell)obj) = card_mark_mask; + *addr_to_deck((cell)obj) = card_mark_mask; +} + +inline void write_barrier(object *obj) +{ + return vm->write_barrier(obj); +} + +/* we need to remember the first object allocated in the card */ +inline void factorvm::allot_barrier(object *address) +{ + card *ptr = addr_to_allot_marker(address); + if(*ptr == invalid_allot_marker) + *ptr = ((cell)address & addr_card_mask); +} + +inline void allot_barrier(object *address) +{ + return vm->allot_barrier(address); +} + +// next method here: + + } diff --git a/vm/write_barrier.hpp b/vm/write_barrier.hpp index e874600b24..b45573b126 100755 --- a/vm/write_barrier.hpp +++ b/vm/write_barrier.hpp @@ -22,110 +22,13 @@ static const cell card_bits = 8; static const cell card_size = (1<> card_bits) + cards_offset); -} - -inline card *addr_to_card(cell a) -{ - return vm->addr_to_card(a); -} - -inline cell factorvm::card_to_addr(card *c) -{ - return ((cell)c - cards_offset) << card_bits; -} - -inline cell card_to_addr(card *c) -{ - return vm->card_to_addr(c); -} - -inline cell factorvm::card_offset(card *c) -{ - return *(c - (cell)data->cards + (cell)data->allot_markers); -} - -inline cell card_offset(card *c) -{ - return vm->card_offset(c); -} typedef u8 card_deck; static const cell deck_bits = (card_bits + 10); static const cell deck_size = (1<> deck_bits) + decks_offset); -} - -inline card_deck *addr_to_deck(cell a) -{ - return vm->addr_to_deck(a); -} - -inline cell factorvm::deck_to_addr(card_deck *c) -{ - return ((cell)c - decks_offset) << deck_bits; -} - -inline cell deck_to_addr(card_deck *c) -{ - return vm->deck_to_addr(c); -} - -inline card *factorvm::deck_to_card(card_deck *d) -{ - return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset); -} - -inline card *deck_to_card(card_deck *d) -{ - return vm->deck_to_card(d); -} - static const cell invalid_allot_marker = 0xff; - extern cell allot_markers_offset; -inline card *factorvm::addr_to_allot_marker(object *a) -{ - return (card *)(((cell)a >> card_bits) + allot_markers_offset); -} - -inline card *addr_to_allot_marker(object *a) -{ - return vm->addr_to_allot_marker(a); -} - -/* the write barrier must be called any time we are potentially storing a -pointer from an older generation to a younger one */ -inline void factorvm::write_barrier(object *obj) -{ - *addr_to_card((cell)obj) = card_mark_mask; - *addr_to_deck((cell)obj) = card_mark_mask; -} - -inline void write_barrier(object *obj) -{ - return vm->write_barrier(obj); -} - -/* we need to remember the first object allocated in the card */ -inline void factorvm::allot_barrier(object *address) -{ - card *ptr = addr_to_allot_marker(address); - if(*ptr == invalid_allot_marker) - *ptr = ((cell)address & addr_card_mask); -} - -inline void allot_barrier(object *address) -{ - return vm->allot_barrier(address); -} - } From 4dabd186c988aa2a34351f8c00d606a573129322 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:11 +0100 Subject: [PATCH 124/345] moved data_gc and local_roots inline functions to vm.hpp --- vm/data_gc.hpp | 129 --------------------------------- vm/local_roots.hpp | 45 ------------ vm/master.hpp | 2 +- vm/vm.hpp | 175 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 175 deletions(-) diff --git a/vm/data_gc.hpp b/vm/data_gc.hpp index 995d539d5d..47d2657587 100755 --- a/vm/data_gc.hpp +++ b/vm/data_gc.hpp @@ -22,19 +22,6 @@ void init_data_gc(); void gc(); -inline bool factorvm::collecting_accumulation_gen_p() -{ - return ((data->have_aging_p() - && collecting_gen == data->aging() - && !collecting_aging_again) - || collecting_gen == data->tenured()); -} - -inline bool collecting_accumulation_gen_p() -{ - return vm->collecting_accumulation_gen_p(); -} - void copy_handle(cell *handle); void garbage_collection(volatile cell gen, @@ -46,90 +33,6 @@ allocation (which does not call GC because of possible roots in volatile registers) does not run out of memory */ static const cell allot_buffer_zone = 1024; -inline object *factorvm::allot_zone(zone *z, cell a) -{ - cell h = z->here; - z->here = h + align8(a); - object *obj = (object *)h; - allot_barrier(obj); - return obj; -} - -inline object *allot_zone(zone *z, cell a) -{ - return vm->allot_zone(z,a); -} - -/* - * It is up to the caller to fill in the object's fields in a meaningful - * fashion! - */ -inline object *factorvm::allot_object(header header, cell size) -{ -#ifdef GC_DEBUG - if(!gc_off) - gc(); -#endif - - object *obj; - - if(nursery.size - allot_buffer_zone > size) - { - /* If there is insufficient room, collect the nursery */ - if(nursery.here + allot_buffer_zone + size > nursery.end) - garbage_collection(data->nursery(),false,0); - - cell h = nursery.here; - nursery.here = h + align8(size); - obj = (object *)h; - } - /* If the object is bigger than the nursery, allocate it in - tenured space */ - else - { - zone *tenured = &data->generations[data->tenured()]; - - /* If tenured space does not have enough room, collect */ - if(tenured->here + size > tenured->end) - { - gc(); - tenured = &data->generations[data->tenured()]; - } - - /* If it still won't fit, grow the heap */ - if(tenured->here + size > tenured->end) - { - garbage_collection(data->tenured(),true,size); - tenured = &data->generations[data->tenured()]; - } - - obj = allot_zone(tenured,size); - - /* Allows initialization code to store old->new pointers - without hitting the write barrier in the common case of - a nursery allocation */ - write_barrier(obj); - } - - obj->h = header; - return obj; -} - -inline object *allot_object(header header, cell size) -{ - return vm->allot_object(header,size); -} - -template TYPE *factorvm::allot(cell size) -{ - return (TYPE *)allot_object(header(TYPE::type_number),size); -} - -template TYPE *allot(cell size) -{ - return vm->allot(size); -} - void copy_reachable_objects(cell scan, cell *end); PRIMITIVE(gc); @@ -140,38 +43,6 @@ PRIMITIVE(become); extern bool growing_data_heap; -inline void factorvm::check_data_pointer(object *pointer) -{ -#ifdef FACTOR_DEBUG - if(!growing_data_heap) - { - assert((cell)pointer >= data->seg->start - && (cell)pointer < data->seg->end); - } -#endif -} - -inline void check_data_pointer(object *pointer) -{ - return vm->check_data_pointer(pointer); -} - -inline void factorvm::check_tagged_pointer(cell tagged) -{ -#ifdef FACTOR_DEBUG - if(!immediate_p(tagged)) - { - object *obj = untag(tagged); - check_data_pointer(obj); - obj->h.hi_tag(); - } -#endif -} - -inline void check_tagged_pointer(cell tagged) -{ - return vm->check_tagged_pointer(tagged); -} VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size); diff --git a/vm/local_roots.hpp b/vm/local_roots.hpp index 26d083be40..412ef35bb4 100644 --- a/vm/local_roots.hpp +++ b/vm/local_roots.hpp @@ -1,49 +1,4 @@ namespace factor { -struct factorvm; - -template -struct gc_root : public tagged -{ - factorvm *myvm; - - void push() { check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } - - //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } - explicit gc_root(cell value_,factorvm *vm) : tagged(value_),myvm(vm) { push(); } - explicit gc_root(TYPE *value_, factorvm *vm) : tagged(value_),myvm(vm) { push(); } - - const gc_root& operator=(const TYPE *x) { tagged::operator=(x); return *this; } - const gc_root& operator=(const cell &x) { tagged::operator=(x); return *this; } - - ~gc_root() { -#ifdef FACTOR_DEBUG - assert(myvm->gc_locals.back() == (cell)this); -#endif - myvm->gc_locals.pop_back(); - } -}; - -/* A similar hack for the bignum implementation */ -struct gc_bignum -{ - bignum **addr; - factorvm *myvm; - gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { - if(*addr_) - check_data_pointer(*addr_); - myvm->gc_bignums.push_back((cell)addr); - } - - ~gc_bignum() { -#ifdef FACTOR_DEBUG - assert(myvm->gc_bignums.back() == (cell)addr); -#endif - myvm->gc_bignums.pop_back(); - } -}; - -#define GC_BIGNUM(x,vm) gc_bignum x##__gc_root(&x,vm) - } diff --git a/vm/master.hpp b/vm/master.hpp index 0b3bf7b474..7b3d6c5c73 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -49,9 +49,9 @@ #include "code_block.hpp" #include "data_heap.hpp" #include "write_barrier.hpp" -#include "vm.hpp" #include "data_gc.hpp" #include "local_roots.hpp" +#include "vm.hpp" #include "generic_arrays.hpp" #include "debug.hpp" #include "arrays.hpp" diff --git a/vm/vm.hpp b/vm/vm.hpp index cfb18d2036..f44c1ca0eb 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -678,6 +678,181 @@ inline void allot_barrier(object *address) return vm->allot_barrier(address); } + +//data_gc.hpp +inline bool factorvm::collecting_accumulation_gen_p() +{ + return ((data->have_aging_p() + && collecting_gen == data->aging() + && !collecting_aging_again) + || collecting_gen == data->tenured()); +} + +inline bool collecting_accumulation_gen_p() +{ + return vm->collecting_accumulation_gen_p(); +} + +inline object *factorvm::allot_zone(zone *z, cell a) +{ + cell h = z->here; + z->here = h + align8(a); + object *obj = (object *)h; + allot_barrier(obj); + return obj; +} + +inline object *allot_zone(zone *z, cell a) +{ + return vm->allot_zone(z,a); +} + +/* + * It is up to the caller to fill in the object's fields in a meaningful + * fashion! + */ +inline object *factorvm::allot_object(header header, cell size) +{ +#ifdef GC_DEBUG + if(!gc_off) + gc(); +#endif + + object *obj; + + if(nursery.size - allot_buffer_zone > size) + { + /* If there is insufficient room, collect the nursery */ + if(nursery.here + allot_buffer_zone + size > nursery.end) + garbage_collection(data->nursery(),false,0); + + cell h = nursery.here; + nursery.here = h + align8(size); + obj = (object *)h; + } + /* If the object is bigger than the nursery, allocate it in + tenured space */ + else + { + zone *tenured = &data->generations[data->tenured()]; + + /* If tenured space does not have enough room, collect */ + if(tenured->here + size > tenured->end) + { + gc(); + tenured = &data->generations[data->tenured()]; + } + + /* If it still won't fit, grow the heap */ + if(tenured->here + size > tenured->end) + { + garbage_collection(data->tenured(),true,size); + tenured = &data->generations[data->tenured()]; + } + + obj = allot_zone(tenured,size); + + /* Allows initialization code to store old->new pointers + without hitting the write barrier in the common case of + a nursery allocation */ + write_barrier(obj); + } + + obj->h = header; + return obj; +} + +inline object *allot_object(header header, cell size) +{ + return vm->allot_object(header,size); +} + +template TYPE *factorvm::allot(cell size) +{ + return (TYPE *)allot_object(header(TYPE::type_number),size); +} + +template TYPE *allot(cell size) +{ + return vm->allot(size); +} + +inline void factorvm::check_data_pointer(object *pointer) +{ +#ifdef FACTOR_DEBUG + if(!growing_data_heap) + { + assert((cell)pointer >= data->seg->start + && (cell)pointer < data->seg->end); + } +#endif +} + +inline void check_data_pointer(object *pointer) +{ + return vm->check_data_pointer(pointer); +} + +inline void factorvm::check_tagged_pointer(cell tagged) +{ +#ifdef FACTOR_DEBUG + if(!immediate_p(tagged)) + { + object *obj = untag(tagged); + check_data_pointer(obj); + obj->h.hi_tag(); + } +#endif +} + +inline void check_tagged_pointer(cell tagged) +{ + return vm->check_tagged_pointer(tagged); +} + +//local_roots.hpp +template +struct gc_root : public tagged +{ + factorvm *myvm; + + void push() { check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } + + //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } + explicit gc_root(cell value_,factorvm *vm) : tagged(value_),myvm(vm) { push(); } + explicit gc_root(TYPE *value_, factorvm *vm) : tagged(value_),myvm(vm) { push(); } + + const gc_root& operator=(const TYPE *x) { tagged::operator=(x); return *this; } + const gc_root& operator=(const cell &x) { tagged::operator=(x); return *this; } + + ~gc_root() { +#ifdef FACTOR_DEBUG + assert(myvm->gc_locals.back() == (cell)this); +#endif + myvm->gc_locals.pop_back(); + } +}; + +/* A similar hack for the bignum implementation */ +struct gc_bignum +{ + bignum **addr; + factorvm *myvm; + gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { + if(*addr_) + check_data_pointer(*addr_); + myvm->gc_bignums.push_back((cell)addr); + } + + ~gc_bignum() { +#ifdef FACTOR_DEBUG + assert(myvm->gc_bignums.back() == (cell)addr); +#endif + myvm->gc_bignums.pop_back(); + } +}; + +#define GC_BIGNUM(x,vm) gc_bignum x##__gc_root(&x,vm) // next method here: From 2e129dfc453096063f5e8a0a867fa1642756a9ed Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:11 +0100 Subject: [PATCH 125/345] moved generic_arrays inline functions to vm.hpp --- vm/generic_arrays.hpp | 47 ----------------------------------------- vm/master.hpp | 2 +- vm/vm.hpp | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 48 deletions(-) diff --git a/vm/generic_arrays.hpp b/vm/generic_arrays.hpp index e134b1a2ad..0125cb7651 100644 --- a/vm/generic_arrays.hpp +++ b/vm/generic_arrays.hpp @@ -19,51 +19,4 @@ template cell array_size(T *array) return array_size(array_capacity(array)); } -template T *factorvm::allot_array_internal(cell capacity) -{ - T *array = allot(array_size(capacity)); - array->capacity = tag_fixnum(capacity); - return array; -} - -template T *allot_array_internal(cell capacity) -{ - return vm->allot_array_internal(capacity); -} - -template bool factorvm::reallot_array_in_place_p(T *array, cell capacity) -{ - return in_zone(&nursery,array) && capacity <= array_capacity(array); -} - -template bool reallot_array_in_place_p(T *array, cell capacity) -{ - return vm->reallot_array_in_place_p(array,capacity); -} - -template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) -{ - gc_root array(array_,this); - - if(reallot_array_in_place_p(array.untagged(),capacity)) - { - array->capacity = tag_fixnum(capacity); - return array.untagged(); - } - else - { - cell to_copy = array_capacity(array.untagged()); - if(capacity < to_copy) - to_copy = capacity; - - TYPE *new_array = allot_array_internal(capacity); - - memcpy(new_array + 1,array.untagged() + 1,to_copy * TYPE::element_size); - memset((char *)(new_array + 1) + to_copy * TYPE::element_size, - 0,(capacity - to_copy) * TYPE::element_size); - - return new_array; - } -} - } diff --git a/vm/master.hpp b/vm/master.hpp index 7b3d6c5c73..c25cbdc3a6 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -51,8 +51,8 @@ #include "write_barrier.hpp" #include "data_gc.hpp" #include "local_roots.hpp" -#include "vm.hpp" #include "generic_arrays.hpp" +#include "vm.hpp" #include "debug.hpp" #include "arrays.hpp" #include "strings.hpp" diff --git a/vm/vm.hpp b/vm/vm.hpp index f44c1ca0eb..8e0c583c5e 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -853,6 +853,55 @@ struct gc_bignum }; #define GC_BIGNUM(x,vm) gc_bignum x##__gc_root(&x,vm) + +//generic_arrays.hpp +template T *factorvm::allot_array_internal(cell capacity) +{ + T *array = allot(array_size(capacity)); + array->capacity = tag_fixnum(capacity); + return array; +} + +template T *allot_array_internal(cell capacity) +{ + return vm->allot_array_internal(capacity); +} + +template bool factorvm::reallot_array_in_place_p(T *array, cell capacity) +{ + return in_zone(&nursery,array) && capacity <= array_capacity(array); +} + +template bool reallot_array_in_place_p(T *array, cell capacity) +{ + return vm->reallot_array_in_place_p(array,capacity); +} + +template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) +{ + gc_root array(array_,this); + + if(reallot_array_in_place_p(array.untagged(),capacity)) + { + array->capacity = tag_fixnum(capacity); + return array.untagged(); + } + else + { + cell to_copy = array_capacity(array.untagged()); + if(capacity < to_copy) + to_copy = capacity; + + TYPE *new_array = allot_array_internal(capacity); + + memcpy(new_array + 1,array.untagged() + 1,to_copy * TYPE::element_size); + memset((char *)(new_array + 1) + to_copy * TYPE::element_size, + 0,(capacity - to_copy) * TYPE::element_size); + + return new_array; + } +} + // next method here: From 209755e2de5a1c97655ac91d2ccae01d995ee3f9 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:11 +0100 Subject: [PATCH 126/345] moved arrays.hpp inline functions to vm.hpp --- vm/arrays.hpp | 25 ++----------------------- vm/master.hpp | 2 +- vm/vm.hpp | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/vm/arrays.hpp b/vm/arrays.hpp index eda30c52fb..282474ade8 100755 --- a/vm/arrays.hpp +++ b/vm/arrays.hpp @@ -10,21 +10,9 @@ inline cell array_nth(array *array, cell slot) return array->data()[slot]; } -inline void factorvm::set_array_nth(array *array, cell slot, cell value) -{ -#ifdef FACTOR_DEBUG - assert(slot < array_capacity(array)); - assert(array->h.hi_tag() == ARRAY_TYPE); - check_tagged_pointer(value); -#endif - array->data()[slot] = value; - write_barrier(array); -} -inline void set_array_nth(array *array, cell slot, cell value) -{ - return vm->set_array_nth(array,slot,value); -} + + array *allot_array(cell capacity, cell fill); @@ -35,14 +23,5 @@ cell allot_array_4(cell v1, cell v2, cell v3, cell v4); PRIMITIVE(array); PRIMITIVE(resize_array); -struct growable_array { - cell count; - gc_root elements; - - growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(allot_array(capacity,F),myvm) {} - - void add(cell elt); - void trim(); -}; } diff --git a/vm/master.hpp b/vm/master.hpp index c25cbdc3a6..98ed043e05 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -52,9 +52,9 @@ #include "data_gc.hpp" #include "local_roots.hpp" #include "generic_arrays.hpp" -#include "vm.hpp" #include "debug.hpp" #include "arrays.hpp" +#include "vm.hpp" #include "strings.hpp" #include "booleans.hpp" #include "byte_arrays.hpp" diff --git a/vm/vm.hpp b/vm/vm.hpp index 8e0c583c5e..0ccb86b970 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -902,6 +902,33 @@ template TYPE *factorvm::reallot_array(TYPE *array_, cell capaci } } +//arrays.hpp +inline void factorvm::set_array_nth(array *array, cell slot, cell value) +{ +#ifdef FACTOR_DEBUG + assert(slot < array_capacity(array)); + assert(array->h.hi_tag() == ARRAY_TYPE); + check_tagged_pointer(value); +#endif + array->data()[slot] = value; + write_barrier(array); +} + +inline void set_array_nth(array *array, cell slot, cell value) +{ + return vm->set_array_nth(array,slot,value); +} + +struct growable_array { + cell count; + gc_root elements; + + growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(allot_array(capacity,F),myvm) {} + + void add(cell elt); + void trim(); +}; + // next method here: From a249b484c47773193f731f0b24bc47b58d701357 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:12 +0100 Subject: [PATCH 127/345] moved byte_arrays.hpp inline functions to vm.hpp --- vm/byte_arrays.hpp | 11 ----------- vm/master.hpp | 2 +- vm/vm.hpp | 13 +++++++++++++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/vm/byte_arrays.hpp b/vm/byte_arrays.hpp index 68b706be7b..4954f31094 100755 --- a/vm/byte_arrays.hpp +++ b/vm/byte_arrays.hpp @@ -7,16 +7,5 @@ PRIMITIVE(byte_array); PRIMITIVE(uninitialized_byte_array); PRIMITIVE(resize_byte_array); -struct growable_byte_array { - cell count; - gc_root elements; - - growable_byte_array(factorvm *vm,cell capacity = 40) : count(0), elements(allot_byte_array(capacity),vm) { } - - void append_bytes(void *elts, cell len); - void append_byte_array(cell elts); - - void trim(); -}; } diff --git a/vm/master.hpp b/vm/master.hpp index 98ed043e05..0eb51e7230 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -54,11 +54,11 @@ #include "generic_arrays.hpp" #include "debug.hpp" #include "arrays.hpp" -#include "vm.hpp" #include "strings.hpp" #include "booleans.hpp" #include "byte_arrays.hpp" #include "tuples.hpp" +#include "vm.hpp" #include "words.hpp" #include "math.hpp" #include "float_bits.hpp" diff --git a/vm/vm.hpp b/vm/vm.hpp index 0ccb86b970..b185a1600e 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -929,6 +929,19 @@ struct growable_array { void trim(); }; +//byte_arrays.hpp +struct growable_byte_array { + cell count; + gc_root elements; + + growable_byte_array(factorvm *vm,cell capacity = 40) : count(0), elements(allot_byte_array(capacity),vm) { } + + void append_bytes(void *elts, cell len); + void append_byte_array(cell elts); + + void trim(); +}; + // next method here: From 9e23e4126772a84cc7010c4cb88af2b8b6e51221 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:12 +0100 Subject: [PATCH 128/345] moved math.hpp inline functions to vm.hpp --- vm/master.hpp | 2 +- vm/math.hpp | 58 +++++++++----------------------------------------- vm/vm.hpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 49 deletions(-) diff --git a/vm/master.hpp b/vm/master.hpp index 0eb51e7230..4adc163123 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -58,9 +58,9 @@ #include "booleans.hpp" #include "byte_arrays.hpp" #include "tuples.hpp" -#include "vm.hpp" #include "words.hpp" #include "math.hpp" +#include "vm.hpp" #include "float_bits.hpp" #include "io.hpp" #include "code_gc.hpp" diff --git a/vm/math.hpp b/vm/math.hpp index cb4f1b1101..0f8de05218 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -42,31 +42,13 @@ PRIMITIVE(bignum_bitp); PRIMITIVE(bignum_log2); PRIMITIVE(byte_array_to_bignum); -inline cell factorvm::allot_integer(fixnum x) -{ - if(x < fixnum_min || x > fixnum_max) - return tag(fixnum_to_bignum(x)); - else - return tag_fixnum(x); -} -inline cell allot_integer(fixnum x) -{ - return vm->allot_integer(x); -} -inline cell factorvm::allot_cell(cell x) -{ - if(x > (cell)fixnum_max) - return tag(cell_to_bignum(x)); - else - return tag_fixnum(x); -} -inline cell allot_cell(cell x) -{ - return vm->allot_cell(x); -} + + + + cell unbox_array_size(); @@ -80,47 +62,27 @@ inline static double untag_float_check(cell tagged) return untag_check(tagged)->n; } -inline cell factorvm::allot_float(double n) -{ - boxed_float *flo = allot(sizeof(boxed_float)); - flo->n = n; - return tag(flo); -} -inline cell allot_float(double n) -{ - return vm->allot_float(n); -} + + inline static fixnum float_to_fixnum(cell tagged) { return (fixnum)untag_float(tagged); } -inline bignum *factorvm::float_to_bignum(cell tagged) -{ - return double_to_bignum(untag_float(tagged)); -} -inline bignum *float_to_bignum(cell tagged) -{ - return vm->float_to_bignum(tagged); -} + + inline double fixnum_to_float(cell tagged) { return (double)untag_fixnum(tagged); } -inline double factorvm::bignum_to_float(cell tagged) -{ - return bignum_to_double(untag(tagged)); -} -inline double bignum_to_float(cell tagged) -{ - return vm->bignum_to_float(tagged); -} + + PRIMITIVE(fixnum_to_float); PRIMITIVE(bignum_to_float); diff --git a/vm/vm.hpp b/vm/vm.hpp index b185a1600e..fdfa48d1e7 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -942,6 +942,65 @@ struct growable_byte_array { void trim(); }; +//math.hpp +inline cell factorvm::allot_integer(fixnum x) +{ + if(x < fixnum_min || x > fixnum_max) + return tag(fixnum_to_bignum(x)); + else + return tag_fixnum(x); +} + +inline cell allot_integer(fixnum x) +{ + return vm->allot_integer(x); +} + +inline cell factorvm::allot_cell(cell x) +{ + if(x > (cell)fixnum_max) + return tag(cell_to_bignum(x)); + else + return tag_fixnum(x); +} + +inline cell allot_cell(cell x) +{ + return vm->allot_cell(x); +} + +inline cell factorvm::allot_float(double n) +{ + boxed_float *flo = allot(sizeof(boxed_float)); + flo->n = n; + return tag(flo); +} + +inline cell allot_float(double n) +{ + return vm->allot_float(n); +} + +inline bignum *factorvm::float_to_bignum(cell tagged) +{ + return double_to_bignum(untag_float(tagged)); +} + +inline bignum *float_to_bignum(cell tagged) +{ + return vm->float_to_bignum(tagged); +} + +inline double factorvm::bignum_to_float(cell tagged) +{ + return bignum_to_double(untag(tagged)); +} + +inline double bignum_to_float(cell tagged) +{ + return vm->bignum_to_float(tagged); +} + // next method here: From fb9f9ac3d35acb073e8e1b818df589654109de27 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:12 +0100 Subject: [PATCH 129/345] moved callstack.hpp inline functions to vm.hpp --- vm/callstack.hpp | 19 ------------------- vm/code_gc.hpp | 2 +- vm/code_heap.hpp | 2 ++ vm/master.hpp | 2 +- vm/vm.hpp | 23 +++++++++++++++++++++-- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/vm/callstack.hpp b/vm/callstack.hpp index 7e7e5cc20b..ee097b528c 100755 --- a/vm/callstack.hpp +++ b/vm/callstack.hpp @@ -33,24 +33,5 @@ template void iterate_callstack(cell top, cell bottom, T &iterator) } } -/* This is a little tricky. The iterator may allocate memory, so we -keep the callstack in a GC root and use relative offsets */ -template void factorvm::iterate_callstack_object(callstack *stack_, T &iterator) -{ - gc_root stack(stack_,vm); - fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); - - while(frame_offset >= 0) - { - stack_frame *frame = stack->frame_at(frame_offset); - frame_offset -= frame->size; - iterator(frame); - } -} - -template void iterate_callstack_object(callstack *stack_, T &iterator) -{ - return vm->iterate_callstack_object(stack_,iterator); -} } diff --git a/vm/code_gc.hpp b/vm/code_gc.hpp index ed59cc5919..1cfafb69c2 100755 --- a/vm/code_gc.hpp +++ b/vm/code_gc.hpp @@ -14,7 +14,7 @@ struct heap { heap_free_list free; }; -//typedef void (*heap_iterator)(heap_block *compiled); +typedef void (*heap_iterator)(heap_block *compiled); void new_heap(heap *h, cell size); void build_free_list(heap *h, cell size); diff --git a/vm/code_heap.hpp b/vm/code_heap.hpp index 20d2b974d3..6f139a4728 100755 --- a/vm/code_heap.hpp +++ b/vm/code_heap.hpp @@ -10,6 +10,8 @@ bool in_code_heap_p(cell ptr); void jit_compile_word(cell word, cell def, bool relocate); +typedef void (*code_heap_iterator)(code_block *compiled); + void iterate_code_heap(code_heap_iterator iter); void copy_code_heap_roots(); diff --git a/vm/master.hpp b/vm/master.hpp index 4adc163123..792e997f6f 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -60,13 +60,13 @@ #include "tuples.hpp" #include "words.hpp" #include "math.hpp" -#include "vm.hpp" #include "float_bits.hpp" #include "io.hpp" #include "code_gc.hpp" #include "code_heap.hpp" #include "image.hpp" #include "callstack.hpp" +#include "vm.hpp" #include "alien.hpp" #include "jit.hpp" #include "quotations.hpp" diff --git a/vm/vm.hpp b/vm/vm.hpp index fdfa48d1e7..1caa30040b 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -11,8 +11,6 @@ struct image_header; typedef u8 card; typedef u8 card_deck; -typedef void (*heap_iterator)(heap_block *compiled); -typedef void (*code_heap_iterator)(code_block *compiled); struct factorvm { @@ -1001,6 +999,27 @@ inline double bignum_to_float(cell tagged) return vm->bignum_to_float(tagged); } +//callstack.hpp +/* This is a little tricky. The iterator may allocate memory, so we +keep the callstack in a GC root and use relative offsets */ +template void factorvm::iterate_callstack_object(callstack *stack_, T &iterator) +{ + gc_root stack(stack_,vm); + fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); + + while(frame_offset >= 0) + { + stack_frame *frame = stack->frame_at(frame_offset); + frame_offset -= frame->size; + iterator(frame); + } +} + +template void iterate_callstack_object(callstack *stack_, T &iterator) +{ + return vm->iterate_callstack_object(stack_,iterator); +} + // next method here: From ecfd9a6075be6906ad2b599d9d8afacbba31944c Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:12 +0100 Subject: [PATCH 130/345] reordered master to untangle dependency chain a bit --- vm/master.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/master.hpp b/vm/master.hpp index 792e997f6f..e118be67c3 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -66,8 +66,8 @@ #include "code_heap.hpp" #include "image.hpp" #include "callstack.hpp" -#include "vm.hpp" #include "alien.hpp" +#include "vm.hpp" #include "jit.hpp" #include "quotations.hpp" #include "dispatch.hpp" From 80716a1b6e34fc31e43301753191a8f136572251 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:12 +0100 Subject: [PATCH 131/345] moved global state from contexts and run into vm Also renamed template type from T to TYPE to prevent clash with vm::T (true) --- vm/booleans.hpp | 4 ---- vm/contexts.cpp | 2 -- vm/contexts.hpp | 2 +- vm/data_gc.cpp | 8 ++++---- vm/data_heap.cpp | 4 ++-- vm/image.cpp | 8 ++++---- vm/jit.hpp | 2 +- vm/run.cpp | 1 - vm/run.hpp | 3 --- vm/vm.hpp | 34 +++++++++++++++++++++++++--------- 10 files changed, 37 insertions(+), 31 deletions(-) mode change 100644 => 100755 vm/run.hpp diff --git a/vm/booleans.hpp b/vm/booleans.hpp index ea16e0536b..c410f67481 100644 --- a/vm/booleans.hpp +++ b/vm/booleans.hpp @@ -1,10 +1,6 @@ namespace factor { -inline static cell tag_boolean(cell untagged) -{ - return (untagged ? T : F); -} VM_C_API void box_boolean(bool value); VM_C_API bool to_boolean(cell value); diff --git a/vm/contexts.cpp b/vm/contexts.cpp index 86156de3b5..03768ec0db 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -5,8 +5,6 @@ factor::context *stack_chain; namespace factor { -cell ds_size, rs_size; -context *unused_contexts; void factorvm::reset_datastack() { diff --git a/vm/contexts.hpp b/vm/contexts.hpp index 4a6f401f0b..56642bcd1a 100644 --- a/vm/contexts.hpp +++ b/vm/contexts.hpp @@ -36,7 +36,7 @@ struct context { context *next; }; -extern cell ds_size, rs_size; +//extern cell ds_size, rs_size; #define ds_bot (stack_chain->datastack_region->start) #define ds_top (stack_chain->datastack_region->end) diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index ea7098912e..ac0402b47d 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -123,22 +123,22 @@ object *resolve_forwarding(object *untagged) return vm->resolve_forwarding(untagged); } -template T *factorvm::copy_untagged_object(T *untagged) +template TYPE *factorvm::copy_untagged_object(TYPE *untagged) { check_data_pointer(untagged); if(untagged->h.forwarding_pointer_p()) - untagged = (T *)resolve_forwarding(untagged->h.forwarding_pointer()); + untagged = (TYPE *)resolve_forwarding(untagged->h.forwarding_pointer()); else { untagged->h.check_header(); - untagged = (T *)copy_object_impl(untagged); + untagged = (TYPE *)copy_object_impl(untagged); } return untagged; } -template T *copy_untagged_object(T *untagged) +template TYPE *copy_untagged_object(TYPE *untagged) { return vm->copy_untagged_object(untagged); } diff --git a/vm/data_heap.cpp b/vm/data_heap.cpp index 377cd6e943..34284659f0 100755 --- a/vm/data_heap.cpp +++ b/vm/data_heap.cpp @@ -453,7 +453,7 @@ PRIMITIVE(end_scan) PRIMITIVE_GETVM()->vmprim_end_scan(); } -template void factorvm::each_object(T &functor) +template void factorvm::each_object(TYPE &functor) { begin_scan(); cell obj; @@ -462,7 +462,7 @@ template void factorvm::each_object(T &functor) end_scan(); } -template void each_object(T &functor) +template void each_object(TYPE &functor) { return vm->each_object(functor); } diff --git a/vm/image.cpp b/vm/image.cpp index 24988b24b5..746461680c 100755 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -202,14 +202,14 @@ void data_fixup(cell *cell) return vm->data_fixup(cell); } -template void factorvm::code_fixup(T **handle) +template void factorvm::code_fixup(TYPE **handle) { - T *ptr = *handle; - T *new_ptr = (T *)(((cell)ptr) + (code.seg->start - code_relocation_base)); + TYPE *ptr = *handle; + TYPE *new_ptr = (TYPE *)(((cell)ptr) + (code.seg->start - code_relocation_base)); *handle = new_ptr; } -template void code_fixup(T **handle) +template void code_fixup(TYPE **handle) { return vm->code_fixup(handle); } diff --git a/vm/jit.hpp b/vm/jit.hpp index 0fa145cded..81754be26a 100644 --- a/vm/jit.hpp +++ b/vm/jit.hpp @@ -42,7 +42,7 @@ struct jit { void emit_subprimitive(cell word_) { gc_root word(word_,myvm); gc_root code_template(word->subprimitive,myvm); - if(array_capacity(code_template.untagged()) > 1) literal(T); + if(array_capacity(code_template.untagged()) > 1) literal(myvm->T); emit(code_template.value()); } diff --git a/vm/run.cpp b/vm/run.cpp index e0ad1abb12..7f4894f1ef 100755 --- a/vm/run.cpp +++ b/vm/run.cpp @@ -5,7 +5,6 @@ factor::cell userenv[USER_ENV]; namespace factor { -cell T; inline void factorvm::vmprim_getenv() { diff --git a/vm/run.hpp b/vm/run.hpp old mode 100644 new mode 100755 index 7527889efb..4b43a156e4 --- a/vm/run.hpp +++ b/vm/run.hpp @@ -98,9 +98,6 @@ inline static bool save_env_p(cell i) return (i >= FIRST_SAVE_ENV && i <= LAST_SAVE_ENV) || i == STACK_TRACES_ENV; } -/* Canonical T object. It's just a word */ -extern cell T; - PRIMITIVE(getenv); PRIMITIVE(setenv); PRIMITIVE(exit); diff --git a/vm/vm.hpp b/vm/vm.hpp index 1caa30040b..a58ac1ad9e 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -15,6 +15,8 @@ typedef u8 card_deck; struct factorvm { // contexts + cell ds_size, rs_size; + context *unused_contexts; void reset_datastack(); void reset_retainstack(); void fix_stacks(); @@ -33,6 +35,7 @@ struct factorvm { inline void vmprim_check_datastack(); // run + cell T; /* Canonical T object. It's just a word */ inline void vmprim_getenv(); inline void vmprim_setenv(); inline void vmprim_exit(); @@ -264,6 +267,7 @@ struct factorvm { //booleans void box_boolean(bool value); bool to_boolean(cell value); + inline cell tag_boolean(cell untagged); //byte arrays byte_array *allot_byte_array(cell size); @@ -853,26 +857,26 @@ struct gc_bignum #define GC_BIGNUM(x,vm) gc_bignum x##__gc_root(&x,vm) //generic_arrays.hpp -template T *factorvm::allot_array_internal(cell capacity) +template TYPE *factorvm::allot_array_internal(cell capacity) { - T *array = allot(array_size(capacity)); + TYPE *array = allot(array_size(capacity)); array->capacity = tag_fixnum(capacity); return array; } -template T *allot_array_internal(cell capacity) +template TYPE *allot_array_internal(cell capacity) { - return vm->allot_array_internal(capacity); + return vm->allot_array_internal(capacity); } -template bool factorvm::reallot_array_in_place_p(T *array, cell capacity) +template bool factorvm::reallot_array_in_place_p(TYPE *array, cell capacity) { return in_zone(&nursery,array) && capacity <= array_capacity(array); } -template bool reallot_array_in_place_p(T *array, cell capacity) +template bool reallot_array_in_place_p(TYPE *array, cell capacity) { - return vm->reallot_array_in_place_p(array,capacity); + return vm->reallot_array_in_place_p(array,capacity); } template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) @@ -1002,7 +1006,7 @@ inline double bignum_to_float(cell tagged) //callstack.hpp /* This is a little tricky. The iterator may allocate memory, so we keep the callstack in a GC root and use relative offsets */ -template void factorvm::iterate_callstack_object(callstack *stack_, T &iterator) +template void factorvm::iterate_callstack_object(callstack *stack_, TYPE &iterator) { gc_root stack(stack_,vm); fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); @@ -1015,11 +1019,23 @@ template void factorvm::iterate_callstack_object(callstack *stack_, } } -template void iterate_callstack_object(callstack *stack_, T &iterator) +template void iterate_callstack_object(callstack *stack_, TYPE &iterator) { return vm->iterate_callstack_object(stack_,iterator); } +//booleans.hpp +inline cell factorvm::tag_boolean(cell untagged) +{ + return (untagged ? T : F); +} + +inline cell tag_boolean(cell untagged) +{ + return vm->tag_boolean(untagged); +} + + // next method here: From 3025cef1c68baa6b5ba971153f2422bb61ce3b0a Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:12 +0100 Subject: [PATCH 132/345] moved global state from data_gc into vm --- vm/data_gc.cpp | 30 ------------------------------ vm/data_gc.hpp | 10 ---------- vm/vm.hpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index ac0402b47d..408a70ea5e 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -3,36 +3,6 @@ namespace factor { -/* used during garbage collection only */ -zone *newspace; -bool performing_gc; -bool performing_compaction; -cell collecting_gen; - -/* if true, we are collecting aging space for the second time, so if it is still -full, we go on to collect tenured */ -bool collecting_aging_again; - -/* in case a generation fills up in the middle of a gc, we jump back -up to try collecting the next generation. */ -jmp_buf gc_jmp; - -gc_stats stats[max_gen_count]; -u64 cards_scanned; -u64 decks_scanned; -u64 card_scan_time; -cell code_heap_scans; - -/* What generation was being collected when copy_code_heap_roots() was last -called? Until the next call to add_code_block(), future -collections of younger generations don't have to touch the code -heap. */ -cell last_code_heap_scan; - -/* sometimes we grow the heap */ -bool growing_data_heap; -data_heap *old_data_heap; - void factorvm::init_data_gc() { performing_gc = false; diff --git a/vm/data_gc.hpp b/vm/data_gc.hpp index 47d2657587..68b2b4a936 100755 --- a/vm/data_gc.hpp +++ b/vm/data_gc.hpp @@ -10,14 +10,6 @@ struct gc_stats { u64 bytes_copied; }; -extern zone *newspace; - -extern bool performing_compaction; -extern cell collecting_gen; -extern bool collecting_aging_again; - -extern cell last_code_heap_scan; - void init_data_gc(); void gc(); @@ -41,8 +33,6 @@ void clear_gc_stats(); PRIMITIVE(clear_gc_stats); PRIMITIVE(become); -extern bool growing_data_heap; - VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size); diff --git a/vm/vm.hpp b/vm/vm.hpp index a58ac1ad9e..59d9d277f8 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -170,6 +170,36 @@ struct factorvm { //data_gc + /* used during garbage collection only */ + zone *newspace; + bool performing_gc; + bool performing_compaction; + cell collecting_gen; + + /* if true, we collecting aging space for the second time, so if it is still + full, we go on to collect tenured */ + bool collecting_aging_again; + + /* in case a generation fills up in the middle of a gc, we jump back + up to try collecting the next generation. */ + jmp_buf gc_jmp; + + gc_stats stats[max_gen_count]; + u64 cards_scanned; + u64 decks_scanned; + u64 card_scan_time; + cell code_heap_scans; + + /* What generation was being collected when copy_code_heap_roots() was last + called? Until the next call to add_code_block(), future + collections of younger generations don't have to touch the code + heap. */ + cell last_code_heap_scan; + + /* sometimes we grow the heap */ + bool growing_data_heap; + data_heap *old_data_heap; + void init_data_gc(); object *copy_untagged_object_impl(object *pointer, cell size); object *copy_object_impl(object *untagged); From 221c0ac5c892612800d38e83e06cf81870c91d2e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:13 +0100 Subject: [PATCH 133/345] moved global state from data_heap into vm --- vm/data_heap.cpp | 12 ++---------- vm/data_heap.hpp | 6 ------ vm/vm.hpp | 6 ++++++ 3 files changed, 8 insertions(+), 16 deletions(-) mode change 100644 => 100755 vm/data_heap.hpp diff --git a/vm/data_heap.cpp b/vm/data_heap.cpp index 34284659f0..020954bf05 100755 --- a/vm/data_heap.cpp +++ b/vm/data_heap.cpp @@ -5,16 +5,12 @@ factor::zone nursery; namespace factor { -/* Set by the -securegc command line argument */ -bool secure_gc; - /* new objects are allocated here */ VM_C_API zone nursery; -/* GC is off during heap walking */ -bool gc_off; -data_heap *data; + + cell factorvm::init_zone(zone *z, cell size, cell start) { @@ -377,10 +373,6 @@ PRIMITIVE(data_room) PRIMITIVE_GETVM()->vmprim_data_room(); } -/* A heap walk allows useful things to be done, like finding all -references to an object for debugging purposes. */ -cell heap_scan_ptr; - /* Disables GC and activates next-object ( -- obj ) primitive */ void factorvm::begin_scan() { diff --git a/vm/data_heap.hpp b/vm/data_heap.hpp old mode 100644 new mode 100755 index 4ef72a6fcb..2bec35b8c1 --- a/vm/data_heap.hpp +++ b/vm/data_heap.hpp @@ -1,8 +1,6 @@ namespace factor { -/* Set by the -securegc command line argument */ -extern bool secure_gc; /* generational copying GC divides memory into zones */ struct zone { @@ -47,7 +45,6 @@ struct data_heap { bool have_aging_p() { return gen_count > 2; } }; -extern data_heap *data; static const cell max_gen_count = 3; @@ -99,9 +96,6 @@ PRIMITIVE(begin_scan); PRIMITIVE(next_object); PRIMITIVE(end_scan); -/* GC is off during heap walking */ -extern bool gc_off; - cell find_all_words(); /* Every object has a regular representation in the runtime, which makes GC diff --git a/vm/vm.hpp b/vm/vm.hpp index 59d9d277f8..1e30c13136 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -131,6 +131,12 @@ struct factorvm { bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p); //data_heap + bool secure_gc; /* Set by the -securegc command line argument */ + bool gc_off; /* GC is off during heap walking */ + data_heap *data; + /* A heap walk allows useful things to be done, like finding all + references to an object for debugging purposes. */ + cell heap_scan_ptr; cell init_zone(zone *z, cell size, cell start); void init_card_decks(); data_heap *alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size); From 396eeeba34496919a7e57b8ad9eb70e07d4c7b21 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:13 +0100 Subject: [PATCH 134/345] moved global state from code_heap into vm --- vm/code_heap.cpp | 3 --- vm/code_heap.hpp | 2 -- vm/vm.hpp | 2 ++ 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index 943ee8d7c0..3d5d2c955a 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -3,8 +3,6 @@ namespace factor { -heap code; - /* Allocate a code heap during startup */ void factorvm::init_code_heap(cell size) { @@ -159,7 +157,6 @@ PRIMITIVE(code_room) PRIMITIVE_GETVM()->vmprim_code_room(); } -static unordered_map forwarding; code_block *factorvm::forward_xt(code_block *compiled) { diff --git a/vm/code_heap.hpp b/vm/code_heap.hpp index 6f139a4728..31116590ad 100755 --- a/vm/code_heap.hpp +++ b/vm/code_heap.hpp @@ -1,8 +1,6 @@ namespace factor { -/* compiled code */ -extern heap code; void init_code_heap(cell size); diff --git a/vm/vm.hpp b/vm/vm.hpp index 1e30c13136..8eda721992 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -468,6 +468,8 @@ struct factorvm { code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_); //code_heap + heap code; + unordered_map forwarding; void init_code_heap(cell size); bool in_code_heap_p(cell ptr); void jit_compile_word(cell word_, cell def_, bool relocate); From c506abc6cdcc548cec12d6c8ac9d78dac6bce3f6 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:13 +0100 Subject: [PATCH 135/345] moved global state from debug into vm --- vm/debug.cpp | 4 ---- vm/vm.hpp | 5 ++++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/vm/debug.cpp b/vm/debug.cpp index 870c817a2b..5d033fd90e 100755 --- a/vm/debug.cpp +++ b/vm/debug.cpp @@ -3,8 +3,6 @@ namespace factor { -static bool fep_disabled; -static bool full_output; void factorvm::print_chars(string* str) { @@ -346,8 +344,6 @@ void dump_objects(cell type) return vm->dump_objects(type); } -cell look_for; -cell obj; void factorvm::find_data_references_step(cell *scan) { diff --git a/vm/vm.hpp b/vm/vm.hpp index 8eda721992..4ad83b86be 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -252,6 +252,10 @@ struct factorvm { template TYPE *reallot_array(TYPE *array_, cell capacity); //debug + bool fep_disabled; + bool full_output; + cell look_for; + cell obj; void print_chars(string* str); void print_word(word* word, cell nesting); void print_factor_string(string* str); @@ -1073,7 +1077,6 @@ inline cell tag_boolean(cell untagged) return vm->tag_boolean(untagged); } - // next method here: From 498b1917dc196a8732c0fef74680aac0964ab77b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:13 +0100 Subject: [PATCH 136/345] moved global state from dispatch into vm --- vm/dispatch.cpp | 3 --- vm/dispatch.hpp | 3 --- vm/vm.hpp | 2 ++ 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/vm/dispatch.cpp b/vm/dispatch.cpp index 1721efe181..25f7a2f2ec 100755 --- a/vm/dispatch.cpp +++ b/vm/dispatch.cpp @@ -3,9 +3,6 @@ namespace factor { -cell megamorphic_cache_hits; -cell megamorphic_cache_misses; - cell factorvm::search_lookup_alist(cell table, cell klass) { array *elements = untag(table); diff --git a/vm/dispatch.hpp b/vm/dispatch.hpp index 75368191a7..f5648c7ebe 100644 --- a/vm/dispatch.hpp +++ b/vm/dispatch.hpp @@ -1,9 +1,6 @@ namespace factor { -extern cell megamorphic_cache_hits; -extern cell megamorphic_cache_misses; - cell lookup_method(cell object, cell methods); PRIMITIVE(lookup_method); diff --git a/vm/vm.hpp b/vm/vm.hpp index 4ad83b86be..8a0808397d 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -559,6 +559,8 @@ struct factorvm { inline void vmprim_quot_compiled_p(); //dispatch + cell megamorphic_cache_hits; + cell megamorphic_cache_misses; cell search_lookup_alist(cell table, cell klass); cell search_lookup_hash(cell table, cell klass, cell hashcode); cell nth_superclass(tuple_layout *layout, fixnum echelon); From 839491a828ec0528918e3b0e97ebe2ea49ea8d68 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:13 +0100 Subject: [PATCH 137/345] moved global state from inline_cache into vm --- vm/image.cpp | 2 -- vm/inline_cache.cpp | 8 -------- vm/inline_cache.hpp | 3 --- vm/vm.hpp | 7 +++++++ 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/vm/image.cpp b/vm/image.cpp index 746461680c..91fa1b801b 100755 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -19,7 +19,6 @@ void init_objects(image_header *h) return vm->init_objects(h); } -cell data_relocation_base; void factorvm::load_data_heap(FILE *file, image_header *h, vm_parameters *p) { @@ -59,7 +58,6 @@ void load_data_heap(FILE *file, image_header *h, vm_parameters *p) return vm->load_data_heap(file,h,p); } -cell code_relocation_base; void factorvm::load_code_heap(FILE *file, image_header *h, vm_parameters *p) { diff --git a/vm/inline_cache.cpp b/vm/inline_cache.cpp index b6a90b1ff4..15d8fcb203 100755 --- a/vm/inline_cache.cpp +++ b/vm/inline_cache.cpp @@ -3,14 +3,6 @@ namespace factor { -cell max_pic_size; - -cell cold_call_to_ic_transitions; -cell ic_to_pic_transitions; -cell pic_to_mega_transitions; - -/* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ -cell pic_counts[4]; void factorvm::init_inline_caching(int max_size) { diff --git a/vm/inline_cache.hpp b/vm/inline_cache.hpp index e2a6ae8cf9..e354d30677 100644 --- a/vm/inline_cache.hpp +++ b/vm/inline_cache.hpp @@ -1,8 +1,5 @@ namespace factor { - -extern cell max_pic_size; - void init_inline_caching(int max_size); PRIMITIVE(reset_inline_cache_stats); diff --git a/vm/vm.hpp b/vm/vm.hpp index 8a0808397d..813bf5c528 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -489,6 +489,8 @@ struct factorvm { void compact_code_heap(); //image + cell code_relocation_base; + cell data_relocation_base; void init_objects(image_header *h); void load_data_heap(FILE *file, image_header *h, vm_parameters *p); void load_code_heap(FILE *file, image_header *h, vm_parameters *p); @@ -578,6 +580,11 @@ struct factorvm { inline void vmprim_dispatch_stats(); //inline cache + cell max_pic_size; + cell cold_call_to_ic_transitions; + cell ic_to_pic_transitions; + cell pic_to_mega_transitions; + cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ void init_inline_caching(int max_size); void deallocate_inline_cache(cell return_address); cell determine_inline_cache_type(array *cache_entries); From efa974f0250946959fd2f46d5e370f383b9e21fd Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:13 +0100 Subject: [PATCH 138/345] moved global state from math into vm --- vm/bignum.cpp | 2 +- vm/bignum.hpp | 4 ---- vm/local_roots.hpp | 1 - vm/math.cpp | 4 ---- vm/math.hpp | 4 ---- vm/vm.hpp | 10 ++++++++++ 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index 62d9952364..03b34edd97 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -371,7 +371,7 @@ bignum *bignum_remainder(bignum * numerator, bignum * denominator) } #define FOO_TO_BIGNUM(name,type,utype) \ - bignum * name##_to_bignum(type n) \ + bignum * factorvm::name##_to_bignum(type n) \ { \ int negative_p; \ bignum_digit_type result_digits [BIGNUM_DIGITS_FOR(type)]; \ diff --git a/vm/bignum.hpp b/vm/bignum.hpp index 296f0dce4c..5f502dcc22 100644 --- a/vm/bignum.hpp +++ b/vm/bignum.hpp @@ -55,10 +55,6 @@ bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder); bignum * bignum_quotient(bignum *, bignum *); bignum * bignum_remainder(bignum *, bignum *); -bignum * fixnum_to_bignum(fixnum); -bignum * cell_to_bignum(cell); -bignum * long_long_to_bignum(s64 n); -bignum * ulong_long_to_bignum(u64 n); fixnum bignum_to_fixnum(bignum *); cell bignum_to_cell(bignum *); s64 bignum_to_long_long(bignum *); diff --git a/vm/local_roots.hpp b/vm/local_roots.hpp index 412ef35bb4..0d6a033f82 100644 --- a/vm/local_roots.hpp +++ b/vm/local_roots.hpp @@ -1,4 +1,3 @@ namespace factor { - } diff --git a/vm/math.cpp b/vm/math.cpp index f273cede0e..74caec3074 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -3,10 +3,6 @@ namespace factor { -cell bignum_zero; -cell bignum_pos_one; -cell bignum_neg_one; - inline void factorvm::vmprim_bignum_to_fixnum() { drepl(tag_fixnum(bignum_to_fixnum(untag(dpeek())))); diff --git a/vm/math.hpp b/vm/math.hpp index 0f8de05218..863fa1b4af 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -1,10 +1,6 @@ namespace factor { -extern cell bignum_zero; -extern cell bignum_pos_one; -extern cell bignum_neg_one; - static const fixnum fixnum_max = (((fixnum)1 << (WORD_SIZE - TAG_BITS - 1)) - 1); static const fixnum fixnum_min = (-((fixnum)1 << (WORD_SIZE - TAG_BITS - 1))); static const fixnum array_size_max = ((cell)1 << (WORD_SIZE - TAG_BITS - 2)); diff --git a/vm/vm.hpp b/vm/vm.hpp index 813bf5c528..fab910be48 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -243,6 +243,9 @@ struct factorvm { inline void check_tagged_pointer(cell tagged); // local roots + /* If a runtime function needs to call another function which potentially + allocates memory, it must wrap any local variable references to Factor + objects in gc_root instances */ std::vector gc_locals; std::vector gc_bignums; @@ -329,10 +332,17 @@ struct factorvm { inline void vmprim_wrapper(); //math + cell bignum_zero; + cell bignum_pos_one; + cell bignum_neg_one; inline void vmprim_bignum_to_fixnum(); inline void vmprim_float_to_fixnum(); inline void vmprim_fixnum_divint(); inline void vmprim_fixnum_divmod(); + bignum *fixnum_to_bignum(fixnum); + bignum *cell_to_bignum(cell); + bignum *long_long_to_bignum(s64 n); + bignum *ulong_long_to_bignum(u64 n); inline fixnum sign_mask(fixnum x); inline fixnum branchless_max(fixnum x, fixnum y); inline fixnum branchless_abs(fixnum x); From 8fa607e9a96d5f348ffdeecb57d499f129b3eb79 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:13 +0100 Subject: [PATCH 139/345] Dev checkpoint --- vm/code_block.cpp | 12 ++++++------ vm/code_block.hpp | 8 +++++--- vm/code_heap.cpp | 2 +- vm/code_heap.hpp | 3 ++- vm/contexts.hpp | 2 -- vm/image.cpp | 4 ++-- vm/profiler.cpp | 1 - vm/profiler.hpp | 1 - vm/vm.hpp | 12 +----------- 9 files changed, 17 insertions(+), 28 deletions(-) mode change 100644 => 100755 vm/profiler.hpp diff --git a/vm/code_block.cpp b/vm/code_block.cpp index d5aa7581c2..0c95fcc424 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -397,9 +397,9 @@ void factorvm::copy_literal_references(code_block *compiled) } } -void copy_literal_references(code_block *compiled) +void copy_literal_references(code_block *compiled, factorvm *myvm) { - return vm->copy_literal_references(compiled); + return myvm->copy_literal_references(compiled); } /* Compute an address to store at a relocation */ @@ -456,9 +456,9 @@ void factorvm::update_word_references(code_block *compiled) } } -void update_word_references(code_block *compiled) +void update_word_references(code_block *compiled, factorvm *myvm) { - return vm->update_word_references(compiled); + return myvm->update_word_references(compiled); } void factorvm::update_literal_and_word_references(code_block *compiled) @@ -574,9 +574,9 @@ void factorvm::relocate_code_block(code_block *compiled) flush_icache_for(compiled); } -void relocate_code_block(code_block *compiled) +void relocate_code_block(code_block *compiled, factorvm *myvm) { - return vm->relocate_code_block(compiled); + return myvm->relocate_code_block(compiled); } /* Fixup labels. This is done at compile time, not image load time */ diff --git a/vm/code_block.hpp b/vm/code_block.hpp index 3497ff33ba..67c1e837f4 100644 --- a/vm/code_block.hpp +++ b/vm/code_block.hpp @@ -62,19 +62,21 @@ typedef u32 relocation_entry; void flush_icache_for(code_block *compiled); +struct factorvm; + typedef void (*relocation_iterator)(relocation_entry rel, cell index, code_block *compiled); void iterate_relocations(code_block *compiled, relocation_iterator iter); void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value); -void relocate_code_block(code_block *compiled); +void relocate_code_block(code_block *compiled, factorvm *myvm); void update_literal_references(code_block *compiled); -void copy_literal_references(code_block *compiled); +void copy_literal_references(code_block *compiled, factorvm *myvm); -void update_word_references(code_block *compiled); +void update_word_references(code_block *compiled, factorvm *myvm); void update_literal_and_word_references(code_block *compiled); diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index 3d5d2c955a..9399c9ae70 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -51,7 +51,7 @@ void factorvm::iterate_code_heap(code_heap_iterator iter) while(scan) { if(scan->status != B_FREE) - iter((code_block *)scan); + iter((code_block *)scan,this); scan = next_block(&code,scan); } } diff --git a/vm/code_heap.hpp b/vm/code_heap.hpp index 31116590ad..26e1faeb19 100755 --- a/vm/code_heap.hpp +++ b/vm/code_heap.hpp @@ -8,7 +8,8 @@ bool in_code_heap_p(cell ptr); void jit_compile_word(cell word, cell def, bool relocate); -typedef void (*code_heap_iterator)(code_block *compiled); +struct factorvm; +typedef void (*code_heap_iterator)(code_block *compiled,factorvm *myvm); void iterate_code_heap(code_heap_iterator iter); diff --git a/vm/contexts.hpp b/vm/contexts.hpp index 56642bcd1a..9828210111 100644 --- a/vm/contexts.hpp +++ b/vm/contexts.hpp @@ -36,8 +36,6 @@ struct context { context *next; }; -//extern cell ds_size, rs_size; - #define ds_bot (stack_chain->datastack_region->start) #define ds_top (stack_chain->datastack_region->end) #define rs_bot (stack_chain->retainstack_region->start) diff --git a/vm/image.cpp b/vm/image.cpp index 91fa1b801b..ee86dd5a3f 100755 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -363,9 +363,9 @@ void factorvm::fixup_code_block(code_block *compiled) relocate_code_block(compiled); } -void fixup_code_block(code_block *compiled) +void fixup_code_block(code_block *compiled,factorvm *myvm) { - return vm->fixup_code_block(compiled); + return myvm->fixup_code_block(compiled); } void factorvm::relocate_code() diff --git a/vm/profiler.cpp b/vm/profiler.cpp index e87dd947fd..8f714a992c 100755 --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -3,7 +3,6 @@ namespace factor { -bool profiling_p; void factorvm::init_profiler() { diff --git a/vm/profiler.hpp b/vm/profiler.hpp old mode 100644 new mode 100755 index b83ef3d354..ab1d27b9d8 --- a/vm/profiler.hpp +++ b/vm/profiler.hpp @@ -1,7 +1,6 @@ namespace factor { -extern bool profiling_p; void init_profiler(); code_block *compile_profiling_stub(cell word); PRIMITIVE(profiling); diff --git a/vm/vm.hpp b/vm/vm.hpp index fab910be48..d0f31e4d3f 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -1,17 +1,6 @@ namespace factor { -struct heap; -struct data_heap; -struct data; -struct zone; -struct vm_parameters; -struct image_header; - -typedef u8 card; -typedef u8 card_deck; - - struct factorvm { // contexts @@ -47,6 +36,7 @@ struct factorvm { inline void vmprim_clone(); // profiler + bool profiling_p; void init_profiler(); code_block *compile_profiling_stub(cell word_); void set_profiling(bool profiling); From 93c665c6535113054a82c130d617f37e1d3ca13d Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:14 +0100 Subject: [PATCH 140/345] Dev checkpoint --- vm/code_block.cpp | 14 +++++++------- vm/code_block.hpp | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/vm/code_block.cpp b/vm/code_block.cpp index 0c95fcc424..1beeddd69e 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -251,7 +251,7 @@ void factorvm::iterate_relocations(code_block *compiled, relocation_iterator ite for(cell i = 0; i < length; i++) { relocation_entry rel = relocation->data()[i]; - iter(rel,index,compiled); + iter(rel,index,compiled,this); index += number_of_parameters(relocation_type_of(rel)); } } @@ -352,9 +352,9 @@ void factorvm::update_literal_references_step(relocation_entry rel, cell index, } } -void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled) +void update_literal_references_step(relocation_entry rel, cell index, code_block *compiled, factorvm *myvm) { - return vm->update_literal_references_step(rel,index,compiled); + return myvm->update_literal_references_step(rel,index,compiled); } /* Update pointers to literals from compiled code. */ @@ -415,9 +415,9 @@ void factorvm::relocate_code_block_step(relocation_entry rel, cell index, code_b compute_relocation(rel,index,compiled)); } -void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled) +void relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled, factorvm *myvm) { - return vm->relocate_code_block_step(rel,index,compiled); + return myvm->relocate_code_block_step(rel,index,compiled); } void factorvm::update_word_references_step(relocation_entry rel, cell index, code_block *compiled) @@ -427,9 +427,9 @@ void factorvm::update_word_references_step(relocation_entry rel, cell index, cod relocate_code_block_step(rel,index,compiled); } -void update_word_references_step(relocation_entry rel, cell index, code_block *compiled) +void update_word_references_step(relocation_entry rel, cell index, code_block *compiled, factorvm *myvm) { - return vm->update_word_references_step(rel,index,compiled); + return myvm->update_word_references_step(rel,index,compiled); } /* Relocate new code blocks completely; updating references to literals, diff --git a/vm/code_block.hpp b/vm/code_block.hpp index 67c1e837f4..e29d161339 100644 --- a/vm/code_block.hpp +++ b/vm/code_block.hpp @@ -64,7 +64,7 @@ void flush_icache_for(code_block *compiled); struct factorvm; -typedef void (*relocation_iterator)(relocation_entry rel, cell index, code_block *compiled); +typedef void (*relocation_iterator)(relocation_entry rel, cell index, code_block *compiled, factorvm *vm); void iterate_relocations(code_block *compiled, relocation_iterator iter); From baaf71eddc9fec772a0b45751140a9f3a5d7aaec Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:14 +0100 Subject: [PATCH 141/345] Dev checkpoint --- vm/code_block.cpp | 4 ++-- vm/code_block.hpp | 2 +- vm/code_gc.cpp | 2 +- vm/code_gc.hpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/vm/code_block.cpp b/vm/code_block.cpp index 1beeddd69e..9486d7f3b3 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -467,9 +467,9 @@ void factorvm::update_literal_and_word_references(code_block *compiled) update_word_references(compiled); } -void update_literal_and_word_references(code_block *compiled) +void update_literal_and_word_references(code_block *compiled, factorvm *myvm) { - return vm->update_literal_and_word_references(compiled); + return myvm->update_literal_and_word_references(compiled); } void factorvm::check_code_address(cell address) diff --git a/vm/code_block.hpp b/vm/code_block.hpp index e29d161339..0addaeb854 100644 --- a/vm/code_block.hpp +++ b/vm/code_block.hpp @@ -78,7 +78,7 @@ void copy_literal_references(code_block *compiled, factorvm *myvm); void update_word_references(code_block *compiled, factorvm *myvm); -void update_literal_and_word_references(code_block *compiled); +void update_literal_and_word_references(code_block *compiled, factorvm *myvm); void mark_code_block(code_block *compiled); diff --git a/vm/code_gc.cpp b/vm/code_gc.cpp index d229fcd3bf..9ccc83aa0f 100755 --- a/vm/code_gc.cpp +++ b/vm/code_gc.cpp @@ -299,7 +299,7 @@ void factorvm::free_unmarked(heap *heap, heap_iterator iter) add_to_free_list(heap,(free_heap_block *)prev); scan->status = B_ALLOCATED; prev = scan; - iter(scan); + iter(scan,this); break; default: critical_error("Invalid scan->status",(cell)scan); diff --git a/vm/code_gc.hpp b/vm/code_gc.hpp index 1cfafb69c2..08bdd327c7 100755 --- a/vm/code_gc.hpp +++ b/vm/code_gc.hpp @@ -14,7 +14,7 @@ struct heap { heap_free_list free; }; -typedef void (*heap_iterator)(heap_block *compiled); +typedef void (*heap_iterator)(heap_block *compiled,factorvm *vm); void new_heap(heap *h, cell size); void build_free_list(heap *h, cell size); From d5da6a3d581f3216147d340efb06349ea19e0e64 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:14 +0100 Subject: [PATCH 142/345] Dev checkpoint --- vm/callstack.cpp | 6 +++--- vm/callstack.hpp | 10 ---------- vm/code_block.cpp | 8 ++++---- vm/code_heap.cpp | 4 ++-- vm/data_heap.hpp | 17 ---------------- vm/debug.cpp | 8 ++++---- vm/image.cpp | 8 ++++---- vm/math.cpp | 3 ++- vm/vm.hpp | 51 +++++++++++++++++++++++++++++++++++++++++++++-- 9 files changed, 68 insertions(+), 47 deletions(-) diff --git a/vm/callstack.cpp b/vm/callstack.cpp index e4f03abca9..8df438206c 100755 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -194,10 +194,10 @@ struct stack_frame_accumulator { stack_frame_accumulator(factorvm *vm) : frames(vm) {} - void operator()(stack_frame *frame) + void operator()(stack_frame *frame, factorvm *myvm) { - gc_root executing(frame_executing(frame),frames.elements.myvm); - gc_root scan(frame_scan(frame),frames.elements.myvm); + gc_root executing(frame_executing(frame),myvm); + gc_root scan(frame_scan(frame),myvm); frames.add(executing.value()); frames.add(scan.value()); diff --git a/vm/callstack.hpp b/vm/callstack.hpp index ee097b528c..82fb93a1bc 100755 --- a/vm/callstack.hpp +++ b/vm/callstack.hpp @@ -22,16 +22,6 @@ PRIMITIVE(set_innermost_stack_frame_quot); VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom); -template void iterate_callstack(cell top, cell bottom, T &iterator) -{ - stack_frame *frame = (stack_frame *)bottom - 1; - - while((cell)frame >= top) - { - iterator(frame); - frame = frame_successor(frame); - } -} } diff --git a/vm/code_block.cpp b/vm/code_block.cpp index 9486d7f3b3..b8d4621d38 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -137,9 +137,9 @@ void factorvm::undefined_symbol() general_error(ERROR_UNDEFINED_SYMBOL,F,F,NULL); } -void undefined_symbol() +void undefined_symbol(factorvm *myvm) { - return vm->undefined_symbol(); + return myvm->undefined_symbol(); } /* Look up an external library symbol referenced by a compiled code block */ @@ -509,9 +509,9 @@ void factorvm::mark_stack_frame_step(stack_frame *frame) mark_code_block(frame_code(frame)); } -void mark_stack_frame_step(stack_frame *frame) +void mark_stack_frame_step(stack_frame *frame, factorvm *myvm) { - return vm->mark_stack_frame_step(frame); + return myvm->mark_stack_frame_step(frame); } /* Mark code blocks executing in currently active stack frames. */ diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index 9399c9ae70..fb24af4044 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -176,9 +176,9 @@ void factorvm::forward_frame_xt(stack_frame *frame) FRAME_RETURN_ADDRESS(frame) = (void *)((cell)forwarded + offset); } -void forward_frame_xt(stack_frame *frame) +void forward_frame_xt(stack_frame *frame,factorvm *myvm) { - return vm->forward_frame_xt(frame); + return myvm->forward_frame_xt(frame); } void factorvm::forward_object_xts() diff --git a/vm/data_heap.hpp b/vm/data_heap.hpp index 2bec35b8c1..67e3109924 100755 --- a/vm/data_heap.hpp +++ b/vm/data_heap.hpp @@ -98,23 +98,6 @@ PRIMITIVE(end_scan); cell find_all_words(); -/* Every object has a regular representation in the runtime, which makes GC -much simpler. Every slot of the object until binary_payload_start is a pointer -to some other object. */ -inline static void do_slots(cell obj, void (* iter)(cell *)) -{ - cell scan = obj; - cell payload_start = binary_payload_start((object *)obj); - cell end = obj + payload_start; - - scan += sizeof(cell); - - while(scan < end) - { - iter((cell *)scan); - scan += sizeof(cell); - } -} } diff --git a/vm/debug.cpp b/vm/debug.cpp index 5d033fd90e..eb64ad22d4 100755 --- a/vm/debug.cpp +++ b/vm/debug.cpp @@ -232,9 +232,9 @@ void factorvm::print_stack_frame(stack_frame *frame) print_string("\n"); } -void print_stack_frame(stack_frame *frame) +void print_stack_frame(stack_frame *frame, factorvm *myvm) { - return vm->print_stack_frame(frame); + return myvm->print_stack_frame(frame); } void factorvm::print_callstack() @@ -356,9 +356,9 @@ void factorvm::find_data_references_step(cell *scan) } } -void find_data_references_step(cell *scan) +void find_data_references_step(cell *scan,factorvm *myvm) { - return vm->find_data_references_step(scan); + return myvm->find_data_references_step(scan); } void factorvm::find_data_references(cell look_for_) diff --git a/vm/image.cpp b/vm/image.cpp index ee86dd5a3f..b8e89d5fcb 100755 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -195,9 +195,9 @@ void factorvm::data_fixup(cell *cell) *cell += (tenured->start - data_relocation_base); } -void data_fixup(cell *cell) +void data_fixup(cell *cell, factorvm *myvm) { - return vm->data_fixup(cell); + return myvm->data_fixup(cell); } template void factorvm::code_fixup(TYPE **handle) @@ -258,9 +258,9 @@ void factorvm::fixup_stack_frame(stack_frame *frame) code_fixup(&FRAME_RETURN_ADDRESS(frame)); } -void fixup_stack_frame(stack_frame *frame) +void fixup_stack_frame(stack_frame *frame, factorvm *myvm) { - return vm->fixup_stack_frame(frame); + return myvm->fixup_stack_frame(frame); } void factorvm::fixup_callstack_object(callstack *stack) diff --git a/vm/math.cpp b/vm/math.cpp index 74caec3074..ed556f268a 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -367,7 +367,8 @@ unsigned int bignum_producer(unsigned int digit) inline void factorvm::vmprim_byte_array_to_bignum() { cell n_digits = array_capacity(untag_check(dpeek())); - bignum * result = factor::digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0); + // bignum * result = factor::digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0); + bignum * result = digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0); drepl(tag(result)); } diff --git a/vm/vm.hpp b/vm/vm.hpp index d0f31e4d3f..42a250832c 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -61,6 +61,8 @@ struct factorvm { void type_error(cell type, cell tagged); void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top); + //callstack + // bignum int bignum_equal_p(bignum * x, bignum * y); enum bignum_comparison bignum_compare(bignum * x, bignum * y); @@ -530,6 +532,10 @@ struct factorvm { inline void vmprim_innermost_stack_frame_scan(); inline void vmprim_set_innermost_stack_frame_quot(); void save_callstack_bottom(stack_frame *callstack_bottom); + template void iterate_callstack(cell top, cell bottom, T &iterator); + inline void do_slots(cell obj, void (* iter)(cell *,factorvm*)); + // next method here: + //alien char *pinned_alien_offset(cell obj); @@ -1066,7 +1072,7 @@ template void factorvm::iterate_callstack_object(callstack *stack { stack_frame *frame = stack->frame_at(frame_offset); frame_offset -= frame->size; - iterator(frame); + iterator(frame,this); } } @@ -1086,7 +1092,48 @@ inline cell tag_boolean(cell untagged) return vm->tag_boolean(untagged); } -// next method here: +// callstack.hpp +template void factorvm::iterate_callstack(cell top, cell bottom, TYPE &iterator) +{ + stack_frame *frame = (stack_frame *)bottom - 1; + + while((cell)frame >= top) + { + iterator(frame,this); + frame = frame_successor(frame); + } +} + +template void iterate_callstack(cell top, cell bottom, TYPE &iterator) +{ + return vm->iterate_callstack(top,bottom,iterator); +} + + +// data_heap.hpp +/* Every object has a regular representation in the runtime, which makes GC +much simpler. Every slot of the object until binary_payload_start is a pointer +to some other object. */ +struct factorvm; +inline void factorvm::do_slots(cell obj, void (* iter)(cell *,factorvm*)) +{ + cell scan = obj; + cell payload_start = binary_payload_start((object *)obj); + cell end = obj + payload_start; + + scan += sizeof(cell); + + while(scan < end) + { + iter((cell *)scan,this); + scan += sizeof(cell); + } +} + +inline void do_slots(cell obj, void (* iter)(cell *,factorvm*)) +{ + return vm->do_slots(obj,iter); +} } From d093ff766f9e45cdf4c564383a0ddcea6991f7e9 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:14 +0100 Subject: [PATCH 143/345] updated function ptr calls (iterators etc..) to take a vm parameter --- vm/bignum.cpp | 8 ++++---- vm/bignum.hpp | 3 ++- vm/math.cpp | 4 ++-- vm/vm.hpp | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index 03b34edd97..f61b44340e 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -1980,14 +1980,14 @@ int bignum_unsigned_logbitp(int shift, bignum * bignum) } /* Allocates memory */ -bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p) +bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm*), unsigned int radix, int negative_p) { BIGNUM_ASSERT ((radix > 1) && (radix <= BIGNUM_RADIX_ROOT)); if (n_digits == 0) return (BIGNUM_ZERO ()); if (n_digits == 1) { - fixnum digit = ((fixnum) ((*producer) (0))); + fixnum digit = ((fixnum) ((*producer) (0,this))); return (fixnum_to_bignum (negative_p ? (- digit) : digit)); } { @@ -2009,14 +2009,14 @@ bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*p { bignum_destructive_scale_up (result, ((bignum_digit_type) radix)); bignum_destructive_add - (result, ((bignum_digit_type) ((*producer) (n_digits)))); + (result, ((bignum_digit_type) ((*producer) (n_digits,this)))); } return (bignum_trim (result)); } } } -bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p) +bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm*), unsigned int radix, int negative_p) { return vm->digit_stream_to_bignum(n_digits,producer,radix,negative_p); } diff --git a/vm/bignum.hpp b/vm/bignum.hpp index 5f502dcc22..8f21702f1a 100644 --- a/vm/bignum.hpp +++ b/vm/bignum.hpp @@ -119,8 +119,9 @@ void bignum_negate_magnitude(bignum *); bignum * bignum_integer_length(bignum * arg1); int bignum_unsigned_logbitp(int shift, bignum * bignum); int bignum_logbitp(int shift, bignum * arg); +struct factorvm; bignum * digit_stream_to_bignum(unsigned int n_digits, - unsigned int (*producer)(unsigned int), + unsigned int (*producer)(unsigned int,factorvm*), unsigned int radix, int negative_p); diff --git a/vm/math.cpp b/vm/math.cpp index ed556f268a..98188059f6 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -359,9 +359,9 @@ unsigned int factorvm::bignum_producer(unsigned int digit) return *(ptr + digit); } -unsigned int bignum_producer(unsigned int digit) +unsigned int bignum_producer(unsigned int digit, factorvm *myvm) { - return vm->bignum_producer(digit); + return myvm->bignum_producer(digit); } inline void factorvm::vmprim_byte_array_to_bignum() diff --git a/vm/vm.hpp b/vm/vm.hpp index 42a250832c..0f7a26c020 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -120,7 +120,7 @@ struct factorvm { bignum *bignum_integer_length(bignum * x); int bignum_logbitp(int shift, bignum * arg); int bignum_unsigned_logbitp(int shift, bignum * bignum); - bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int), unsigned int radix, int negative_p); + bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm *), unsigned int radix, int negative_p); //data_heap bool secure_gc; /* Set by the -securegc command line argument */ From 2e81b174a712e445ba333c1f8f7760aebd8911b1 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:14 +0100 Subject: [PATCH 144/345] removed some stub functions from contexts --- vm/contexts.cpp | 40 ---------------------------------------- vm/contexts.hpp | 5 ----- 2 files changed, 45 deletions(-) diff --git a/vm/contexts.cpp b/vm/contexts.cpp index 03768ec0db..448351baf7 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -11,21 +11,11 @@ void factorvm::reset_datastack() ds = ds_bot - sizeof(cell); } -void reset_datastack() -{ - return vm->reset_datastack(); -} - void factorvm::reset_retainstack() { rs = rs_bot - sizeof(cell); } -void reset_retainstack() -{ - return vm->reset_retainstack(); -} - static const cell stack_reserved = (64 * sizeof(cell)); void factorvm::fix_stacks() @@ -34,11 +24,6 @@ void factorvm::fix_stacks() if(rs + sizeof(cell) < rs_bot || rs + stack_reserved >= rs_top) reset_retainstack(); } -void fix_stacks() -{ - return vm->fix_stacks(); -} - /* called before entry into foreign C code. Note that ds and rs might be stored in registers, so callbacks must save and restore the correct values */ void factorvm::save_stacks() @@ -74,22 +59,12 @@ context *factorvm::alloc_context() return new_context; } -context *alloc_context() -{ - return vm->alloc_context(); -} - void factorvm::dealloc_context(context *old_context) { old_context->next = unused_contexts; unused_contexts = old_context; } -void dealloc_context(context *old_context) -{ - return vm->dealloc_context(old_context); -} - /* called on entry into a compiled callback */ void factorvm::nest_stacks() { @@ -156,11 +131,6 @@ void factorvm::init_stacks(cell ds_size_, cell rs_size_) unused_contexts = NULL; } -void init_stacks(cell ds_size_, cell rs_size_) -{ - return vm->init_stacks(ds_size_,rs_size_); -} - bool factorvm::stack_to_array(cell bottom, cell top) { fixnum depth = (fixnum)(top - bottom + sizeof(cell)); @@ -176,11 +146,6 @@ bool factorvm::stack_to_array(cell bottom, cell top) } } -bool stack_to_array(cell bottom, cell top) -{ - return vm->stack_to_array(bottom,top); -} - inline void factorvm::vmprim_datastack() { if(!stack_to_array(ds_bot,ds)) @@ -211,11 +176,6 @@ cell factorvm::array_to_stack(array *array, cell bottom) return bottom + depth - sizeof(cell); } -cell array_to_stack(array *array, cell bottom) -{ - return vm->array_to_stack(array,bottom); -} - inline void factorvm::vmprim_set_datastack() { ds = array_to_stack(untag_check(dpop()),ds_bot); diff --git a/vm/contexts.hpp b/vm/contexts.hpp index 9828210111..00d9646424 100644 --- a/vm/contexts.hpp +++ b/vm/contexts.hpp @@ -44,11 +44,6 @@ struct context { DEFPUSHPOP(d,ds) DEFPUSHPOP(r,rs) -void reset_datastack(); -void reset_retainstack(); -void fix_stacks(); -void init_stacks(cell ds_size, cell rs_size); - PRIMITIVE(datastack); PRIMITIVE(retainstack); PRIMITIVE(set_datastack); From a66cf7e609f4de7883e07a41787288ef2f67f5e8 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:14 +0100 Subject: [PATCH 145/345] removed stub function from run --- vm/run.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/vm/run.cpp b/vm/run.cpp index 7f4894f1ef..f8c099bbfd 100755 --- a/vm/run.cpp +++ b/vm/run.cpp @@ -102,11 +102,6 @@ cell factorvm::clone_object(cell obj_) } } -cell clone_object(cell obj_) -{ - return vm->clone_object(obj_); -} - inline void factorvm::vmprim_clone() { drepl(clone_object(dpeek())); From 75c81af691f12fb850f8ec8d80917ef13ba4ec34 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:15 +0100 Subject: [PATCH 146/345] moved more math.hpp inline functions to vm --- vm/math.hpp | 40 ---------------------------------------- vm/vm.hpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/vm/math.hpp b/vm/math.hpp index 863fa1b4af..4633721194 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -38,48 +38,8 @@ PRIMITIVE(bignum_bitp); PRIMITIVE(bignum_log2); PRIMITIVE(byte_array_to_bignum); - - - - - - - - cell unbox_array_size(); -inline static double untag_float(cell tagged) -{ - return untag(tagged)->n; -} - -inline static double untag_float_check(cell tagged) -{ - return untag_check(tagged)->n; -} - - - - - -inline static fixnum float_to_fixnum(cell tagged) -{ - return (fixnum)untag_float(tagged); -} - - - - - -inline double fixnum_to_float(cell tagged) -{ - return (double)untag_fixnum(tagged); -} - - - - - PRIMITIVE(fixnum_to_float); PRIMITIVE(bignum_to_float); PRIMITIVE(str_to_float); diff --git a/vm/vm.hpp b/vm/vm.hpp index 0f7a26c020..30b7395a70 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -406,6 +406,11 @@ struct factorvm { inline cell allot_float(double n); inline bignum *float_to_bignum(cell tagged); inline double bignum_to_float(cell tagged); + inline double untag_float(cell tagged); + inline double untag_float_check(cell tagged); + inline fixnum float_to_fixnum(cell tagged); + inline double fixnum_to_float(cell tagged); + // next method here: //io void init_c_io(); @@ -534,7 +539,6 @@ struct factorvm { void save_callstack_bottom(stack_frame *callstack_bottom); template void iterate_callstack(cell top, cell bottom, T &iterator); inline void do_slots(cell obj, void (* iter)(cell *,factorvm*)); - // next method here: //alien @@ -1060,6 +1064,47 @@ inline double bignum_to_float(cell tagged) return vm->bignum_to_float(tagged); } +inline double factorvm::untag_float(cell tagged) +{ + return untag(tagged)->n; +} + +inline double untag_float(cell tagged) +{ + return vm->untag_float(tagged); +} + +inline double factorvm::untag_float_check(cell tagged) +{ + return untag_check(tagged)->n; +} + +inline double untag_float_check(cell tagged) +{ + return vm->untag_float_check(tagged); +} + +inline fixnum factorvm::float_to_fixnum(cell tagged) +{ + return (fixnum)untag_float(tagged); +} + +inline static fixnum float_to_fixnum(cell tagged) +{ + return vm->float_to_fixnum(tagged); +} + +inline double factorvm::fixnum_to_float(cell tagged) +{ + return (double)untag_fixnum(tagged); +} + +inline double fixnum_to_float(cell tagged) +{ + return vm->fixnum_to_float(tagged); +} + + //callstack.hpp /* This is a little tricky. The iterator may allocate memory, so we keep the callstack in a GC root and use relative offsets */ From e4f92cdbf25253b2bcc545c2f490b6d2050394bb Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:15 +0100 Subject: [PATCH 147/345] moved tagged.hpp templates to vm.hpp --- vm/tagged.hpp | 63 ++------------------------------------------ vm/vm.hpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 61 deletions(-) mode change 100644 => 100755 vm/tagged.hpp diff --git a/vm/tagged.hpp b/vm/tagged.hpp old mode 100644 new mode 100755 index ea1942e10c..ca208555da --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -1,72 +1,13 @@ namespace factor { -template cell tag(T *value) +template cell tag(TYPE *value) { - return RETAG(value,tag_for(T::type_number)); + return RETAG(value,tag_for(TYPE::type_number)); } inline static cell tag_dynamic(object *value) { return RETAG(value,tag_for(value->h.hi_tag())); } - -template -struct tagged -{ - cell value_; - - cell value() const { return value_; } - T *untagged() const { return (T *)(UNTAG(value_)); } - - cell type() const { - cell tag = TAG(value_); - if(tag == OBJECT_TYPE) - return untagged()->h.hi_tag(); - else - return tag; - } - - bool type_p(cell type_) const { return type() == type_; } - - T *untag_check() const { - if(T::type_number != TYPE_COUNT && !type_p(T::type_number)) - type_error(T::type_number,value_); - return untagged(); - } - - explicit tagged(cell tagged) : value_(tagged) { -#ifdef FACTOR_DEBUG - untag_check(); -#endif - } - - explicit tagged(T *untagged) : value_(factor::tag(untagged)) { -#ifdef FACTOR_DEBUG - untag_check(); -#endif - } - - T *operator->() const { return untagged(); } - cell *operator&() const { return &value_; } - - const tagged& operator=(const T *x) { value_ = tag(x); return *this; } - const tagged& operator=(const cell &x) { value_ = x; return *this; } - - bool operator==(const tagged &x) { return value_ == x.value_; } - bool operator!=(const tagged &x) { return value_ != x.value_; } - - template tagged as() { return tagged(value_); } -}; - -template T *untag_check(cell value) -{ - return tagged(value).untag_check(); -} - -template T *untag(cell value) -{ - return tagged(value).untagged(); -} - } diff --git a/vm/vm.hpp b/vm/vm.hpp index 30b7395a70..24cb7a98f1 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -410,6 +410,8 @@ struct factorvm { inline double untag_float_check(cell tagged); inline fixnum float_to_fixnum(cell tagged); inline double fixnum_to_float(cell tagged); + template T *untag_check(cell value); + template T *untag(cell value); // next method here: //io @@ -642,6 +644,77 @@ struct factorvm { extern factorvm *vm; +//tagged.hpp + +template +struct tagged +{ + cell value_; + + cell value() const { return value_; } + TYPE *untagged() const { return (TYPE *)(UNTAG(value_)); } + + cell type() const { + cell tag = TAG(value_); + if(tag == OBJECT_TYPE) + return untagged()->h.hi_tag(); + else + return tag; + } + + bool type_p(cell type_) const { return type() == type_; } + + TYPE *untag_check() const { + if(TYPE::type_number != TYPE_COUNT && !type_p(TYPE::type_number)) + type_error(TYPE::type_number,value_); + return untagged(); + } + + explicit tagged(cell tagged) : value_(tagged) { +#ifdef FACTOR_DEBUG + untag_check(); +#endif + } + + explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { +#ifdef FACTOR_DEBUG + untag_check(); +#endif + } + + TYPE *operator->() const { return untagged(); } + cell *operator&() const { return &value_; } + + const tagged& operator=(const TYPE *x) { value_ = tag(x); return *this; } + const tagged& operator=(const cell &x) { value_ = x; return *this; } + + bool operator==(const tagged &x) { return value_ == x.value_; } + bool operator!=(const tagged &x) { return value_ != x.value_; } + + template tagged as() { return tagged(value_); } +}; + +template TYPE *factorvm::untag_check(cell value) +{ + return tagged(value).untag_check(); +} + +template TYPE *untag_check(cell value) +{ + return vm->untag_check(value); +} + +template TYPE *factorvm::untag(cell value) +{ + return tagged(value).untagged(); +} + +template TYPE *untag(cell value) +{ + return vm->untag(value); +} + + // write_barrier.hpp From e08a6e21cbe15319b94558e489201e057f898419 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:15 +0100 Subject: [PATCH 148/345] split the moved inline stuff into separate header file --- vm/inlineimpls.hpp | 616 +++++++++++++++++++++++++++++++++++++++++++++ vm/master.hpp | 1 + vm/vm.hpp | 610 -------------------------------------------- 3 files changed, 617 insertions(+), 610 deletions(-) create mode 100644 vm/inlineimpls.hpp diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp new file mode 100644 index 0000000000..b88b3254df --- /dev/null +++ b/vm/inlineimpls.hpp @@ -0,0 +1,616 @@ +namespace factor +{ + +// I've had to copy inline implementations here to make dependencies work. Hopefully this can be better factored +// once the rest of the reentrant changes are done. -PD + +//tagged.hpp + +template +struct tagged +{ + cell value_; + + cell value() const { return value_; } + TYPE *untagged() const { return (TYPE *)(UNTAG(value_)); } + + cell type() const { + cell tag = TAG(value_); + if(tag == OBJECT_TYPE) + return untagged()->h.hi_tag(); + else + return tag; + } + + bool type_p(cell type_) const { return type() == type_; } + + TYPE *untag_check() const { + if(TYPE::type_number != TYPE_COUNT && !type_p(TYPE::type_number)) + type_error(TYPE::type_number,value_); + return untagged(); + } + + explicit tagged(cell tagged) : value_(tagged) { +#ifdef FACTOR_DEBUG + untag_check(); +#endif + } + + explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { +#ifdef FACTOR_DEBUG + untag_check(); +#endif + } + + TYPE *operator->() const { return untagged(); } + cell *operator&() const { return &value_; } + + const tagged& operator=(const TYPE *x) { value_ = tag(x); return *this; } + const tagged& operator=(const cell &x) { value_ = x; return *this; } + + bool operator==(const tagged &x) { return value_ == x.value_; } + bool operator!=(const tagged &x) { return value_ != x.value_; } + + template tagged as() { return tagged(value_); } +}; + +template TYPE *factorvm::untag_check(cell value) +{ + return tagged(value).untag_check(); +} + +template TYPE *untag_check(cell value) +{ + return vm->untag_check(value); +} + +template TYPE *factorvm::untag(cell value) +{ + return tagged(value).untagged(); +} + +template TYPE *untag(cell value) +{ + return vm->untag(value); +} + + + +// write_barrier.hpp + +inline card *factorvm::addr_to_card(cell a) +{ + return (card*)(((cell)(a) >> card_bits) + cards_offset); +} + +inline card *addr_to_card(cell a) +{ + return vm->addr_to_card(a); +} + +inline cell factorvm::card_to_addr(card *c) +{ + return ((cell)c - cards_offset) << card_bits; +} + +inline cell card_to_addr(card *c) +{ + return vm->card_to_addr(c); +} + +inline cell factorvm::card_offset(card *c) +{ + return *(c - (cell)data->cards + (cell)data->allot_markers); +} + +inline cell card_offset(card *c) +{ + return vm->card_offset(c); +} + +inline card_deck *factorvm::addr_to_deck(cell a) +{ + return (card_deck *)(((cell)a >> deck_bits) + decks_offset); +} + +inline card_deck *addr_to_deck(cell a) +{ + return vm->addr_to_deck(a); +} + +inline cell factorvm::deck_to_addr(card_deck *c) +{ + return ((cell)c - decks_offset) << deck_bits; +} + +inline cell deck_to_addr(card_deck *c) +{ + return vm->deck_to_addr(c); +} + +inline card *factorvm::deck_to_card(card_deck *d) +{ + return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset); +} + +inline card *deck_to_card(card_deck *d) +{ + return vm->deck_to_card(d); +} + +inline card *factorvm::addr_to_allot_marker(object *a) +{ + return (card *)(((cell)a >> card_bits) + allot_markers_offset); +} + +inline card *addr_to_allot_marker(object *a) +{ + return vm->addr_to_allot_marker(a); +} + +/* the write barrier must be called any time we are potentially storing a +pointer from an older generation to a younger one */ +inline void factorvm::write_barrier(object *obj) +{ + *addr_to_card((cell)obj) = card_mark_mask; + *addr_to_deck((cell)obj) = card_mark_mask; +} + +inline void write_barrier(object *obj) +{ + return vm->write_barrier(obj); +} + +/* we need to remember the first object allocated in the card */ +inline void factorvm::allot_barrier(object *address) +{ + card *ptr = addr_to_allot_marker(address); + if(*ptr == invalid_allot_marker) + *ptr = ((cell)address & addr_card_mask); +} + +inline void allot_barrier(object *address) +{ + return vm->allot_barrier(address); +} + + +//data_gc.hpp +inline bool factorvm::collecting_accumulation_gen_p() +{ + return ((data->have_aging_p() + && collecting_gen == data->aging() + && !collecting_aging_again) + || collecting_gen == data->tenured()); +} + +inline bool collecting_accumulation_gen_p() +{ + return vm->collecting_accumulation_gen_p(); +} + +inline object *factorvm::allot_zone(zone *z, cell a) +{ + cell h = z->here; + z->here = h + align8(a); + object *obj = (object *)h; + allot_barrier(obj); + return obj; +} + +inline object *allot_zone(zone *z, cell a) +{ + return vm->allot_zone(z,a); +} + +/* + * It is up to the caller to fill in the object's fields in a meaningful + * fashion! + */ +inline object *factorvm::allot_object(header header, cell size) +{ +#ifdef GC_DEBUG + if(!gc_off) + gc(); +#endif + + object *obj; + + if(nursery.size - allot_buffer_zone > size) + { + /* If there is insufficient room, collect the nursery */ + if(nursery.here + allot_buffer_zone + size > nursery.end) + garbage_collection(data->nursery(),false,0); + + cell h = nursery.here; + nursery.here = h + align8(size); + obj = (object *)h; + } + /* If the object is bigger than the nursery, allocate it in + tenured space */ + else + { + zone *tenured = &data->generations[data->tenured()]; + + /* If tenured space does not have enough room, collect */ + if(tenured->here + size > tenured->end) + { + gc(); + tenured = &data->generations[data->tenured()]; + } + + /* If it still won't fit, grow the heap */ + if(tenured->here + size > tenured->end) + { + garbage_collection(data->tenured(),true,size); + tenured = &data->generations[data->tenured()]; + } + + obj = allot_zone(tenured,size); + + /* Allows initialization code to store old->new pointers + without hitting the write barrier in the common case of + a nursery allocation */ + write_barrier(obj); + } + + obj->h = header; + return obj; +} + +inline object *allot_object(header header, cell size) +{ + return vm->allot_object(header,size); +} + +template TYPE *factorvm::allot(cell size) +{ + return (TYPE *)allot_object(header(TYPE::type_number),size); +} + +template TYPE *allot(cell size) +{ + return vm->allot(size); +} + +inline void factorvm::check_data_pointer(object *pointer) +{ +#ifdef FACTOR_DEBUG + if(!growing_data_heap) + { + assert((cell)pointer >= data->seg->start + && (cell)pointer < data->seg->end); + } +#endif +} + +inline void check_data_pointer(object *pointer) +{ + return vm->check_data_pointer(pointer); +} + +inline void factorvm::check_tagged_pointer(cell tagged) +{ +#ifdef FACTOR_DEBUG + if(!immediate_p(tagged)) + { + object *obj = untag(tagged); + check_data_pointer(obj); + obj->h.hi_tag(); + } +#endif +} + +inline void check_tagged_pointer(cell tagged) +{ + return vm->check_tagged_pointer(tagged); +} + +//local_roots.hpp +template +struct gc_root : public tagged +{ + factorvm *myvm; + + void push() { check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } + + //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } + explicit gc_root(cell value_,factorvm *vm) : tagged(value_),myvm(vm) { push(); } + explicit gc_root(TYPE *value_, factorvm *vm) : tagged(value_),myvm(vm) { push(); } + + const gc_root& operator=(const TYPE *x) { tagged::operator=(x); return *this; } + const gc_root& operator=(const cell &x) { tagged::operator=(x); return *this; } + + ~gc_root() { +#ifdef FACTOR_DEBUG + assert(myvm->gc_locals.back() == (cell)this); +#endif + myvm->gc_locals.pop_back(); + } +}; + +/* A similar hack for the bignum implementation */ +struct gc_bignum +{ + bignum **addr; + factorvm *myvm; + gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { + if(*addr_) + check_data_pointer(*addr_); + myvm->gc_bignums.push_back((cell)addr); + } + + ~gc_bignum() { +#ifdef FACTOR_DEBUG + assert(myvm->gc_bignums.back() == (cell)addr); +#endif + myvm->gc_bignums.pop_back(); + } +}; + +#define GC_BIGNUM(x,vm) gc_bignum x##__gc_root(&x,vm) + +//generic_arrays.hpp +template TYPE *factorvm::allot_array_internal(cell capacity) +{ + TYPE *array = allot(array_size(capacity)); + array->capacity = tag_fixnum(capacity); + return array; +} + +template TYPE *allot_array_internal(cell capacity) +{ + return vm->allot_array_internal(capacity); +} + +template bool factorvm::reallot_array_in_place_p(TYPE *array, cell capacity) +{ + return in_zone(&nursery,array) && capacity <= array_capacity(array); +} + +template bool reallot_array_in_place_p(TYPE *array, cell capacity) +{ + return vm->reallot_array_in_place_p(array,capacity); +} + +template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) +{ + gc_root array(array_,this); + + if(reallot_array_in_place_p(array.untagged(),capacity)) + { + array->capacity = tag_fixnum(capacity); + return array.untagged(); + } + else + { + cell to_copy = array_capacity(array.untagged()); + if(capacity < to_copy) + to_copy = capacity; + + TYPE *new_array = allot_array_internal(capacity); + + memcpy(new_array + 1,array.untagged() + 1,to_copy * TYPE::element_size); + memset((char *)(new_array + 1) + to_copy * TYPE::element_size, + 0,(capacity - to_copy) * TYPE::element_size); + + return new_array; + } +} + +//arrays.hpp +inline void factorvm::set_array_nth(array *array, cell slot, cell value) +{ +#ifdef FACTOR_DEBUG + assert(slot < array_capacity(array)); + assert(array->h.hi_tag() == ARRAY_TYPE); + check_tagged_pointer(value); +#endif + array->data()[slot] = value; + write_barrier(array); +} + +inline void set_array_nth(array *array, cell slot, cell value) +{ + return vm->set_array_nth(array,slot,value); +} + +struct growable_array { + cell count; + gc_root elements; + + growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(allot_array(capacity,F),myvm) {} + + void add(cell elt); + void trim(); +}; + +//byte_arrays.hpp +struct growable_byte_array { + cell count; + gc_root elements; + + growable_byte_array(factorvm *vm,cell capacity = 40) : count(0), elements(allot_byte_array(capacity),vm) { } + + void append_bytes(void *elts, cell len); + void append_byte_array(cell elts); + + void trim(); +}; + +//math.hpp +inline cell factorvm::allot_integer(fixnum x) +{ + if(x < fixnum_min || x > fixnum_max) + return tag(fixnum_to_bignum(x)); + else + return tag_fixnum(x); +} + +inline cell allot_integer(fixnum x) +{ + return vm->allot_integer(x); +} + +inline cell factorvm::allot_cell(cell x) +{ + if(x > (cell)fixnum_max) + return tag(cell_to_bignum(x)); + else + return tag_fixnum(x); +} + +inline cell allot_cell(cell x) +{ + return vm->allot_cell(x); +} + +inline cell factorvm::allot_float(double n) +{ + boxed_float *flo = allot(sizeof(boxed_float)); + flo->n = n; + return tag(flo); +} + +inline cell allot_float(double n) +{ + return vm->allot_float(n); +} + +inline bignum *factorvm::float_to_bignum(cell tagged) +{ + return double_to_bignum(untag_float(tagged)); +} + +inline bignum *float_to_bignum(cell tagged) +{ + return vm->float_to_bignum(tagged); +} + +inline double factorvm::bignum_to_float(cell tagged) +{ + return bignum_to_double(untag(tagged)); +} + +inline double bignum_to_float(cell tagged) +{ + return vm->bignum_to_float(tagged); +} + +inline double factorvm::untag_float(cell tagged) +{ + return untag(tagged)->n; +} + +inline double untag_float(cell tagged) +{ + return vm->untag_float(tagged); +} + +inline double factorvm::untag_float_check(cell tagged) +{ + return untag_check(tagged)->n; +} + +inline double untag_float_check(cell tagged) +{ + return vm->untag_float_check(tagged); +} + +inline fixnum factorvm::float_to_fixnum(cell tagged) +{ + return (fixnum)untag_float(tagged); +} + +inline static fixnum float_to_fixnum(cell tagged) +{ + return vm->float_to_fixnum(tagged); +} + +inline double factorvm::fixnum_to_float(cell tagged) +{ + return (double)untag_fixnum(tagged); +} + +inline double fixnum_to_float(cell tagged) +{ + return vm->fixnum_to_float(tagged); +} + + +//callstack.hpp +/* This is a little tricky. The iterator may allocate memory, so we +keep the callstack in a GC root and use relative offsets */ +template void factorvm::iterate_callstack_object(callstack *stack_, TYPE &iterator) +{ + gc_root stack(stack_,vm); + fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); + + while(frame_offset >= 0) + { + stack_frame *frame = stack->frame_at(frame_offset); + frame_offset -= frame->size; + iterator(frame,this); + } +} + +template void iterate_callstack_object(callstack *stack_, TYPE &iterator) +{ + return vm->iterate_callstack_object(stack_,iterator); +} + +//booleans.hpp +inline cell factorvm::tag_boolean(cell untagged) +{ + return (untagged ? T : F); +} + +inline cell tag_boolean(cell untagged) +{ + return vm->tag_boolean(untagged); +} + +// callstack.hpp +template void factorvm::iterate_callstack(cell top, cell bottom, TYPE &iterator) +{ + stack_frame *frame = (stack_frame *)bottom - 1; + + while((cell)frame >= top) + { + iterator(frame,this); + frame = frame_successor(frame); + } +} + +template void iterate_callstack(cell top, cell bottom, TYPE &iterator) +{ + return vm->iterate_callstack(top,bottom,iterator); +} + + +// data_heap.hpp +/* Every object has a regular representation in the runtime, which makes GC +much simpler. Every slot of the object until binary_payload_start is a pointer +to some other object. */ +struct factorvm; +inline void factorvm::do_slots(cell obj, void (* iter)(cell *,factorvm*)) +{ + cell scan = obj; + cell payload_start = binary_payload_start((object *)obj); + cell end = obj + payload_start; + + scan += sizeof(cell); + + while(scan < end) + { + iter((cell *)scan,this); + scan += sizeof(cell); + } +} + +inline void do_slots(cell obj, void (* iter)(cell *,factorvm*)) +{ + return vm->do_slots(obj,iter); +} + +} diff --git a/vm/master.hpp b/vm/master.hpp index e118be67c3..5d95fa440e 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -68,6 +68,7 @@ #include "callstack.hpp" #include "alien.hpp" #include "vm.hpp" +#include "inlineimpls.hpp" #include "jit.hpp" #include "quotations.hpp" #include "dispatch.hpp" diff --git a/vm/vm.hpp b/vm/vm.hpp index 24cb7a98f1..1ed6d965bc 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -644,614 +644,4 @@ struct factorvm { extern factorvm *vm; -//tagged.hpp - -template -struct tagged -{ - cell value_; - - cell value() const { return value_; } - TYPE *untagged() const { return (TYPE *)(UNTAG(value_)); } - - cell type() const { - cell tag = TAG(value_); - if(tag == OBJECT_TYPE) - return untagged()->h.hi_tag(); - else - return tag; - } - - bool type_p(cell type_) const { return type() == type_; } - - TYPE *untag_check() const { - if(TYPE::type_number != TYPE_COUNT && !type_p(TYPE::type_number)) - type_error(TYPE::type_number,value_); - return untagged(); - } - - explicit tagged(cell tagged) : value_(tagged) { -#ifdef FACTOR_DEBUG - untag_check(); -#endif - } - - explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { -#ifdef FACTOR_DEBUG - untag_check(); -#endif - } - - TYPE *operator->() const { return untagged(); } - cell *operator&() const { return &value_; } - - const tagged& operator=(const TYPE *x) { value_ = tag(x); return *this; } - const tagged& operator=(const cell &x) { value_ = x; return *this; } - - bool operator==(const tagged &x) { return value_ == x.value_; } - bool operator!=(const tagged &x) { return value_ != x.value_; } - - template tagged as() { return tagged(value_); } -}; - -template TYPE *factorvm::untag_check(cell value) -{ - return tagged(value).untag_check(); -} - -template TYPE *untag_check(cell value) -{ - return vm->untag_check(value); -} - -template TYPE *factorvm::untag(cell value) -{ - return tagged(value).untagged(); -} - -template TYPE *untag(cell value) -{ - return vm->untag(value); -} - - - -// write_barrier.hpp - -inline card *factorvm::addr_to_card(cell a) -{ - return (card*)(((cell)(a) >> card_bits) + cards_offset); -} - -inline card *addr_to_card(cell a) -{ - return vm->addr_to_card(a); -} - -inline cell factorvm::card_to_addr(card *c) -{ - return ((cell)c - cards_offset) << card_bits; -} - -inline cell card_to_addr(card *c) -{ - return vm->card_to_addr(c); -} - -inline cell factorvm::card_offset(card *c) -{ - return *(c - (cell)data->cards + (cell)data->allot_markers); -} - -inline cell card_offset(card *c) -{ - return vm->card_offset(c); -} - -inline card_deck *factorvm::addr_to_deck(cell a) -{ - return (card_deck *)(((cell)a >> deck_bits) + decks_offset); -} - -inline card_deck *addr_to_deck(cell a) -{ - return vm->addr_to_deck(a); -} - -inline cell factorvm::deck_to_addr(card_deck *c) -{ - return ((cell)c - decks_offset) << deck_bits; -} - -inline cell deck_to_addr(card_deck *c) -{ - return vm->deck_to_addr(c); -} - -inline card *factorvm::deck_to_card(card_deck *d) -{ - return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset); -} - -inline card *deck_to_card(card_deck *d) -{ - return vm->deck_to_card(d); -} - -inline card *factorvm::addr_to_allot_marker(object *a) -{ - return (card *)(((cell)a >> card_bits) + allot_markers_offset); -} - -inline card *addr_to_allot_marker(object *a) -{ - return vm->addr_to_allot_marker(a); -} - -/* the write barrier must be called any time we are potentially storing a -pointer from an older generation to a younger one */ -inline void factorvm::write_barrier(object *obj) -{ - *addr_to_card((cell)obj) = card_mark_mask; - *addr_to_deck((cell)obj) = card_mark_mask; -} - -inline void write_barrier(object *obj) -{ - return vm->write_barrier(obj); -} - -/* we need to remember the first object allocated in the card */ -inline void factorvm::allot_barrier(object *address) -{ - card *ptr = addr_to_allot_marker(address); - if(*ptr == invalid_allot_marker) - *ptr = ((cell)address & addr_card_mask); -} - -inline void allot_barrier(object *address) -{ - return vm->allot_barrier(address); -} - - -//data_gc.hpp -inline bool factorvm::collecting_accumulation_gen_p() -{ - return ((data->have_aging_p() - && collecting_gen == data->aging() - && !collecting_aging_again) - || collecting_gen == data->tenured()); -} - -inline bool collecting_accumulation_gen_p() -{ - return vm->collecting_accumulation_gen_p(); -} - -inline object *factorvm::allot_zone(zone *z, cell a) -{ - cell h = z->here; - z->here = h + align8(a); - object *obj = (object *)h; - allot_barrier(obj); - return obj; -} - -inline object *allot_zone(zone *z, cell a) -{ - return vm->allot_zone(z,a); -} - -/* - * It is up to the caller to fill in the object's fields in a meaningful - * fashion! - */ -inline object *factorvm::allot_object(header header, cell size) -{ -#ifdef GC_DEBUG - if(!gc_off) - gc(); -#endif - - object *obj; - - if(nursery.size - allot_buffer_zone > size) - { - /* If there is insufficient room, collect the nursery */ - if(nursery.here + allot_buffer_zone + size > nursery.end) - garbage_collection(data->nursery(),false,0); - - cell h = nursery.here; - nursery.here = h + align8(size); - obj = (object *)h; - } - /* If the object is bigger than the nursery, allocate it in - tenured space */ - else - { - zone *tenured = &data->generations[data->tenured()]; - - /* If tenured space does not have enough room, collect */ - if(tenured->here + size > tenured->end) - { - gc(); - tenured = &data->generations[data->tenured()]; - } - - /* If it still won't fit, grow the heap */ - if(tenured->here + size > tenured->end) - { - garbage_collection(data->tenured(),true,size); - tenured = &data->generations[data->tenured()]; - } - - obj = allot_zone(tenured,size); - - /* Allows initialization code to store old->new pointers - without hitting the write barrier in the common case of - a nursery allocation */ - write_barrier(obj); - } - - obj->h = header; - return obj; -} - -inline object *allot_object(header header, cell size) -{ - return vm->allot_object(header,size); -} - -template TYPE *factorvm::allot(cell size) -{ - return (TYPE *)allot_object(header(TYPE::type_number),size); -} - -template TYPE *allot(cell size) -{ - return vm->allot(size); -} - -inline void factorvm::check_data_pointer(object *pointer) -{ -#ifdef FACTOR_DEBUG - if(!growing_data_heap) - { - assert((cell)pointer >= data->seg->start - && (cell)pointer < data->seg->end); - } -#endif -} - -inline void check_data_pointer(object *pointer) -{ - return vm->check_data_pointer(pointer); -} - -inline void factorvm::check_tagged_pointer(cell tagged) -{ -#ifdef FACTOR_DEBUG - if(!immediate_p(tagged)) - { - object *obj = untag(tagged); - check_data_pointer(obj); - obj->h.hi_tag(); - } -#endif -} - -inline void check_tagged_pointer(cell tagged) -{ - return vm->check_tagged_pointer(tagged); -} - -//local_roots.hpp -template -struct gc_root : public tagged -{ - factorvm *myvm; - - void push() { check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } - - //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } - explicit gc_root(cell value_,factorvm *vm) : tagged(value_),myvm(vm) { push(); } - explicit gc_root(TYPE *value_, factorvm *vm) : tagged(value_),myvm(vm) { push(); } - - const gc_root& operator=(const TYPE *x) { tagged::operator=(x); return *this; } - const gc_root& operator=(const cell &x) { tagged::operator=(x); return *this; } - - ~gc_root() { -#ifdef FACTOR_DEBUG - assert(myvm->gc_locals.back() == (cell)this); -#endif - myvm->gc_locals.pop_back(); - } -}; - -/* A similar hack for the bignum implementation */ -struct gc_bignum -{ - bignum **addr; - factorvm *myvm; - gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { - if(*addr_) - check_data_pointer(*addr_); - myvm->gc_bignums.push_back((cell)addr); - } - - ~gc_bignum() { -#ifdef FACTOR_DEBUG - assert(myvm->gc_bignums.back() == (cell)addr); -#endif - myvm->gc_bignums.pop_back(); - } -}; - -#define GC_BIGNUM(x,vm) gc_bignum x##__gc_root(&x,vm) - -//generic_arrays.hpp -template TYPE *factorvm::allot_array_internal(cell capacity) -{ - TYPE *array = allot(array_size(capacity)); - array->capacity = tag_fixnum(capacity); - return array; -} - -template TYPE *allot_array_internal(cell capacity) -{ - return vm->allot_array_internal(capacity); -} - -template bool factorvm::reallot_array_in_place_p(TYPE *array, cell capacity) -{ - return in_zone(&nursery,array) && capacity <= array_capacity(array); -} - -template bool reallot_array_in_place_p(TYPE *array, cell capacity) -{ - return vm->reallot_array_in_place_p(array,capacity); -} - -template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) -{ - gc_root array(array_,this); - - if(reallot_array_in_place_p(array.untagged(),capacity)) - { - array->capacity = tag_fixnum(capacity); - return array.untagged(); - } - else - { - cell to_copy = array_capacity(array.untagged()); - if(capacity < to_copy) - to_copy = capacity; - - TYPE *new_array = allot_array_internal(capacity); - - memcpy(new_array + 1,array.untagged() + 1,to_copy * TYPE::element_size); - memset((char *)(new_array + 1) + to_copy * TYPE::element_size, - 0,(capacity - to_copy) * TYPE::element_size); - - return new_array; - } -} - -//arrays.hpp -inline void factorvm::set_array_nth(array *array, cell slot, cell value) -{ -#ifdef FACTOR_DEBUG - assert(slot < array_capacity(array)); - assert(array->h.hi_tag() == ARRAY_TYPE); - check_tagged_pointer(value); -#endif - array->data()[slot] = value; - write_barrier(array); -} - -inline void set_array_nth(array *array, cell slot, cell value) -{ - return vm->set_array_nth(array,slot,value); -} - -struct growable_array { - cell count; - gc_root elements; - - growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(allot_array(capacity,F),myvm) {} - - void add(cell elt); - void trim(); -}; - -//byte_arrays.hpp -struct growable_byte_array { - cell count; - gc_root elements; - - growable_byte_array(factorvm *vm,cell capacity = 40) : count(0), elements(allot_byte_array(capacity),vm) { } - - void append_bytes(void *elts, cell len); - void append_byte_array(cell elts); - - void trim(); -}; - -//math.hpp -inline cell factorvm::allot_integer(fixnum x) -{ - if(x < fixnum_min || x > fixnum_max) - return tag(fixnum_to_bignum(x)); - else - return tag_fixnum(x); -} - -inline cell allot_integer(fixnum x) -{ - return vm->allot_integer(x); -} - -inline cell factorvm::allot_cell(cell x) -{ - if(x > (cell)fixnum_max) - return tag(cell_to_bignum(x)); - else - return tag_fixnum(x); -} - -inline cell allot_cell(cell x) -{ - return vm->allot_cell(x); -} - -inline cell factorvm::allot_float(double n) -{ - boxed_float *flo = allot(sizeof(boxed_float)); - flo->n = n; - return tag(flo); -} - -inline cell allot_float(double n) -{ - return vm->allot_float(n); -} - -inline bignum *factorvm::float_to_bignum(cell tagged) -{ - return double_to_bignum(untag_float(tagged)); -} - -inline bignum *float_to_bignum(cell tagged) -{ - return vm->float_to_bignum(tagged); -} - -inline double factorvm::bignum_to_float(cell tagged) -{ - return bignum_to_double(untag(tagged)); -} - -inline double bignum_to_float(cell tagged) -{ - return vm->bignum_to_float(tagged); -} - -inline double factorvm::untag_float(cell tagged) -{ - return untag(tagged)->n; -} - -inline double untag_float(cell tagged) -{ - return vm->untag_float(tagged); -} - -inline double factorvm::untag_float_check(cell tagged) -{ - return untag_check(tagged)->n; -} - -inline double untag_float_check(cell tagged) -{ - return vm->untag_float_check(tagged); -} - -inline fixnum factorvm::float_to_fixnum(cell tagged) -{ - return (fixnum)untag_float(tagged); -} - -inline static fixnum float_to_fixnum(cell tagged) -{ - return vm->float_to_fixnum(tagged); -} - -inline double factorvm::fixnum_to_float(cell tagged) -{ - return (double)untag_fixnum(tagged); -} - -inline double fixnum_to_float(cell tagged) -{ - return vm->fixnum_to_float(tagged); -} - - -//callstack.hpp -/* This is a little tricky. The iterator may allocate memory, so we -keep the callstack in a GC root and use relative offsets */ -template void factorvm::iterate_callstack_object(callstack *stack_, TYPE &iterator) -{ - gc_root stack(stack_,vm); - fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); - - while(frame_offset >= 0) - { - stack_frame *frame = stack->frame_at(frame_offset); - frame_offset -= frame->size; - iterator(frame,this); - } -} - -template void iterate_callstack_object(callstack *stack_, TYPE &iterator) -{ - return vm->iterate_callstack_object(stack_,iterator); -} - -//booleans.hpp -inline cell factorvm::tag_boolean(cell untagged) -{ - return (untagged ? T : F); -} - -inline cell tag_boolean(cell untagged) -{ - return vm->tag_boolean(untagged); -} - -// callstack.hpp -template void factorvm::iterate_callstack(cell top, cell bottom, TYPE &iterator) -{ - stack_frame *frame = (stack_frame *)bottom - 1; - - while((cell)frame >= top) - { - iterator(frame,this); - frame = frame_successor(frame); - } -} - -template void iterate_callstack(cell top, cell bottom, TYPE &iterator) -{ - return vm->iterate_callstack(top,bottom,iterator); -} - - -// data_heap.hpp -/* Every object has a regular representation in the runtime, which makes GC -much simpler. Every slot of the object until binary_payload_start is a pointer -to some other object. */ -struct factorvm; -inline void factorvm::do_slots(cell obj, void (* iter)(cell *,factorvm*)) -{ - cell scan = obj; - cell payload_start = binary_payload_start((object *)obj); - cell end = obj + payload_start; - - scan += sizeof(cell); - - while(scan < end) - { - iter((cell *)scan,this); - scan += sizeof(cell); - } -} - -inline void do_slots(cell obj, void (* iter)(cell *,factorvm*)) -{ - return vm->do_slots(obj,iter); -} - - } From e2993558a8f0a7d33be646efe9f3c68514086dda Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:15 +0100 Subject: [PATCH 149/345] moved tagged template code back into tagged.hpp header --- vm/inlineimpls.hpp | 70 ---------------------------------------------- vm/master.hpp | 2 +- vm/tagged.hpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 71 deletions(-) diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index b88b3254df..8885c09404 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -6,76 +6,6 @@ namespace factor //tagged.hpp -template -struct tagged -{ - cell value_; - - cell value() const { return value_; } - TYPE *untagged() const { return (TYPE *)(UNTAG(value_)); } - - cell type() const { - cell tag = TAG(value_); - if(tag == OBJECT_TYPE) - return untagged()->h.hi_tag(); - else - return tag; - } - - bool type_p(cell type_) const { return type() == type_; } - - TYPE *untag_check() const { - if(TYPE::type_number != TYPE_COUNT && !type_p(TYPE::type_number)) - type_error(TYPE::type_number,value_); - return untagged(); - } - - explicit tagged(cell tagged) : value_(tagged) { -#ifdef FACTOR_DEBUG - untag_check(); -#endif - } - - explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { -#ifdef FACTOR_DEBUG - untag_check(); -#endif - } - - TYPE *operator->() const { return untagged(); } - cell *operator&() const { return &value_; } - - const tagged& operator=(const TYPE *x) { value_ = tag(x); return *this; } - const tagged& operator=(const cell &x) { value_ = x; return *this; } - - bool operator==(const tagged &x) { return value_ == x.value_; } - bool operator!=(const tagged &x) { return value_ != x.value_; } - - template tagged as() { return tagged(value_); } -}; - -template TYPE *factorvm::untag_check(cell value) -{ - return tagged(value).untag_check(); -} - -template TYPE *untag_check(cell value) -{ - return vm->untag_check(value); -} - -template TYPE *factorvm::untag(cell value) -{ - return tagged(value).untagged(); -} - -template TYPE *untag(cell value) -{ - return vm->untag(value); -} - - - // write_barrier.hpp inline card *factorvm::addr_to_card(cell a) diff --git a/vm/master.hpp b/vm/master.hpp index 5d95fa440e..bf60d1e4f6 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -41,7 +41,6 @@ #include "segments.hpp" #include "contexts.hpp" #include "run.hpp" -#include "tagged.hpp" #include "profiler.hpp" #include "errors.hpp" #include "bignumint.hpp" @@ -68,6 +67,7 @@ #include "callstack.hpp" #include "alien.hpp" #include "vm.hpp" +#include "tagged.hpp" #include "inlineimpls.hpp" #include "jit.hpp" #include "quotations.hpp" diff --git a/vm/tagged.hpp b/vm/tagged.hpp index ca208555da..4a1babb599 100755 --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -10,4 +10,74 @@ inline static cell tag_dynamic(object *value) { return RETAG(value,tag_for(value->h.hi_tag())); } + +template +struct tagged +{ + cell value_; + + cell value() const { return value_; } + TYPE *untagged() const { return (TYPE *)(UNTAG(value_)); } + + cell type() const { + cell tag = TAG(value_); + if(tag == OBJECT_TYPE) + return untagged()->h.hi_tag(); + else + return tag; + } + + bool type_p(cell type_) const { return type() == type_; } + + TYPE *untag_check() const { + if(TYPE::type_number != TYPE_COUNT && !type_p(TYPE::type_number)) + type_error(TYPE::type_number,value_); + return untagged(); + } + + explicit tagged(cell tagged) : value_(tagged) { +#ifdef FACTOR_DEBUG + untag_check(); +#endif + } + + explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { +#ifdef FACTOR_DEBUG + untag_check(); +#endif + } + + TYPE *operator->() const { return untagged(); } + cell *operator&() const { return &value_; } + + const tagged& operator=(const TYPE *x) { value_ = tag(x); return *this; } + const tagged& operator=(const cell &x) { value_ = x; return *this; } + + bool operator==(const tagged &x) { return value_ == x.value_; } + bool operator!=(const tagged &x) { return value_ != x.value_; } + + template tagged as() { return tagged(value_); } +}; + +template TYPE *factorvm::untag_check(cell value) +{ + return tagged(value).untag_check(); +} + +template TYPE *untag_check(cell value) +{ + return vm->untag_check(value); +} + +template TYPE *factorvm::untag(cell value) +{ + return tagged(value).untagged(); +} + +template TYPE *untag(cell value) +{ + return vm->untag(value); +} + + } From 82e1ea71109a8d456a8272ee1e4adb4edc599cc2 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:15 +0100 Subject: [PATCH 150/345] vm ptr passed to untag_check --- vm/alien.cpp | 4 ++-- vm/callstack.cpp | 6 +++--- vm/image.cpp | 4 ++-- vm/inlineimpls.hpp | 4 +--- vm/io.cpp | 4 ++-- vm/quotations.cpp | 2 +- vm/tagged.hpp | 6 +++--- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/vm/alien.cpp b/vm/alien.cpp index 0419e3cec3..ffd49f60b0 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -139,7 +139,7 @@ DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset) inline void factorvm::vmprim_dlopen() { gc_root path(dpop(),this); - path.untag_check(); + path.untag_check(this); gc_root library(allot(sizeof(dll)),this); library->path = path.value(); ffi_dlopen(library.untagged()); @@ -156,7 +156,7 @@ inline void factorvm::vmprim_dlsym() { gc_root library(dpop(),this); gc_root name(dpop(),this); - name.untag_check(); + name.untag_check(this); symbol_char *sym = name->data(); diff --git a/vm/callstack.cpp b/vm/callstack.cpp index 8df438206c..c330e38064 100755 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -242,7 +242,7 @@ stack_frame *innermost_stack_frame(callstack *stack) stack_frame *factorvm::innermost_stack_frame_quot(callstack *callstack) { stack_frame *inner = innermost_stack_frame(callstack); - tagged(frame_executing(inner)).untag_check(); + tagged(frame_executing(inner)).untag_check(this); return inner; } @@ -278,8 +278,8 @@ inline void factorvm::vmprim_set_innermost_stack_frame_quot() gc_root callstack(dpop(),this); gc_root quot(dpop(),this); - callstack.untag_check(); - quot.untag_check(); + callstack.untag_check(this); + quot.untag_check(this); jit_compile(quot.value(),true); diff --git a/vm/image.cpp b/vm/image.cpp index b8e89d5fcb..5ceefdfeb4 100755 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -146,7 +146,7 @@ inline void factorvm::vmprim_save_image() gc(); gc_root path(dpop(),this); - path.untag_check(); + path.untag_check(this); save_image((vm_char *)(path.untagged() + 1)); } @@ -161,7 +161,7 @@ inline void factorvm::vmprim_save_image_and_exit() where we might throw an error, so we have to throw an error here since later steps destroy the current image. */ gc_root path(dpop(),this); - path.untag_check(); + path.untag_check(this); /* strip out userenv data which is set on startup anyway */ for(cell i = 0; i < USER_ENV; i++) diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index 8885c09404..b7aaacbc58 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -1,11 +1,9 @@ namespace factor { -// I've had to copy inline implementations here to make dependencies work. Hopefully this can be better factored +// I've had to copy inline implementations here to make dependencies work. Am hoping to move this code back into include files // once the rest of the reentrant changes are done. -PD -//tagged.hpp - // write_barrier.hpp inline card *factorvm::addr_to_card(cell a) diff --git a/vm/io.cpp b/vm/io.cpp index 93ae005b6a..cfbafda907 100755 --- a/vm/io.cpp +++ b/vm/io.cpp @@ -45,8 +45,8 @@ inline void factorvm::vmprim_fopen() { gc_root mode(dpop(),this); gc_root path(dpop(),this); - mode.untag_check(); - path.untag_check(); + mode.untag_check(this); + path.untag_check(this); for(;;) { diff --git a/vm/quotations.cpp b/vm/quotations.cpp index 0237651e17..ef615dc095 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -396,7 +396,7 @@ VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack) inline void factorvm::vmprim_quot_compiled_p() { tagged quot(dpop()); - quot.untag_check(); + quot.untag_check(this); dpush(tag_boolean(quot->code != NULL)); } diff --git a/vm/tagged.hpp b/vm/tagged.hpp index 4a1babb599..9f8a0eb411 100755 --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -29,9 +29,9 @@ struct tagged bool type_p(cell type_) const { return type() == type_; } - TYPE *untag_check() const { + TYPE *untag_check(factorvm *myvm) const { if(TYPE::type_number != TYPE_COUNT && !type_p(TYPE::type_number)) - type_error(TYPE::type_number,value_); + myvm->type_error(TYPE::type_number,value_); return untagged(); } @@ -61,7 +61,7 @@ struct tagged template TYPE *factorvm::untag_check(cell value) { - return tagged(value).untag_check(); + return tagged(value).untag_check(this); } template TYPE *untag_check(cell value) From 7a20e1648cb8dd405dfb2947a9d3b82850db4263 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:15 +0100 Subject: [PATCH 151/345] Dev checkpoint --- vm/data_heap.cpp | 7 ++++++- vm/vm.hpp | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/vm/data_heap.cpp b/vm/data_heap.cpp index 020954bf05..e484a18b7a 100755 --- a/vm/data_heap.cpp +++ b/vm/data_heap.cpp @@ -237,7 +237,7 @@ void init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size, } /* Size of the object pointed to by a tagged pointer */ -cell object_size(cell tagged) +cell factorvm::object_size(cell tagged) { if(immediate_p(tagged)) return 0; @@ -245,6 +245,11 @@ cell object_size(cell tagged) return untagged_object_size(untag(tagged)); } +cell object_size(cell tagged) +{ + return vm->object_size(tagged); +} + /* Size of the object pointed to by an untagged pointer */ cell factorvm::untagged_object_size(object *pointer) { diff --git a/vm/vm.hpp b/vm/vm.hpp index 1ed6d965bc..3aab1061ff 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -154,6 +154,9 @@ struct factorvm { inline void vmprim_end_scan(); template void each_object(T &functor); cell find_all_words(); + cell object_size(cell tagged); + // next method here: + //write barrier inline card *addr_to_card(cell a); @@ -412,7 +415,6 @@ struct factorvm { inline double fixnum_to_float(cell tagged); template T *untag_check(cell value); template T *untag(cell value); - // next method here: //io void init_c_io(); From b2f52ed109f4d08fe5cba55db737d2e988a8ed7f Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:16 +0100 Subject: [PATCH 152/345] removed non-primitive global functions from data_heap --- vm/data_heap.cpp | 89 ------------------------------------------------ vm/data_heap.hpp | 36 +------------------- 2 files changed, 1 insertion(+), 124 deletions(-) diff --git a/vm/data_heap.cpp b/vm/data_heap.cpp index e484a18b7a..e790c63122 100755 --- a/vm/data_heap.cpp +++ b/vm/data_heap.cpp @@ -8,10 +8,6 @@ namespace factor /* new objects are allocated here */ VM_C_API zone nursery; - - - - cell factorvm::init_zone(zone *z, cell size, cell start) { z->size = size; @@ -20,10 +16,6 @@ cell factorvm::init_zone(zone *z, cell size, cell start) return z->end; } -cell init_zone(zone *z, cell size, cell start) -{ - return vm->init_zone(z,size,start); -} void factorvm::init_card_decks() { @@ -33,11 +25,6 @@ void factorvm::init_card_decks() decks_offset = (cell)data->decks - (start >> deck_bits); } -void init_card_decks() -{ - return vm->init_card_decks(); -} - data_heap *factorvm::alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size) { young_size = align(young_size,deck_size); @@ -102,10 +89,6 @@ data_heap *factorvm::alloc_data_heap(cell gens, cell young_size,cell aging_size, return data; } -data_heap *alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size) -{ - return vm->alloc_data_heap(gens,young_size,aging_size,tenured_size); -} data_heap *factorvm::grow_data_heap(data_heap *data, cell requested_bytes) { @@ -117,10 +100,6 @@ data_heap *factorvm::grow_data_heap(data_heap *data, cell requested_bytes) new_tenured_size); } -data_heap *grow_data_heap(data_heap *data, cell requested_bytes) -{ - return vm->grow_data_heap(data,requested_bytes); -} void factorvm::dealloc_data_heap(data_heap *data) { @@ -133,10 +112,6 @@ void factorvm::dealloc_data_heap(data_heap *data) free(data); } -void dealloc_data_heap(data_heap *data) -{ - return vm->dealloc_data_heap(data); -} void factorvm::clear_cards(cell from, cell to) { @@ -146,10 +121,6 @@ void factorvm::clear_cards(cell from, cell to) memset(first_card,0,last_card - first_card); } -void clear_cards(cell from, cell to) -{ - return vm->clear_cards(from,to); -} void factorvm::clear_decks(cell from, cell to) { @@ -159,10 +130,6 @@ void factorvm::clear_decks(cell from, cell to) memset(first_deck,0,last_deck - first_deck); } -void clear_decks(cell from, cell to) -{ - return vm->clear_decks(from,to); -} void factorvm::clear_allot_markers(cell from, cell to) { @@ -172,10 +139,6 @@ void factorvm::clear_allot_markers(cell from, cell to) memset(first_card,invalid_allot_marker,last_card - first_card); } -void clear_allot_markers(cell from, cell to) -{ - return vm->clear_allot_markers(from,to); -} void factorvm::reset_generation(cell i) { @@ -186,10 +149,6 @@ void factorvm::reset_generation(cell i) memset((void*)z->start,69,z->size); } -void reset_generation(cell i) -{ - return vm->reset_generation(i); -} /* After garbage collection, any generations which are now empty need to have their allocation pointers and cards reset. */ @@ -204,10 +163,6 @@ void factorvm::reset_generations(cell from, cell to) clear_allot_markers(from,to); } -void reset_generations(cell from, cell to) -{ - return vm->reset_generations(from,to); -} void factorvm::set_data_heap(data_heap *data_) { @@ -219,10 +174,6 @@ void factorvm::set_data_heap(data_heap *data_) clear_allot_markers(data->nursery(),data->tenured()); } -void set_data_heap(data_heap *data_) -{ - return vm->set_data_heap(data_); -} void factorvm::init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size,bool secure_gc_) { @@ -231,10 +182,6 @@ void factorvm::init_data_heap(cell gens,cell young_size,cell aging_size,cell ten init_data_gc(); } -void init_data_heap(cell gens,cell young_size,cell aging_size,cell tenured_size,bool secure_gc_) -{ - return vm->init_data_heap(gens,young_size,aging_size,tenured_size,secure_gc_); -} /* Size of the object pointed to by a tagged pointer */ cell factorvm::object_size(cell tagged) @@ -245,10 +192,6 @@ cell factorvm::object_size(cell tagged) return untagged_object_size(untag(tagged)); } -cell object_size(cell tagged) -{ - return vm->object_size(tagged); -} /* Size of the object pointed to by an untagged pointer */ cell factorvm::untagged_object_size(object *pointer) @@ -256,10 +199,6 @@ cell factorvm::untagged_object_size(object *pointer) return align8(unaligned_object_size(pointer)); } -cell untagged_object_size(object *pointer) -{ - return vm->untagged_object_size(pointer); -} /* Size of the data area of an object pointed to by an untagged pointer */ cell factorvm::unaligned_object_size(object *pointer) @@ -296,10 +235,6 @@ cell factorvm::unaligned_object_size(object *pointer) } } -cell unaligned_object_size(object *pointer) -{ - return vm->unaligned_object_size(pointer); -} inline void factorvm::vmprim_size() { @@ -348,10 +283,6 @@ cell factorvm::binary_payload_start(object *pointer) } } -cell binary_payload_start(object *pointer) -{ - return vm->binary_payload_start(pointer); -} /* Push memory usage statistics in data heap */ inline void factorvm::vmprim_data_room() @@ -385,20 +316,12 @@ void factorvm::begin_scan() gc_off = true; } -void begin_scan() -{ - return vm->begin_scan(); -} void factorvm::end_scan() { gc_off = false; } -void end_scan() -{ - return vm->end_scan(); -} inline void factorvm::vmprim_begin_scan() { @@ -423,10 +346,6 @@ cell factorvm::next_object() return tag_dynamic(obj); } -cell next_object() -{ - return vm->next_object(); -} /* Push object at heap scan cursor and advance; pushes f when done */ inline void factorvm::vmprim_next_object() @@ -459,10 +378,6 @@ template void factorvm::each_object(TYPE &functor) end_scan(); } -template void each_object(TYPE &functor) -{ - return vm->each_object(functor); -} namespace { @@ -491,9 +406,5 @@ cell factorvm::find_all_words() return accum.words.elements.value(); } -cell find_all_words() -{ - return vm->find_all_words(); -} } diff --git a/vm/data_heap.hpp b/vm/data_heap.hpp index 67e3109924..88316ffd8d 100755 --- a/vm/data_heap.hpp +++ b/vm/data_heap.hpp @@ -53,42 +53,11 @@ inline static bool in_zone(zone *z, object *pointer) return (cell)pointer >= z->start && (cell)pointer < z->end; } -cell init_zone(zone *z, cell size, cell base); - -void init_card_decks(); - -data_heap *grow_data_heap(data_heap *data, cell requested_bytes); - -void dealloc_data_heap(data_heap *data); - -void clear_cards(cell from, cell to); -void clear_decks(cell from, cell to); -void clear_allot_markers(cell from, cell to); -void reset_generation(cell i); -void reset_generations(cell from, cell to); - -void set_data_heap(data_heap *data_heap_); - -void init_data_heap(cell gens, - cell young_size, - cell aging_size, - cell tenured_size, - bool secure_gc_); - /* set up guard pages to check for under/overflow. size must be a multiple of the page size */ -segment *alloc_segment(cell size); +segment *alloc_segment(cell size); // defined in OS-*.cpp files PD void dealloc_segment(segment *block); -cell untagged_object_size(object *pointer); -cell unaligned_object_size(object *pointer); -cell binary_payload_start(object *pointer); -cell object_size(cell tagged); - -void begin_scan(); -void end_scan(); -cell next_object(); - PRIMITIVE(data_room); PRIMITIVE(size); @@ -96,9 +65,6 @@ PRIMITIVE(begin_scan); PRIMITIVE(next_object); PRIMITIVE(end_scan); -cell find_all_words(); - - } /* new objects are allocated here */ From 39dc71e6121009f5009d8f4897d28d3c254fc228 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:16 +0100 Subject: [PATCH 153/345] removed global functions from data_gc --- vm/data_gc.cpp | 104 ++----------------------------------------------- vm/data_gc.hpp | 14 ------- vm/vm.hpp | 1 + 3 files changed, 5 insertions(+), 114 deletions(-) diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index 408a70ea5e..dfc1067690 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -10,10 +10,6 @@ void factorvm::init_data_gc() collecting_aging_again = false; } -void init_data_gc() -{ - return vm->init_data_gc(); -} /* Given a pointer to oldspace, copy it to newspace */ object *factorvm::copy_untagged_object_impl(object *pointer, cell size) @@ -30,10 +26,6 @@ object *factorvm::copy_untagged_object_impl(object *pointer, cell size) return newpointer; } -object *copy_untagged_object_impl(object *pointer, cell size) -{ - return vm->copy_untagged_object_impl(pointer,size); -} object *factorvm::copy_object_impl(object *untagged) { @@ -42,10 +34,6 @@ object *factorvm::copy_object_impl(object *untagged) return newpointer; } -object *copy_object_impl(object *untagged) -{ - return vm->copy_object_impl(untagged); -} bool factorvm::should_copy_p(object *untagged) { @@ -64,10 +52,6 @@ bool factorvm::should_copy_p(object *untagged) } } -bool should_copy_p(object *untagged) -{ - return vm->should_copy_p(untagged); -} /* Follow a chain of forwarding pointers */ object *factorvm::resolve_forwarding(object *untagged) @@ -88,10 +72,6 @@ object *factorvm::resolve_forwarding(object *untagged) } } -object *resolve_forwarding(object *untagged) -{ - return vm->resolve_forwarding(untagged); -} template TYPE *factorvm::copy_untagged_object(TYPE *untagged) { @@ -108,20 +88,12 @@ template TYPE *factorvm::copy_untagged_object(TYPE *untagged) return untagged; } -template TYPE *copy_untagged_object(TYPE *untagged) -{ - return vm->copy_untagged_object(untagged); -} cell factorvm::copy_object(cell pointer) { return RETAG(copy_untagged_object(untag(pointer)),TAG(pointer)); } -cell copy_object(cell pointer) -{ - return vm->copy_object(pointer); -} void factorvm::copy_handle(cell *handle) { @@ -136,10 +108,6 @@ void factorvm::copy_handle(cell *handle) } } -void copy_handle(cell *handle) -{ - return vm->copy_handle(handle); -} /* Scan all the objects in the card */ void factorvm::copy_card(card *ptr, cell gen, cell here) @@ -155,10 +123,6 @@ void factorvm::copy_card(card *ptr, cell gen, cell here) cards_scanned++; } -void copy_card(card *ptr, cell gen, cell here) -{ - return vm->copy_card(ptr,gen,here); -} void factorvm::copy_card_deck(card_deck *deck, cell gen, card mask, card unmask) { @@ -191,10 +155,6 @@ void factorvm::copy_card_deck(card_deck *deck, cell gen, card mask, card unmask) decks_scanned++; } -void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask) -{ - return vm->copy_card_deck(deck,gen,mask,unmask); -} /* Copy all newspace objects referenced from marked cards to the destination */ void factorvm::copy_gen_cards(cell gen) @@ -262,10 +222,6 @@ void factorvm::copy_gen_cards(cell gen) } } -void copy_gen_cards(cell gen) -{ - return vm->copy_gen_cards(gen); -} /* Scan cards in all generations older than the one being collected, copying old->new references */ @@ -280,10 +236,6 @@ void factorvm::copy_cards() card_scan_time += (current_micros() - start); } -void copy_cards() -{ - return vm->copy_cards(); -} /* Copy all tagged pointers in a range of memory */ void factorvm::copy_stack_elements(segment *region, cell top) @@ -294,10 +246,6 @@ void factorvm::copy_stack_elements(segment *region, cell top) copy_handle((cell*)ptr); } -void copy_stack_elements(segment *region, cell top) -{ - return vm->copy_stack_elements(region,top); -} void factorvm::copy_registered_locals() { @@ -308,10 +256,6 @@ void factorvm::copy_registered_locals() copy_handle((cell *)(*iter)); } -void copy_registered_locals() -{ - return vm->copy_registered_locals(); -} void factorvm::copy_registered_bignums() { @@ -335,10 +279,6 @@ void factorvm::copy_registered_bignums() } } -void copy_registered_bignums() -{ - return vm->copy_registered_bignums(); -} /* Copy roots over at the start of GC, namely various constants, stacks, the user environment and extra roots registered by local_roots.hpp */ @@ -376,10 +316,6 @@ void factorvm::copy_roots() copy_handle(&userenv[i]); } -void copy_roots() -{ - return vm->copy_roots(); -} cell factorvm::copy_next_from_nursery(cell scan) { @@ -409,10 +345,6 @@ cell factorvm::copy_next_from_nursery(cell scan) return scan + untagged_object_size((object *)scan); } -cell copy_next_from_nursery(cell scan) -{ - return vm->copy_next_from_nursery(scan); -} cell factorvm::copy_next_from_aging(cell scan) { @@ -446,10 +378,6 @@ cell factorvm::copy_next_from_aging(cell scan) return scan + untagged_object_size((object *)scan); } -cell copy_next_from_aging(cell scan) -{ - return vm->copy_next_from_aging(scan); -} cell factorvm::copy_next_from_tenured(cell scan) { @@ -481,10 +409,6 @@ cell factorvm::copy_next_from_tenured(cell scan) return scan + untagged_object_size((object *)scan); } -cell copy_next_from_tenured(cell scan) -{ - return vm->copy_next_from_tenured(scan); -} void factorvm::copy_reachable_objects(cell scan, cell *end) { @@ -505,10 +429,6 @@ void factorvm::copy_reachable_objects(cell scan, cell *end) } } -void copy_reachable_objects(cell scan, cell *end) -{ - return vm->copy_reachable_objects(scan,end); -} /* Prepare to start copying reachable objects into an unused zone */ void factorvm::begin_gc(cell requested_bytes) @@ -544,10 +464,6 @@ void factorvm::begin_gc(cell requested_bytes) } } -void begin_gc(cell requested_bytes) -{ - return vm->begin_gc(requested_bytes); -} void factorvm::end_gc(cell gc_elapsed) { @@ -587,10 +503,6 @@ void factorvm::end_gc(cell gc_elapsed) collecting_aging_again = false; } -void end_gc(cell gc_elapsed) -{ - return vm->end_gc(gc_elapsed); -} /* Collect gen and all younger generations. If growing_data_heap_ is true, we must grow the data heap to such a size that @@ -673,20 +585,12 @@ void factorvm::garbage_collection(cell gen,bool growing_data_heap_,cell requeste performing_gc = false; } -void garbage_collection(cell gen,bool growing_data_heap_,cell requested_bytes) -{ - return vm->garbage_collection(gen,growing_data_heap_,requested_bytes); -} void factorvm::gc() { garbage_collection(data->tenured(),false,0); } -void gc() -{ - return vm->gc(); -} inline void factorvm::vmprim_gc() { @@ -744,14 +648,14 @@ void factorvm::clear_gc_stats() code_heap_scans = 0; } -void clear_gc_stats() +inline void factorvm::vmprim_clear_gc_stats() { - return vm->clear_gc_stats(); + clear_gc_stats(); } PRIMITIVE(clear_gc_stats) { - clear_gc_stats(); + PRIMITIVE_GETVM()->vmprim_clear_gc_stats(); } /* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this @@ -803,7 +707,7 @@ VM_ASM_API void factorvm::inline_gc(cell *gc_roots_base, cell gc_roots_size) VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size) { - return vm->inline_gc(gc_roots_base,gc_roots_size); + return vm->inline_gc(gc_roots_base,gc_roots_size); } } diff --git a/vm/data_gc.hpp b/vm/data_gc.hpp index 68b2b4a936..950990a91b 100755 --- a/vm/data_gc.hpp +++ b/vm/data_gc.hpp @@ -10,30 +10,16 @@ struct gc_stats { u64 bytes_copied; }; -void init_data_gc(); - -void gc(); - -void copy_handle(cell *handle); - -void garbage_collection(volatile cell gen, - bool growing_data_heap_, - cell requested_bytes); - /* We leave this many bytes free at the top of the nursery so that inline allocation (which does not call GC because of possible roots in volatile registers) does not run out of memory */ static const cell allot_buffer_zone = 1024; -void copy_reachable_objects(cell scan, cell *end); - PRIMITIVE(gc); PRIMITIVE(gc_stats); -void clear_gc_stats(); PRIMITIVE(clear_gc_stats); PRIMITIVE(become); - VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size); } diff --git a/vm/vm.hpp b/vm/vm.hpp index 3aab1061ff..1c4832e289 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -236,6 +236,7 @@ struct factorvm { template TYPE *allot(cell size); inline void check_data_pointer(object *pointer); inline void check_tagged_pointer(cell tagged); + inline void vmprim_clear_gc_stats(); // local roots /* If a runtime function needs to call another function which potentially From 00087e681450aec4eccd6145ca9617c98100321e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:16 +0100 Subject: [PATCH 154/345] removed global functions from code_gc --- vm/code_gc.cpp | 65 -------------------------------------------------- vm/code_gc.hpp | 12 ---------- 2 files changed, 77 deletions(-) diff --git a/vm/code_gc.cpp b/vm/code_gc.cpp index 9ccc83aa0f..4a86359f1f 100755 --- a/vm/code_gc.cpp +++ b/vm/code_gc.cpp @@ -8,10 +8,6 @@ void factorvm::clear_free_list(heap *heap) memset(&heap->free,0,sizeof(heap_free_list)); } -void clear_free_list(heap *heap) -{ - return vm->clear_free_list(heap); -} /* This malloc-style heap code is reasonably generic. Maybe in the future, it will be used for the data heap too, if we ever get incremental @@ -25,10 +21,6 @@ void factorvm::new_heap(heap *heap, cell size) clear_free_list(heap); } -void new_heap(heap *heap, cell size) -{ - return vm->new_heap(heap,size); -} void factorvm::add_to_free_list(heap *heap, free_heap_block *block) { @@ -45,10 +37,6 @@ void factorvm::add_to_free_list(heap *heap, free_heap_block *block) } } -void add_to_free_list(heap *heap, free_heap_block *block) -{ - return vm->add_to_free_list(heap,block); -} /* Called after reading the code heap from the image file, and after code GC. @@ -106,10 +94,6 @@ void factorvm::build_free_list(heap *heap, cell size) } -void build_free_list(heap *heap, cell size) -{ - return vm->build_free_list(heap,size); -} void factorvm::assert_free_block(free_heap_block *block) { @@ -117,10 +101,6 @@ void factorvm::assert_free_block(free_heap_block *block) critical_error("Invalid block in free list",(cell)block); } -void assert_free_block(free_heap_block *block) -{ - return vm->assert_free_block(block); -} free_heap_block *factorvm::find_free_block(heap *heap, cell size) { @@ -162,10 +142,6 @@ free_heap_block *factorvm::find_free_block(heap *heap, cell size) return NULL; } -free_heap_block *find_free_block(heap *heap, cell size) -{ - return vm->find_free_block(heap,size); -} free_heap_block *factorvm::split_free_block(heap *heap, free_heap_block *block, cell size) { @@ -183,10 +159,6 @@ free_heap_block *factorvm::split_free_block(heap *heap, free_heap_block *block, return block; } -free_heap_block *split_free_block(heap *heap, free_heap_block *block, cell size) -{ - return vm->split_free_block(heap,block,size); -} /* Allocate a block of memory from the mark and sweep GC heap */ heap_block *factorvm::heap_allot(heap *heap, cell size) @@ -205,10 +177,6 @@ heap_block *factorvm::heap_allot(heap *heap, cell size) return NULL; } -heap_block *heap_allot(heap *heap, cell size) -{ - return vm->heap_allot(heap,size); -} /* Deallocates a block manually */ void factorvm::heap_free(heap *heap, heap_block *block) @@ -217,10 +185,6 @@ void factorvm::heap_free(heap *heap, heap_block *block) add_to_free_list(heap,(free_heap_block *)block); } -void heap_free(heap *heap, heap_block *block) -{ - return vm->heap_free(heap,block); -} void factorvm::mark_block(heap_block *block) { @@ -238,10 +202,6 @@ void factorvm::mark_block(heap_block *block) } } -void mark_block(heap_block *block) -{ - return vm->mark_block(block); -} /* If in the middle of code GC, we have to grow the heap, data GC restarts from scratch, so we have to unmark any marked blocks. */ @@ -258,10 +218,6 @@ void factorvm::unmark_marked(heap *heap) } } -void unmark_marked(heap *heap) -{ - return vm->unmark_marked(heap); -} /* After code GC, all referenced code blocks have status set to B_MARKED, so any which are allocated and not marked can be reclaimed. */ @@ -312,10 +268,6 @@ void factorvm::free_unmarked(heap *heap, heap_iterator iter) add_to_free_list(heap,(free_heap_block *)prev); } -void free_unmarked(heap *heap, heap_iterator iter) -{ - return vm->free_unmarked(heap,iter); -} /* Compute total sum of sizes of free blocks, and size of largest free block */ void factorvm::heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free) @@ -346,10 +298,6 @@ void factorvm::heap_usage(heap *heap, cell *used, cell *total_free, cell *max_fr } } -void heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free) -{ - return vm->heap_usage(heap,used,total_free,max_free); -} /* The size of the heap, not including the last block if it's free */ cell factorvm::heap_size(heap *heap) @@ -367,10 +315,6 @@ cell factorvm::heap_size(heap *heap) return heap->seg->size; } -cell heap_size(heap *heap) -{ - return vm->heap_size(heap); -} /* Compute where each block is going to go, after compaction */ cell factorvm::compute_heap_forwarding(heap *heap, unordered_map &forwarding) @@ -394,10 +338,6 @@ cell factorvm::compute_heap_forwarding(heap *heap, unordered_mapseg->start; } -cell compute_heap_forwarding(heap *heap, unordered_map &forwarding) -{ - return vm->compute_heap_forwarding(heap,forwarding); -} void factorvm::compact_heap(heap *heap, unordered_map &forwarding) { @@ -413,9 +353,4 @@ void factorvm::compact_heap(heap *heap, unordered_map &forw } } -void compact_heap(heap *heap, unordered_map &forwarding) -{ - return vm->compact_heap(heap,forwarding); -} - } diff --git a/vm/code_gc.hpp b/vm/code_gc.hpp index 08bdd327c7..c59980dc30 100755 --- a/vm/code_gc.hpp +++ b/vm/code_gc.hpp @@ -16,18 +16,6 @@ struct heap { typedef void (*heap_iterator)(heap_block *compiled,factorvm *vm); -void new_heap(heap *h, cell size); -void build_free_list(heap *h, cell size); -heap_block *heap_allot(heap *h, cell size); -void heap_free(heap *h, heap_block *block); -void mark_block(heap_block *block); -void unmark_marked(heap *heap); -void free_unmarked(heap *heap, heap_iterator iter); -void heap_usage(heap *h, cell *used, cell *total_free, cell *max_free); -cell heap_size(heap *h); -cell compute_heap_forwarding(heap *h, unordered_map &forwarding); -void compact_heap(heap *h, unordered_map &forwarding); - inline static heap_block *next_block(heap *h, heap_block *block) { cell next = ((cell)block + block->size); From afe1cf0c735edbb7052ea8ff1982790e22fc982b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:16 +0100 Subject: [PATCH 155/345] removed some global functions from code_heap --- vm/code_heap.cpp | 37 ------------------------------------- vm/code_heap.hpp | 21 +-------------------- vm/inlineimpls.hpp | 9 +++++++++ vm/vm.hpp | 3 ++- 4 files changed, 12 insertions(+), 58 deletions(-) diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index fb24af4044..39f0dfd52a 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -9,10 +9,6 @@ void factorvm::init_code_heap(cell size) new_heap(&code,size); } -void init_code_heap(cell size) -{ - return vm->init_code_heap(size); -} bool factorvm::in_code_heap_p(cell ptr) { @@ -38,10 +34,6 @@ void factorvm::jit_compile_word(cell word_, cell def_, bool relocate) if(word->pic_tail_def != F) jit_compile(word->pic_tail_def,relocate); } -void jit_compile_word(cell word_, cell def_, bool relocate) -{ - return vm->jit_compile_word(word_,def_,relocate); -} /* Apply a function to every code block */ void factorvm::iterate_code_heap(code_heap_iterator iter) @@ -56,10 +48,6 @@ void factorvm::iterate_code_heap(code_heap_iterator iter) } } -void iterate_code_heap(code_heap_iterator iter) -{ - return vm->iterate_code_heap(iter); -} /* Copy literals referenced from all code blocks to newspace. Only for aging and nursery collections */ @@ -68,10 +56,6 @@ void factorvm::copy_code_heap_roots() iterate_code_heap(factor::copy_literal_references); } -void copy_code_heap_roots() -{ - return vm->copy_code_heap_roots(); -} /* Update pointers to words referenced from all code blocks. Only after defining a new word. */ @@ -80,10 +64,6 @@ void factorvm::update_code_heap_words() iterate_code_heap(factor::update_word_references); } -void update_code_heap_words() -{ - return vm->update_code_heap_words(); -} inline void factorvm::vmprim_modify_code_heap() { @@ -163,10 +143,6 @@ code_block *factorvm::forward_xt(code_block *compiled) return (code_block *)forwarding[compiled]; } -code_block *forward_xt(code_block *compiled) -{ - return vm->forward_xt(compiled); -} void factorvm::forward_frame_xt(stack_frame *frame) { @@ -223,10 +199,6 @@ void factorvm::forward_object_xts() end_scan(); } -void forward_object_xts() -{ - return vm->forward_object_xts(); -} /* Set the XT fields now that the heap has been compacted */ void factorvm::fixup_object_xts() @@ -257,10 +229,6 @@ void factorvm::fixup_object_xts() end_scan(); } -void fixup_object_xts() -{ - return vm->fixup_object_xts(); -} /* Move all free space to the end of the code heap. This is not very efficient, since it makes several passes over the code and data heaps, but we only ever @@ -288,9 +256,4 @@ void factorvm::compact_code_heap() build_free_list(&code,size); } -void compact_code_heap() -{ - return vm->compact_code_heap(); -} - } diff --git a/vm/code_heap.hpp b/vm/code_heap.hpp index 26e1faeb19..32e1dacfee 100755 --- a/vm/code_heap.hpp +++ b/vm/code_heap.hpp @@ -1,31 +1,12 @@ namespace factor { - -void init_code_heap(cell size); - -bool in_code_heap_p(cell ptr); - -void jit_compile_word(cell word, cell def, bool relocate); +bool in_code_heap_p(cell ptr); // Used by platform specific code struct factorvm; typedef void (*code_heap_iterator)(code_block *compiled,factorvm *myvm); -void iterate_code_heap(code_heap_iterator iter); - -void copy_code_heap_roots(); - PRIMITIVE(modify_code_heap); - PRIMITIVE(code_room); -void compact_code_heap(); - -inline static void check_code_pointer(cell ptr) -{ -#ifdef FACTOR_DEBUG - assert(in_code_heap_p(ptr)); -#endif -} - } diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index b7aaacbc58..241c958b2c 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -541,4 +541,13 @@ inline void do_slots(cell obj, void (* iter)(cell *,factorvm*)) return vm->do_slots(obj,iter); } +// code_heap.hpp + +inline void factorvm::check_code_pointer(cell ptr) +{ +#ifdef FACTOR_DEBUG + assert(in_code_heap_p(ptr)); +#endif +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 1c4832e289..a4c58a2a0b 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -155,7 +155,6 @@ struct factorvm { template void each_object(T &functor); cell find_all_words(); cell object_size(cell tagged); - // next method here: //write barrier @@ -499,6 +498,8 @@ struct factorvm { void forward_object_xts(); void fixup_object_xts(); void compact_code_heap(); + inline void check_code_pointer(cell ptr); + // next method here: //image cell code_relocation_base; From 100c26c38fdc7cd5879219bbc2cdb11d5ed35d0a Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:16 +0100 Subject: [PATCH 156/345] removed global functions from profiler --- vm/profiler.cpp | 12 ------------ vm/profiler.hpp | 2 -- 2 files changed, 14 deletions(-) diff --git a/vm/profiler.cpp b/vm/profiler.cpp index 8f714a992c..1b7c7c1ac5 100755 --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -9,10 +9,6 @@ void factorvm::init_profiler() profiling_p = false; } -void init_profiler() -{ - return vm->init_profiler(); -} /* Allocates memory */ code_block *factorvm::compile_profiling_stub(cell word_) @@ -25,10 +21,6 @@ code_block *factorvm::compile_profiling_stub(cell word_) return jit.to_code_block(); } -code_block *compile_profiling_stub(cell word_) -{ - return vm->compile_profiling_stub(word_); -} /* Allocates memory */ void factorvm::set_profiling(bool profiling) @@ -58,10 +50,6 @@ void factorvm::set_profiling(bool profiling) iterate_code_heap(factor::relocate_code_block); } -void set_profiling(bool profiling) -{ - return vm->set_profiling(profiling); -} inline void factorvm::vmprim_profiling() { diff --git a/vm/profiler.hpp b/vm/profiler.hpp index ab1d27b9d8..28bfbcc09f 100755 --- a/vm/profiler.hpp +++ b/vm/profiler.hpp @@ -1,8 +1,6 @@ namespace factor { -void init_profiler(); -code_block *compile_profiling_stub(cell word); PRIMITIVE(profiling); } From 32eace1a11170264f67c59a8d38cb1f021c75817 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:17 +0100 Subject: [PATCH 157/345] removed global functions from bignum.cpp --- vm/bignum.cpp | 207 +------------------------------------------------- vm/bignum.hpp | 75 ------------------ vm/vm.hpp | 4 + 3 files changed, 5 insertions(+), 281 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index f61b44340e..4c01b12820 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -73,10 +73,6 @@ int factorvm::bignum_equal_p(bignum * x, bignum * y) && (bignum_equal_p_unsigned (x, y)))); } -int bignum_equal_p(bignum * x, bignum * y) -{ - return vm->bignum_equal_p(x,y); -} enum bignum_comparison factorvm::bignum_compare(bignum * x, bignum * y) { @@ -100,10 +96,6 @@ enum bignum_comparison factorvm::bignum_compare(bignum * x, bignum * y) : (bignum_compare_unsigned (x, y)))); } -enum bignum_comparison bignum_compare(bignum * x, bignum * y) -{ - return vm->bignum_compare(x,y); -} /* allocates memory */ bignum *factorvm::bignum_add(bignum * x, bignum * y) @@ -122,11 +114,6 @@ bignum *factorvm::bignum_add(bignum * x, bignum * y) : (bignum_add_unsigned (x, y, 0))))); } -bignum *bignum_add(bignum * x, bignum * y) -{ - return vm->bignum_add(x,y); -} - /* allocates memory */ bignum *factorvm::bignum_subtract(bignum * x, bignum * y) { @@ -146,10 +133,6 @@ bignum *factorvm::bignum_subtract(bignum * x, bignum * y) : (bignum_subtract_unsigned (x, y)))))); } -bignum *bignum_subtract(bignum * x, bignum * y) -{ - return vm->bignum_subtract(x,y); -} /* allocates memory */ bignum *factorvm::bignum_multiply(bignum * x, bignum * y) @@ -183,10 +166,6 @@ bignum *factorvm::bignum_multiply(bignum * x, bignum * y) return (bignum_multiply_unsigned (x, y, negative_p)); } -bignum *bignum_multiply(bignum * x, bignum * y) -{ - return vm->bignum_multiply(x,y); -} /* allocates memory */ void factorvm::bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder) @@ -259,10 +238,6 @@ void factorvm::bignum_divide(bignum * numerator, bignum * denominator, bignum * } } -void bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder) -{ - return vm->bignum_divide(numerator,denominator,quotient,remainder); -} /* allocates memory */ bignum *factorvm::bignum_quotient(bignum * numerator, bignum * denominator) @@ -316,10 +291,6 @@ bignum *factorvm::bignum_quotient(bignum * numerator, bignum * denominator) } } -bignum *bignum_quotient(bignum * numerator, bignum * denominator) -{ - return vm->bignum_quotient(numerator,denominator); -} /* allocates memory */ bignum *factorvm::bignum_remainder(bignum * numerator, bignum * denominator) @@ -365,10 +336,6 @@ bignum *factorvm::bignum_remainder(bignum * numerator, bignum * denominator) } } -bignum *bignum_remainder(bignum * numerator, bignum * denominator) -{ - return vm->bignum_remainder(numerator, denominator); -} #define FOO_TO_BIGNUM(name,type,utype) \ bignum * factorvm::name##_to_bignum(type n) \ @@ -407,7 +374,7 @@ FOO_TO_BIGNUM(long_long,s64,u64) FOO_TO_BIGNUM(ulong_long,u64,u64) #define BIGNUM_TO_FOO(name,type,utype) \ - type bignum_to_##name(bignum * bignum) \ +type factorvm::bignum_to_##name(bignum * bignum) \ { \ if (BIGNUM_ZERO_P (bignum)) \ return (0); \ @@ -441,10 +408,6 @@ double factorvm::bignum_to_double(bignum * bignum) } } -double bignum_to_double(bignum * bignum) -{ - return vm->bignum_to_double(bignum); -} #define DTB_WRITE_DIGIT(factor) \ { \ @@ -488,10 +451,6 @@ bignum *factorvm::double_to_bignum(double x) } } -bignum *double_to_bignum(double x) -{ - return vm->double_to_bignum(x); -} #undef DTB_WRITE_DIGIT @@ -514,10 +473,6 @@ int factorvm::bignum_equal_p_unsigned(bignum * x, bignum * y) } } -int bignum_equal_p_unsigned(bignum * x, bignum * y) -{ - return vm->bignum_equal_p_unsigned(x,y); -} enum bignum_comparison factorvm::bignum_compare_unsigned(bignum * x, bignum * y) { @@ -544,10 +499,6 @@ enum bignum_comparison factorvm::bignum_compare_unsigned(bignum * x, bignum * y) return (bignum_comparison_equal); } -enum bignum_comparison bignum_compare_unsigned(bignum * x, bignum * y) -{ - return vm->bignum_compare_unsigned(x,y); -} /* Addition */ @@ -616,10 +567,6 @@ bignum *factorvm::bignum_add_unsigned(bignum * x, bignum * y, int negative_p) } } -bignum *bignum_add_unsigned(bignum * x, bignum * y, int negative_p) -{ - return vm->bignum_add_unsigned(x,y,negative_p); -} /* Subtraction */ @@ -695,10 +642,6 @@ bignum *factorvm::bignum_subtract_unsigned(bignum * x, bignum * y) } } -bignum *bignum_subtract_unsigned(bignum * x, bignum * y) -{ - return vm->bignum_subtract_unsigned(x,y); -} /* Multiplication Maximum value for product_low or product_high: @@ -777,10 +720,6 @@ bignum *factorvm::bignum_multiply_unsigned(bignum * x, bignum * y, int negative_ } } -bignum *bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) -{ - return vm->bignum_multiply_unsigned(x,y,negative_p); -} /* allocates memory */ bignum *factorvm::bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p) @@ -797,10 +736,6 @@ bignum *factorvm::bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit return (bignum_trim (p)); } -bignum *bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit_type y,int negative_p) -{ - return vm->bignum_multiply_unsigned_small_factor(x,y,negative_p); -} void factorvm::bignum_destructive_add(bignum * bignum, bignum_digit_type n) { @@ -825,10 +760,6 @@ void factorvm::bignum_destructive_add(bignum * bignum, bignum_digit_type n) } } -void bignum_destructive_add(bignum * bignum, bignum_digit_type n) -{ - return vm->bignum_destructive_add(bignum,n); -} void factorvm::bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) { @@ -859,10 +790,6 @@ void factorvm::bignum_destructive_scale_up(bignum * bignum, bignum_digit_type fa #undef product_high } -void bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) -{ - return vm->bignum_destructive_scale_up(bignum,factor); -} /* Division */ @@ -929,10 +856,6 @@ void factorvm::bignum_divide_unsigned_large_denominator(bignum * numerator, bign return; } -void bignum_divide_unsigned_large_denominator(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder, int q_negative_p, int r_negative_p) -{ - return vm->bignum_divide_unsigned_large_denominator(numerator,denominator,quotient,remainder,q_negative_p,r_negative_p); -} void factorvm::bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) { @@ -1008,10 +931,6 @@ void factorvm::bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum #undef qj } -void bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) -{ - return vm->bignum_divide_unsigned_normalized(u,v,q); -} bignum_digit_type factorvm::bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, bignum_digit_type guess, bignum_digit_type * u_start) { @@ -1088,10 +1007,6 @@ bignum_digit_type factorvm::bignum_divide_subtract(bignum_digit_type * v_start, return (guess - 1); } -bignum_digit_type bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, bignum_digit_type guess, bignum_digit_type * u_start) -{ - return vm->bignum_divide_subtract(v_start,v_end,guess,u_start); -} /* allocates memory */ void factorvm::bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) @@ -1152,10 +1067,6 @@ void factorvm::bignum_divide_unsigned_medium_denominator(bignum * numerator,bign return; } -void bignum_divide_unsigned_medium_denominator(bignum * numerator,bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) -{ - vm->bignum_divide_unsigned_medium_denominator(numerator,denominator,quotient,remainder,q_negative_p,r_negative_p); -} void factorvm::bignum_destructive_normalization(bignum * source, bignum * target, int shift_left) { @@ -1180,10 +1091,6 @@ void factorvm::bignum_destructive_normalization(bignum * source, bignum * target return; } -void bignum_destructive_normalization(bignum * source, bignum * target, int shift_left) -{ - return vm->bignum_destructive_normalization(source,target,shift_left); -} void factorvm::bignum_destructive_unnormalization(bignum * bignum, int shift_right) { @@ -1203,10 +1110,6 @@ void factorvm::bignum_destructive_unnormalization(bignum * bignum, int shift_rig return; } -void bignum_destructive_unnormalization(bignum * bignum, int shift_right) -{ - return vm->bignum_destructive_unnormalization(bignum,shift_right); -} /* This is a reduced version of the division algorithm, applied to the case of dividing two bignum digits by one bignum digit. It is @@ -1272,10 +1175,6 @@ bignum_digit_type factorvm::bignum_digit_divide(bignum_digit_type uh, bignum_dig return (HD_CONS ((u[2]), (u[3]))); } -bignum_digit_type bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, bignum_digit_type v, bignum_digit_type * q) /* return value */ -{ - return vm->bignum_digit_divide(uh,ul,v,q); -} #undef BDD_STEP @@ -1340,10 +1239,6 @@ bignum_digit_type factorvm::bignum_digit_divide_subtract(bignum_digit_type v1, b return (guess - 1); } -bignum_digit_type bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, bignum_digit_type guess, bignum_digit_type * u) -{ - return vm->bignum_digit_divide_subtract(v1,v2,guess,u); -} #undef BDDS_MULSUB #undef BDDS_ADD @@ -1368,10 +1263,6 @@ void factorvm::bignum_divide_unsigned_small_denominator(bignum * numerator, bign return; } -void bignum_divide_unsigned_small_denominator(bignum * numerator, bignum_digit_type denominator, bignum * * quotient, bignum * * remainder,int q_negative_p, int r_negative_p) -{ - return vm->bignum_divide_unsigned_small_denominator(numerator,denominator,quotient,remainder,q_negative_p,r_negative_p); -} /* Given (denominator > 1), it is fairly easy to show that (quotient_high < BIGNUM_RADIX_ROOT), after which it is easy to see @@ -1399,10 +1290,6 @@ bignum_digit_type factorvm::bignum_destructive_scale_down(bignum * bignum, bignu #undef quotient_high } -bignum_digit_type bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator) -{ - return vm->bignum_destructive_scale_down(bignum,denominator); -} /* allocates memory */ bignum * factorvm::bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p) @@ -1423,10 +1310,6 @@ bignum * factorvm::bignum_remainder_unsigned_small_denominator(bignum * n, bignu return (bignum_digit_to_bignum (r, negative_p)); } -bignum * bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p) -{ - return vm->bignum_remainder_unsigned_small_denominator(n,d,negative_p); -} /* allocates memory */ bignum *factorvm::bignum_digit_to_bignum(bignum_digit_type digit, int negative_p) @@ -1441,10 +1324,6 @@ bignum *factorvm::bignum_digit_to_bignum(bignum_digit_type digit, int negative_p } } -bignum *bignum_digit_to_bignum(bignum_digit_type digit, int negative_p) -{ - return vm->bignum_digit_to_bignum(digit, negative_p); -} /* allocates memory */ bignum *factorvm::allot_bignum(bignum_length_type length, int negative_p) @@ -1455,10 +1334,6 @@ bignum *factorvm::allot_bignum(bignum_length_type length, int negative_p) return (result); } -bignum *allot_bignum(bignum_length_type length, int negative_p) -{ - return vm->allot_bignum(length,negative_p); -} /* allocates memory */ bignum * factorvm::allot_bignum_zeroed(bignum_length_type length, int negative_p) @@ -1471,10 +1346,6 @@ bignum * factorvm::allot_bignum_zeroed(bignum_length_type length, int negative_p return (result); } -bignum * allot_bignum_zeroed(bignum_length_type length, int negative_p) -{ - return vm->allot_bignum_zeroed(length,negative_p); -} #define BIGNUM_REDUCE_LENGTH(source, length) \ source = reallot_array(source,length + 1) @@ -1492,10 +1363,6 @@ bignum *factorvm::bignum_shorten_length(bignum * bignum, bignum_length_type leng return (bignum); } -bignum *bignum_shorten_length(bignum * bignum, bignum_length_type length) -{ - return vm->bignum_shorten_length(bignum,length); -} /* allocates memory */ bignum *factorvm::bignum_trim(bignum * bignum) @@ -1515,10 +1382,6 @@ bignum *factorvm::bignum_trim(bignum * bignum) return (bignum); } -bignum *bignum_trim(bignum * bignum) -{ - return vm->bignum_trim(bignum); -} /* Copying */ @@ -1532,10 +1395,6 @@ bignum *factorvm::bignum_new_sign(bignum * x, int negative_p) return (result); } -bignum *bignum_new_sign(bignum * x, int negative_p) -{ - return vm->bignum_new_sign(x,negative_p); -} /* allocates memory */ bignum *factorvm::bignum_maybe_new_sign(bignum * x, int negative_p) @@ -1551,10 +1410,6 @@ bignum *factorvm::bignum_maybe_new_sign(bignum * x, int negative_p) } } -bignum *bignum_maybe_new_sign(bignum * x, int negative_p) -{ - return vm->bignum_maybe_new_sign(x,negative_p); -} void factorvm::bignum_destructive_copy(bignum * source, bignum * target) { @@ -1567,10 +1422,6 @@ void factorvm::bignum_destructive_copy(bignum * source, bignum * target) return; } -void bignum_destructive_copy(bignum * source, bignum * target) -{ - return vm->bignum_destructive_copy(source,target); -} /* * Added bitwise operations (and oddp). @@ -1582,10 +1433,6 @@ bignum *factorvm::bignum_bitwise_not(bignum * x) return bignum_subtract(BIGNUM_ONE(1), x); } -bignum *bignum_bitwise_not(bignum * x) -{ - return vm->bignum_bitwise_not(x); -} /* allocates memory */ bignum *factorvm::bignum_arithmetic_shift(bignum * arg1, fixnum n) @@ -1596,10 +1443,6 @@ bignum *factorvm::bignum_arithmetic_shift(bignum * arg1, fixnum n) return bignum_magnitude_ash(arg1, n); } -bignum *bignum_arithmetic_shift(bignum * arg1, fixnum n) -{ - return vm->bignum_arithmetic_shift(arg1,n); -} #define AND_OP 0 #define IOR_OP 1 @@ -1619,10 +1462,6 @@ bignum *factorvm::bignum_bitwise_and(bignum * arg1, bignum * arg2) ); } -bignum *bignum_bitwise_and(bignum * arg1, bignum * arg2) -{ - return vm->bignum_bitwise_and(arg1,arg2); -} /* allocates memory */ bignum *factorvm::bignum_bitwise_ior(bignum * arg1, bignum * arg2) @@ -1638,10 +1477,6 @@ bignum *factorvm::bignum_bitwise_ior(bignum * arg1, bignum * arg2) ); } -bignum *bignum_bitwise_ior(bignum * arg1, bignum * arg2) -{ - return vm->bignum_bitwise_ior(arg1,arg2); -} /* allocates memory */ bignum *factorvm::bignum_bitwise_xor(bignum * arg1, bignum * arg2) @@ -1657,10 +1492,6 @@ bignum *factorvm::bignum_bitwise_xor(bignum * arg1, bignum * arg2) ); } -bignum *bignum_bitwise_xor(bignum * arg1, bignum * arg2) -{ - return vm->bignum_bitwise_xor(arg1,arg2); -} /* allocates memory */ /* ash for the magnitude */ @@ -1725,10 +1556,6 @@ bignum *factorvm::bignum_magnitude_ash(bignum * arg1, fixnum n) return (bignum_trim (result)); } -bignum *bignum_magnitude_ash(bignum * arg1, fixnum n) -{ - return vm->bignum_magnitude_ash(arg1,n); -} /* allocates memory */ bignum *factorvm::bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) @@ -1764,10 +1591,6 @@ bignum *factorvm::bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) return bignum_trim(result); } -bignum *bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) -{ - return vm->bignum_pospos_bitwise_op(op,arg1,arg2); -} /* allocates memory */ bignum *factorvm::bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) @@ -1821,10 +1644,6 @@ bignum *factorvm::bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) return bignum_trim(result); } -bignum *bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) -{ - return vm->bignum_posneg_bitwise_op(op,arg1,arg2); -} /* allocates memory */ bignum *factorvm::bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) @@ -1886,10 +1705,6 @@ bignum *factorvm::bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) return bignum_trim(result); } -bignum *bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) -{ - return vm->bignum_negneg_bitwise_op(op,arg1,arg2); -} void factorvm::bignum_negate_magnitude(bignum * arg) { @@ -1918,10 +1733,6 @@ void factorvm::bignum_negate_magnitude(bignum * arg) } } -void bignum_negate_magnitude(bignum * arg) -{ - return vm->bignum_negate_magnitude(arg); -} /* Allocates memory */ bignum *factorvm::bignum_integer_length(bignum * x) @@ -1944,10 +1755,6 @@ bignum *factorvm::bignum_integer_length(bignum * x) return (bignum_trim (result)); } -bignum *bignum_integer_length(bignum * x) -{ - return vm->bignum_integer_length(x); -} /* Allocates memory */ int factorvm::bignum_logbitp(int shift, bignum * arg) @@ -1957,10 +1764,6 @@ int factorvm::bignum_logbitp(int shift, bignum * arg) : bignum_unsigned_logbitp (shift,arg)); } -int bignum_logbitp(int shift, bignum * arg) -{ - return vm->bignum_logbitp(shift,arg); -} int factorvm::bignum_unsigned_logbitp(int shift, bignum * bignum) { @@ -1974,10 +1777,6 @@ int factorvm::bignum_unsigned_logbitp(int shift, bignum * bignum) return (digit & mask) ? 1 : 0; } -int bignum_unsigned_logbitp(int shift, bignum * bignum) -{ - return vm->bignum_unsigned_logbitp(shift,bignum); -} /* Allocates memory */ bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm*), unsigned int radix, int negative_p) @@ -2016,9 +1815,5 @@ bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*p } } -bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm*), unsigned int radix, int negative_p) -{ - return vm->digit_stream_to_bignum(n_digits,producer,radix,negative_p); -} } diff --git a/vm/bignum.hpp b/vm/bignum.hpp index 8f21702f1a..efa050667b 100644 --- a/vm/bignum.hpp +++ b/vm/bignum.hpp @@ -44,81 +44,6 @@ enum bignum_comparison bignum_comparison_greater = 1 }; -int bignum_equal_p(bignum *, bignum *); -enum bignum_comparison bignum_compare(bignum *, bignum *); -bignum * bignum_add(bignum *, bignum *); -bignum * bignum_subtract(bignum *, bignum *); -bignum * bignum_negate(bignum *); -bignum * bignum_multiply(bignum *, bignum *); -void -bignum_divide(bignum * numerator, bignum * denominator, - bignum * * quotient, bignum * * remainder); -bignum * bignum_quotient(bignum *, bignum *); -bignum * bignum_remainder(bignum *, bignum *); -fixnum bignum_to_fixnum(bignum *); -cell bignum_to_cell(bignum *); -s64 bignum_to_long_long(bignum *); -u64 bignum_to_ulong_long(bignum *); -bignum * double_to_bignum(double); -double bignum_to_double(bignum *); - -/* Added bitwise operators. */ - -bignum * bignum_bitwise_not(bignum *); -bignum * bignum_arithmetic_shift(bignum *, fixnum); -bignum * bignum_bitwise_and(bignum *, bignum *); -bignum * bignum_bitwise_ior(bignum *, bignum *); -bignum * bignum_bitwise_xor(bignum *, bignum *); - -/* Forward references */ -int bignum_equal_p_unsigned(bignum *, bignum *); -enum bignum_comparison bignum_compare_unsigned(bignum *, bignum *); -bignum * bignum_add_unsigned(bignum *, bignum *, int); -bignum * bignum_subtract_unsigned(bignum *, bignum *); -bignum * bignum_multiply_unsigned(bignum *, bignum *, int); -bignum * bignum_multiply_unsigned_small_factor - (bignum *, bignum_digit_type, int); -void bignum_destructive_scale_up(bignum *, bignum_digit_type); -void bignum_destructive_add(bignum *, bignum_digit_type); -void bignum_divide_unsigned_large_denominator - (bignum *, bignum *, bignum * *, bignum * *, int, int); -void bignum_destructive_normalization(bignum *, bignum *, int); -void bignum_destructive_unnormalization(bignum *, int); -void bignum_divide_unsigned_normalized(bignum *, bignum *, bignum *); -bignum_digit_type bignum_divide_subtract - (bignum_digit_type *, bignum_digit_type *, bignum_digit_type, - bignum_digit_type *); -void bignum_divide_unsigned_medium_denominator - (bignum *, bignum_digit_type, bignum * *, bignum * *, int, int); -bignum_digit_type bignum_digit_divide - (bignum_digit_type, bignum_digit_type, bignum_digit_type, bignum_digit_type *); -bignum_digit_type bignum_digit_divide_subtract - (bignum_digit_type, bignum_digit_type, bignum_digit_type, bignum_digit_type *); -void bignum_divide_unsigned_small_denominator - (bignum *, bignum_digit_type, bignum * *, bignum * *, int, int); -bignum_digit_type bignum_destructive_scale_down - (bignum *, bignum_digit_type); -bignum * bignum_remainder_unsigned_small_denominator - (bignum *, bignum_digit_type, int); -bignum * bignum_digit_to_bignum(bignum_digit_type, int); -bignum * allot_bignum(bignum_length_type, int); -bignum * allot_bignum_zeroed(bignum_length_type, int); -bignum * bignum_shorten_length(bignum *, bignum_length_type); -bignum * bignum_trim(bignum *); -bignum * bignum_new_sign(bignum *, int); -bignum * bignum_maybe_new_sign(bignum *, int); -void bignum_destructive_copy(bignum *, bignum *); - -/* Added for bitwise operations. */ -bignum * bignum_magnitude_ash(bignum * arg1, fixnum n); -bignum * bignum_pospos_bitwise_op(int op, bignum *, bignum *); -bignum * bignum_posneg_bitwise_op(int op, bignum *, bignum *); -bignum * bignum_negneg_bitwise_op(int op, bignum *, bignum *); -void bignum_negate_magnitude(bignum *); - -bignum * bignum_integer_length(bignum * arg1); -int bignum_unsigned_logbitp(int shift, bignum * bignum); -int bignum_logbitp(int shift, bignum * arg); struct factorvm; bignum * digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int,factorvm*), diff --git a/vm/vm.hpp b/vm/vm.hpp index a4c58a2a0b..f259096766 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -72,6 +72,10 @@ struct factorvm { void bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder); bignum *bignum_quotient(bignum * numerator, bignum * denominator); bignum *bignum_remainder(bignum * numerator, bignum * denominator); + cell bignum_to_cell(bignum * bignum); + fixnum bignum_to_fixnum(bignum * bignum); + s64 bignum_to_long_long(bignum * bignum); + u64 bignum_to_ulong_long(bignum * bignum); double bignum_to_double(bignum * bignum); bignum *double_to_bignum(double x); int bignum_equal_p_unsigned(bignum * x, bignum * y); From 959da30f05f306e3b1129aead6220329b713aa89 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:17 +0100 Subject: [PATCH 158/345] removed global functions from code_block.cpp --- vm/code_block.cpp | 92 ----------------------------------------------- vm/code_block.hpp | 22 +----------- vm/jit.cpp | 2 +- 3 files changed, 2 insertions(+), 114 deletions(-) diff --git a/vm/code_block.cpp b/vm/code_block.cpp index b8d4621d38..f19759fb0e 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -8,40 +8,24 @@ relocation_type factorvm::relocation_type_of(relocation_entry r) return (relocation_type)((r & 0xf0000000) >> 28); } -relocation_type relocation_type_of(relocation_entry r) -{ - return vm->relocation_type_of(r); -} relocation_class factorvm::relocation_class_of(relocation_entry r) { return (relocation_class)((r & 0x0f000000) >> 24); } -relocation_class relocation_class_of(relocation_entry r) -{ - return vm->relocation_class_of(r); -} cell factorvm::relocation_offset_of(relocation_entry r) { return (r & 0x00ffffff); } -cell relocation_offset_of(relocation_entry r) -{ - return vm->relocation_offset_of(r); -} void factorvm::flush_icache_for(code_block *block) { flush_icache((cell)block,block->size); } -void flush_icache_for(code_block *block) -{ - return vm->flush_icache_for(block); -} int factorvm::number_of_parameters(relocation_type type) { @@ -67,10 +51,6 @@ int factorvm::number_of_parameters(relocation_type type) } } -int number_of_parameters(relocation_type type) -{ - return vm->number_of_parameters(type); -} void *factorvm::object_xt(cell obj) { @@ -86,10 +66,6 @@ void *factorvm::object_xt(cell obj) } } -void *object_xt(cell obj) -{ - return vm->object_xt(obj); -} void *factorvm::xt_pic(word *w, cell tagged_quot) { @@ -105,30 +81,18 @@ void *factorvm::xt_pic(word *w, cell tagged_quot) } } -void *xt_pic(word *w, cell tagged_quot) -{ - return vm->xt_pic(w,tagged_quot); -} void *factorvm::word_xt_pic(word *w) { return xt_pic(w,w->pic_def); } -void *word_xt_pic(word *w) -{ - return vm->word_xt_pic(w); -} void *factorvm::word_xt_pic_tail(word *w) { return xt_pic(w,w->pic_tail_def); } -void *word_xt_pic_tail(word *w) -{ - return vm->word_xt_pic_tail(w); -} /* References to undefined symbols are patched up to call this function on image load */ @@ -187,10 +151,6 @@ void *factorvm::get_rel_symbol(array *literals, cell index) } } -void *get_rel_symbol(array *literals, cell index) -{ - return vm->get_rel_symbol(literals,index); -} cell factorvm::compute_relocation(relocation_entry rel, cell index, code_block *compiled) { @@ -234,10 +194,6 @@ cell factorvm::compute_relocation(relocation_entry rel, cell index, code_block * #undef ARG } -cell compute_relocation(relocation_entry rel, cell index, code_block *compiled) -{ - return vm->compute_relocation(rel,index,compiled); -} void factorvm::iterate_relocations(code_block *compiled, relocation_iterator iter) { @@ -257,10 +213,6 @@ void factorvm::iterate_relocations(code_block *compiled, relocation_iterator ite } } -void iterate_relocations(code_block *compiled, relocation_iterator iter) -{ - return vm->iterate_relocations(compiled,iter); -} /* Store a 32-bit value into a PowerPC LIS/ORI sequence */ void factorvm::store_address_2_2(cell *ptr, cell value) @@ -269,10 +221,6 @@ void factorvm::store_address_2_2(cell *ptr, cell value) ptr[ 0] = ((ptr[ 0] & ~0xffff) | (value & 0xffff)); } -void store_address_2_2(cell *ptr, cell value) -{ - return vm->store_address_2_2(ptr,value); -} /* Store a value into a bitfield of a PowerPC instruction */ void factorvm::store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift) @@ -285,10 +233,6 @@ void factorvm::store_address_masked(cell *ptr, fixnum value, cell mask, fixnum s *ptr = ((*ptr & ~mask) | ((value >> shift) & mask)); } -void store_address_masked(cell *ptr, fixnum value, cell mask, fixnum shift) -{ - return vm->store_address_masked(ptr,value,mask,shift); -} /* Perform a fixup on a code block */ void factorvm::store_address_in_code_block(cell klass, cell offset, fixnum absolute_value) @@ -336,10 +280,6 @@ void factorvm::store_address_in_code_block(cell klass, cell offset, fixnum absol } } -void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value) -{ - return vm->store_address_in_code_block(klass,offset,absolute_value); -} void factorvm::update_literal_references_step(relocation_entry rel, cell index, code_block *compiled) { @@ -367,10 +307,6 @@ void factorvm::update_literal_references(code_block *compiled) } } -void update_literal_references(code_block *compiled) -{ - return vm->update_literal_references(compiled); -} /* Copy all literals referenced from a code block to newspace. Only for aging and nursery collections */ @@ -479,10 +415,6 @@ void factorvm::check_code_address(cell address) #endif } -void check_code_address(cell address) -{ - return vm->check_code_address(address); -} /* Update references to words. This is done after a new code block is added to the heap. */ @@ -499,10 +431,6 @@ void factorvm::mark_code_block(code_block *compiled) copy_handle(&compiled->relocation); } -void mark_code_block(code_block *compiled) -{ - return vm->mark_code_block(compiled); -} void factorvm::mark_stack_frame_step(stack_frame *frame) { @@ -526,10 +454,6 @@ void factorvm::mark_active_blocks(context *stacks) } } -void mark_active_blocks(context *stacks) -{ - return vm->mark_active_blocks(stacks); -} void factorvm::mark_object_code_block(object *object) { @@ -560,10 +484,6 @@ void factorvm::mark_object_code_block(object *object) } } -void mark_object_code_block(object *object) -{ - return vm->mark_object_code_block(object); -} /* Perform all fixups on a code block */ void factorvm::relocate_code_block(code_block *compiled) @@ -597,10 +517,6 @@ void factorvm::fixup_labels(array *labels, code_block *compiled) } } -void fixup_labels(array *labels, code_block *compiled) -{ - return vm->fixup_labels(labels,compiled); -} /* Might GC */ code_block *factorvm::allot_code_block(cell size) @@ -630,10 +546,6 @@ code_block *factorvm::allot_code_block(cell size) return (code_block *)block; } -code_block *allot_code_block(cell size) -{ - return vm->allot_code_block(size); -} /* Might GC */ code_block *factorvm::add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_) @@ -672,9 +584,5 @@ code_block *factorvm::add_code_block(cell type,cell code_,cell labels_,cell relo return compiled; } -code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_) -{ - return vm->add_code_block(type,code_,labels_,relocation_,literals_); -} } diff --git a/vm/code_block.hpp b/vm/code_block.hpp index 0addaeb854..60046ed380 100644 --- a/vm/code_block.hpp +++ b/vm/code_block.hpp @@ -60,39 +60,19 @@ static const cell rel_relative_arm_3_mask = 0xffffff; /* code relocation table consists of a table of entries for each fixup */ typedef u32 relocation_entry; -void flush_icache_for(code_block *compiled); - struct factorvm; typedef void (*relocation_iterator)(relocation_entry rel, cell index, code_block *compiled, factorvm *vm); -void iterate_relocations(code_block *compiled, relocation_iterator iter); - -void store_address_in_code_block(cell klass, cell offset, fixnum absolute_value); - +// callback functions void relocate_code_block(code_block *compiled, factorvm *myvm); - -void update_literal_references(code_block *compiled); - void copy_literal_references(code_block *compiled, factorvm *myvm); - void update_word_references(code_block *compiled, factorvm *myvm); - void update_literal_and_word_references(code_block *compiled, factorvm *myvm); -void mark_code_block(code_block *compiled); - -void mark_active_blocks(context *stacks); - -void mark_object_code_block(object *scan); - -void relocate_code_block(code_block *relocating); - inline bool stack_traces_p() { return userenv[STACK_TRACES_ENV] != F; } -code_block *add_code_block(cell type, cell code, cell labels, cell relocation, cell literals); - } diff --git a/vm/jit.cpp b/vm/jit.cpp index 9757e54dc4..7177f26073 100644 --- a/vm/jit.cpp +++ b/vm/jit.cpp @@ -102,7 +102,7 @@ code_block *jit::to_code_block() relocation.trim(); literals.trim(); - return add_code_block( + return myvm->add_code_block( type, code.elements.value(), F, /* no labels */ From 5a0c4d18aa3779777c66b1e0639b12dbfddf29eb Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:17 +0100 Subject: [PATCH 159/345] removed global functions from debug.cpp --- vm/debug.cpp | 76 ---------------------------------------------------- vm/debug.hpp | 5 ---- 2 files changed, 81 deletions(-) mode change 100644 => 100755 vm/debug.hpp diff --git a/vm/debug.cpp b/vm/debug.cpp index eb64ad22d4..6009e922f7 100755 --- a/vm/debug.cpp +++ b/vm/debug.cpp @@ -11,10 +11,6 @@ void factorvm::print_chars(string* str) putchar(string_nth(str,i)); } -void print_chars(string* str) -{ - return vm->print_chars(str); -} void factorvm::print_word(word* word, cell nesting) { @@ -34,10 +30,6 @@ void factorvm::print_word(word* word, cell nesting) } } -void print_word(word* word, cell nesting) -{ - return vm->print_word(word,nesting); -} void factorvm::print_factor_string(string* str) { @@ -46,10 +38,6 @@ void factorvm::print_factor_string(string* str) putchar('"'); } -void print_factor_string(string* str) -{ - return vm->print_factor_string(str); -} void factorvm::print_array(array* array, cell nesting) { @@ -75,10 +63,6 @@ void factorvm::print_array(array* array, cell nesting) print_string("..."); } -void print_array(array* array, cell nesting) -{ - return vm->print_array(array,nesting); -} void factorvm::print_tuple(tuple *tuple, cell nesting) { @@ -109,10 +93,6 @@ void factorvm::print_tuple(tuple *tuple, cell nesting) print_string("..."); } -void print_tuple(tuple *tuple, cell nesting) -{ - return vm->print_tuple(tuple,nesting); -} void factorvm::print_nested_obj(cell obj, fixnum nesting) { @@ -164,20 +144,12 @@ void factorvm::print_nested_obj(cell obj, fixnum nesting) } } -void print_nested_obj(cell obj, fixnum nesting) -{ - return vm->print_nested_obj(obj,nesting); -} void factorvm::print_obj(cell obj) { print_nested_obj(obj,10); } -void print_obj(cell obj) -{ - return vm->print_obj(obj); -} void factorvm::print_objects(cell *start, cell *end) { @@ -188,10 +160,6 @@ void factorvm::print_objects(cell *start, cell *end) } } -void print_objects(cell *start, cell *end) -{ - return vm->print_objects(start,end); -} void factorvm::print_datastack() { @@ -199,10 +167,6 @@ void factorvm::print_datastack() print_objects((cell *)ds_bot,(cell *)ds); } -void print_datastack() -{ - return vm->print_datastack(); -} void factorvm::print_retainstack() { @@ -210,10 +174,6 @@ void factorvm::print_retainstack() print_objects((cell *)rs_bot,(cell *)rs); } -void print_retainstack() -{ - return vm->print_retainstack(); -} void factorvm::print_stack_frame(stack_frame *frame) { @@ -245,10 +205,6 @@ void factorvm::print_callstack() iterate_callstack(top,bottom,factor::print_stack_frame); } -void print_callstack() -{ - return vm->print_callstack(); -} void factorvm::dump_cell(cell x) { @@ -258,10 +214,6 @@ void factorvm::dump_cell(cell x) nl(); } -void dump_cell(cell x) -{ - return vm->dump_cell(x); -} void factorvm::dump_memory(cell from, cell to) { @@ -271,10 +223,6 @@ void factorvm::dump_memory(cell from, cell to) dump_cell(from); } -void dump_memory(cell from, cell to) -{ - return vm->dump_memory(from,to); -} void factorvm::dump_zone(zone *z) { @@ -283,10 +231,6 @@ void factorvm::dump_zone(zone *z) print_string(", here="); print_cell(z->here - z->start); nl(); } -void dump_zone(zone *z) -{ - return vm->dump_zone(z); -} void factorvm::dump_generations() { @@ -314,10 +258,6 @@ void factorvm::dump_generations() nl(); } -void dump_generations() -{ - return vm->dump_generations(); -} void factorvm::dump_objects(cell type) { @@ -339,10 +279,6 @@ void factorvm::dump_objects(cell type) end_scan(); } -void dump_objects(cell type) -{ - return vm->dump_objects(type); -} void factorvm::find_data_references_step(cell *scan) @@ -373,10 +309,6 @@ void factorvm::find_data_references(cell look_for_) end_scan(); } -void find_data_references(cell look_for_) -{ - return vm->find_data_references(look_for_); -} /* Dump all code blocks for debugging */ void factorvm::dump_code_heap() @@ -419,10 +351,6 @@ void factorvm::dump_code_heap() print_cell(literal_size); print_string(" bytes of literal data\n"); } -void dump_code_heap() -{ - return vm->dump_code_heap(); -} void factorvm::factorbug() { @@ -568,10 +496,6 @@ void factorvm::factorbug() } } -void factorbug() -{ - return vm->factorbug(); -} inline void factorvm::vmprim_die() { diff --git a/vm/debug.hpp b/vm/debug.hpp old mode 100644 new mode 100755 index cb84c9256c..48566f1b2d --- a/vm/debug.hpp +++ b/vm/debug.hpp @@ -1,11 +1,6 @@ namespace factor { -void print_obj(cell obj); -void print_nested_obj(cell obj, fixnum nesting); -void dump_generations(); -void factorbug(); -void dump_zone(zone *z); PRIMITIVE(die); From d21b1b2e1ec5a9756271a52794f3ad860a175c21 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:17 +0100 Subject: [PATCH 160/345] removed global functions from arrays.cpp --- vm/arrays.cpp | 16 ---------------- vm/arrays.hpp | 10 ---------- vm/inlineimpls.hpp | 2 +- 3 files changed, 1 insertion(+), 27 deletions(-) diff --git a/vm/arrays.cpp b/vm/arrays.cpp index 8d964c4d21..62992c4e7c 100644 --- a/vm/arrays.cpp +++ b/vm/arrays.cpp @@ -23,10 +23,6 @@ array *factorvm::allot_array(cell capacity, cell fill_) return new_array.untagged(); } -array *allot_array(cell capacity, cell fill_) -{ - return vm->allot_array(capacity,fill_); -} /* push a new array on the stack */ inline void factorvm::vmprim_array() @@ -49,10 +45,6 @@ cell factorvm::allot_array_1(cell obj_) return a.value(); } -cell allot_array_1(cell obj_) -{ - return vm->allot_array_1(obj_); -} cell factorvm::allot_array_2(cell v1_, cell v2_) { @@ -64,10 +56,6 @@ cell factorvm::allot_array_2(cell v1_, cell v2_) return a.value(); } -cell allot_array_2(cell v1_, cell v2_) -{ - return vm->allot_array_2(v1_,v2_); -} cell factorvm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) { @@ -83,10 +71,6 @@ cell factorvm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) return a.value(); } -cell allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) -{ - return vm->allot_array_4(v1_,v2_,v3_,v4_); -} inline void factorvm::vmprim_resize_array() { diff --git a/vm/arrays.hpp b/vm/arrays.hpp index 282474ade8..e3eaccfba3 100755 --- a/vm/arrays.hpp +++ b/vm/arrays.hpp @@ -10,16 +10,6 @@ inline cell array_nth(array *array, cell slot) return array->data()[slot]; } - - - - -array *allot_array(cell capacity, cell fill); - -cell allot_array_1(cell obj); -cell allot_array_2(cell v1, cell v2); -cell allot_array_4(cell v1, cell v2, cell v3, cell v4); - PRIMITIVE(array); PRIMITIVE(resize_array); diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index 241c958b2c..3932177a72 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -347,7 +347,7 @@ struct growable_array { cell count; gc_root elements; - growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(allot_array(capacity,F),myvm) {} + growable_array(factorvm *myvm, cell capacity = 10) : count(0), elements(myvm->allot_array(capacity,F),myvm) {} void add(cell elt); void trim(); From 9e2d40a228b40c2c5d772c5d5dfbe9f3edfc0524 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:17 +0100 Subject: [PATCH 161/345] removed global functions from strings.cpp --- vm/strings.cpp | 36 ------------------------------------ vm/strings.hpp | 7 ------- 2 files changed, 43 deletions(-) diff --git a/vm/strings.cpp b/vm/strings.cpp index a8962ee921..82db8430eb 100644 --- a/vm/strings.cpp +++ b/vm/strings.cpp @@ -22,20 +22,12 @@ cell factorvm::string_nth(string* str, cell index) } } -cell string_nth(string* str, cell index) -{ - return vm->string_nth(str,index); -} void factorvm::set_string_nth_fast(string *str, cell index, cell ch) { str->data()[index] = ch; } -void set_string_nth_fast(string *str, cell index, cell ch) -{ - return vm->set_string_nth_fast(str,index,ch); -} void factorvm::set_string_nth_slow(string *str_, cell index, cell ch) { @@ -64,10 +56,6 @@ void factorvm::set_string_nth_slow(string *str_, cell index, cell ch) aux->data()[index] = ((ch >> 7) ^ 1); } -void set_string_nth_slow(string *str_, cell index, cell ch) -{ - return vm->set_string_nth_slow(str_,index,ch); -} /* allocates memory */ void factorvm::set_string_nth(string *str, cell index, cell ch) @@ -78,10 +66,6 @@ void factorvm::set_string_nth(string *str, cell index, cell ch) set_string_nth_slow(str,index,ch); } -void set_string_nth(string *str, cell index, cell ch) -{ - return vm->set_string_nth(str,index,ch); -} /* Allocates memory */ string *factorvm::allot_string_internal(cell capacity) @@ -95,10 +79,6 @@ string *factorvm::allot_string_internal(cell capacity) return str; } -string *allot_string_internal(cell capacity) -{ - return vm->allot_string_internal(capacity); -} /* Allocates memory */ void factorvm::fill_string(string *str_, cell start, cell capacity, cell fill) @@ -116,10 +96,6 @@ void factorvm::fill_string(string *str_, cell start, cell capacity, cell fill) } } -void fill_string(string *str_, cell start, cell capacity, cell fill) -{ - return vm->fill_string(str_,start,capacity,fill); -} /* Allocates memory */ string *factorvm::allot_string(cell capacity, cell fill) @@ -129,10 +105,6 @@ string *factorvm::allot_string(cell capacity, cell fill) return str.untagged(); } -string *allot_string(cell capacity, cell fill) -{ - return vm->allot_string(capacity,fill); -} inline void factorvm::vmprim_string() { @@ -153,10 +125,6 @@ bool factorvm::reallot_string_in_place_p(string *str, cell capacity) && capacity <= string_capacity(str); } -bool reallot_string_in_place_p(string *str, cell capacity) -{ - return vm->reallot_string_in_place_p(str,capacity); -} string* factorvm::reallot_string(string *str_, cell capacity) { @@ -200,10 +168,6 @@ string* factorvm::reallot_string(string *str_, cell capacity) } } -string* reallot_string(string *str_, cell capacity) -{ - return vm->reallot_string(str_,capacity); -} inline void factorvm::vmprim_resize_string() { diff --git a/vm/strings.hpp b/vm/strings.hpp index 9a082b0b83..87beb9a0a8 100644 --- a/vm/strings.hpp +++ b/vm/strings.hpp @@ -11,16 +11,9 @@ inline static cell string_size(cell size) return sizeof(string) + size; } -string* allot_string_internal(cell capacity); -string* allot_string(cell capacity, cell fill); PRIMITIVE(string); -string *reallot_string(string *string, cell capacity); PRIMITIVE(resize_string); -/* String getters and setters */ -cell string_nth(string* string, cell index); -void set_string_nth(string* string, cell index, cell value); - PRIMITIVE(string_nth); PRIMITIVE(set_string_nth_slow); PRIMITIVE(set_string_nth_fast); From 10bf5ca17c35b2cad76b8c5b50e598fa04202e61 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:17 +0100 Subject: [PATCH 162/345] removed global functions from byte_arrays.cpp and tuples.cpp --- vm/byte_arrays.cpp | 4 ---- vm/byte_arrays.hpp | 2 -- vm/inlineimpls.hpp | 2 +- vm/tuples.cpp | 5 ----- 4 files changed, 1 insertion(+), 12 deletions(-) diff --git a/vm/byte_arrays.cpp b/vm/byte_arrays.cpp index 0f4591a89c..4a197d8452 100644 --- a/vm/byte_arrays.cpp +++ b/vm/byte_arrays.cpp @@ -10,10 +10,6 @@ byte_array *factorvm::allot_byte_array(cell size) return array; } -byte_array *allot_byte_array(cell size) -{ - return vm->allot_byte_array(size); -} inline void factorvm::vmprim_byte_array() { diff --git a/vm/byte_arrays.hpp b/vm/byte_arrays.hpp index 4954f31094..c1adcd95f0 100755 --- a/vm/byte_arrays.hpp +++ b/vm/byte_arrays.hpp @@ -1,8 +1,6 @@ namespace factor { -byte_array *allot_byte_array(cell size); - PRIMITIVE(byte_array); PRIMITIVE(uninitialized_byte_array); PRIMITIVE(resize_byte_array); diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index 3932177a72..90ccd8d869 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -358,7 +358,7 @@ struct growable_byte_array { cell count; gc_root elements; - growable_byte_array(factorvm *vm,cell capacity = 40) : count(0), elements(allot_byte_array(capacity),vm) { } + growable_byte_array(factorvm *myvm,cell capacity = 40) : count(0), elements(myvm->allot_byte_array(capacity),myvm) { } void append_bytes(void *elts, cell len); void append_byte_array(cell elts); diff --git a/vm/tuples.cpp b/vm/tuples.cpp index 5c4c6e17b0..520bc55d4d 100644 --- a/vm/tuples.cpp +++ b/vm/tuples.cpp @@ -12,11 +12,6 @@ tuple *factorvm::allot_tuple(cell layout_) return t.untagged(); } -tuple *allot_tuple(cell layout_) -{ - return vm->allot_tuple(layout_); -} - inline void factorvm::vmprim_tuple() { gc_root layout(dpop(),this); From 2dba15535f82997762937dfbba9d47326ad69be5 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:18 +0100 Subject: [PATCH 163/345] removed global functions from words.cpp --- vm/words.cpp | 10 ---------- vm/words.hpp | 4 ---- 2 files changed, 14 deletions(-) diff --git a/vm/words.cpp b/vm/words.cpp index 68b6afd9d0..f3c511efe9 100644 --- a/vm/words.cpp +++ b/vm/words.cpp @@ -31,11 +31,6 @@ word *factorvm::allot_word(cell vocab_, cell name_) return new_word.untagged(); } -word *allot_word(cell vocab_, cell name_) -{ - return vm->allot_word(vocab_,name_); -} - /* ( name vocabulary -- word ) */ inline void factorvm::vmprim_word() { @@ -79,11 +74,6 @@ void factorvm::update_word_xt(cell w_) w->xt = w->code->xt(); } -void update_word_xt(cell w_) -{ - return vm->update_word_xt(w_); -} - inline void factorvm::vmprim_optimized_p() { drepl(tag_boolean(word_optimized_p(untag_check(dpeek())))); diff --git a/vm/words.hpp b/vm/words.hpp index f9d5a7aff4..d3be2bde07 100644 --- a/vm/words.hpp +++ b/vm/words.hpp @@ -1,11 +1,8 @@ namespace factor { -word *allot_word(cell vocab, cell name); - PRIMITIVE(word); PRIMITIVE(word_xt); -void update_word_xt(cell word); inline bool word_optimized_p(word *word) { @@ -13,7 +10,6 @@ inline bool word_optimized_p(word *word) } PRIMITIVE(optimized_p); - PRIMITIVE(wrapper); } From fc5c51e2cdfa9ae8530c5c7f887dd7b8459226f8 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:18 +0100 Subject: [PATCH 164/345] removed some global functions from math.cpp --- vm/math.cpp | 16 ---------------- vm/math.hpp | 2 -- 2 files changed, 18 deletions(-) diff --git a/vm/math.cpp b/vm/math.cpp index 98188059f6..0a46252d7f 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -71,30 +71,18 @@ inline fixnum factorvm::sign_mask(fixnum x) return x >> (WORD_SIZE - 1); } -inline fixnum sign_mask(fixnum x) -{ - return vm->sign_mask(x); -} inline fixnum factorvm::branchless_max(fixnum x, fixnum y) { return (x - ((x - y) & sign_mask(x - y))); } -inline fixnum branchless_max(fixnum x, fixnum y) -{ - return vm->branchless_max(x,y); -} inline fixnum factorvm::branchless_abs(fixnum x) { return (x ^ sign_mask(x)) - sign_mask(x); } -inline fixnum branchless_abs(fixnum x) -{ - return vm->branchless_abs(x); -} inline void factorvm::vmprim_fixnum_shift() { @@ -410,10 +398,6 @@ cell factorvm::unbox_array_size() return 0; /* can't happen */ } -cell unbox_array_size() -{ - return vm->unbox_array_size(); -} inline void factorvm::vmprim_fixnum_to_float() { diff --git a/vm/math.hpp b/vm/math.hpp index 4633721194..e8347fe0e2 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -38,8 +38,6 @@ PRIMITIVE(bignum_bitp); PRIMITIVE(bignum_log2); PRIMITIVE(byte_array_to_bignum); -cell unbox_array_size(); - PRIMITIVE(fixnum_to_float); PRIMITIVE(bignum_to_float); PRIMITIVE(str_to_float); From 0397f92569206005050396cfd9277d3c903cc6cf Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:18 +0100 Subject: [PATCH 165/345] removed some global functions from io.cpp --- vm/io.cpp | 8 -------- vm/io.hpp | 3 --- 2 files changed, 11 deletions(-) mode change 100644 => 100755 vm/io.hpp diff --git a/vm/io.cpp b/vm/io.cpp index cfbafda907..47d964eaad 100755 --- a/vm/io.cpp +++ b/vm/io.cpp @@ -21,10 +21,6 @@ void factorvm::init_c_io() userenv[STDERR_ENV] = allot_alien(F,(cell)stderr); } -void init_c_io() -{ - return vm->init_c_io(); -} void factorvm::io_error() { @@ -36,10 +32,6 @@ void factorvm::io_error() general_error(ERROR_IO,tag_fixnum(errno),F,NULL); } -void io_error() -{ - return vm->io_error(); -} inline void factorvm::vmprim_fopen() { diff --git a/vm/io.hpp b/vm/io.hpp old mode 100644 new mode 100755 index d94d6402d9..1b5e281b54 --- a/vm/io.hpp +++ b/vm/io.hpp @@ -1,9 +1,6 @@ namespace factor { -void init_c_io(); -void io_error(); - PRIMITIVE(fopen); PRIMITIVE(fgetc); PRIMITIVE(fread); From 1887a16ca399bd96821bb34b8a33747a70bf7993 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:18 +0100 Subject: [PATCH 166/345] removed some global functions from image.cpp --- vm/image.cpp | 52 ---------------------------------------------------- vm/image.hpp | 3 --- 2 files changed, 55 deletions(-) mode change 100644 => 100755 vm/image.hpp diff --git a/vm/image.cpp b/vm/image.cpp index 5ceefdfeb4..747e0cc37e 100755 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -14,10 +14,6 @@ void factorvm::init_objects(image_header *h) bignum_neg_one = h->bignum_neg_one; } -void init_objects(image_header *h) -{ - return vm->init_objects(h); -} void factorvm::load_data_heap(FILE *file, image_header *h, vm_parameters *p) @@ -53,10 +49,6 @@ void factorvm::load_data_heap(FILE *file, image_header *h, vm_parameters *p) data_relocation_base = h->data_relocation_base; } -void load_data_heap(FILE *file, image_header *h, vm_parameters *p) -{ - return vm->load_data_heap(file,h,p); -} void factorvm::load_code_heap(FILE *file, image_header *h, vm_parameters *p) @@ -84,10 +76,6 @@ void factorvm::load_code_heap(FILE *file, image_header *h, vm_parameters *p) build_free_list(&code,h->code_size); } -void load_code_heap(FILE *file, image_header *h, vm_parameters *p) -{ - return vm->load_code_heap(file,h,p); -} /* Save the current image to disk */ bool factorvm::save_image(const vm_char *filename) @@ -135,10 +123,6 @@ bool factorvm::save_image(const vm_char *filename) return ok; } -bool save_image(const vm_char *filename) -{ - return vm->save_image(filename); -} inline void factorvm::vmprim_save_image() { @@ -207,10 +191,6 @@ template void factorvm::code_fixup(TYPE **handle) *handle = new_ptr; } -template void code_fixup(TYPE **handle) -{ - return vm->code_fixup(handle); -} void factorvm::fixup_word(word *word) { @@ -221,10 +201,6 @@ void factorvm::fixup_word(word *word) code_fixup(&word->xt); } -void fixup_word(word *word) -{ - return vm->fixup_word(word); -} void factorvm::fixup_quotation(quotation *quot) { @@ -237,20 +213,12 @@ void factorvm::fixup_quotation(quotation *quot) quot->xt = (void *)lazy_jit_compile; } -void fixup_quotation(quotation *quot) -{ - return vm->fixup_quotation(quot); -} void factorvm::fixup_alien(alien *d) { d->expired = T; } -void fixup_alien(alien *d) -{ - return vm->fixup_alien(d); -} void factorvm::fixup_stack_frame(stack_frame *frame) { @@ -268,10 +236,6 @@ void factorvm::fixup_callstack_object(callstack *stack) iterate_callstack_object(stack,factor::fixup_stack_frame); } -void fixup_callstack_object(callstack *stack) -{ - return vm->fixup_callstack_object(stack); -} /* Initialize an object in a newly-loaded image */ void factorvm::relocate_object(object *object) @@ -317,10 +281,6 @@ void factorvm::relocate_object(object *object) } } -void relocate_object(object *object) -{ - return vm->relocate_object(object); -} /* Since the image might have been saved with a different base address than where it is loaded, we need to fix up pointers in the image. */ @@ -349,10 +309,6 @@ void factorvm::relocate_data() } } -void relocate_data() -{ - return vm->relocate_data(); -} void factorvm::fixup_code_block(code_block *compiled) { @@ -373,10 +329,6 @@ void factorvm::relocate_code() iterate_code_heap(factor::fixup_code_block); } -void relocate_code() -{ - return vm->relocate_code(); -} /* Read an image file from disk, only done once during startup */ /* This function also initializes the data and code heaps */ @@ -414,9 +366,5 @@ void factorvm::load_image(vm_parameters *p) userenv[IMAGE_ENV] = allot_alien(F,(cell)p->image_path); } -void load_image(vm_parameters *p) -{ - return vm->load_image(p); -} } diff --git a/vm/image.hpp b/vm/image.hpp old mode 100644 new mode 100755 index 807a7a6bcf..eab0343716 --- a/vm/image.hpp +++ b/vm/image.hpp @@ -41,9 +41,6 @@ struct vm_parameters { cell max_pic_size; }; -void load_image(vm_parameters *p); -bool save_image(const vm_char *file); - PRIMITIVE(save_image); PRIMITIVE(save_image_and_exit); From 551a800d2f71ca5c9f06fdb17fa1c31f9d182e9a Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:18 +0100 Subject: [PATCH 167/345] removed some global functions from callstack.cpp --- vm/callstack.cpp | 58 ++---------------------------------------------- vm/callstack.hpp | 7 ------ 2 files changed, 2 insertions(+), 63 deletions(-) diff --git a/vm/callstack.cpp b/vm/callstack.cpp index c330e38064..676e4260c9 100755 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -11,11 +11,6 @@ void factorvm::check_frame(stack_frame *frame) #endif } -void check_frame(stack_frame *frame) -{ - return vm->check_frame(frame); -} - callstack *factorvm::allot_callstack(cell size) { callstack *stack = allot(callstack_size(size)); @@ -23,11 +18,6 @@ callstack *factorvm::allot_callstack(cell size) return stack; } -callstack *allot_callstack(cell size) -{ - return vm->allot_callstack(size); -} - stack_frame *factorvm::fix_callstack_top(stack_frame *top, stack_frame *bottom) { stack_frame *frame = bottom - 1; @@ -38,11 +28,6 @@ stack_frame *factorvm::fix_callstack_top(stack_frame *top, stack_frame *bottom) return frame + 1; } -stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom) -{ - return vm->fix_callstack_top(top,bottom); -} - /* We ignore the topmost frame, the one calling 'callstack', so that set-callstack doesn't get stuck in an infinite loop. @@ -61,11 +46,6 @@ stack_frame *factorvm::capture_start() return frame + 1; } -stack_frame *capture_start() -{ - return vm->capture_start(); -} - inline void factorvm::vmprim_callstack() { stack_frame *top = capture_start(); @@ -109,21 +89,12 @@ code_block *factorvm::frame_code(stack_frame *frame) return (code_block *)frame->xt - 1; } -code_block *frame_code(stack_frame *frame) -{ - return vm->frame_code(frame); -} cell factorvm::frame_type(stack_frame *frame) { return frame_code(frame)->type; } -cell frame_type(stack_frame *frame) -{ - return vm->frame_type(frame); -} - cell factorvm::frame_executing(stack_frame *frame) { code_block *compiled = frame_code(frame); @@ -138,22 +109,12 @@ cell factorvm::frame_executing(stack_frame *frame) } } -cell frame_executing(stack_frame *frame) -{ - return vm->frame_executing(frame); -} - stack_frame *factorvm::frame_successor(stack_frame *frame) { check_frame(frame); return (stack_frame *)((cell)frame - frame->size); } -stack_frame *frame_successor(stack_frame *frame) -{ - return vm->frame_successor(frame); -} - /* Allocates memory */ cell factorvm::frame_scan(stack_frame *frame) { @@ -181,11 +142,6 @@ cell factorvm::frame_scan(stack_frame *frame) } } -cell frame_scan(stack_frame *frame) -{ - return vm->frame_scan(frame); -} - namespace { @@ -196,8 +152,8 @@ struct stack_frame_accumulator { void operator()(stack_frame *frame, factorvm *myvm) { - gc_root executing(frame_executing(frame),myvm); - gc_root scan(frame_scan(frame),myvm); + gc_root executing(myvm->frame_executing(frame),myvm); + gc_root scan(myvm->frame_scan(frame),myvm); frames.add(executing.value()); frames.add(scan.value()); @@ -234,11 +190,6 @@ stack_frame *factorvm::innermost_stack_frame(callstack *stack) return frame; } -stack_frame *innermost_stack_frame(callstack *stack) -{ - return vm->innermost_stack_frame(stack); -} - stack_frame *factorvm::innermost_stack_frame_quot(callstack *callstack) { stack_frame *inner = innermost_stack_frame(callstack); @@ -246,11 +197,6 @@ stack_frame *factorvm::innermost_stack_frame_quot(callstack *callstack) return inner; } -stack_frame *innermost_stack_frame_quot(callstack *callstack) -{ - return vm->innermost_stack_frame_quot(callstack); -} - /* Some primitives implementing a limited form of callstack mutation. Used by the single stepper. */ inline void factorvm::vmprim_innermost_stack_frame_executing() diff --git a/vm/callstack.hpp b/vm/callstack.hpp index 82fb93a1bc..406d8e7154 100755 --- a/vm/callstack.hpp +++ b/vm/callstack.hpp @@ -6,13 +6,6 @@ inline static cell callstack_size(cell size) return sizeof(callstack) + size; } -stack_frame *fix_callstack_top(stack_frame *top, stack_frame *bottom); -stack_frame *frame_successor(stack_frame *frame); -code_block *frame_code(stack_frame *frame); -cell frame_executing(stack_frame *frame); -cell frame_scan(stack_frame *frame); -cell frame_type(stack_frame *frame); - PRIMITIVE(callstack); PRIMITIVE(set_callstack); PRIMITIVE(callstack_to_array); From 7f70b6320c3dbcea833ea892947ea74a66eb92c3 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:18 +0100 Subject: [PATCH 168/345] removed some global functions from alien.cpp --- vm/alien.cpp | 17 ++++------------- vm/alien.hpp | 2 -- 2 files changed, 4 insertions(+), 15 deletions(-) mode change 100644 => 100755 vm/alien.hpp diff --git a/vm/alien.cpp b/vm/alien.cpp index ffd49f60b0..41a1e8d522 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -24,11 +24,6 @@ char *factorvm::pinned_alien_offset(cell obj) } } -char *pinned_alien_offset(cell obj) -{ - return vm->pinned_alien_offset(obj); -} - /* make an alien */ cell factorvm::allot_alien(cell delegate_, cell displacement) { @@ -50,11 +45,6 @@ cell factorvm::allot_alien(cell delegate_, cell displacement) return new_alien.value(); } -cell allot_alien(cell delegate_, cell displacement) -{ - return vm->allot_alien(delegate_,displacement); -} - /* make an alien pointing at an offset of another alien */ inline void factorvm::vmprim_displaced_alien() { @@ -108,15 +98,16 @@ void *alien_pointer() return vm->alien_pointer(); } + /* define words to read/write values at an alien address */ #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \ PRIMITIVE(alien_##name) \ { \ - boxer(*(type*)alien_pointer()); \ + boxer(*(type*)PRIMITIVE_GETVM()->alien_pointer()); \ } \ PRIMITIVE(set_alien_##name) \ { \ - type *ptr = (type *)alien_pointer(); \ + type *ptr = (type *)PRIMITIVE_GETVM()->alien_pointer(); \ type value = to(dpop()); \ *ptr = value; \ } @@ -133,7 +124,7 @@ DEFINE_ALIEN_ACCESSOR(signed_1,s8,box_signed_1,to_fixnum) DEFINE_ALIEN_ACCESSOR(unsigned_1,u8,box_unsigned_1,to_cell) DEFINE_ALIEN_ACCESSOR(float,float,box_float,to_float) DEFINE_ALIEN_ACCESSOR(double,double,box_double,to_double) -DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset) +DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,PRIMITIVE_GETVM()->pinned_alien_offset) /* open a native library and push a handle */ inline void factorvm::vmprim_dlopen() diff --git a/vm/alien.hpp b/vm/alien.hpp old mode 100644 new mode 100755 index 6235a2d6c7..0a4f3a7e93 --- a/vm/alien.hpp +++ b/vm/alien.hpp @@ -1,8 +1,6 @@ namespace factor { -cell allot_alien(cell delegate, cell displacement); - PRIMITIVE(displaced_alien); PRIMITIVE(alien_address); From 0de0d5f2567f076a2c54a8ba1b4cac89c9ccb474 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:19 +0100 Subject: [PATCH 169/345] removed some global functions from quotations.cpp --- vm/quotations.cpp | 30 +++++------------------------- vm/quotations.hpp | 6 ------ 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/vm/quotations.cpp b/vm/quotations.cpp index ef615dc095..b7bce0bfd7 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -198,8 +198,8 @@ void quotation_jit::iterate_quotation() if(compiling) { - jit_compile(array_nth(elements.untagged(),i),relocate); - jit_compile(array_nth(elements.untagged(),i + 1),relocate); + myvm->jit_compile(array_nth(elements.untagged(),i),relocate); + myvm->jit_compile(array_nth(elements.untagged(),i + 1),relocate); } literal(array_nth(elements.untagged(),i)); @@ -214,7 +214,7 @@ void quotation_jit::iterate_quotation() else if(fast_dip_p(i)) { if(compiling) - jit_compile(obj.value(),relocate); + myvm->jit_compile(obj.value(),relocate); emit_with(userenv[JIT_DIP],obj.value()); i++; break; @@ -223,7 +223,7 @@ void quotation_jit::iterate_quotation() else if(fast_2dip_p(i)) { if(compiling) - jit_compile(obj.value(),relocate); + myvm->jit_compile(obj.value(),relocate); emit_with(userenv[JIT_2DIP],obj.value()); i++; break; @@ -232,7 +232,7 @@ void quotation_jit::iterate_quotation() else if(fast_3dip_p(i)) { if(compiling) - jit_compile(obj.value(),relocate); + myvm->jit_compile(obj.value(),relocate); emit_with(userenv[JIT_3DIP],obj.value()); i++; break; @@ -274,11 +274,6 @@ void factorvm::set_quot_xt(quotation *quot, code_block *code) quot->xt = code->xt(); } -void set_quot_xt(quotation *quot, code_block *code) -{ - return vm->set_quot_xt(quot,code); -} - /* Allocates memory */ void factorvm::jit_compile(cell quot_, bool relocating) { @@ -294,11 +289,6 @@ void factorvm::jit_compile(cell quot_, bool relocating) if(relocating) relocate_code_block(compiled); } -void jit_compile(cell quot_, bool relocating) -{ - return vm->jit_compile(quot_,relocating); -} - inline void factorvm::vmprim_jit_compile() { jit_compile(dpop(),true); @@ -357,11 +347,6 @@ void factorvm::compile_all_words() iterate_code_heap(factor::relocate_code_block); } -void compile_all_words() -{ - return vm->compile_all_words(); -} - /* Allocates memory */ fixnum factorvm::quot_code_offset_to_scan(cell quot_, cell offset) { @@ -375,11 +360,6 @@ fixnum factorvm::quot_code_offset_to_scan(cell quot_, cell offset) return compiler.get_position(); } -fixnum quot_code_offset_to_scan(cell quot_, cell offset) -{ - return vm->quot_code_offset_to_scan(quot_,offset); -} - cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) { gc_root quot(quot_,this); diff --git a/vm/quotations.hpp b/vm/quotations.hpp index dad41917e0..6c8b17db21 100755 --- a/vm/quotations.hpp +++ b/vm/quotations.hpp @@ -22,14 +22,8 @@ struct quotation_jit : public jit { void iterate_quotation(); }; -void set_quot_xt(quotation *quot, code_block *code); -void jit_compile(cell quot, bool relocate); -fixnum quot_code_offset_to_scan(cell quot, cell offset); - PRIMITIVE(jit_compile); -void compile_all_words(); - PRIMITIVE(array_to_quotation); PRIMITIVE(quotation_xt); From 6234b7957f972c6c6d84b03db1cee6a3eaf1fa0f Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:19 +0100 Subject: [PATCH 170/345] removed some global functions from dispatch.cpp --- vm/dispatch.cpp | 55 ------------------------------------------------- vm/dispatch.hpp | 9 -------- 2 files changed, 64 deletions(-) diff --git a/vm/dispatch.cpp b/vm/dispatch.cpp index 25f7a2f2ec..85d92f90a0 100755 --- a/vm/dispatch.cpp +++ b/vm/dispatch.cpp @@ -18,11 +18,6 @@ cell factorvm::search_lookup_alist(cell table, cell klass) return F; } -cell search_lookup_alist(cell table, cell klass) -{ - return vm->search_lookup_alist(table,klass); -} - cell factorvm::search_lookup_hash(cell table, cell klass, cell hashcode) { array *buckets = untag(table); @@ -33,33 +28,18 @@ cell factorvm::search_lookup_hash(cell table, cell klass, cell hashcode) return search_lookup_alist(bucket,klass); } -cell search_lookup_hash(cell table, cell klass, cell hashcode) -{ - return vm->search_lookup_hash(table,klass,hashcode); -} - cell factorvm::nth_superclass(tuple_layout *layout, fixnum echelon) { cell *ptr = (cell *)(layout + 1); return ptr[echelon * 2]; } -cell nth_superclass(tuple_layout *layout, fixnum echelon) -{ - return vm->nth_superclass(layout,echelon); -} - cell factorvm::nth_hashcode(tuple_layout *layout, fixnum echelon) { cell *ptr = (cell *)(layout + 1); return ptr[echelon * 2 + 1]; } -cell nth_hashcode(tuple_layout *layout, fixnum echelon) -{ - return vm->nth_hashcode(layout,echelon); -} - cell factorvm::lookup_tuple_method(cell obj, cell methods) { tuple_layout *layout = untag(untag(obj)->layout); @@ -92,11 +72,6 @@ cell factorvm::lookup_tuple_method(cell obj, cell methods) return F; } -cell lookup_tuple_method(cell obj, cell methods) -{ - return vm->lookup_tuple_method(obj,methods); -} - cell factorvm::lookup_hi_tag_method(cell obj, cell methods) { array *hi_tag_methods = untag(methods); @@ -107,11 +82,6 @@ cell factorvm::lookup_hi_tag_method(cell obj, cell methods) return array_nth(hi_tag_methods,tag); } -cell lookup_hi_tag_method(cell obj, cell methods) -{ - return vm->lookup_hi_tag_method(obj,methods); -} - cell factorvm::lookup_hairy_method(cell obj, cell methods) { cell method = array_nth(untag(methods),TAG(obj)); @@ -134,11 +104,6 @@ cell factorvm::lookup_hairy_method(cell obj, cell methods) } } -cell lookup_hairy_method(cell obj, cell methods) -{ - return vm->lookup_hairy_method(obj,methods); -} - cell factorvm::lookup_method(cell obj, cell methods) { cell tag = TAG(obj); @@ -148,11 +113,6 @@ cell factorvm::lookup_method(cell obj, cell methods) return array_nth(untag(methods),TAG(obj)); } -cell lookup_method(cell obj, cell methods) -{ - return vm->lookup_method(obj,methods); -} - inline void factorvm::vmprim_lookup_method() { cell methods = dpop(); @@ -178,22 +138,12 @@ cell factorvm::object_class(cell obj) } } -cell object_class(cell obj) -{ - return vm->object_class(obj); -} - cell factorvm::method_cache_hashcode(cell klass, array *array) { cell capacity = (array_capacity(array) >> 1) - 1; return ((klass >> TAG_BITS) & capacity) << 1; } -cell method_cache_hashcode(cell klass, array *array) -{ - return vm->method_cache_hashcode(klass,array); -} - void factorvm::update_method_cache(cell cache, cell klass, cell method) { array *cache_elements = untag(cache); @@ -202,11 +152,6 @@ void factorvm::update_method_cache(cell cache, cell klass, cell method) set_array_nth(cache_elements,hashcode + 1,method); } -void update_method_cache(cell cache, cell klass, cell method) -{ - return vm->update_method_cache(cache,klass,method); -} - inline void factorvm::vmprim_mega_cache_miss() { megamorphic_cache_misses++; diff --git a/vm/dispatch.hpp b/vm/dispatch.hpp index f5648c7ebe..b9cbcbbd85 100644 --- a/vm/dispatch.hpp +++ b/vm/dispatch.hpp @@ -1,18 +1,9 @@ namespace factor { -cell lookup_method(cell object, cell methods); PRIMITIVE(lookup_method); - -cell object_class(cell object); - PRIMITIVE(mega_cache_miss); - PRIMITIVE(reset_dispatch_stats); PRIMITIVE(dispatch_stats); -void jit_emit_class_lookup(jit *jit, fixnum index, cell type); - -void jit_emit_mega_cache_lookup(jit *jit, cell methods, fixnum index, cell cache); - } From 1c656e1bac44711cc6a726f32ffe1de85697c28a Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:19 +0100 Subject: [PATCH 171/345] removed some global functions from inline_cache.cpp --- vm/inline_cache.cpp | 52 ++++----------------------------------------- vm/inline_cache.hpp | 2 -- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/vm/inline_cache.cpp b/vm/inline_cache.cpp index 15d8fcb203..24008cae79 100755 --- a/vm/inline_cache.cpp +++ b/vm/inline_cache.cpp @@ -9,11 +9,6 @@ void factorvm::init_inline_caching(int max_size) max_pic_size = max_size; } -void init_inline_caching(int max_size) -{ - return vm->init_inline_caching(max_size); -} - void factorvm::deallocate_inline_cache(cell return_address) { /* Find the call target. */ @@ -33,11 +28,6 @@ void factorvm::deallocate_inline_cache(cell return_address) heap_free(&code,old_block); } -void deallocate_inline_cache(cell return_address) -{ - return vm->deallocate_inline_cache(return_address); -} - /* Figure out what kind of type check the PIC needs based on the methods it contains */ cell factorvm::determine_inline_cache_type(array *cache_entries) @@ -77,21 +67,11 @@ cell factorvm::determine_inline_cache_type(array *cache_entries) return 0; } -cell determine_inline_cache_type(array *cache_entries) -{ - return vm->determine_inline_cache_type(cache_entries); -} - void factorvm::update_pic_count(cell type) { pic_counts[type - PIC_TAG]++; } -void update_pic_count(cell type) -{ - return vm->update_pic_count(type); -} - struct inline_cache_jit : public jit { fixnum index; @@ -128,8 +108,8 @@ void inline_cache_jit::compile_inline_cache(fixnum index, gc_root methods(methods_,myvm); gc_root cache_entries(cache_entries_,myvm); - cell inline_cache_type = determine_inline_cache_type(cache_entries.untagged()); - update_pic_count(inline_cache_type); + cell inline_cache_type = myvm->determine_inline_cache_type(cache_entries.untagged()); + myvm->update_pic_count(inline_cache_type); /* Generate machine code to determine the object's class. */ emit_class_lookup(index,inline_cache_type); @@ -176,32 +156,17 @@ code_block *factorvm::compile_inline_cache(fixnum index,cell generic_word_,cell return code; } -code_block *compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p) -{ - return vm->compile_inline_cache(index,generic_word_,methods_,cache_entries_,tail_call_p); -} - /* A generic word's definition performs general method lookup. Allocates memory */ void *factorvm::megamorphic_call_stub(cell generic_word) { return untag(generic_word)->xt; } -void *megamorphic_call_stub(cell generic_word) -{ - return vm->megamorphic_call_stub(generic_word); -} - cell factorvm::inline_cache_size(cell cache_entries) { return array_capacity(untag_check(cache_entries)) / 2; } -cell inline_cache_size(cell cache_entries) -{ - return vm->inline_cache_size(cache_entries); -} - /* Allocates memory */ cell factorvm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_) { @@ -216,11 +181,6 @@ cell factorvm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell met return new_cache_entries.value(); } -cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_) -{ - return vm->add_inline_cache_entry(cache_entries_,klass_,method_); -} - void factorvm::update_pic_transitions(cell pic_size) { if(pic_size == max_pic_size) @@ -231,11 +191,6 @@ void factorvm::update_pic_transitions(cell pic_size) ic_to_pic_transitions++; } -void update_pic_transitions(cell pic_size) -{ - return vm->update_pic_transitions(pic_size); -} - /* The cache_entries parameter is either f (on cold call site) or an array (on cache miss). Called from assembly with the actual return address */ void *factorvm::inline_cache_miss(cell return_address) @@ -290,11 +245,12 @@ void *factorvm::inline_cache_miss(cell return_address) return xt; } -void *inline_cache_miss(cell return_address) +VM_C_API void *inline_cache_miss(cell return_address) { return vm->inline_cache_miss(return_address); } + inline void factorvm::vmprim_reset_inline_cache_stats() { cold_call_to_ic_transitions = ic_to_pic_transitions = pic_to_mega_transitions = 0; diff --git a/vm/inline_cache.hpp b/vm/inline_cache.hpp index e354d30677..5b1bbdf516 100644 --- a/vm/inline_cache.hpp +++ b/vm/inline_cache.hpp @@ -1,7 +1,5 @@ namespace factor { -void init_inline_caching(int max_size); - PRIMITIVE(reset_inline_cache_stats); PRIMITIVE(inline_cache_stats); PRIMITIVE(inline_cache_miss); From 390712b00ab740715e8417edcf1c826d52e39281 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:19 +0100 Subject: [PATCH 172/345] removed some global functions from utilities.cpp --- vm/utilities.cpp | 30 ------------------------------ vm/utilities.hpp | 8 -------- 2 files changed, 38 deletions(-) mode change 100644 => 100755 vm/utilities.hpp diff --git a/vm/utilities.cpp b/vm/utilities.cpp index ce17e8f526..a1e3f30e00 100755 --- a/vm/utilities.cpp +++ b/vm/utilities.cpp @@ -35,11 +35,6 @@ void factorvm::nl() fputs("\n",stdout); } -void nl() -{ - return vm->nl(); -} - void factorvm::print_string(const char *str) { fputs(str,stdout); @@ -55,41 +50,21 @@ void factorvm::print_cell(cell x) printf(CELL_FORMAT,x); } -void print_cell(cell x) -{ - return vm->print_cell(x); -} - void factorvm::print_cell_hex(cell x) { printf(CELL_HEX_FORMAT,x); } -void print_cell_hex(cell x) -{ - return vm->print_cell_hex(x); -} - void factorvm::print_cell_hex_pad(cell x) { printf(CELL_HEX_PAD_FORMAT,x); } -void print_cell_hex_pad(cell x) -{ - return vm->print_cell_hex_pad(x); -} - void factorvm::print_fixnum(fixnum x) { printf(FIXNUM_FORMAT,x); } -void print_fixnum(fixnum x) -{ - return vm->print_fixnum(x); -} - cell factorvm::read_cell_hex() { cell cell; @@ -97,9 +72,4 @@ cell factorvm::read_cell_hex() return cell; } -cell read_cell_hex() -{ - return vm->read_cell_hex(); -}; - } diff --git a/vm/utilities.hpp b/vm/utilities.hpp old mode 100644 new mode 100755 index 7e7765170e..bc7f7d918a --- a/vm/utilities.hpp +++ b/vm/utilities.hpp @@ -1,15 +1,7 @@ namespace factor { - void *safe_malloc(size_t size); vm_char *safe_strdup(const vm_char *str); - -void nl(); void print_string(const char *str); -void print_cell(cell x); -void print_cell_hex(cell x); -void print_cell_hex_pad(cell x); -void print_fixnum(fixnum x); -cell read_cell_hex(); } From d3b5321b6e9a0cc92732987f24f312c5ec10f99e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:19 +0100 Subject: [PATCH 173/345] removed some global functions from errors.cpp --- vm/errors.cpp | 25 ------------------------- vm/errors.hpp | 5 ----- 2 files changed, 30 deletions(-) mode change 100644 => 100755 vm/errors.hpp diff --git a/vm/errors.cpp b/vm/errors.cpp index 23833960e1..f2cab405a8 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -92,11 +92,6 @@ void factorvm::throw_error(cell error, stack_frame *callstack_top) } } -void throw_error(cell error, stack_frame *callstack_top) -{ - return vm->throw_error(error, callstack_top); -} - void factorvm::general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top) { throw_error(allot_array_4(userenv[ERROR_ENV], @@ -113,11 +108,6 @@ void factorvm::type_error(cell type, cell tagged) general_error(ERROR_TYPE,tag_fixnum(type),tagged,NULL); } -void type_error(cell type, cell tagged) -{ - return vm->type_error(type, tagged); -} - void factorvm::not_implemented_error() { general_error(ERROR_NOT_IMPLEMENTED,F,F,NULL); @@ -139,11 +129,6 @@ bool factorvm::in_page(cell fault, cell area, cell area_size, int offset) return fault >= area && fault <= area + pagesize; } -bool in_page(cell fault, cell area, cell area_size, int offset) -{ - return vm->in_page(fault,area,area_size,offset); -} - void factorvm::memory_protection_error(cell addr, stack_frame *native_stack) { if(in_page(addr, ds_bot, 0, -1)) @@ -160,21 +145,11 @@ void factorvm::memory_protection_error(cell addr, stack_frame *native_stack) general_error(ERROR_MEMORY,allot_cell(addr),F,native_stack); } -void memory_protection_error(cell addr, stack_frame *native_stack) -{ - return vm->memory_protection_error(addr,native_stack); -} - void factorvm::signal_error(int signal, stack_frame *native_stack) { general_error(ERROR_SIGNAL,tag_fixnum(signal),F,native_stack); } -void signal_error(int signal, stack_frame *native_stack) -{ - return vm->signal_error(signal, native_stack); -} - void factorvm::divide_by_zero_error() { general_error(ERROR_DIVIDE_BY_ZERO,F,F,NULL); diff --git a/vm/errors.hpp b/vm/errors.hpp old mode 100644 new mode 100755 index 7f3c4dcd4a..69a90b70e2 --- a/vm/errors.hpp +++ b/vm/errors.hpp @@ -29,12 +29,7 @@ void critical_error(const char* msg, cell tagged); PRIMITIVE(die); -void throw_error(cell error, stack_frame *native_stack); void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *native_stack); -void divide_by_zero_error(); -void memory_protection_error(cell addr, stack_frame *native_stack); -void signal_error(int signal, stack_frame *native_stack); -void type_error(cell type, cell tagged); void not_implemented_error(); void fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top); From 75a3db3bfb9dfa7459f8e5a04a1e562c62ecbf32 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 17 Aug 2009 21:37:19 +0100 Subject: [PATCH 174/345] Fixed typo from upstream --- vm/vm.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/vm.hpp b/vm/vm.hpp index f259096766..0407185012 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -180,7 +180,7 @@ struct factorvm { bool performing_compaction; cell collecting_gen; - /* if true, we collecting aging space for the second time, so if it is still + /* if true, we are collecting aging space for the second time, so if it is still full, we go on to collect tenured */ bool collecting_aging_again; From 97addbaf7a16866370a1192fea10281a5f19fac6 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Wed, 26 Aug 2009 16:05:50 +0100 Subject: [PATCH 175/345] got os-macosx.mm to compile --- vm/os-macosx.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/os-macosx.mm b/vm/os-macosx.mm index 792ba0d541..c7e9c0bf6b 100644 --- a/vm/os-macosx.mm +++ b/vm/os-macosx.mm @@ -13,7 +13,7 @@ NS_DURING c_to_factor(quot); NS_VOIDRETURN; NS_HANDLER - dpush(allot_alien(F,(cell)localException)); + dpush(vm->allot_alien(F,(cell)localException)); quot = userenv[COCOA_EXCEPTION_ENV]; if(!tagged(quot).type_p(QUOTATION_TYPE)) { From 7592a424e89ebabbea16063873537e5e26ff1e6b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 19:02:04 +0100 Subject: [PATCH 176/345] Dev checkpoint --- vm/os-windows.cpp | 70 ++++++++++++++++++++++++++++++++++++++++------- vm/vm.hpp | 18 ++++++++++-- 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/vm/os-windows.cpp b/vm/os-windows.cpp index 7db19ff560..8acd207058 100644 --- a/vm/os-windows.cpp +++ b/vm/os-windows.cpp @@ -5,29 +5,49 @@ namespace factor HMODULE hFactorDll; -void init_ffi() +void factorvm::init_ffi() { hFactorDll = GetModuleHandle(FACTOR_DLL); if(!hFactorDll) fatal_error("GetModuleHandle(\"" FACTOR_DLL_NAME "\") failed", 0); } -void ffi_dlopen(dll *dll) +void init_ffi() +{ + return vm->init_ffi(); +} + +void factorvm::ffi_dlopen(dll *dll) { dll->dll = LoadLibraryEx((WCHAR *)alien_offset(dll->path), NULL, 0); } -void *ffi_dlsym(dll *dll, symbol_char *symbol) +void ffi_dlopen(dll *dll) +{ + return vm->ffi_dlopen(dll); +} + +void *factorvm::ffi_dlsym(dll *dll, symbol_char *symbol) { return (void *)GetProcAddress(dll ? (HMODULE)dll->dll : hFactorDll, symbol); } -void ffi_dlclose(dll *dll) +void *ffi_dlsym(dll *dll, symbol_char *symbol) +{ + return vm->ffi_dlsym(dll,symbol); +} + +void factorvm::ffi_dlclose(dll *dll) { FreeLibrary((HMODULE)dll->dll); dll->dll = NULL; } +void ffi_dlclose(dll *dll) +{ + return vm->ffi_dlclose(dll); +} + bool windows_stat(vm_char *path) { BY_HANDLE_FILE_INFORMATION bhfi; @@ -82,7 +102,7 @@ const vm_char *default_image_path() } /* You must free() this yourself. */ -const vm_char *vm_executable_path() +const vm_char *factorvm::vm_executable_path() { vm_char full_path[MAX_UNICODE_PATH]; if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH)) @@ -90,14 +110,24 @@ const vm_char *vm_executable_path() return safe_strdup(full_path); } +const vm_char *vm_executable_path() +{ + return vm->vm_executable_path(); +} -PRIMITIVE(existsp) + +inline void factorvm::vmprim_existsp() { vm_char *path = untag_check(dpop())->data(); box_boolean(windows_stat(path)); } -segment *alloc_segment(cell size) +PRIMITIVE(existsp) +{ + PRIMITIVE_GETVM()->vmprim_existsp(); +} + +segment *factorvm::alloc_segment(cell size) { char *mem; DWORD ignore; @@ -122,7 +152,12 @@ segment *alloc_segment(cell size) return block; } -void dealloc_segment(segment *block) +segment *alloc_segment(cell size) +{ + return vm->alloc_segment(size); +} + +void factorvm::dealloc_segment(segment *block) { SYSTEM_INFO si; GetSystemInfo(&si); @@ -131,7 +166,12 @@ void dealloc_segment(segment *block) free(block); } -long getpagesize() +void dealloc_segment(segment *block) +{ + return vm->dealloc_segment(block); +} + +long factorvm::getpagesize() { static long g_pagesize = 0; if (! g_pagesize) @@ -143,9 +183,19 @@ long getpagesize() return g_pagesize; } -void sleep_micros(u64 usec) +long getpagesize() +{ + return vm->getpagesize(); +} + +void factorvm::sleep_micros(u64 usec) { Sleep((DWORD)(usec / 1000)); } +void sleep_micros(u64 usec) +{ + return vm->sleep_micros(usec); +} + } diff --git a/vm/vm.hpp b/vm/vm.hpp index 0407185012..3a22826137 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -503,7 +503,7 @@ struct factorvm { void fixup_object_xts(); void compact_code_heap(); inline void check_code_pointer(cell ptr); - // next method here: + //image cell code_relocation_base; @@ -645,8 +645,20 @@ struct factorvm { cell read_cell_hex(); - - + // os-windows +#if defined(WINDOWS) + void init_ffi(); + void ffi_dlopen(dll *dll); + void *ffi_dlsym(dll *dll, symbol_char *symbol); + void ffi_dlclose(dll *dll); + void sleep_micros(u64 usec); + long getpagesize(); + void dealloc_segment(segment *block); + segment *alloc_segment(cell size); + const vm_char *vm_executable_path(); + inline void vmprim_existsp(); + // next method here: +#endif }; From aa58b54c2e99506827119d5425dc1c9f419f6da0 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 19:35:12 +0100 Subject: [PATCH 177/345] moved align_page into vm --- vm/inlineimpls.hpp | 12 ++++++++++++ vm/segments.hpp | 5 ----- vm/vm.hpp | 3 +++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index 90ccd8d869..ca0b13be39 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -4,6 +4,18 @@ namespace factor // I've had to copy inline implementations here to make dependencies work. Am hoping to move this code back into include files // once the rest of the reentrant changes are done. -PD +// segments.hpp + +inline cell factorvm::align_page(cell a) +{ + return align(a,getpagesize()); +} + +inline static cell align_page(cell a) +{ + return vm->align_page(a); +} + // write_barrier.hpp inline card *factorvm::addr_to_card(cell a) diff --git a/vm/segments.hpp b/vm/segments.hpp index 36b5bc747b..a715b4dabc 100644 --- a/vm/segments.hpp +++ b/vm/segments.hpp @@ -7,9 +7,4 @@ struct segment { cell end; }; -inline static cell align_page(cell a) -{ - return align(a,getpagesize()); -} - } diff --git a/vm/vm.hpp b/vm/vm.hpp index 3a22826137..9ee5a2d81f 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -3,6 +3,9 @@ namespace factor struct factorvm { + // segments + inline cell align_page(cell a); + // contexts cell ds_size, rs_size; context *unused_contexts; From 7cebe088a16e7ddab668a055cb4b704650ffd7e7 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 19:52:11 +0100 Subject: [PATCH 178/345] moved some os-windows functions into the vm --- vm/os-windows.cpp | 52 ++++------------------------------------------- vm/os-windows.hpp | 9 -------- vm/vm.hpp | 3 +++ 3 files changed, 7 insertions(+), 57 deletions(-) diff --git a/vm/os-windows.cpp b/vm/os-windows.cpp index 8acd207058..38c8b944a4 100644 --- a/vm/os-windows.cpp +++ b/vm/os-windows.cpp @@ -12,43 +12,23 @@ void factorvm::init_ffi() fatal_error("GetModuleHandle(\"" FACTOR_DLL_NAME "\") failed", 0); } -void init_ffi() -{ - return vm->init_ffi(); -} - void factorvm::ffi_dlopen(dll *dll) { dll->dll = LoadLibraryEx((WCHAR *)alien_offset(dll->path), NULL, 0); } -void ffi_dlopen(dll *dll) -{ - return vm->ffi_dlopen(dll); -} - void *factorvm::ffi_dlsym(dll *dll, symbol_char *symbol) { return (void *)GetProcAddress(dll ? (HMODULE)dll->dll : hFactorDll, symbol); } -void *ffi_dlsym(dll *dll, symbol_char *symbol) -{ - return vm->ffi_dlsym(dll,symbol); -} - void factorvm::ffi_dlclose(dll *dll) { FreeLibrary((HMODULE)dll->dll); dll->dll = NULL; } -void ffi_dlclose(dll *dll) -{ - return vm->ffi_dlclose(dll); -} - -bool windows_stat(vm_char *path) +bool factorvm::windows_stat(vm_char *path) { BY_HANDLE_FILE_INFORMATION bhfi; HANDLE h = CreateFileW(path, @@ -76,14 +56,15 @@ bool windows_stat(vm_char *path) return ret; } -void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length) + +void factorvm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length) { snwprintf(temp_path, length-1, L"%s.image", full_path); temp_path[sizeof(temp_path) - 1] = 0; } /* You must free() this yourself. */ -const vm_char *default_image_path() +const vm_char *factorvm::default_image_path() { vm_char full_path[MAX_UNICODE_PATH]; vm_char *ptr; @@ -110,11 +91,6 @@ const vm_char *factorvm::vm_executable_path() return safe_strdup(full_path); } -const vm_char *vm_executable_path() -{ - return vm->vm_executable_path(); -} - inline void factorvm::vmprim_existsp() { @@ -152,11 +128,6 @@ segment *factorvm::alloc_segment(cell size) return block; } -segment *alloc_segment(cell size) -{ - return vm->alloc_segment(size); -} - void factorvm::dealloc_segment(segment *block) { SYSTEM_INFO si; @@ -166,11 +137,6 @@ void factorvm::dealloc_segment(segment *block) free(block); } -void dealloc_segment(segment *block) -{ - return vm->dealloc_segment(block); -} - long factorvm::getpagesize() { static long g_pagesize = 0; @@ -183,19 +149,9 @@ long factorvm::getpagesize() return g_pagesize; } -long getpagesize() -{ - return vm->getpagesize(); -} - void factorvm::sleep_micros(u64 usec) { Sleep((DWORD)(usec / 1000)); } -void sleep_micros(u64 usec) -{ - return vm->sleep_micros(usec); -} - } diff --git a/vm/os-windows.hpp b/vm/os-windows.hpp index 27e2775289..e5617213f4 100644 --- a/vm/os-windows.hpp +++ b/vm/os-windows.hpp @@ -41,18 +41,9 @@ typedef wchar_t vm_char; /* Difference between Jan 1 00:00:00 1601 and Jan 1 00:00:00 1970 */ #define EPOCH_OFFSET 0x019db1ded53e8000LL -void init_ffi(); -void ffi_dlopen(dll *dll); -void *ffi_dlsym(dll *dll, symbol_char *symbol); -void ffi_dlclose(dll *dll); - -void sleep_micros(u64 msec); inline static void init_signals() {} inline static void early_init() {} -const vm_char *vm_executable_path(); -const vm_char *default_image_path(); -long getpagesize (); s64 current_micros(); diff --git a/vm/vm.hpp b/vm/vm.hpp index 9ee5a2d81f..4257d17fd9 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -660,6 +660,9 @@ struct factorvm { segment *alloc_segment(cell size); const vm_char *vm_executable_path(); inline void vmprim_existsp(); + const vm_char *default_image_path(); + void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length); + bool windows_stat(vm_char *path); // next method here: #endif From 01ecb1163595c1cee0100fd2f5a7e6d0c87a5b26 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 20:03:11 +0100 Subject: [PATCH 179/345] Dev checkpoint --- vm/os-windows-nt.cpp | 18 ++++++++++++++++-- vm/vm.hpp | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index b50c9b7af8..8a8b2132ad 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -3,7 +3,7 @@ namespace factor { -s64 current_micros() +s64 factorvm::current_micros() { FILETIME t; GetSystemTimeAsFileTime(&t); @@ -11,6 +11,11 @@ s64 current_micros() - EPOCH_OFFSET) / 10; } +s64 current_micros() +{ + return vm->current_micros(); +} + FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) { PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; @@ -58,7 +63,7 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) return EXCEPTION_CONTINUE_EXECUTION; } -void c_to_factor_toplevel(cell quot) +void factorvm::c_to_factor_toplevel(cell quot) { if(!AddVectoredExceptionHandler(0, (PVECTORED_EXCEPTION_HANDLER)exception_handler)) fatal_error("AddVectoredExceptionHandler failed", 0); @@ -66,6 +71,15 @@ void c_to_factor_toplevel(cell quot) RemoveVectoredExceptionHandler((void *)exception_handler); } +void c_to_factor_toplevel(cell quot) +{ + return vm->c_to_factor_toplevel(quot); +} + +void factorvm::open_console() +{ +} + void open_console() { } diff --git a/vm/vm.hpp b/vm/vm.hpp index 4257d17fd9..fa4c365da6 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -649,7 +649,7 @@ struct factorvm { // os-windows -#if defined(WINDOWS) + #if defined(WINDOWS) void init_ffi(); void ffi_dlopen(dll *dll); void *ffi_dlsym(dll *dll, symbol_char *symbol); @@ -663,8 +663,20 @@ struct factorvm { const vm_char *default_image_path(); void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length); bool windows_stat(vm_char *path); - // next method here: -#endif + + #if defined(WINCE) + #else /* WINNT */ + s64 current_micros(); + void c_to_factor_toplevel(cell quot); + void open_console(); + // next method here: + #endif + + + #ifdef FACTOR_X86 + #endif + + #endif }; From d48dffcfa023e85f1496a8312661c870c1815187 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 20:22:11 +0100 Subject: [PATCH 180/345] moved os-windows-nt functions into the vm --- vm/os-windows-nt.cpp | 14 -------------- vm/os-windows-nt.hpp | 2 -- vm/vm.hpp | 10 ++-------- 3 files changed, 2 insertions(+), 24 deletions(-) diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 8a8b2132ad..26781ee4f9 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -11,11 +11,6 @@ s64 factorvm::current_micros() - EPOCH_OFFSET) / 10; } -s64 current_micros() -{ - return vm->current_micros(); -} - FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) { PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; @@ -71,17 +66,8 @@ void factorvm::c_to_factor_toplevel(cell quot) RemoveVectoredExceptionHandler((void *)exception_handler); } -void c_to_factor_toplevel(cell quot) -{ - return vm->c_to_factor_toplevel(quot); -} - void factorvm::open_console() { } -void open_console() -{ -} - } diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index 088103bb5b..c083844ae0 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -19,9 +19,7 @@ typedef char symbol_char; #define FACTOR_STDCALL __attribute__((stdcall)) -void c_to_factor_toplevel(cell quot); FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe); -void open_console(); // SSE traps raise these exception codes, which are defined in internal NT headers // but not winbase.h diff --git a/vm/vm.hpp b/vm/vm.hpp index fa4c365da6..9af0c5c8f9 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -664,18 +664,12 @@ struct factorvm { void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length); bool windows_stat(vm_char *path); - #if defined(WINCE) - #else /* WINNT */ + #if defined(WINNT) s64 current_micros(); void c_to_factor_toplevel(cell quot); void open_console(); // next method here: - #endif - - - #ifdef FACTOR_X86 - #endif - + #endif #endif }; From a5f24c8fb99b4959e3d93c741b2d77d3e9eeb3e7 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 20:40:26 +0100 Subject: [PATCH 181/345] added VM relocation type --- basis/compiler/constants/constants.factor | 1 + vm/code_block.cpp | 3 +++ vm/code_block.hpp | 2 ++ vm/master.hpp | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/basis/compiler/constants/constants.factor b/basis/compiler/constants/constants.factor index b795862970..cc6003b89c 100644 --- a/basis/compiler/constants/constants.factor +++ b/basis/compiler/constants/constants.factor @@ -50,6 +50,7 @@ CONSTANT: rt-immediate 8 CONSTANT: rt-stack-chain 9 CONSTANT: rt-untagged 10 CONSTANT: rt-megamorphic-cache-hits 11 +CONSTANT: rt-vm 12 : rc-absolute? ( n -- ? ) ${ rc-absolute-ppc-2/2 rc-absolute-cell rc-absolute } member? ; diff --git a/vm/code_block.cpp b/vm/code_block.cpp index f19759fb0e..a64a8d2c2d 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -44,6 +44,7 @@ int factorvm::number_of_parameters(relocation_type type) case RT_THIS: case RT_STACK_CHAIN: case RT_MEGAMORPHIC_CACHE_HITS: + case RT_VM: return 0; default: critical_error("Bad rel type",type); @@ -186,6 +187,8 @@ cell factorvm::compute_relocation(relocation_entry rel, cell index, code_block * return untag_fixnum(ARG); case RT_MEGAMORPHIC_CACHE_HITS: return (cell)&megamorphic_cache_hits; + case RT_VM: + return (cell)this; default: critical_error("Bad rel type",rel); return 0; /* Can't happen */ diff --git a/vm/code_block.hpp b/vm/code_block.hpp index 60046ed380..50d937f4af 100644 --- a/vm/code_block.hpp +++ b/vm/code_block.hpp @@ -26,6 +26,8 @@ enum relocation_type { RT_UNTAGGED, /* address of megamorphic_cache_hits var */ RT_MEGAMORPHIC_CACHE_HITS, + /* address of vm object*/ + RT_VM, }; enum relocation_class { diff --git a/vm/master.hpp b/vm/master.hpp index bf60d1e4f6..c5c14e6999 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -35,8 +35,8 @@ /* Factor headers */ #include "layouts.hpp" -#include "platform.hpp" #include "primitives.hpp" +#include "platform.hpp" #include "stacks.hpp" #include "segments.hpp" #include "contexts.hpp" From 57011aed515ea1bcb10213b9ce417c04c42ebae1 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 20:49:22 +0100 Subject: [PATCH 182/345] vm ptr passed to primitives on X86.32 (other cpus still use singleton vm ptr) --- basis/cpu/x86/32/bootstrap.factor | 2 ++ vm/cpu-x86.32.hpp | 1 - vm/master.hpp | 2 +- vm/primitives.hpp | 14 ++++++++++---- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/basis/cpu/x86/32/bootstrap.factor b/basis/cpu/x86/32/bootstrap.factor index 674cc817d7..da55ea5ccd 100644 --- a/basis/cpu/x86/32/bootstrap.factor +++ b/basis/cpu/x86/32/bootstrap.factor @@ -27,6 +27,8 @@ IN: bootstrap.x86 temp0 0 [] MOV rc-absolute-cell rt-stack-chain jit-rel ! save stack pointer temp0 [] stack-reg MOV + ! pass vm ptr to primitive + EAX 0 MOV rc-absolute-cell rt-vm jit-rel ! call the primitive 0 JMP rc-relative rt-primitive jit-rel ] jit-primitive jit-define diff --git a/vm/cpu-x86.32.hpp b/vm/cpu-x86.32.hpp index 902b33b0b4..351865f776 100644 --- a/vm/cpu-x86.32.hpp +++ b/vm/cpu-x86.32.hpp @@ -7,5 +7,4 @@ register cell ds asm("esi"); register cell rs asm("edi"); #define VM_ASM_API VM_C_API __attribute__ ((regparm (2))) - } diff --git a/vm/master.hpp b/vm/master.hpp index c5c14e6999..bf60d1e4f6 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -35,8 +35,8 @@ /* Factor headers */ #include "layouts.hpp" -#include "primitives.hpp" #include "platform.hpp" +#include "primitives.hpp" #include "stacks.hpp" #include "segments.hpp" #include "contexts.hpp" diff --git a/vm/primitives.hpp b/vm/primitives.hpp index c7534ec1cc..c6d704ffc7 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -1,9 +1,15 @@ namespace factor { -extern "C" typedef void (*primitive_type)(); -extern const primitive_type primitives[]; +#if defined(FACTOR_X86) + extern "C" __attribute__ ((regparm (1))) typedef void (*primitive_type)(void *myvm); + #define PRIMITIVE(name) extern "C" __attribute__ ((regparm (1))) void primitive_##name(void *myvm) + #define PRIMITIVE_GETVM() ((factorvm*)myvm) +#else + extern "C" typedef void (*primitive_type)(void *myvm); + #define PRIMITIVE(name) extern "C" void primitive_##name(void *myvm) + #define PRIMITIVE_GETVM() vm +#endif -#define PRIMITIVE(name) extern "C" void primitive_##name() -#define PRIMITIVE_GETVM() vm +extern const primitive_type primitives[]; } From 6a193bb0d54d8728576aa0ca670ff7d02920fec3 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 18 Aug 2009 21:03:22 +0100 Subject: [PATCH 183/345] Added %vm-invoke to pass vm ptr to vm functions (x86.32 only, otherwise uses singleton vm) --- basis/cpu/architecture/architecture.factor | 2 ++ basis/cpu/x86/32/32.factor | 6 ++++++ basis/cpu/x86/64/64.factor | 2 ++ basis/cpu/x86/x86.factor | 2 +- vm/cpu-x86.32.hpp | 2 ++ vm/data_gc.cpp | 6 +++--- vm/data_gc.hpp | 4 ++-- 7 files changed, 18 insertions(+), 6 deletions(-) diff --git a/basis/cpu/architecture/architecture.factor b/basis/cpu/architecture/architecture.factor index d6611c3384..da1bcfc61f 100644 --- a/basis/cpu/architecture/architecture.factor +++ b/basis/cpu/architecture/architecture.factor @@ -297,6 +297,8 @@ M: object %prepare-var-args ; HOOK: %alien-invoke cpu ( function library -- ) +HOOK: %vm-invoke cpu ( function library -- ) + HOOK: %cleanup cpu ( params -- ) M: object %cleanup ( params -- ) drop ; diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index 9939154512..3ec08d5507 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -47,6 +47,12 @@ M: x86.32 reserved-area-size 0 ; M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ; +M: x86.32 %vm-invoke + temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as the 3rd argument + temp-reg PUSH + %alien-invoke + temp-reg POP ; + M: x86.32 return-struct-in-registers? ( c-type -- ? ) c-type [ return-in-registers?>> ] diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index f4018b1508..4d041d2334 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -172,6 +172,8 @@ M: x86.64 %alien-invoke rc-absolute-cell rel-dlsym R11 CALL ; +M: x86.64 %vm-invoke %alien-invoke ; + M: x86.64 %prepare-alien-indirect ( -- ) "unbox_alien" f %alien-invoke RBP RAX MOV ; diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 27b6667c05..da391f6320 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -610,7 +610,7 @@ M:: x86 %call-gc ( gc-root-count -- ) ! Pass number of roots as second parameter param-reg-2 gc-root-count MOV ! Call GC - "inline_gc" f %alien-invoke ; + "inline_gc" f %vm-invoke ; M: x86 %alien-global [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ; diff --git a/vm/cpu-x86.32.hpp b/vm/cpu-x86.32.hpp index 351865f776..28f2e2978e 100644 --- a/vm/cpu-x86.32.hpp +++ b/vm/cpu-x86.32.hpp @@ -7,4 +7,6 @@ register cell ds asm("esi"); register cell rs asm("edi"); #define VM_ASM_API VM_C_API __attribute__ ((regparm (2))) +#undef VM_PTR +#define VM_PTR myvm } diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index dfc1067690..5a7f34ca4b 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -694,7 +694,7 @@ PRIMITIVE(become) PRIMITIVE_GETVM()->vmprim_become(); } -VM_ASM_API void factorvm::inline_gc(cell *gc_roots_base, cell gc_roots_size) +void factorvm::inline_gc(cell *gc_roots_base, cell gc_roots_size) { for(cell i = 0; i < gc_roots_size; i++) gc_locals.push_back((cell)&gc_roots_base[i]); @@ -705,9 +705,9 @@ VM_ASM_API void factorvm::inline_gc(cell *gc_roots_base, cell gc_roots_size) gc_locals.pop_back(); } -VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size) +VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size, factorvm *myvm) { - return vm->inline_gc(gc_roots_base,gc_roots_size); + return VM_PTR->inline_gc(gc_roots_base,gc_roots_size); } } diff --git a/vm/data_gc.hpp b/vm/data_gc.hpp index 950990a91b..84c824d779 100755 --- a/vm/data_gc.hpp +++ b/vm/data_gc.hpp @@ -19,7 +19,7 @@ PRIMITIVE(gc); PRIMITIVE(gc_stats); PRIMITIVE(clear_gc_stats); PRIMITIVE(become); - -VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size); +struct factorvm; +VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size, factorvm *myvm); } From 4afc16e95b853b3f9e3a9e0a70c428330b29a222 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Wed, 19 Aug 2009 19:02:12 +0100 Subject: [PATCH 184/345] passing vm ptr to lazy_jit_compile mostly working --- basis/cpu/x86/32/32.factor | 1 + basis/cpu/x86/32/bootstrap.factor | 1 + basis/cpu/x86/64/unix/bootstrap.factor | 1 + basis/cpu/x86/64/winnt/bootstrap.factor | 1 + basis/cpu/x86/bootstrap.factor | 2 ++ vm/cpu-ppc.hpp | 6 +++--- vm/cpu-x86.S | 13 +++++++++---- vm/cpu-x86.hpp | 4 ++-- vm/os-genunix.cpp | 2 +- vm/os-macosx.mm | 2 +- vm/os-windows-ce.cpp | 2 +- vm/os-windows-nt.cpp | 2 +- vm/quotations.cpp | 2 +- vm/quotations.hpp | 2 +- 14 files changed, 26 insertions(+), 15 deletions(-) diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index 3ec08d5507..a48528d3fd 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -240,6 +240,7 @@ M: x86.32 %alien-callback ( quot -- ) 4 [ EAX swap %load-reference EAX PUSH + param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup "c_to_factor" f %alien-invoke ] with-aligned-stack ; diff --git a/basis/cpu/x86/32/bootstrap.factor b/basis/cpu/x86/32/bootstrap.factor index da55ea5ccd..afa7c245a0 100644 --- a/basis/cpu/x86/32/bootstrap.factor +++ b/basis/cpu/x86/32/bootstrap.factor @@ -12,6 +12,7 @@ IN: bootstrap.x86 : div-arg ( -- reg ) EAX ; : mod-arg ( -- reg ) EDX ; : arg ( -- reg ) EAX ; +: arg2 ( -- reg ) EDX ; : temp0 ( -- reg ) EAX ; : temp1 ( -- reg ) EDX ; : temp2 ( -- reg ) ECX ; diff --git a/basis/cpu/x86/64/unix/bootstrap.factor b/basis/cpu/x86/64/unix/bootstrap.factor index b6d56840e2..199fe8daf4 100644 --- a/basis/cpu/x86/64/unix/bootstrap.factor +++ b/basis/cpu/x86/64/unix/bootstrap.factor @@ -6,6 +6,7 @@ IN: bootstrap.x86 : stack-frame-size ( -- n ) 4 bootstrap-cells ; : arg ( -- reg ) RDI ; +: arg2 ( -- reg ) RSI ; << "vocab:cpu/x86/64/bootstrap.factor" parse-file parsed >> call diff --git a/basis/cpu/x86/64/winnt/bootstrap.factor b/basis/cpu/x86/64/winnt/bootstrap.factor index 0228082956..72b9d27ca4 100644 --- a/basis/cpu/x86/64/winnt/bootstrap.factor +++ b/basis/cpu/x86/64/winnt/bootstrap.factor @@ -7,6 +7,7 @@ IN: bootstrap.x86 : stack-frame-size ( -- n ) 8 bootstrap-cells ; : arg ( -- reg ) RCX ; +: arg2 ( -- reg ) RDX ; << "vocab:cpu/x86/64/bootstrap.factor" parse-file parsed >> call diff --git a/basis/cpu/x86/bootstrap.factor b/basis/cpu/x86/bootstrap.factor index 0dafc3d9c4..8949b24b1a 100644 --- a/basis/cpu/x86/bootstrap.factor +++ b/basis/cpu/x86/bootstrap.factor @@ -251,6 +251,8 @@ big-endian off arg ds-reg [] MOV ! pop stack ds-reg bootstrap-cell SUB + ! pass vm pointer + arg2 0 MOV rc-absolute-cell rt-vm jit-rel ! call quotation arg quot-xt-offset [+] JMP ] \ (call) define-sub-primitive diff --git a/vm/cpu-ppc.hpp b/vm/cpu-ppc.hpp index 2124e03350..495eb375ec 100644 --- a/vm/cpu-ppc.hpp +++ b/vm/cpu-ppc.hpp @@ -81,9 +81,9 @@ inline static unsigned int fpu_status(unsigned int status) } /* Defined in assembly */ -VM_ASM_API void c_to_factor(cell quot); -VM_ASM_API void throw_impl(cell quot, stack_frame *rewind); -VM_ASM_API void lazy_jit_compile(cell quot); +VM_ASM_API void c_to_factor(cell quot, void *vm); +VM_ASM_API void throw_impl(cell quot, stack_frame *rewind, void *vm); +VM_ASM_API void lazy_jit_compile(cell quot, void *vm); VM_ASM_API void flush_icache(cell start, cell len); VM_ASM_API void set_callstack(stack_frame *to, diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index d229b2cb79..384bd794c5 100644 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -34,17 +34,19 @@ multiply_overflow: mov ARITH_TEMP_2,ARG1 jmp MANGLE(overflow_fixnum_multiply) -DEF(F_FASTCALL void,c_to_factor,(CELL quot)): +DEF(F_FASTCALL void,c_to_factor,(CELL quot, void *vm)): PUSH_NONVOLATILE mov ARG0,NV_TEMP_REG - + //mov $35,ARG1 /* Create register shadow area for Win64 */ sub $32,STACK_REG /* Save stack pointer */ lea -CELL_SIZE(STACK_REG),ARG0 + push ARG1 /* save vm ptr */ call MANGLE(save_callstack_bottom) - + pop ARG1 + /* Call quot-xt */ mov NV_TEMP_REG,ARG0 call *QUOT_XT_OFFSET(ARG0) @@ -65,10 +67,13 @@ DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to)): mov ARG1,STACK_REG jmp *QUOT_XT_OFFSET(ARG0) -DEF(F_FASTCALL void,lazy_jit_compile,(CELL quot)): +DEF(F_FASTCALL void,lazy_jit_compile,(CELL quot, void *vm)): + mov ARG1,NV_TEMP_REG /* stash vm ptr */ mov STACK_REG,ARG1 /* Save stack pointer */ sub $STACK_PADDING,STACK_REG + push NV_TEMP_REG /* push vm ptr as arg3 */ call MANGLE(lazy_jit_compile_impl) + pop NV_TEMP_REG mov RETURN_REG,ARG0 /* No-op on 32-bit */ add $STACK_PADDING,STACK_REG jmp *QUOT_XT_OFFSET(ARG0) /* Call the quotation */ diff --git a/vm/cpu-x86.hpp b/vm/cpu-x86.hpp index 4a37a17889..cf706a62b0 100644 --- a/vm/cpu-x86.hpp +++ b/vm/cpu-x86.hpp @@ -69,9 +69,9 @@ inline static unsigned int fpu_status(unsigned int status) } /* Defined in assembly */ -VM_ASM_API void c_to_factor(cell quot); +VM_ASM_API void c_to_factor(cell quot,void *vm); VM_ASM_API void throw_impl(cell quot, stack_frame *rewind_to); -VM_ASM_API void lazy_jit_compile(cell quot); +VM_ASM_API void lazy_jit_compile(cell quot, void *vm); VM_C_API void set_callstack(stack_frame *to, stack_frame *from, diff --git a/vm/os-genunix.cpp b/vm/os-genunix.cpp index 6cca455eb7..29c3e79859 100644 --- a/vm/os-genunix.cpp +++ b/vm/os-genunix.cpp @@ -5,7 +5,7 @@ namespace factor void c_to_factor_toplevel(cell quot) { - c_to_factor(quot); + c_to_factor(quot,vm); } void init_signals() diff --git a/vm/os-macosx.mm b/vm/os-macosx.mm index c7e9c0bf6b..865371b8ac 100644 --- a/vm/os-macosx.mm +++ b/vm/os-macosx.mm @@ -10,7 +10,7 @@ void c_to_factor_toplevel(cell quot) for(;;) { NS_DURING - c_to_factor(quot); + c_to_factor(quot,vm); NS_VOIDRETURN; NS_HANDLER dpush(vm->allot_alien(F,(cell)localException)); diff --git a/vm/os-windows-ce.cpp b/vm/os-windows-ce.cpp index 2e69a1eb5b..a3192b07f5 100644 --- a/vm/os-windows-ce.cpp +++ b/vm/os-windows-ce.cpp @@ -37,7 +37,7 @@ PRIMITIVE(os_envs) void c_to_factor_toplevel(cell quot) { - c_to_factor(quot); + c_to_factor(quot,vm); } void open_console() { } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 26781ee4f9..535e7b8640 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -62,7 +62,7 @@ void factorvm::c_to_factor_toplevel(cell quot) { if(!AddVectoredExceptionHandler(0, (PVECTORED_EXCEPTION_HANDLER)exception_handler)) fatal_error("AddVectoredExceptionHandler failed", 0); - c_to_factor(quot); + c_to_factor(quot,this); RemoveVectoredExceptionHandler((void *)exception_handler); } diff --git a/vm/quotations.cpp b/vm/quotations.cpp index b7bce0bfd7..34fe6a12a6 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -368,7 +368,7 @@ cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) return quot.value(); } -VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack) +VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack, factorvm *myvm) { return vm->lazy_jit_compile_impl(quot_,stack); } diff --git a/vm/quotations.hpp b/vm/quotations.hpp index 6c8b17db21..ae24a522f9 100755 --- a/vm/quotations.hpp +++ b/vm/quotations.hpp @@ -27,7 +27,7 @@ PRIMITIVE(jit_compile); PRIMITIVE(array_to_quotation); PRIMITIVE(quotation_xt); -VM_ASM_API cell lazy_jit_compile_impl(cell quot, stack_frame *stack); +VM_ASM_API cell lazy_jit_compile_impl(cell quot, stack_frame *stack, factorvm *myvm); PRIMITIVE(quot_compiled_p); From 465f06ebc24361566e0eba6cdb40b434a13d04c1 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Wed, 19 Aug 2009 19:40:09 +0100 Subject: [PATCH 185/345] throw_impl now forwards the vm ptr --- basis/cpu/x86/bootstrap.factor | 2 +- vm/cpu-x86.32.S | 13 +++++++++++++ vm/cpu-x86.64.S | 11 +++++++++++ vm/cpu-x86.S | 11 ----------- vm/cpu-x86.hpp | 2 +- vm/errors.cpp | 4 ++-- vm/quotations.cpp | 2 +- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/basis/cpu/x86/bootstrap.factor b/basis/cpu/x86/bootstrap.factor index 8949b24b1a..5bc5272ab4 100644 --- a/basis/cpu/x86/bootstrap.factor +++ b/basis/cpu/x86/bootstrap.factor @@ -252,7 +252,7 @@ big-endian off ! pop stack ds-reg bootstrap-cell SUB ! pass vm pointer - arg2 0 MOV rc-absolute-cell rt-vm jit-rel + arg2 0 MOV rc-absolute-cell rt-vm jit-rel ! call quotation arg quot-xt-offset [+] JMP ] \ (call) define-sub-primitive diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index 87a0e03f99..9d2bf082d1 100644 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -79,6 +79,19 @@ DEF(void,set_x87_env,(const void*)): fldcw 2(%eax) ret +DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to, void *vm)): + mov CELL_SIZE(STACK_REG),NV_TEMP_REG /* stash vm ptr */ + /* clear x87 stack, but preserve rounding mode and exception flags */ + sub $2,STACK_REG + fnstcw (STACK_REG) + fninit + fldcw (STACK_REG) + /* rewind_to */ + mov ARG1,STACK_REG + mov NV_TEMP_REG,ARG1 + jmp *QUOT_XT_OFFSET(ARG0) + + #include "cpu-x86.S" #ifdef WINDOWS diff --git a/vm/cpu-x86.64.S b/vm/cpu-x86.64.S index 0da360e675..606c81c582 100644 --- a/vm/cpu-x86.64.S +++ b/vm/cpu-x86.64.S @@ -88,6 +88,7 @@ DEF(void,primitive_inline_cache_miss_tail,(void)): add $STACK_PADDING,%rsp jmp *%rax +<<<<<<< HEAD DEF(void,get_sse_env,(void*)): stmxcsr (%rdi) ret @@ -106,4 +107,14 @@ DEF(void,set_x87_env,(const void*)): fldcw 2(%rdi) ret +DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to, void *vm)): + /* clear x87 stack, but preserve rounding mode and exception flags */ + sub $2,STACK_REG + fnstcw (STACK_REG) + fninit + fldcw (STACK_REG) + /* rewind_to */ + mov ARG1,STACK_REG + jmp *QUOT_XT_OFFSET(ARG0) + #include "cpu-x86.S" diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index 384bd794c5..03f08fdabc 100644 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -37,7 +37,6 @@ multiply_overflow: DEF(F_FASTCALL void,c_to_factor,(CELL quot, void *vm)): PUSH_NONVOLATILE mov ARG0,NV_TEMP_REG - //mov $35,ARG1 /* Create register shadow area for Win64 */ sub $32,STACK_REG @@ -57,16 +56,6 @@ DEF(F_FASTCALL void,c_to_factor,(CELL quot, void *vm)): POP_NONVOLATILE ret -DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to)): - /* clear x87 stack, but preserve rounding mode and exception flags */ - sub $2,STACK_REG - fnstcw (STACK_REG) - fninit - fldcw (STACK_REG) - /* rewind_to */ - mov ARG1,STACK_REG - jmp *QUOT_XT_OFFSET(ARG0) - DEF(F_FASTCALL void,lazy_jit_compile,(CELL quot, void *vm)): mov ARG1,NV_TEMP_REG /* stash vm ptr */ mov STACK_REG,ARG1 /* Save stack pointer */ diff --git a/vm/cpu-x86.hpp b/vm/cpu-x86.hpp index cf706a62b0..8fe0cc4b10 100644 --- a/vm/cpu-x86.hpp +++ b/vm/cpu-x86.hpp @@ -70,7 +70,7 @@ inline static unsigned int fpu_status(unsigned int status) /* Defined in assembly */ VM_ASM_API void c_to_factor(cell quot,void *vm); -VM_ASM_API void throw_impl(cell quot, stack_frame *rewind_to); +VM_ASM_API void throw_impl(cell quot, stack_frame *rewind_to, void *vm); VM_ASM_API void lazy_jit_compile(cell quot, void *vm); VM_C_API void set_callstack(stack_frame *to, diff --git a/vm/errors.cpp b/vm/errors.cpp index f2cab405a8..e1266cf608 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -78,7 +78,7 @@ void factorvm::throw_error(cell error, stack_frame *callstack_top) else callstack_top = stack_chain->callstack_top; - throw_impl(userenv[BREAK_ENV],callstack_top); + throw_impl(userenv[BREAK_ENV],callstack_top,this); } /* Error was thrown in early startup before error handler is set, just crash. */ @@ -167,7 +167,7 @@ void factorvm::fp_trap_error(unsigned int fpu_status, stack_frame *signal_callst inline void factorvm::vmprim_call_clear() { - throw_impl(dpop(),stack_chain->callstack_bottom); + throw_impl(dpop(),stack_chain->callstack_bottom,this); } PRIMITIVE(call_clear) diff --git a/vm/quotations.cpp b/vm/quotations.cpp index 34fe6a12a6..9654e2eff3 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -370,7 +370,7 @@ cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack, factorvm *myvm) { - return vm->lazy_jit_compile_impl(quot_,stack); + return VM_PTR->lazy_jit_compile_impl(quot_,stack); } inline void factorvm::vmprim_quot_compiled_p() From 25bbca2f66dd44dfa205a8e6122a39958cf5b77e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 20 Aug 2009 18:57:04 +0100 Subject: [PATCH 186/345] removed save_stacks global function --- vm/contexts.cpp | 5 ----- vm/contexts.hpp | 1 - 2 files changed, 6 deletions(-) diff --git a/vm/contexts.cpp b/vm/contexts.cpp index 448351baf7..3d627ab050 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -35,11 +35,6 @@ void factorvm::save_stacks() } } -void save_stacks() -{ - return vm->save_stacks(); -} - context *factorvm::alloc_context() { context *new_context; diff --git a/vm/contexts.hpp b/vm/contexts.hpp index 00d9646424..905d3d5b49 100644 --- a/vm/contexts.hpp +++ b/vm/contexts.hpp @@ -50,7 +50,6 @@ PRIMITIVE(set_datastack); PRIMITIVE(set_retainstack); PRIMITIVE(check_datastack); -VM_C_API void save_stacks(); VM_C_API void nest_stacks(); VM_C_API void unnest_stacks(); From 9a37b6abb63149c7aadc68bc6a32b8568a0f8a53 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 20 Aug 2009 19:20:48 +0100 Subject: [PATCH 187/345] moved stack_chain into vm struct --- basis/cpu/x86/x86.factor | 4 ++-- vm/contexts.cpp | 2 -- vm/contexts.hpp | 1 - vm/cpu-x86.64.S | 2 +- vm/vm.hpp | 3 +++ 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index da391f6320..798b67bc1a 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -612,7 +612,7 @@ M:: x86 %call-gc ( gc-root-count -- ) ! Call GC "inline_gc" f %vm-invoke ; -M: x86 %alien-global +M: x86 %alien-global ( dst symbol library -- ) [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ; M: x86 %epilogue ( n -- ) cell - incr-stack-reg ; @@ -742,7 +742,7 @@ M:: x86 %save-context ( temp1 temp2 callback-allowed? -- ) #! Save Factor stack pointers in case the C code calls a #! callback which does a GC, which must reliably trace #! all roots. - temp1 "stack_chain" f %alien-global + temp1 0 MOV rc-absolute-cell rt-vm rel-fixup ! stack-chain is first item in vm struct. TODO: make vm C-STRUCT temp1 temp1 [] MOV temp2 stack-reg cell neg [+] LEA temp1 [] temp2 MOV diff --git a/vm/contexts.cpp b/vm/contexts.cpp index 3d627ab050..f5c63f1e7f 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -1,7 +1,5 @@ #include "master.hpp" -factor::context *stack_chain; - namespace factor { diff --git a/vm/contexts.hpp b/vm/contexts.hpp index 905d3d5b49..17f8a7eb70 100644 --- a/vm/contexts.hpp +++ b/vm/contexts.hpp @@ -55,4 +55,3 @@ VM_C_API void unnest_stacks(); } -VM_C_API factor::context *stack_chain; diff --git a/vm/cpu-x86.64.S b/vm/cpu-x86.64.S index 606c81c582..98addeb80d 100644 --- a/vm/cpu-x86.64.S +++ b/vm/cpu-x86.64.S @@ -88,7 +88,7 @@ DEF(void,primitive_inline_cache_miss_tail,(void)): add $STACK_PADDING,%rsp jmp *%rax -<<<<<<< HEAD + DEF(void,get_sse_env,(void*)): stmxcsr (%rdi) ret diff --git a/vm/vm.hpp b/vm/vm.hpp index 9af0c5c8f9..40be36b249 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -3,6 +3,9 @@ namespace factor struct factorvm { + factor::context *stack_chain; + + // segments inline cell align_page(cell a); From 88d31793580584773f7e5235f7a7eb8d6cddb483 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 20 Aug 2009 19:39:40 +0100 Subject: [PATCH 188/345] Added a vm C-STRUCT, using it for struct offsets in x86 asm --- basis/cpu/x86/x86.factor | 13 +++++++++++-- basis/vm/authors.txt | 1 + basis/vm/summary.txt | 1 + basis/vm/vm.factor | 8 ++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 basis/vm/authors.txt create mode 100644 basis/vm/summary.txt create mode 100644 basis/vm/vm.factor diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 798b67bc1a..c142818c7f 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -1,5 +1,6 @@ ! Copyright (C) 2005, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. +<<<<<<< HEAD USING: accessors assocs alien alien.c-types arrays strings cpu.x86.assembler cpu.x86.assembler.private cpu.x86.assembler.operands cpu.architecture kernel kernel.private math memory namespaces make @@ -12,6 +13,14 @@ compiler.cfg.comparisons compiler.cfg.stack-frame compiler.codegen compiler.codegen.fixup ; +======= +USING: accessors alien combinators compiler.cfg.comparisons +compiler.cfg.intrinsics compiler.cfg.registers +compiler.cfg.stack-frame compiler.codegen.fixup compiler.constants +cpu.architecture cpu.x86.assembler cpu.x86.assembler.operands fry +kernel layouts locals make math math.order namespaces sequences system +vm ; +>>>>>>> Added a vm C-STRUCT, using it for struct offsets in x86 asm IN: cpu.x86 << enable-fixnum-log2 >> @@ -742,8 +751,8 @@ M:: x86 %save-context ( temp1 temp2 callback-allowed? -- ) #! Save Factor stack pointers in case the C code calls a #! callback which does a GC, which must reliably trace #! all roots. - temp1 0 MOV rc-absolute-cell rt-vm rel-fixup ! stack-chain is first item in vm struct. TODO: make vm C-STRUCT - temp1 temp1 [] MOV + temp1 0 MOV rc-absolute-cell rt-vm rel-fixup + temp1 temp1 "stack_chain" vm-offset [+] MOV temp2 stack-reg cell neg [+] LEA temp1 [] temp2 MOV callback-allowed? [ diff --git a/basis/vm/authors.txt b/basis/vm/authors.txt new file mode 100644 index 0000000000..b125620d17 --- /dev/null +++ b/basis/vm/authors.txt @@ -0,0 +1 @@ +Phil Dawes \ No newline at end of file diff --git a/basis/vm/summary.txt b/basis/vm/summary.txt new file mode 100644 index 0000000000..bfa1067bc7 --- /dev/null +++ b/basis/vm/summary.txt @@ -0,0 +1 @@ +Layout of the C vm structure diff --git a/basis/vm/vm.factor b/basis/vm/vm.factor new file mode 100644 index 0000000000..335f80918b --- /dev/null +++ b/basis/vm/vm.factor @@ -0,0 +1,8 @@ +! Copyright (C) 2009 Phil Dawes. +! See http://factorcode.org/license.txt for BSD license. +USING: alien.structs alien.syntax ; +IN: vm + +C-STRUCT: vm { "context*" "stack_chain" } ; + +: vm-offset ( field -- offset ) "vm" offset-of ; \ No newline at end of file From 0b0937cf0ebac8dc7f15727e2d2c29679d77aab6 Mon Sep 17 00:00:00 2001 From: sheeple Date: Sun, 30 Aug 2009 14:55:46 -0500 Subject: [PATCH 189/345] ppc asm to get stack_chain using vm ptr --- basis/cpu/ppc/ppc.factor | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index 9c829bc390..44309b15c6 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -7,7 +7,7 @@ cpu.ppc.assembler cpu.ppc.assembler.backend compiler.cfg.registers compiler.cfg.instructions compiler.cfg.comparisons compiler.codegen.fixup compiler.cfg.intrinsics compiler.cfg.stack-frame compiler.cfg.build-stack-frame -compiler.units compiler.constants compiler.codegen ; +compiler.units compiler.constants compiler.codegen vm ; FROM: cpu.ppc.assembler => B ; IN: cpu.ppc @@ -678,11 +678,18 @@ M: ppc %box-large-struct ( n c-type -- ) ! Call the function "box_value_struct" f %alien-invoke ; +: %load-vm-addr ( reg -- ) + 0 swap LOAD32 rc-absolute-ppc-2/2 rt-vm rel-fixup ; + +: %load-vm-field-addr ( reg symbol -- ) + [ drop %load-vm-addr ] + [ [ dup ] dip vm-offset ADDI ] 2bi ; + M:: ppc %save-context ( temp1 temp2 callback-allowed? -- ) #! Save Factor stack pointers in case the C code calls a #! callback which does a GC, which must reliably trace #! all roots. - temp1 "stack_chain" f %alien-global + temp1 "stack_chain" %load-vm-field-addr temp1 temp1 0 LWZ 1 temp1 0 STW callback-allowed? [ From 0be499de8aeee84ac2b17f545b145d3c148253b5 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 20 Aug 2009 19:58:39 +0100 Subject: [PATCH 190/345] renamed to vm-field-offset. Slava's better at naming than me --- basis/cpu/ppc/ppc.factor | 2 +- basis/cpu/x86/x86.factor | 2 +- basis/vm/vm.factor | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index 44309b15c6..8491a933db 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -683,7 +683,7 @@ M: ppc %box-large-struct ( n c-type -- ) : %load-vm-field-addr ( reg symbol -- ) [ drop %load-vm-addr ] - [ [ dup ] dip vm-offset ADDI ] 2bi ; + [ [ dup ] dip vm-field-offset ADDI ] 2bi ; M:: ppc %save-context ( temp1 temp2 callback-allowed? -- ) #! Save Factor stack pointers in case the C code calls a diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index c142818c7f..9222e63b7f 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -752,7 +752,7 @@ M:: x86 %save-context ( temp1 temp2 callback-allowed? -- ) #! callback which does a GC, which must reliably trace #! all roots. temp1 0 MOV rc-absolute-cell rt-vm rel-fixup - temp1 temp1 "stack_chain" vm-offset [+] MOV + temp1 temp1 "stack_chain" vm-field-offset [+] MOV temp2 stack-reg cell neg [+] LEA temp1 [] temp2 MOV callback-allowed? [ diff --git a/basis/vm/vm.factor b/basis/vm/vm.factor index 335f80918b..5ae82c1c7a 100644 --- a/basis/vm/vm.factor +++ b/basis/vm/vm.factor @@ -5,4 +5,4 @@ IN: vm C-STRUCT: vm { "context*" "stack_chain" } ; -: vm-offset ( field -- offset ) "vm" offset-of ; \ No newline at end of file +: vm-field-offset ( field -- offset ) "vm" offset-of ; \ No newline at end of file From c010afc34597d3dedb218cf13357f2cc24c8c094 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 20 Aug 2009 20:20:35 +0100 Subject: [PATCH 191/345] nursery global variable moved into vm --- basis/cpu/ppc/ppc.factor | 17 +++++++++-------- basis/cpu/x86/x86.factor | 3 ++- basis/vm/vm.factor | 14 +++++++++++++- vm/data_heap.cpp | 5 ----- vm/data_heap.hpp | 3 --- vm/vm.hpp | 7 ++++--- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index 8491a933db..a6d70f88e8 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -29,6 +29,14 @@ enable-float-intrinsics \ ##float>integer t frame-required? set-word-prop >> +: %load-vm-addr ( reg -- ) + 0 swap LOAD32 rc-absolute-ppc-2/2 rt-vm rel-fixup ; + +: %load-vm-field-addr ( reg symbol -- ) + [ drop %load-vm-addr ] + [ [ dup ] dip vm-field-offset ADDI ] 2bi ; + + M: ppc machine-registers { { int-regs $[ 2 12 [a,b] 15 29 [a,b] append ] } @@ -418,7 +426,7 @@ M: ppc %set-alien-float swap 0 STFS ; M: ppc %set-alien-double swap 0 STFD ; : load-zone-ptr ( reg -- ) - "nursery" f %alien-global ; + "nursery" %load-vm-field-addr ; : load-allot-ptr ( nursery-ptr allot-ptr -- ) [ drop load-zone-ptr ] [ swap 4 LWZ ] 2bi ; @@ -678,13 +686,6 @@ M: ppc %box-large-struct ( n c-type -- ) ! Call the function "box_value_struct" f %alien-invoke ; -: %load-vm-addr ( reg -- ) - 0 swap LOAD32 rc-absolute-ppc-2/2 rt-vm rel-fixup ; - -: %load-vm-field-addr ( reg symbol -- ) - [ drop %load-vm-addr ] - [ [ dup ] dip vm-field-offset ADDI ] 2bi ; - M:: ppc %save-context ( temp1 temp2 callback-allowed? -- ) #! Save Factor stack pointers in case the C code calls a #! callback which does a GC, which must reliably trace diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 9222e63b7f..4f79f50f96 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -566,7 +566,8 @@ M: x86 %sar [ SAR ] emit-shift ; : load-zone-ptr ( reg -- ) #! Load pointer to start of zone array - 0 MOV "nursery" f rc-absolute-cell rel-dlsym ; + [ 0 MOV rc-absolute-cell rt-vm rel-fixup ] + [ "nursery" vm-field-offset ADD ] bi ; : load-allot-ptr ( nursery-ptr allot-ptr -- ) [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ; diff --git a/basis/vm/vm.factor b/basis/vm/vm.factor index 5ae82c1c7a..19d009f121 100644 --- a/basis/vm/vm.factor +++ b/basis/vm/vm.factor @@ -3,6 +3,18 @@ USING: alien.structs alien.syntax ; IN: vm -C-STRUCT: vm { "context*" "stack_chain" } ; +TYPEDEF: void* cell + +C-STRUCT: zone + { "cell" "start" } + { "cell" "here" } + { "cell" "size" } + { "cell" "end" } + ; + +C-STRUCT: vm + { "context*" "stack_chain" } + { "zone" "nursery" } + ; : vm-field-offset ( field -- offset ) "vm" offset-of ; \ No newline at end of file diff --git a/vm/data_heap.cpp b/vm/data_heap.cpp index e790c63122..de3d8d87be 100755 --- a/vm/data_heap.cpp +++ b/vm/data_heap.cpp @@ -1,13 +1,8 @@ #include "master.hpp" -factor::zone nursery; - namespace factor { -/* new objects are allocated here */ -VM_C_API zone nursery; - cell factorvm::init_zone(zone *z, cell size, cell start) { z->size = size; diff --git a/vm/data_heap.hpp b/vm/data_heap.hpp index 88316ffd8d..7e6ff81e70 100755 --- a/vm/data_heap.hpp +++ b/vm/data_heap.hpp @@ -66,6 +66,3 @@ PRIMITIVE(next_object); PRIMITIVE(end_scan); } - -/* new objects are allocated here */ -VM_C_API factor::zone nursery; diff --git a/vm/vm.hpp b/vm/vm.hpp index 40be36b249..12f767404f 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -2,9 +2,10 @@ namespace factor { struct factorvm { - - factor::context *stack_chain; - + // if you change this struct, also change vm.factor + context *stack_chain; + /* new objects are allocated here */ + zone nursery; // segments inline cell align_page(cell a); From c6d855d494984df5879635feb7b6db68efb72b53 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 20 Aug 2009 20:35:54 +0100 Subject: [PATCH 192/345] moved allot_markers_offset variable into vm struct --- vm/vm.hpp | 2 ++ vm/write_barrier.cpp | 4 ---- vm/write_barrier.hpp | 1 - 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/vm/vm.hpp b/vm/vm.hpp index 12f767404f..7508abf5ea 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -7,6 +7,7 @@ struct factorvm { /* new objects are allocated here */ zone nursery; + // segments inline cell align_page(cell a); @@ -169,6 +170,7 @@ struct factorvm { //write barrier + cell allot_markers_offset; inline card *addr_to_card(cell a); inline cell card_to_addr(card *c); inline cell card_offset(card *c); diff --git a/vm/write_barrier.cpp b/vm/write_barrier.cpp index 0e87434b56..7ce764722e 100644 --- a/vm/write_barrier.cpp +++ b/vm/write_barrier.cpp @@ -5,7 +5,3 @@ using namespace factor; cell cards_offset; cell decks_offset; -namespace factor -{ - cell allot_markers_offset; -} diff --git a/vm/write_barrier.hpp b/vm/write_barrier.hpp index b45573b126..b2b370d274 100755 --- a/vm/write_barrier.hpp +++ b/vm/write_barrier.hpp @@ -29,6 +29,5 @@ static const cell deck_bits = (card_bits + 10); static const cell deck_size = (1< Date: Thu, 20 Aug 2009 20:45:06 +0100 Subject: [PATCH 193/345] moved cards_offset and decks_offset into vm struct (for x86) --- basis/cpu/ppc/ppc.factor | 4 ++-- basis/cpu/x86/x86.factor | 8 ++++++-- basis/vm/vm.factor | 2 ++ vm/vm.hpp | 6 +++--- vm/write_barrier.cpp | 2 -- vm/write_barrier.hpp | 3 --- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index a6d70f88e8..37a5369259 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -449,10 +449,10 @@ M:: ppc %allot ( dst size class nursery-ptr -- ) dst class store-tagged ; : load-cards-offset ( dst -- ) - [ "cards_offset" f %alien-global ] [ dup 0 LWZ ] bi ; + [ "cards_offset" %load-vm-field-addr ] [ dup 0 LWZ ] bi ; : load-decks-offset ( dst -- ) - [ "decks_offset" f %alien-global ] [ dup 0 LWZ ] bi ; + [ "decks_offset" %load-vm-field-addr ] [ dup 0 LWZ ] bi ; M:: ppc %write-barrier ( src card# table -- ) card-mark scratch-reg LI diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 4f79f50f96..7e73275dde 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -587,18 +587,22 @@ M:: x86 %allot ( dst size class nursery-ptr -- ) dst class store-tagged nursery-ptr size inc-allot-ptr ; +: %vm-field-ptr ( reg field -- ) + [ drop 0 MOV rc-absolute-cell rt-vm rel-fixup ] + [ vm-field-offset ADD ] 2bi ; + M:: x86 %write-barrier ( src card# table -- ) #! Mark the card pointed to by vreg. ! Mark the card card# src MOV card# card-bits SHR - table "cards_offset" f %alien-global + table "cards_offset" %vm-field-ptr table table [] MOV table card# [+] card-mark MOV ! Mark the card deck card# deck-bits card-bits - SHR - table "decks_offset" f %alien-global + table "decks_offset" %vm-field-ptr table table [] MOV table card# [+] card-mark MOV ; diff --git a/basis/vm/vm.factor b/basis/vm/vm.factor index 19d009f121..655250d755 100644 --- a/basis/vm/vm.factor +++ b/basis/vm/vm.factor @@ -15,6 +15,8 @@ C-STRUCT: zone C-STRUCT: vm { "context*" "stack_chain" } { "zone" "nursery" } + { "cell" "cards_offset" } + { "cell" "decks_offset" } ; : vm-field-offset ( field -- offset ) "vm" offset-of ; \ No newline at end of file diff --git a/vm/vm.hpp b/vm/vm.hpp index 7508abf5ea..00a24e4502 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -4,9 +4,9 @@ namespace factor struct factorvm { // if you change this struct, also change vm.factor context *stack_chain; - /* new objects are allocated here */ - zone nursery; - + zone nursery; /* new objects are allocated here */ + cell cards_offset; + cell decks_offset; // segments inline cell align_page(cell a); diff --git a/vm/write_barrier.cpp b/vm/write_barrier.cpp index 7ce764722e..72879aab4b 100644 --- a/vm/write_barrier.cpp +++ b/vm/write_barrier.cpp @@ -2,6 +2,4 @@ using namespace factor; -cell cards_offset; -cell decks_offset; diff --git a/vm/write_barrier.hpp b/vm/write_barrier.hpp index b2b370d274..7c0241a31a 100755 --- a/vm/write_barrier.hpp +++ b/vm/write_barrier.hpp @@ -6,9 +6,6 @@ card has a slot written to. the offset of the first object is set by the allocator. */ -VM_C_API factor::cell cards_offset; -VM_C_API factor::cell decks_offset; - namespace factor { From 43787e2664c5e8dc38ba2f14a753fa3c20abc0a5 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 08:21:44 +0100 Subject: [PATCH 194/345] moved stack_traces_p into the vm --- vm/code_block.hpp | 5 ----- vm/jit.cpp | 2 +- vm/vm.hpp | 10 +++++++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/vm/code_block.hpp b/vm/code_block.hpp index 50d937f4af..17ccdfe8ab 100644 --- a/vm/code_block.hpp +++ b/vm/code_block.hpp @@ -72,9 +72,4 @@ void copy_literal_references(code_block *compiled, factorvm *myvm); void update_word_references(code_block *compiled, factorvm *myvm); void update_literal_and_word_references(code_block *compiled, factorvm *myvm); -inline bool stack_traces_p() -{ - return userenv[STACK_TRACES_ENV] != F; -} - } diff --git a/vm/jit.cpp b/vm/jit.cpp index 7177f26073..d474d23a18 100644 --- a/vm/jit.cpp +++ b/vm/jit.cpp @@ -21,7 +21,7 @@ jit::jit(cell type_, cell owner_, factorvm *vm) offset(0), myvm(vm) { - if(stack_traces_p()) literal(owner.value()); + if(myvm->stack_traces_p()) literal(owner.value()); } void jit::emit_relocation(cell code_template_) diff --git a/vm/vm.hpp b/vm/vm.hpp index 00a24e4502..88b323adf4 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -2,11 +2,15 @@ namespace factor { struct factorvm { - // if you change this struct, also change vm.factor + + // if you change this struct, also change vm.factor k-------- context *stack_chain; zone nursery; /* new objects are allocated here */ cell cards_offset; cell decks_offset; + // cell userenv[USER_ENV]; // prob best to put this last + + // segments inline cell align_page(cell a); @@ -494,6 +498,10 @@ struct factorvm { void fixup_labels(array *labels, code_block *compiled); code_block *allot_code_block(cell size); code_block *add_code_block(cell type,cell code_,cell labels_,cell relocation_,cell literals_); + inline bool stack_traces_p() + { + return userenv[STACK_TRACES_ENV] != F; + } //code_heap heap code; From a4a4439fc54a44ad27f1bb3587fc3390846f9f01 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 18:33:04 +0100 Subject: [PATCH 195/345] got debug compiles working again --- vm/code_block.cpp | 4 ++-- vm/tagged.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vm/code_block.cpp b/vm/code_block.cpp index a64a8d2c2d..9eec4a2ea6 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -345,8 +345,8 @@ void copy_literal_references(code_block *compiled, factorvm *myvm) void factorvm::relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled) { #ifdef FACTOR_DEBUG - tagged(compiled->literals).untag_check(); - tagged(compiled->relocation).untag_check(); + tagged(compiled->literals).untag_check(vm); + tagged(compiled->relocation).untag_check(vm); #endif store_address_in_code_block(relocation_class_of(rel), diff --git a/vm/tagged.hpp b/vm/tagged.hpp index 9f8a0eb411..b73f3aeb7a 100755 --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -37,13 +37,13 @@ struct tagged explicit tagged(cell tagged) : value_(tagged) { #ifdef FACTOR_DEBUG - untag_check(); + untag_check(vm); #endif } explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { #ifdef FACTOR_DEBUG - untag_check(); + untag_check(vm); #endif } From ef16c4be66727649ed66b16f1dc363868c6eebf3 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 19:27:40 +0100 Subject: [PATCH 196/345] moved userenv into vm in C code (DOESNT BOOTSTRAP YET!!!) --- vm/dispatch.cpp | 10 +++++----- vm/inline_cache.cpp | 8 ++++---- vm/jit.cpp | 4 ++-- vm/jit.hpp | 8 ++++---- vm/quotations.cpp | 44 ++++++++++++++++++++++---------------------- vm/run.cpp | 2 -- vm/run.hpp | 3 +-- vm/vm.hpp | 3 +-- 8 files changed, 39 insertions(+), 43 deletions(-) diff --git a/vm/dispatch.cpp b/vm/dispatch.cpp index 85d92f90a0..e87cdeac70 100755 --- a/vm/dispatch.cpp +++ b/vm/dispatch.cpp @@ -207,21 +207,21 @@ void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cac emit_class_lookup(index,PIC_HI_TAG_TUPLE); /* Do a cache lookup. */ - emit_with(userenv[MEGA_LOOKUP],cache.value()); + emit_with(myvm->userenv[MEGA_LOOKUP],cache.value()); /* If we end up here, the cache missed. */ - emit(userenv[JIT_PROLOG]); + emit(myvm->userenv[JIT_PROLOG]); /* Push index, method table and cache on the stack. */ push(methods.value()); push(tag_fixnum(index)); push(cache.value()); - word_call(userenv[MEGA_MISS_WORD]); + word_call(myvm->userenv[MEGA_MISS_WORD]); /* Now the new method has been stored into the cache, and its on the stack. */ - emit(userenv[JIT_EPILOG]); - emit(userenv[JIT_EXECUTE_JUMP]); + emit(myvm->userenv[JIT_EPILOG]); + emit(myvm->userenv[JIT_EXECUTE_JUMP]); } } diff --git a/vm/inline_cache.cpp b/vm/inline_cache.cpp index 24008cae79..35479c29f5 100755 --- a/vm/inline_cache.cpp +++ b/vm/inline_cache.cpp @@ -89,9 +89,9 @@ void inline_cache_jit::emit_check(cell klass) { cell code_template; if(TAG(klass) == FIXNUM_TYPE && untag_fixnum(klass) < HEADER_TYPE) - code_template = userenv[PIC_CHECK_TAG]; + code_template = myvm->userenv[PIC_CHECK_TAG]; else - code_template = userenv[PIC_CHECK]; + code_template = myvm->userenv[PIC_CHECK]; emit_with(code_template,klass); } @@ -124,7 +124,7 @@ void inline_cache_jit::compile_inline_cache(fixnum index, /* Yes? Jump to method */ cell method = array_nth(cache_entries.untagged(),i + 1); - emit_with(userenv[PIC_HIT],method); + emit_with(myvm->userenv[PIC_HIT],method); } /* Generate machine code to handle a cache miss, which ultimately results in @@ -136,7 +136,7 @@ void inline_cache_jit::compile_inline_cache(fixnum index, push(methods.value()); push(tag_fixnum(index)); push(cache_entries.value()); - word_special(userenv[tail_call_p ? PIC_MISS_TAIL_WORD : PIC_MISS_WORD]); + word_special(myvm->userenv[tail_call_p ? PIC_MISS_TAIL_WORD : PIC_MISS_WORD]); } code_block *factorvm::compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p) diff --git a/vm/jit.cpp b/vm/jit.cpp index d474d23a18..cdb5acace3 100644 --- a/vm/jit.cpp +++ b/vm/jit.cpp @@ -81,8 +81,8 @@ void jit::emit_with(cell code_template_, cell argument_) { void jit::emit_class_lookup(fixnum index, cell type) { - emit_with(userenv[PIC_LOAD],tag_fixnum(-index * sizeof(cell))); - emit(userenv[type]); + emit_with(myvm->userenv[PIC_LOAD],tag_fixnum(-index * sizeof(cell))); + emit(myvm->userenv[type]); } /* Facility to convert compiled code offsets to quotation offsets. diff --git a/vm/jit.hpp b/vm/jit.hpp index 81754be26a..a44f359ffe 100644 --- a/vm/jit.hpp +++ b/vm/jit.hpp @@ -22,21 +22,21 @@ struct jit { void emit_with(cell code_template_, cell literal_); void push(cell literal) { - emit_with(userenv[JIT_PUSH_IMMEDIATE],literal); + emit_with(myvm->userenv[JIT_PUSH_IMMEDIATE],literal); } void word_jump(cell word) { literal(tag_fixnum(xt_tail_pic_offset)); literal(word); - emit(userenv[JIT_WORD_JUMP]); + emit(myvm->userenv[JIT_WORD_JUMP]); } void word_call(cell word) { - emit_with(userenv[JIT_WORD_CALL],word); + emit_with(myvm->userenv[JIT_WORD_CALL],word); } void word_special(cell word) { - emit_with(userenv[JIT_WORD_SPECIAL],word); + emit_with(myvm->userenv[JIT_WORD_SPECIAL],word); } void emit_subprimitive(cell word_) { diff --git a/vm/quotations.cpp b/vm/quotations.cpp index 9654e2eff3..7b03ada175 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -40,7 +40,7 @@ bool quotation_jit::primitive_call_p(cell i) { return (i + 2) == array_capacity(elements.untagged()) && tagged(array_nth(elements.untagged(),i)).type_p(FIXNUM_TYPE) - && array_nth(elements.untagged(),i + 1) == userenv[JIT_PRIMITIVE_WORD]; + && array_nth(elements.untagged(),i + 1) == myvm->userenv[JIT_PRIMITIVE_WORD]; } bool quotation_jit::fast_if_p(cell i) @@ -48,28 +48,28 @@ bool quotation_jit::fast_if_p(cell i) return (i + 3) == array_capacity(elements.untagged()) && tagged(array_nth(elements.untagged(),i)).type_p(QUOTATION_TYPE) && tagged(array_nth(elements.untagged(),i + 1)).type_p(QUOTATION_TYPE) - && array_nth(elements.untagged(),i + 2) == userenv[JIT_IF_WORD]; + && array_nth(elements.untagged(),i + 2) == myvm->userenv[JIT_IF_WORD]; } bool quotation_jit::fast_dip_p(cell i) { return (i + 2) <= array_capacity(elements.untagged()) && tagged(array_nth(elements.untagged(),i)).type_p(QUOTATION_TYPE) - && array_nth(elements.untagged(),i + 1) == userenv[JIT_DIP_WORD]; + && array_nth(elements.untagged(),i + 1) == myvm->userenv[JIT_DIP_WORD]; } bool quotation_jit::fast_2dip_p(cell i) { return (i + 2) <= array_capacity(elements.untagged()) && tagged(array_nth(elements.untagged(),i)).type_p(QUOTATION_TYPE) - && array_nth(elements.untagged(),i + 1) == userenv[JIT_2DIP_WORD]; + && array_nth(elements.untagged(),i + 1) == myvm->userenv[JIT_2DIP_WORD]; } bool quotation_jit::fast_3dip_p(cell i) { return (i + 2) <= array_capacity(elements.untagged()) && tagged(array_nth(elements.untagged(),i)).type_p(QUOTATION_TYPE) - && array_nth(elements.untagged(),i + 1) == userenv[JIT_3DIP_WORD]; + && array_nth(elements.untagged(),i + 1) == myvm->userenv[JIT_3DIP_WORD]; } bool quotation_jit::mega_lookup_p(cell i) @@ -78,7 +78,7 @@ bool quotation_jit::mega_lookup_p(cell i) && tagged(array_nth(elements.untagged(),i)).type_p(ARRAY_TYPE) && tagged(array_nth(elements.untagged(),i + 1)).type_p(FIXNUM_TYPE) && tagged(array_nth(elements.untagged(),i + 2)).type_p(ARRAY_TYPE) - && array_nth(elements.untagged(),i + 3) == userenv[MEGA_LOOKUP_WORD]; + && array_nth(elements.untagged(),i + 3) == myvm->userenv[MEGA_LOOKUP_WORD]; } bool quotation_jit::stack_frame_p() @@ -115,7 +115,7 @@ void quotation_jit::iterate_quotation() set_position(0); if(stack_frame) - emit(userenv[JIT_PROLOG]); + emit(myvm->userenv[JIT_PROLOG]); cell i; cell length = array_capacity(elements.untagged()); @@ -134,23 +134,23 @@ void quotation_jit::iterate_quotation() if(obj.as()->subprimitive != F) emit_subprimitive(obj.value()); /* The (execute) primitive is special-cased */ - else if(obj.value() == userenv[JIT_EXECUTE_WORD]) + else if(obj.value() == myvm->userenv[JIT_EXECUTE_WORD]) { if(i == length - 1) { - if(stack_frame) emit(userenv[JIT_EPILOG]); + if(stack_frame) emit(myvm->userenv[JIT_EPILOG]); tail_call = true; - emit(userenv[JIT_EXECUTE_JUMP]); + emit(myvm->userenv[JIT_EXECUTE_JUMP]); } else - emit(userenv[JIT_EXECUTE_CALL]); + emit(myvm->userenv[JIT_EXECUTE_CALL]); } /* Everything else */ else { if(i == length - 1) { - if(stack_frame) emit(userenv[JIT_EPILOG]); + if(stack_frame) emit(myvm->userenv[JIT_EPILOG]); tail_call = true; /* Inline cache misses are special-cased. The calling convention for tail @@ -160,8 +160,8 @@ void quotation_jit::iterate_quotation() the inline cache miss primitive, and we don't want to clobber the saved address. */ - if(obj.value() == userenv[PIC_MISS_WORD] - || obj.value() == userenv[PIC_MISS_TAIL_WORD]) + if(obj.value() == myvm->userenv[PIC_MISS_WORD] + || obj.value() == myvm->userenv[PIC_MISS_TAIL_WORD]) { word_special(obj.value()); } @@ -181,7 +181,7 @@ void quotation_jit::iterate_quotation() /* Primitive calls */ if(primitive_call_p(i)) { - emit_with(userenv[JIT_PRIMITIVE],obj.value()); + emit_with(myvm->userenv[JIT_PRIMITIVE],obj.value()); i++; @@ -193,7 +193,7 @@ void quotation_jit::iterate_quotation() mutually recursive in the library, but both still work) */ if(fast_if_p(i)) { - if(stack_frame) emit(userenv[JIT_EPILOG]); + if(stack_frame) emit(myvm->userenv[JIT_EPILOG]); tail_call = true; if(compiling) @@ -204,7 +204,7 @@ void quotation_jit::iterate_quotation() literal(array_nth(elements.untagged(),i)); literal(array_nth(elements.untagged(),i + 1)); - emit(userenv[JIT_IF]); + emit(myvm->userenv[JIT_IF]); i += 2; @@ -215,7 +215,7 @@ void quotation_jit::iterate_quotation() { if(compiling) myvm->jit_compile(obj.value(),relocate); - emit_with(userenv[JIT_DIP],obj.value()); + emit_with(myvm->userenv[JIT_DIP],obj.value()); i++; break; } @@ -224,7 +224,7 @@ void quotation_jit::iterate_quotation() { if(compiling) myvm->jit_compile(obj.value(),relocate); - emit_with(userenv[JIT_2DIP],obj.value()); + emit_with(myvm->userenv[JIT_2DIP],obj.value()); i++; break; } @@ -233,7 +233,7 @@ void quotation_jit::iterate_quotation() { if(compiling) myvm->jit_compile(obj.value(),relocate); - emit_with(userenv[JIT_3DIP],obj.value()); + emit_with(myvm->userenv[JIT_3DIP],obj.value()); i++; break; } @@ -260,8 +260,8 @@ void quotation_jit::iterate_quotation() set_position(length); if(stack_frame) - emit(userenv[JIT_EPILOG]); - emit(userenv[JIT_RETURN]); + emit(myvm->userenv[JIT_EPILOG]); + emit(myvm->userenv[JIT_RETURN]); } } diff --git a/vm/run.cpp b/vm/run.cpp index f8c099bbfd..1d670e3625 100755 --- a/vm/run.cpp +++ b/vm/run.cpp @@ -1,7 +1,5 @@ #include "master.hpp" -factor::cell userenv[USER_ENV]; - namespace factor { diff --git a/vm/run.hpp b/vm/run.hpp index 4b43a156e4..d10a6678b8 100755 --- a/vm/run.hpp +++ b/vm/run.hpp @@ -109,5 +109,4 @@ PRIMITIVE(clone); } -/* TAGGED user environment data; see getenv/setenv prims */ -VM_C_API factor::cell userenv[USER_ENV]; + diff --git a/vm/vm.hpp b/vm/vm.hpp index 88b323adf4..967956e0cf 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -8,8 +8,7 @@ struct factorvm { zone nursery; /* new objects are allocated here */ cell cards_offset; cell decks_offset; - // cell userenv[USER_ENV]; // prob best to put this last - + cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ // segments From 3b3ed501c773e336e5fd184a913f7213fa306981 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 19:54:47 +0100 Subject: [PATCH 197/345] added padding to align userenv to an 8byte boundary --- vm/vm.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/vm.hpp b/vm/vm.hpp index 967956e0cf..e0b598de6f 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -8,6 +8,7 @@ struct factorvm { zone nursery; /* new objects are allocated here */ cell cards_offset; cell decks_offset; + cell __padding__ ; // align to 8byte boundary (for 32bit platforms) cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ From ff8f2b10ece80f628b4055a656b714d0e573576c Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 20:13:49 +0100 Subject: [PATCH 198/345] fixed up getenv compiler intrinsic to use vm struct userenv --- .../cfg/alias-analysis/alias-analysis.factor | 2 ++ basis/compiler/cfg/hats/hats.factor | 2 +- .../cfg/instructions/instructions.factor | 4 ++++ .../compiler/cfg/intrinsics/misc/misc.factor | 2 +- basis/compiler/codegen/codegen.factor | 3 +++ basis/cpu/architecture/architecture.factor | 1 + basis/cpu/ppc/ppc.factor | 1 + basis/cpu/x86/x86.factor | 23 ++++++------------- basis/vm/vm.factor | 2 ++ vm/os-macosx.mm | 2 +- vm/vm.hpp | 8 +++++-- 11 files changed, 29 insertions(+), 21 deletions(-) diff --git a/basis/compiler/cfg/alias-analysis/alias-analysis.factor b/basis/compiler/cfg/alias-analysis/alias-analysis.factor index fcfc89ea52..cb8b2de543 100644 --- a/basis/compiler/cfg/alias-analysis/alias-analysis.factor +++ b/basis/compiler/cfg/alias-analysis/alias-analysis.factor @@ -190,12 +190,14 @@ M: ##slot-imm insn-slot# slot>> ; M: ##set-slot insn-slot# slot>> constant ; M: ##set-slot-imm insn-slot# slot>> ; M: ##alien-global insn-slot# [ library>> ] [ symbol>> ] bi 2array ; +M: ##vm-field-ptr insn-slot# fieldname>> 1array ; ! is this right? M: ##slot insn-object obj>> resolve ; M: ##slot-imm insn-object obj>> resolve ; M: ##set-slot insn-object obj>> resolve ; M: ##set-slot-imm insn-object obj>> resolve ; M: ##alien-global insn-object drop \ ##alien-global ; +M: ##vm-field-ptr insn-object drop \ ##vm-field-ptr ; : init-alias-analysis ( insns -- insns' ) H{ } clone histories set diff --git a/basis/compiler/cfg/hats/hats.factor b/basis/compiler/cfg/hats/hats.factor index 469ba37703..1b99b5d4dd 100644 --- a/basis/compiler/cfg/hats/hats.factor +++ b/basis/compiler/cfg/hats/hats.factor @@ -57,4 +57,4 @@ insn-classes get [ : ^^allot-byte-array ( n -- dst ) 2 cells + byte-array ^^allot ; inline : ^^offset>slot ( vreg -- vreg' ) cell 4 = [ 1 ^^shr-imm ] [ any-rep ^^copy ] if ; inline : ^^tag-fixnum ( src -- dst ) tag-bits get ^^shl-imm ; inline -: ^^untag-fixnum ( src -- dst ) tag-bits get ^^sar-imm ; inline \ No newline at end of file +: ^^untag-fixnum ( src -- dst ) tag-bits get ^^sar-imm ; inline diff --git a/basis/compiler/cfg/instructions/instructions.factor b/basis/compiler/cfg/instructions/instructions.factor index 32e5d46c61..7c28198f67 100644 --- a/basis/compiler/cfg/instructions/instructions.factor +++ b/basis/compiler/cfg/instructions/instructions.factor @@ -450,6 +450,10 @@ INSN: ##alien-global def: dst/int-rep literal: symbol library ; +INSN: ##vm-field-ptr +def: dst/int-rep +literal: fieldname ; + ! FFI INSN: ##alien-invoke literal: params stack-frame ; diff --git a/basis/compiler/cfg/intrinsics/misc/misc.factor b/basis/compiler/cfg/intrinsics/misc/misc.factor index f9f2182a4e..f9f3488773 100644 --- a/basis/compiler/cfg/intrinsics/misc/misc.factor +++ b/basis/compiler/cfg/intrinsics/misc/misc.factor @@ -10,7 +10,7 @@ IN: compiler.cfg.intrinsics.misc ds-pop tag-mask get ^^and-imm ^^tag-fixnum ds-push ; : emit-getenv ( node -- ) - "userenv" f ^^alien-global + "userenv" ^^vm-field-ptr swap node-input-infos first literal>> [ ds-drop 0 ^^slot-imm ] [ ds-pop ^^offset>slot 0 ^^slot ] if* ds-push ; diff --git a/basis/compiler/codegen/codegen.factor b/basis/compiler/codegen/codegen.factor index 0456ff485f..de15cda21c 100755 --- a/basis/compiler/codegen/codegen.factor +++ b/basis/compiler/codegen/codegen.factor @@ -270,6 +270,9 @@ M: ##alien-global generate-insn [ dst>> ] [ symbol>> ] [ library>> ] tri %alien-global ; +M: ##vm-field-ptr generate-insn + [ dst>> ] [ fieldname>> ] bi %vm-field-ptr ; + ! ##alien-invoke GENERIC: next-fastcall-param ( rep -- ) diff --git a/basis/cpu/architecture/architecture.factor b/basis/cpu/architecture/architecture.factor index da1bcfc61f..b9d07f578e 100644 --- a/basis/cpu/architecture/architecture.factor +++ b/basis/cpu/architecture/architecture.factor @@ -202,6 +202,7 @@ HOOK: %set-alien-double cpu ( ptr value -- ) HOOK: %set-alien-vector cpu ( ptr value rep -- ) HOOK: %alien-global cpu ( dst symbol library -- ) +HOOK: %vm-field-ptr cpu ( dst fieldname -- ) HOOK: %allot cpu ( dst size class temp -- ) HOOK: %write-barrier cpu ( src card# table -- ) diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index 37a5369259..fc6a122101 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -36,6 +36,7 @@ enable-float-intrinsics [ drop %load-vm-addr ] [ [ dup ] dip vm-field-offset ADDI ] 2bi ; +M: ppc %vm-field-ptr ( dst field -- ) %load-vm-field-addr ; M: ppc machine-registers { diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 7e73275dde..57517ba319 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -1,6 +1,5 @@ ! Copyright (C) 2005, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -<<<<<<< HEAD USING: accessors assocs alien alien.c-types arrays strings cpu.x86.assembler cpu.x86.assembler.private cpu.x86.assembler.operands cpu.architecture kernel kernel.private math memory namespaces make @@ -12,15 +11,7 @@ compiler.cfg.intrinsics compiler.cfg.comparisons compiler.cfg.stack-frame compiler.codegen -compiler.codegen.fixup ; -======= -USING: accessors alien combinators compiler.cfg.comparisons -compiler.cfg.intrinsics compiler.cfg.registers -compiler.cfg.stack-frame compiler.codegen.fixup compiler.constants -cpu.architecture cpu.x86.assembler cpu.x86.assembler.operands fry -kernel layouts locals make math math.order namespaces sequences system -vm ; ->>>>>>> Added a vm C-STRUCT, using it for struct offsets in x86 asm +compiler.codegen.fixup vm ; IN: cpu.x86 << enable-fixnum-log2 >> @@ -564,10 +555,13 @@ M: x86 %shl [ SHL ] emit-shift ; M: x86 %shr [ SHR ] emit-shift ; M: x86 %sar [ SAR ] emit-shift ; +M: x86 %vm-field-ptr ( dst field -- ) + [ drop 0 MOV rc-absolute-cell rt-vm rel-fixup ] + [ vm-field-offset ADD ] 2bi ; + : load-zone-ptr ( reg -- ) #! Load pointer to start of zone array - [ 0 MOV rc-absolute-cell rt-vm rel-fixup ] - [ "nursery" vm-field-offset ADD ] bi ; + "nursery" %vm-field-ptr ; : load-allot-ptr ( nursery-ptr allot-ptr -- ) [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ; @@ -587,9 +581,6 @@ M:: x86 %allot ( dst size class nursery-ptr -- ) dst class store-tagged nursery-ptr size inc-allot-ptr ; -: %vm-field-ptr ( reg field -- ) - [ drop 0 MOV rc-absolute-cell rt-vm rel-fixup ] - [ vm-field-offset ADD ] 2bi ; M:: x86 %write-barrier ( src card# table -- ) #! Mark the card pointed to by vreg. @@ -627,7 +618,7 @@ M:: x86 %call-gc ( gc-root-count -- ) "inline_gc" f %vm-invoke ; M: x86 %alien-global ( dst symbol library -- ) - [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ; + [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ; M: x86 %epilogue ( n -- ) cell - incr-stack-reg ; diff --git a/basis/vm/vm.factor b/basis/vm/vm.factor index 655250d755..e2e7fc879c 100644 --- a/basis/vm/vm.factor +++ b/basis/vm/vm.factor @@ -17,6 +17,8 @@ C-STRUCT: vm { "zone" "nursery" } { "cell" "cards_offset" } { "cell" "decks_offset" } + { "cell" "__padding__" } + { "cell[70]" "userenv" } ; : vm-field-offset ( field -- offset ) "vm" offset-of ; \ No newline at end of file diff --git a/vm/os-macosx.mm b/vm/os-macosx.mm index 865371b8ac..e4da7b2221 100644 --- a/vm/os-macosx.mm +++ b/vm/os-macosx.mm @@ -14,7 +14,7 @@ NS_DURING NS_VOIDRETURN; NS_HANDLER dpush(vm->allot_alien(F,(cell)localException)); - quot = userenv[COCOA_EXCEPTION_ENV]; + quot = vm->userenv[COCOA_EXCEPTION_ENV]; if(!tagged(quot).type_p(QUOTATION_TYPE)) { /* No Cocoa exception handler was registered, so diff --git a/vm/vm.hpp b/vm/vm.hpp index e0b598de6f..8372c3f0ba 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -8,9 +8,13 @@ struct factorvm { zone nursery; /* new objects are allocated here */ cell cards_offset; cell decks_offset; - cell __padding__ ; // align to 8byte boundary (for 32bit platforms) +#ifndef FACTOR_64 + cell __padding__ ; // align to 8 byte boundary +#endif cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ - +#ifndef FACTOR_64 + cell __padding2__; // not sure why we need this, bootstrap doesn't work without it +#endif // segments inline cell align_page(cell a); From 65a264aa1fba38b52123fab084a8f7bab11bdd3b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 20:44:06 +0100 Subject: [PATCH 199/345] turned errno() methods back into functions since they should already be thread safe --- vm/io.cpp | 15 ++------------- vm/vm.hpp | 2 -- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/vm/io.cpp b/vm/io.cpp index 47d964eaad..650afb8f8a 100755 --- a/vm/io.cpp +++ b/vm/io.cpp @@ -258,24 +258,13 @@ PRIMITIVE(fclose) /* This function is used by FFI I/O. Accessing the errno global directly is not portable, since on some libc's errno is not a global but a funky macro that reads thread-local storage. */ -int factorvm::err_no() +VM_C_API int err_no() { return errno; } -VM_C_API int err_no() -{ - return vm->err_no(); -} - -void factorvm::clear_err_no() +VM_C_API void clear_err_no() { errno = 0; } - -VM_C_API void clear_err_no() -{ - return vm->clear_err_no(); -} - } diff --git a/vm/vm.hpp b/vm/vm.hpp index 8372c3f0ba..1b0274e0b1 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -448,8 +448,6 @@ struct factorvm { inline void vmprim_fseek(); inline void vmprim_fflush(); inline void vmprim_fclose(); - int err_no(); - void clear_err_no(); //code_gc void clear_free_list(heap *heap); From c5119218c53b5d51154b73aee2fa6fe61750f0ae Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 21:02:13 +0100 Subject: [PATCH 200/345] moved gc_locals accessors into vm --- vm/vm.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vm/vm.hpp b/vm/vm.hpp index 1b0274e0b1..f72076c7b1 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -221,6 +221,9 @@ struct factorvm { bool growing_data_heap; data_heap *old_data_heap; + DEFPUSHPOP(gc_local_,gc_locals) + + void init_data_gc(); object *copy_untagged_object_impl(object *pointer, cell size); object *copy_object_impl(object *untagged); From ff54a57eb314e8f672c1056244333736ceb5498e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 21 Aug 2009 21:24:53 +0100 Subject: [PATCH 201/345] added code to pass vm ptr to some unboxers --- basis/cpu/architecture/architecture.factor | 2 +- basis/cpu/x86/32/32.factor | 11 +++++---- basis/cpu/x86/64/64.factor | 2 +- basis/cpu/x86/x86.factor | 2 +- vm/alien.cpp | 4 ++-- vm/cpu-x86.32.hpp | 2 ++ vm/math.cpp | 26 ++++++++++++---------- vm/math.hpp | 12 +++++----- vm/primitives.hpp | 2 ++ 9 files changed, 36 insertions(+), 27 deletions(-) diff --git a/basis/cpu/architecture/architecture.factor b/basis/cpu/architecture/architecture.factor index b9d07f578e..6d88944881 100644 --- a/basis/cpu/architecture/architecture.factor +++ b/basis/cpu/architecture/architecture.factor @@ -298,7 +298,7 @@ M: object %prepare-var-args ; HOOK: %alien-invoke cpu ( function library -- ) -HOOK: %vm-invoke cpu ( function library -- ) +HOOK: %vm-invoke cpu ( function -- ) HOOK: %cleanup cpu ( params -- ) diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index a48528d3fd..306771a4b5 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -47,10 +47,10 @@ M: x86.32 reserved-area-size 0 ; M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ; -M: x86.32 %vm-invoke - temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as the 3rd argument +M: x86.32 %vm-invoke ( function -- ) + temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument temp-reg PUSH - %alien-invoke + f %alien-invoke temp-reg POP ; M: x86.32 return-struct-in-registers? ( c-type -- ? ) @@ -163,7 +163,10 @@ M: x86.32 %prepare-unbox ( -- ) ESI 4 SUB ; : call-unbox-func ( func -- ) - 4 [ + 8 [ + ! push vm ptr + temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument + temp-reg PUSH ! Push parameter EAX PUSH ! Call the unboxer diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 4d041d2334..06592078d8 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -172,7 +172,7 @@ M: x86.64 %alien-invoke rc-absolute-cell rel-dlsym R11 CALL ; -M: x86.64 %vm-invoke %alien-invoke ; +M: x86.64 %vm-invoke ( function -- ) f %alien-invoke ; M: x86.64 %prepare-alien-indirect ( -- ) "unbox_alien" f %alien-invoke diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 57517ba319..da4e7d5286 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -615,7 +615,7 @@ M:: x86 %call-gc ( gc-root-count -- ) ! Pass number of roots as second parameter param-reg-2 gc-root-count MOV ! Call GC - "inline_gc" f %vm-invoke ; + "inline_gc" %vm-invoke ; M: x86 %alien-global ( dst symbol library -- ) [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ; diff --git a/vm/alien.cpp b/vm/alien.cpp index 41a1e8d522..da33c01df1 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -108,7 +108,7 @@ void *alien_pointer() PRIMITIVE(set_alien_##name) \ { \ type *ptr = (type *)PRIMITIVE_GETVM()->alien_pointer(); \ - type value = to(dpop()); \ + type value = PRIMITIVE_GETVM()->to(dpop()); \ *ptr = value; \ } @@ -124,7 +124,7 @@ DEFINE_ALIEN_ACCESSOR(signed_1,s8,box_signed_1,to_fixnum) DEFINE_ALIEN_ACCESSOR(unsigned_1,u8,box_unsigned_1,to_cell) DEFINE_ALIEN_ACCESSOR(float,float,box_float,to_float) DEFINE_ALIEN_ACCESSOR(double,double,box_double,to_double) -DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,PRIMITIVE_GETVM()->pinned_alien_offset) +DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset) /* open a native library and push a handle */ inline void factorvm::vmprim_dlopen() diff --git a/vm/cpu-x86.32.hpp b/vm/cpu-x86.32.hpp index 28f2e2978e..f9895cefbd 100644 --- a/vm/cpu-x86.32.hpp +++ b/vm/cpu-x86.32.hpp @@ -9,4 +9,6 @@ register cell rs asm("edi"); #define VM_ASM_API VM_C_API __attribute__ ((regparm (2))) #undef VM_PTR #define VM_PTR myvm +#undef ASSERTVM +#define ASSERTVM() assert(vm==myvm) } diff --git a/vm/math.cpp b/vm/math.cpp index 0a46252d7f..1bb8f0abe5 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -618,9 +618,9 @@ fixnum factorvm::to_fixnum(cell tagged) } } -VM_C_API fixnum to_fixnum(cell tagged) +VM_C_API fixnum to_fixnum(cell tagged,factorvm *myvm) { - return vm->to_fixnum(tagged); + return VM_PTR->to_fixnum(tagged); } cell factorvm::to_cell(cell tagged) @@ -628,9 +628,9 @@ cell factorvm::to_cell(cell tagged) return (cell)to_fixnum(tagged); } -VM_C_API cell to_cell(cell tagged) +VM_C_API cell to_cell(cell tagged, factorvm *myvm) { - return vm->to_cell(tagged); + return VM_PTR->to_cell(tagged); } void factorvm::box_signed_1(s8 n) @@ -740,9 +740,10 @@ s64 factorvm::to_signed_8(cell obj) } } -VM_C_API s64 to_signed_8(cell obj) +VM_C_API s64 to_signed_8(cell obj,factorvm *myvm) { - return vm->to_signed_8(obj); + ASSERTVM(); + return VM_PTR->to_signed_8(obj); } void factorvm::box_unsigned_8(u64 n) @@ -772,9 +773,10 @@ u64 factorvm::to_unsigned_8(cell obj) } } -VM_C_API u64 to_unsigned_8(cell obj) +VM_C_API u64 to_unsigned_8(cell obj,factorvm *myvm) { - return vm->to_unsigned_8(obj); + ASSERTVM(); + return VM_PTR->to_unsigned_8(obj); } void factorvm::box_float(float flo) @@ -792,9 +794,9 @@ float factorvm::to_float(cell value) return untag_float_check(value); } -VM_C_API float to_float(cell value) +VM_C_API float to_float(cell value,factorvm *myvm) { - return vm->to_float(value); + return VM_PTR->to_float(value); } void factorvm::box_double(double flo) @@ -812,9 +814,9 @@ double factorvm::to_double(cell value) return untag_float_check(value); } -VM_C_API double to_double(cell value) +VM_C_API double to_double(cell value,factorvm *myvm) { - return vm->to_double(value); + return VM_PTR->to_double(value); } /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On diff --git a/vm/math.hpp b/vm/math.hpp index e8347fe0e2..8affd8edd2 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -61,9 +61,9 @@ PRIMITIVE(double_bits); PRIMITIVE(bits_double); VM_C_API void box_float(float flo); -VM_C_API float to_float(cell value); +VM_C_API float to_float(cell value, factorvm *vm); VM_C_API void box_double(double flo); -VM_C_API double to_double(cell value); +VM_C_API double to_double(cell value, factorvm *vm); VM_C_API void box_signed_1(s8 n); VM_C_API void box_unsigned_1(u8 n); @@ -76,11 +76,11 @@ VM_C_API void box_unsigned_cell(cell cell); VM_C_API void box_signed_8(s64 n); VM_C_API void box_unsigned_8(u64 n); -VM_C_API s64 to_signed_8(cell obj); -VM_C_API u64 to_unsigned_8(cell obj); +VM_C_API s64 to_signed_8(cell obj, factorvm *vm); +VM_C_API u64 to_unsigned_8(cell obj, factorvm *vm); -VM_C_API fixnum to_fixnum(cell tagged); -VM_C_API cell to_cell(cell tagged); +VM_C_API fixnum to_fixnum(cell tagged, factorvm *vm); +VM_C_API cell to_cell(cell tagged, factorvm *vm); VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y); VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y); diff --git a/vm/primitives.hpp b/vm/primitives.hpp index c6d704ffc7..212126f322 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -12,4 +12,6 @@ namespace factor #endif extern const primitive_type primitives[]; +#define VM_PTR vm +#define ASSERTVM() } From 3b52df9e02738fee2fa2d76df00405cf86297aa0 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sat, 22 Aug 2009 10:21:32 +0100 Subject: [PATCH 202/345] added vm ptr to x86.32 boxing asm --- basis/cpu/x86/32/32.factor | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index 306771a4b5..adddc4c5b2 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -47,9 +47,12 @@ M: x86.32 reserved-area-size 0 ; M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ; -M: x86.32 %vm-invoke ( function -- ) +: push-vm-ptr ( -- ) temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument - temp-reg PUSH + temp-reg PUSH ; + +M: x86.32 %vm-invoke ( function -- ) + push-vm-ptr f %alien-invoke temp-reg POP ; @@ -109,9 +112,12 @@ M: x86.32 %save-param-reg 3drop ; #! parameter being passed to a callback from C. over [ load-return-reg ] [ 2drop ] if ; +CONSTANT: vm-ptr-size 4 + M:: x86.32 %box ( n rep func -- ) n rep (%box) - rep rep-size [ + rep rep-size vm-ptr-size + [ + push-vm-ptr rep push-return-reg func f %alien-invoke ] with-aligned-stack ; @@ -164,9 +170,8 @@ M: x86.32 %prepare-unbox ( -- ) : call-unbox-func ( func -- ) 8 [ - ! push vm ptr - temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument - temp-reg PUSH + ! push the vm ptr as an argument + push-vm-ptr ! Push parameter EAX PUSH ! Call the unboxer From 199fba7a9992cff85aac74587d3ce6df39b21756 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sat, 22 Aug 2009 10:50:52 +0100 Subject: [PATCH 203/345] converted box_* integer functions to use vm (x86 windows) --- vm/alien.cpp | 8 +++++--- vm/math.cpp | 50 ++++++++++++++++++++++++++++++-------------------- vm/math.hpp | 20 ++++++++++---------- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/vm/alien.cpp b/vm/alien.cpp index da33c01df1..e2298630e1 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -103,12 +103,14 @@ void *alien_pointer() #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \ PRIMITIVE(alien_##name) \ { \ - boxer(*(type*)PRIMITIVE_GETVM()->alien_pointer()); \ + factorvm *myvm = PRIMITIVE_GETVM(); \ + myvm->boxer(*(type*)myvm->alien_pointer()); \ } \ PRIMITIVE(set_alien_##name) \ { \ - type *ptr = (type *)PRIMITIVE_GETVM()->alien_pointer(); \ - type value = PRIMITIVE_GETVM()->to(dpop()); \ + factorvm *myvm = PRIMITIVE_GETVM(); \ + type *ptr = (type *)myvm->alien_pointer(); \ + type value = myvm->to(dpop()); \ *ptr = value; \ } diff --git a/vm/math.cpp b/vm/math.cpp index 1bb8f0abe5..e6e1abf80a 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -638,9 +638,10 @@ void factorvm::box_signed_1(s8 n) dpush(tag_fixnum(n)); } -VM_C_API void box_signed_1(s8 n) +VM_C_API void box_signed_1(s8 n,factorvm *myvm) { - return vm->box_signed_1(n); + ASSERTVM(); + return VM_PTR->box_signed_1(n); } void factorvm::box_unsigned_1(u8 n) @@ -648,9 +649,10 @@ void factorvm::box_unsigned_1(u8 n) dpush(tag_fixnum(n)); } -VM_C_API void box_unsigned_1(u8 n) +VM_C_API void box_unsigned_1(u8 n,factorvm *myvm) { - return vm->box_unsigned_1(n); + ASSERTVM(); + return VM_PTR->box_unsigned_1(n); } void factorvm::box_signed_2(s16 n) @@ -658,9 +660,10 @@ void factorvm::box_signed_2(s16 n) dpush(tag_fixnum(n)); } -VM_C_API void box_signed_2(s16 n) +VM_C_API void box_signed_2(s16 n,factorvm *myvm) { - return vm->box_signed_2(n); + ASSERTVM(); + return VM_PTR->box_signed_2(n); } void factorvm::box_unsigned_2(u16 n) @@ -668,9 +671,10 @@ void factorvm::box_unsigned_2(u16 n) dpush(tag_fixnum(n)); } -VM_C_API void box_unsigned_2(u16 n) +VM_C_API void box_unsigned_2(u16 n,factorvm *myvm) { - return vm->box_unsigned_2(n); + ASSERTVM(); + return VM_PTR->box_unsigned_2(n); } void factorvm::box_signed_4(s32 n) @@ -678,9 +682,10 @@ void factorvm::box_signed_4(s32 n) dpush(allot_integer(n)); } -VM_C_API void box_signed_4(s32 n) +VM_C_API void box_signed_4(s32 n,factorvm *myvm) { - return vm->box_signed_4(n); + ASSERTVM(); + return VM_PTR->box_signed_4(n); } void factorvm::box_unsigned_4(u32 n) @@ -688,9 +693,10 @@ void factorvm::box_unsigned_4(u32 n) dpush(allot_cell(n)); } -VM_C_API void box_unsigned_4(u32 n) +VM_C_API void box_unsigned_4(u32 n,factorvm *myvm) { - return vm->box_unsigned_4(n); + ASSERTVM(); + return VM_PTR->box_unsigned_4(n); } void factorvm::box_signed_cell(fixnum integer) @@ -698,9 +704,10 @@ void factorvm::box_signed_cell(fixnum integer) dpush(allot_integer(integer)); } -VM_C_API void box_signed_cell(fixnum integer) +VM_C_API void box_signed_cell(fixnum integer,factorvm *myvm) { - return vm->box_signed_cell(integer); + ASSERTVM(); + return VM_PTR->box_signed_cell(integer); } void factorvm::box_unsigned_cell(cell cell) @@ -708,9 +715,10 @@ void factorvm::box_unsigned_cell(cell cell) dpush(allot_cell(cell)); } -VM_C_API void box_unsigned_cell(cell cell) +VM_C_API void box_unsigned_cell(cell cell,factorvm *myvm) { - return vm->box_unsigned_cell(cell); + ASSERTVM(); + return VM_PTR->box_unsigned_cell(cell); } void factorvm::box_signed_8(s64 n) @@ -721,9 +729,10 @@ void factorvm::box_signed_8(s64 n) dpush(tag_fixnum(n)); } -VM_C_API void box_signed_8(s64 n) +VM_C_API void box_signed_8(s64 n,factorvm *myvm) { - return vm->box_signed_8(n); + ASSERTVM(); + return VM_PTR->box_signed_8(n); } s64 factorvm::to_signed_8(cell obj) @@ -754,9 +763,10 @@ void factorvm::box_unsigned_8(u64 n) dpush(tag_fixnum(n)); } -VM_C_API void box_unsigned_8(u64 n) +VM_C_API void box_unsigned_8(u64 n,factorvm *myvm) { - return vm->box_unsigned_8(n); + ASSERTVM(); + return VM_PTR->box_unsigned_8(n); } u64 factorvm::to_unsigned_8(cell obj) diff --git a/vm/math.hpp b/vm/math.hpp index 8affd8edd2..11c43a01a1 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -65,16 +65,16 @@ VM_C_API float to_float(cell value, factorvm *vm); VM_C_API void box_double(double flo); VM_C_API double to_double(cell value, factorvm *vm); -VM_C_API void box_signed_1(s8 n); -VM_C_API void box_unsigned_1(u8 n); -VM_C_API void box_signed_2(s16 n); -VM_C_API void box_unsigned_2(u16 n); -VM_C_API void box_signed_4(s32 n); -VM_C_API void box_unsigned_4(u32 n); -VM_C_API void box_signed_cell(fixnum integer); -VM_C_API void box_unsigned_cell(cell cell); -VM_C_API void box_signed_8(s64 n); -VM_C_API void box_unsigned_8(u64 n); +VM_C_API void box_signed_1(s8 n, factorvm *vm); +VM_C_API void box_unsigned_1(u8 n, factorvm *vm); +VM_C_API void box_signed_2(s16 n, factorvm *vm); +VM_C_API void box_unsigned_2(u16 n, factorvm *vm); +VM_C_API void box_signed_4(s32 n, factorvm *vm); +VM_C_API void box_unsigned_4(u32 n, factorvm *vm); +VM_C_API void box_signed_cell(fixnum integer, factorvm *vm); +VM_C_API void box_unsigned_cell(cell cell, factorvm *vm); +VM_C_API void box_signed_8(s64 n, factorvm *vm); +VM_C_API void box_unsigned_8(u64 n, factorvm *vm); VM_C_API s64 to_signed_8(cell obj, factorvm *vm); VM_C_API u64 to_unsigned_8(cell obj, factorvm *vm); From 81106f9e209eaa2c241f60a6f51f2f168a55b0a9 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Wed, 2 Sep 2009 08:06:00 +0100 Subject: [PATCH 204/345] converted box_* integer functions to use vm (x86 windows) --- vm/alien.cpp | 8 +++----- vm/cpu-x86.32.hpp | 4 ---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/vm/alien.cpp b/vm/alien.cpp index e2298630e1..6c83f87182 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -103,14 +103,12 @@ void *alien_pointer() #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \ PRIMITIVE(alien_##name) \ { \ - factorvm *myvm = PRIMITIVE_GETVM(); \ - myvm->boxer(*(type*)myvm->alien_pointer()); \ + PRIMITIVE_GETVM()->boxer(*(type*)PRIMITIVE_GETVM()->alien_pointer()); \ } \ PRIMITIVE(set_alien_##name) \ { \ - factorvm *myvm = PRIMITIVE_GETVM(); \ - type *ptr = (type *)myvm->alien_pointer(); \ - type value = myvm->to(dpop()); \ + type *ptr = (type *)PRIMITIVE_GETVM()->alien_pointer(); \ + type value = PRIMITIVE_GETVM()->to(dpop()); \ *ptr = value; \ } diff --git a/vm/cpu-x86.32.hpp b/vm/cpu-x86.32.hpp index f9895cefbd..351865f776 100644 --- a/vm/cpu-x86.32.hpp +++ b/vm/cpu-x86.32.hpp @@ -7,8 +7,4 @@ register cell ds asm("esi"); register cell rs asm("edi"); #define VM_ASM_API VM_C_API __attribute__ ((regparm (2))) -#undef VM_PTR -#define VM_PTR myvm -#undef ASSERTVM -#define ASSERTVM() assert(vm==myvm) } From a39bf2f8e223dd7190053dc5f473b34272494da6 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sat, 22 Aug 2009 11:04:34 +0100 Subject: [PATCH 205/345] converted box_* float functions to use vm (x86 win32) --- vm/math.cpp | 12 +++++++----- vm/math.hpp | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/vm/math.cpp b/vm/math.cpp index e6e1abf80a..c403073804 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -788,15 +788,16 @@ VM_C_API u64 to_unsigned_8(cell obj,factorvm *myvm) ASSERTVM(); return VM_PTR->to_unsigned_8(obj); } - + void factorvm::box_float(float flo) { dpush(allot_float(flo)); } -VM_C_API void box_float(float flo) +VM_C_API void box_float(float flo,factorvm *myvm) // not sure if this is ever called { - return vm->box_float(flo); + ASSERTVM(); + return VM_PTR->box_float(flo); } float factorvm::to_float(cell value) @@ -814,9 +815,10 @@ void factorvm::box_double(double flo) dpush(allot_float(flo)); } -VM_C_API void box_double(double flo) +VM_C_API void box_double(double flo,factorvm *myvm) // not sure if this is ever called { - return vm->box_double(flo); + ASSERTVM(); + return VM_PTR->box_double(flo); } double factorvm::to_double(cell value) diff --git a/vm/math.hpp b/vm/math.hpp index 11c43a01a1..5939b25b37 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -60,9 +60,9 @@ PRIMITIVE(bits_float); PRIMITIVE(double_bits); PRIMITIVE(bits_double); -VM_C_API void box_float(float flo); +VM_C_API void box_float(float flo, factorvm *vm); VM_C_API float to_float(cell value, factorvm *vm); -VM_C_API void box_double(double flo); +VM_C_API void box_double(double flo, factorvm *vm); VM_C_API double to_double(cell value, factorvm *vm); VM_C_API void box_signed_1(s8 n, factorvm *vm); From 7759b89de9e70a31d47b76284bc7320d0f942d64 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sat, 22 Aug 2009 11:15:58 +0100 Subject: [PATCH 206/345] removed all vm-> singleton accesses from inlineimpls --- vm/arrays.cpp | 2 +- vm/inlineimpls.hpp | 163 +-------------------------------------------- vm/math.cpp | 6 +- vm/math.hpp | 1 + vm/primitives.hpp | 1 + vm/vm.hpp | 3 - 6 files changed, 8 insertions(+), 168 deletions(-) diff --git a/vm/arrays.cpp b/vm/arrays.cpp index 62992c4e7c..3052563dea 100644 --- a/vm/arrays.cpp +++ b/vm/arrays.cpp @@ -91,7 +91,7 @@ void growable_array::add(cell elt_) if(count == array_capacity(elements.untagged())) elements = myvm->reallot_array(elements.untagged(),count * 2); - set_array_nth(elements.untagged(),count++,elt.value()); + myvm->set_array_nth(elements.untagged(),count++,elt.value()); } void growable_array::trim() diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index ca0b13be39..6b74634715 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -11,11 +11,6 @@ inline cell factorvm::align_page(cell a) return align(a,getpagesize()); } -inline static cell align_page(cell a) -{ - return vm->align_page(a); -} - // write_barrier.hpp inline card *factorvm::addr_to_card(cell a) @@ -23,71 +18,38 @@ inline card *factorvm::addr_to_card(cell a) return (card*)(((cell)(a) >> card_bits) + cards_offset); } -inline card *addr_to_card(cell a) -{ - return vm->addr_to_card(a); -} inline cell factorvm::card_to_addr(card *c) { return ((cell)c - cards_offset) << card_bits; } -inline cell card_to_addr(card *c) -{ - return vm->card_to_addr(c); -} inline cell factorvm::card_offset(card *c) { return *(c - (cell)data->cards + (cell)data->allot_markers); } -inline cell card_offset(card *c) -{ - return vm->card_offset(c); -} - inline card_deck *factorvm::addr_to_deck(cell a) { return (card_deck *)(((cell)a >> deck_bits) + decks_offset); } -inline card_deck *addr_to_deck(cell a) -{ - return vm->addr_to_deck(a); -} - inline cell factorvm::deck_to_addr(card_deck *c) { return ((cell)c - decks_offset) << deck_bits; } -inline cell deck_to_addr(card_deck *c) -{ - return vm->deck_to_addr(c); -} - inline card *factorvm::deck_to_card(card_deck *d) { return (card *)((((cell)d - decks_offset) << (deck_bits - card_bits)) + cards_offset); } -inline card *deck_to_card(card_deck *d) -{ - return vm->deck_to_card(d); -} - inline card *factorvm::addr_to_allot_marker(object *a) { return (card *)(((cell)a >> card_bits) + allot_markers_offset); } -inline card *addr_to_allot_marker(object *a) -{ - return vm->addr_to_allot_marker(a); -} - /* the write barrier must be called any time we are potentially storing a pointer from an older generation to a younger one */ inline void factorvm::write_barrier(object *obj) @@ -96,11 +58,6 @@ inline void factorvm::write_barrier(object *obj) *addr_to_deck((cell)obj) = card_mark_mask; } -inline void write_barrier(object *obj) -{ - return vm->write_barrier(obj); -} - /* we need to remember the first object allocated in the card */ inline void factorvm::allot_barrier(object *address) { @@ -109,11 +66,6 @@ inline void factorvm::allot_barrier(object *address) *ptr = ((cell)address & addr_card_mask); } -inline void allot_barrier(object *address) -{ - return vm->allot_barrier(address); -} - //data_gc.hpp inline bool factorvm::collecting_accumulation_gen_p() @@ -124,11 +76,6 @@ inline bool factorvm::collecting_accumulation_gen_p() || collecting_gen == data->tenured()); } -inline bool collecting_accumulation_gen_p() -{ - return vm->collecting_accumulation_gen_p(); -} - inline object *factorvm::allot_zone(zone *z, cell a) { cell h = z->here; @@ -138,11 +85,6 @@ inline object *factorvm::allot_zone(zone *z, cell a) return obj; } -inline object *allot_zone(zone *z, cell a) -{ - return vm->allot_zone(z,a); -} - /* * It is up to the caller to fill in the object's fields in a meaningful * fashion! @@ -198,21 +140,11 @@ inline object *factorvm::allot_object(header header, cell size) return obj; } -inline object *allot_object(header header, cell size) -{ - return vm->allot_object(header,size); -} - template TYPE *factorvm::allot(cell size) { return (TYPE *)allot_object(header(TYPE::type_number),size); } -template TYPE *allot(cell size) -{ - return vm->allot(size); -} - inline void factorvm::check_data_pointer(object *pointer) { #ifdef FACTOR_DEBUG @@ -224,11 +156,6 @@ inline void factorvm::check_data_pointer(object *pointer) #endif } -inline void check_data_pointer(object *pointer) -{ - return vm->check_data_pointer(pointer); -} - inline void factorvm::check_tagged_pointer(cell tagged) { #ifdef FACTOR_DEBUG @@ -241,18 +168,13 @@ inline void factorvm::check_tagged_pointer(cell tagged) #endif } -inline void check_tagged_pointer(cell tagged) -{ - return vm->check_tagged_pointer(tagged); -} - //local_roots.hpp template struct gc_root : public tagged { factorvm *myvm; - void push() { check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } + void push() { myvm->check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } explicit gc_root(cell value_,factorvm *vm) : tagged(value_),myvm(vm) { push(); } @@ -276,7 +198,7 @@ struct gc_bignum factorvm *myvm; gc_bignum(bignum **addr_, factorvm *vm) : addr(addr_), myvm(vm) { if(*addr_) - check_data_pointer(*addr_); + myvm->check_data_pointer(*addr_); myvm->gc_bignums.push_back((cell)addr); } @@ -298,21 +220,11 @@ template TYPE *factorvm::allot_array_internal(cell capacity) return array; } -template TYPE *allot_array_internal(cell capacity) -{ - return vm->allot_array_internal(capacity); -} - template bool factorvm::reallot_array_in_place_p(TYPE *array, cell capacity) { return in_zone(&nursery,array) && capacity <= array_capacity(array); } -template bool reallot_array_in_place_p(TYPE *array, cell capacity) -{ - return vm->reallot_array_in_place_p(array,capacity); -} - template TYPE *factorvm::reallot_array(TYPE *array_, cell capacity) { gc_root array(array_,this); @@ -350,11 +262,6 @@ inline void factorvm::set_array_nth(array *array, cell slot, cell value) write_barrier(array); } -inline void set_array_nth(array *array, cell slot, cell value) -{ - return vm->set_array_nth(array,slot,value); -} - struct growable_array { cell count; gc_root elements; @@ -387,11 +294,6 @@ inline cell factorvm::allot_integer(fixnum x) return tag_fixnum(x); } -inline cell allot_integer(fixnum x) -{ - return vm->allot_integer(x); -} - inline cell factorvm::allot_cell(cell x) { if(x > (cell)fixnum_max) @@ -400,11 +302,6 @@ inline cell factorvm::allot_cell(cell x) return tag_fixnum(x); } -inline cell allot_cell(cell x) -{ - return vm->allot_cell(x); -} - inline cell factorvm::allot_float(double n) { boxed_float *flo = allot(sizeof(boxed_float)); @@ -412,72 +309,36 @@ inline cell factorvm::allot_float(double n) return tag(flo); } -inline cell allot_float(double n) -{ - return vm->allot_float(n); -} - inline bignum *factorvm::float_to_bignum(cell tagged) { return double_to_bignum(untag_float(tagged)); } -inline bignum *float_to_bignum(cell tagged) -{ - return vm->float_to_bignum(tagged); -} - inline double factorvm::bignum_to_float(cell tagged) { return bignum_to_double(untag(tagged)); } -inline double bignum_to_float(cell tagged) -{ - return vm->bignum_to_float(tagged); -} - inline double factorvm::untag_float(cell tagged) { return untag(tagged)->n; } -inline double untag_float(cell tagged) -{ - return vm->untag_float(tagged); -} - inline double factorvm::untag_float_check(cell tagged) { return untag_check(tagged)->n; } -inline double untag_float_check(cell tagged) -{ - return vm->untag_float_check(tagged); -} - inline fixnum factorvm::float_to_fixnum(cell tagged) { return (fixnum)untag_float(tagged); } -inline static fixnum float_to_fixnum(cell tagged) -{ - return vm->float_to_fixnum(tagged); -} - inline double factorvm::fixnum_to_float(cell tagged) { return (double)untag_fixnum(tagged); } -inline double fixnum_to_float(cell tagged) -{ - return vm->fixnum_to_float(tagged); -} - - //callstack.hpp /* This is a little tricky. The iterator may allocate memory, so we keep the callstack in a GC root and use relative offsets */ @@ -494,22 +355,12 @@ template void factorvm::iterate_callstack_object(callstack *stack } } -template void iterate_callstack_object(callstack *stack_, TYPE &iterator) -{ - return vm->iterate_callstack_object(stack_,iterator); -} - //booleans.hpp inline cell factorvm::tag_boolean(cell untagged) { return (untagged ? T : F); } -inline cell tag_boolean(cell untagged) -{ - return vm->tag_boolean(untagged); -} - // callstack.hpp template void factorvm::iterate_callstack(cell top, cell bottom, TYPE &iterator) { @@ -522,11 +373,6 @@ template void factorvm::iterate_callstack(cell top, cell bottom, } } -template void iterate_callstack(cell top, cell bottom, TYPE &iterator) -{ - return vm->iterate_callstack(top,bottom,iterator); -} - // data_heap.hpp /* Every object has a regular representation in the runtime, which makes GC @@ -548,11 +394,6 @@ inline void factorvm::do_slots(cell obj, void (* iter)(cell *,factorvm*)) } } -inline void do_slots(cell obj, void (* iter)(cell *,factorvm*)) -{ - return vm->do_slots(obj,iter); -} - // code_heap.hpp inline void factorvm::check_code_pointer(cell ptr) diff --git a/vm/math.cpp b/vm/math.cpp index c403073804..8d213b391d 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -841,7 +841,7 @@ void factorvm::overflow_fixnum_add(fixnum x, fixnum y) VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y) { - return vm->overflow_fixnum_add(x,y); + return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y); } void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) @@ -852,7 +852,7 @@ void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y) { - return vm->overflow_fixnum_subtract(x,y); + return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y); } void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) @@ -866,7 +866,7 @@ void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y) { - return vm->overflow_fixnum_multiply(x,y); + return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y); } } diff --git a/vm/math.hpp b/vm/math.hpp index 5939b25b37..b45284f693 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -5,6 +5,7 @@ static const fixnum fixnum_max = (((fixnum)1 << (WORD_SIZE - TAG_BITS - 1)) - 1) static const fixnum fixnum_min = (-((fixnum)1 << (WORD_SIZE - TAG_BITS - 1))); static const fixnum array_size_max = ((cell)1 << (WORD_SIZE - TAG_BITS - 2)); +// defined in assembler PRIMITIVE(fixnum_add); PRIMITIVE(fixnum_subtract); PRIMITIVE(fixnum_multiply); diff --git a/vm/primitives.hpp b/vm/primitives.hpp index 212126f322..8e6c3b8f51 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -12,6 +12,7 @@ namespace factor #endif extern const primitive_type primitives[]; +#define PRIMITIVE_OVERFLOW_GETVM() vm #define VM_PTR vm #define ASSERTVM() } diff --git a/vm/vm.hpp b/vm/vm.hpp index f72076c7b1..1b0274e0b1 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -221,9 +221,6 @@ struct factorvm { bool growing_data_heap; data_heap *old_data_heap; - DEFPUSHPOP(gc_local_,gc_locals) - - void init_data_gc(); object *copy_untagged_object_impl(object *pointer, cell size); object *copy_object_impl(object *untagged); From fa2dccd6d3688ed7fa0965057f3fe2c804c654b9 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sat, 22 Aug 2009 21:35:47 +0100 Subject: [PATCH 207/345] vm passed in box_alien and alien_offset (win32) --- vm/alien.cpp | 15 +++++++++++---- vm/alien.hpp | 4 ++-- vm/os-unix.cpp | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/vm/alien.cpp b/vm/alien.cpp index 6c83f87182..23ca60bf6a 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -218,9 +218,10 @@ char *factorvm::alien_offset(cell obj) } } -VM_C_API char *alien_offset(cell obj) +VM_C_API char *alien_offset(cell obj, factorvm *myvm) { - return vm->alien_offset(obj); + ASSERTVM(); + return VM_PTR->alien_offset(obj); } /* pop an object representing a C pointer */ @@ -231,6 +232,7 @@ char *factorvm::unbox_alien() VM_C_API char *unbox_alien() { + printf("*PHIL unbox_alien\n"); return vm->unbox_alien(); } @@ -243,9 +245,10 @@ void factorvm::box_alien(void *ptr) dpush(allot_alien(F,(cell)ptr)); } -VM_C_API void box_alien(void *ptr) +VM_C_API void box_alien(void *ptr, factorvm *myvm) { - return vm->box_alien(ptr); + ASSERTVM(); + return VM_PTR->box_alien(ptr); } /* for FFI calls passing structs by value */ @@ -256,6 +259,7 @@ void factorvm::to_value_struct(cell src, void *dest, cell size) VM_C_API void to_value_struct(cell src, void *dest, cell size) { + printf("PHIL to_value_struct\n"); return vm->to_value_struct(src,dest,size); } @@ -269,6 +273,7 @@ void factorvm::box_value_struct(void *src, cell size) VM_C_API void box_value_struct(void *src, cell size) { + printf("PHIL box_value_struct\n"); return vm->box_value_struct(src,size); } @@ -283,6 +288,7 @@ void factorvm::box_small_struct(cell x, cell y, cell size) VM_C_API void box_small_struct(cell x, cell y, cell size) { + printf("PHIL box_small_struct\n"); return vm->box_small_struct(x,y,size); } @@ -299,6 +305,7 @@ void factorvm::box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) { + printf("PHIL box_medium_struct\n"); return vm->box_medium_struct(x1, x2, x3, x4, size); } diff --git a/vm/alien.hpp b/vm/alien.hpp index 0a4f3a7e93..327d791406 100755 --- a/vm/alien.hpp +++ b/vm/alien.hpp @@ -36,9 +36,9 @@ PRIMITIVE(dlsym); PRIMITIVE(dlclose); PRIMITIVE(dll_validp); -VM_C_API char *alien_offset(cell object); +VM_C_API char *alien_offset(cell object, factorvm *vm); VM_C_API char *unbox_alien(); -VM_C_API void box_alien(void *ptr); +VM_C_API void box_alien(void *ptr, factorvm *vm); VM_C_API void to_value_struct(cell src, void *dest, cell size); VM_C_API void box_value_struct(void *src, cell size); VM_C_API void box_small_struct(cell x, cell y, cell size); diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 189fca0cf7..3785aeda72 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -39,7 +39,7 @@ void init_ffi() void ffi_dlopen(dll *dll) { - dll->dll = dlopen(alien_offset(dll->path), RTLD_LAZY); + dll->dll = dlopen(alien_offset(dll->path,vm), RTLD_LAZY); } void *ffi_dlsym(dll *dll, symbol_char *symbol) From 0a15e20e1282565dc760200c3b92deb4a02f6c85 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 23 Aug 2009 14:36:24 +0100 Subject: [PATCH 208/345] Added basic win32 start-thread support --- vm/factor.cpp | 26 ++++++++++++++++++++++++++ vm/factor.hpp | 1 + vm/os-windows-nt.cpp | 5 +++++ vm/os-windows-nt.hpp | 3 +++ 4 files changed, 35 insertions(+) diff --git a/vm/factor.cpp b/vm/factor.cpp index 55d7abcc49..883b4f9462 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -277,4 +277,30 @@ VM_C_API void factor_sleep(long us) return vm->factor_sleep(us); } +struct startargs { + int argc; + vm_char **argv; +}; + +void* start_standalone_factor_thread(void *arg) +{ + factorvm *newvm = new factorvm; + startargs *args = (startargs*) arg; + vm_parameters p; + newvm->default_parameters(&p); + newvm->init_parameters_from_args(&p,args->argc, args->argv); + newvm->init_factor(&p); + newvm->pass_args_to_factor(args->argc,args->argv); + newvm->start_factor(&p); + return 0; +} + + +VM_C_API void start_standalone_factor_in_new_thread(int argc, vm_char **argv) +{ + startargs *args = new startargs; // leaks startargs structure + args->argc = argc; args->argv = argv; + start_thread(start_standalone_factor_thread,args); +} + } diff --git a/vm/factor.hpp b/vm/factor.hpp index 6e00bc012e..b2aeccd1a6 100644 --- a/vm/factor.hpp +++ b/vm/factor.hpp @@ -7,6 +7,7 @@ VM_C_API void init_factor(vm_parameters *p); VM_C_API void pass_args_to_factor(int argc, vm_char **argv); VM_C_API void start_embedded_factor(vm_parameters *p); VM_C_API void start_standalone_factor(int argc, vm_char **argv); +VM_C_API void start_standalone_factor_in_new_thread(int argc, vm_char **argv); VM_C_API char *factor_eval_string(char *string); VM_C_API void factor_eval_free(char *result); diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 535e7b8640..9187b88986 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -3,6 +3,11 @@ namespace factor { +void start_thread(void *(*start_routine)(void *),void *args){ + CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); +} + + s64 factorvm::current_micros() { FILETIME t; diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index c083844ae0..27923a000a 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -21,9 +21,12 @@ typedef char symbol_char; FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe); + // SSE traps raise these exception codes, which are defined in internal NT headers // but not winbase.h #define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4 #define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5 +void start_thread(void *(*start_routine)(void *),void *args); + } From 58190c06dca6da81a177830bf7ef2964b241d588 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 23 Aug 2009 14:45:09 +0100 Subject: [PATCH 209/345] passing ptr in boolean boxing and save_callstack_bottom --- vm/booleans.cpp | 10 ++++++---- vm/booleans.hpp | 4 ++-- vm/callstack.cpp | 5 +++-- vm/callstack.hpp | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/vm/booleans.cpp b/vm/booleans.cpp index 6a1bb79298..aa3f392b3e 100644 --- a/vm/booleans.cpp +++ b/vm/booleans.cpp @@ -8,9 +8,10 @@ void factorvm::box_boolean(bool value) dpush(value ? T : F); } -VM_C_API void box_boolean(bool value) +VM_C_API void box_boolean(bool value, factorvm *myvm) { - return vm->box_boolean(value); + ASSERTVM(); + return VM_PTR->box_boolean(value); } bool factorvm::to_boolean(cell value) @@ -18,9 +19,10 @@ bool factorvm::to_boolean(cell value) return value != F; } -VM_C_API bool to_boolean(cell value) +VM_C_API bool to_boolean(cell value, factorvm *myvm) { - return vm->to_boolean(value); + ASSERTVM(); + return VM_PTR->to_boolean(value); } } diff --git a/vm/booleans.hpp b/vm/booleans.hpp index c410f67481..843cd7fd66 100644 --- a/vm/booleans.hpp +++ b/vm/booleans.hpp @@ -2,7 +2,7 @@ namespace factor { -VM_C_API void box_boolean(bool value); -VM_C_API bool to_boolean(cell value); +VM_C_API void box_boolean(bool value, factorvm *vm); +VM_C_API bool to_boolean(cell value, factorvm *vm); } diff --git a/vm/callstack.cpp b/vm/callstack.cpp index 676e4260c9..b89dd0cfef 100755 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -246,9 +246,10 @@ void factorvm::save_callstack_bottom(stack_frame *callstack_bottom) stack_chain->callstack_bottom = callstack_bottom; } -VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom) +VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom, factorvm *myvm) { - return vm->save_callstack_bottom(callstack_bottom); + ASSERTVM(); + return VM_PTR->save_callstack_bottom(callstack_bottom); } } diff --git a/vm/callstack.hpp b/vm/callstack.hpp index 406d8e7154..d34cd618e3 100755 --- a/vm/callstack.hpp +++ b/vm/callstack.hpp @@ -13,7 +13,7 @@ PRIMITIVE(innermost_stack_frame_executing); PRIMITIVE(innermost_stack_frame_scan); PRIMITIVE(set_innermost_stack_frame_quot); -VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom); +VM_ASM_API void save_callstack_bottom(stack_frame *callstack_bottom,factorvm *vm); From cdb6304fef1c3181100dbe3cf0116dcd3b8e6e71 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 23 Aug 2009 19:40:59 +0100 Subject: [PATCH 210/345] Dev checkpoint --- basis/cpu/x86/32/32.factor | 2 ++ vm/code_heap.cpp | 2 +- vm/contexts.cpp | 6 ++++-- vm/contexts.hpp | 5 +++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index adddc4c5b2..fe365f2e50 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -257,11 +257,13 @@ M: x86.32 %callback-value ( ctype -- ) ESP 12 SUB ! Save top of data stack in non-volatile register %prepare-unbox + push-vm-ptr EAX PUSH ! Restore data/call/retain stacks "unnest_stacks" f %alien-invoke ! Place top of data stack in EAX EAX POP + temp-reg POP ! Restore C stack ESP 12 ADD ! Unbox EAX diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index 39f0dfd52a..d91e349db7 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -17,7 +17,7 @@ bool factorvm::in_code_heap_p(cell ptr) bool in_code_heap_p(cell ptr) { - return vm->in_code_heap_p(ptr); + return vm->in_code_heap_p(ptr); // used by os specific signal handlers } /* Compile a word definition with the non-optimizing compiler. Allocates memory */ diff --git a/vm/contexts.cpp b/vm/contexts.cpp index f5c63f1e7f..26197a4863 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -90,8 +90,9 @@ void factorvm::nest_stacks() reset_retainstack(); } -void nest_stacks() +void nest_stacks(factorvm *myvm) { + printf("PHIL nest_stacks %d %d\n",vm,myvm);fflush(stdout); return vm->nest_stacks(); } @@ -110,8 +111,9 @@ void factorvm::unnest_stacks() dealloc_context(old_stacks); } -void unnest_stacks() +void unnest_stacks(factorvm *myvm) { + printf("PHIL unnest_stacks %d %d\n",vm,myvm);fflush(stdout); return vm->unnest_stacks(); } diff --git a/vm/contexts.hpp b/vm/contexts.hpp index 17f8a7eb70..060b15fad7 100644 --- a/vm/contexts.hpp +++ b/vm/contexts.hpp @@ -50,8 +50,9 @@ PRIMITIVE(set_datastack); PRIMITIVE(set_retainstack); PRIMITIVE(check_datastack); -VM_C_API void nest_stacks(); -VM_C_API void unnest_stacks(); +struct factorvm; +VM_C_API void nest_stacks(factorvm *vm); +VM_C_API void unnest_stacks(factorvm *vm); } From 005549ba43bae34eae3166ea1e1f418a38259d1e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 23 Aug 2009 20:04:05 +0100 Subject: [PATCH 211/345] vm pointer passed to nest_stacks and unnest_stacks (win32) --- basis/compiler/codegen/codegen.factor | 4 ++-- basis/cpu/x86/32/32.factor | 4 ++-- basis/cpu/x86/x86.factor | 2 +- vm/contexts.cpp | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/basis/compiler/codegen/codegen.factor b/basis/compiler/codegen/codegen.factor index de15cda21c..6d43adf933 100755 --- a/basis/compiler/codegen/codegen.factor +++ b/basis/compiler/codegen/codegen.factor @@ -437,7 +437,7 @@ M: ##alien-indirect generate-insn ! Generate code for boxing input parameters in a callback. [ dup \ %save-param-reg move-parameters - "nest_stacks" f %alien-invoke + "nest_stacks" %vm-invoke box-parameters ] with-param-regs ; @@ -475,7 +475,7 @@ TUPLE: callback-context ; [ callback-context new do-callback ] % ] [ ] make ; -: %unnest-stacks ( -- ) "unnest_stacks" f %alien-invoke ; +: %unnest-stacks ( -- ) "unnest_stacks" %vm-invoke ; M: ##callback-return generate-insn #! All the extra book-keeping for %unwind is only for x86. diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index fe365f2e50..9499df7aaf 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -257,13 +257,13 @@ M: x86.32 %callback-value ( ctype -- ) ESP 12 SUB ! Save top of data stack in non-volatile register %prepare-unbox - push-vm-ptr EAX PUSH + push-vm-ptr ! Restore data/call/retain stacks "unnest_stacks" f %alien-invoke ! Place top of data stack in EAX - EAX POP temp-reg POP + EAX POP ! Restore C stack ESP 12 ADD ! Unbox EAX diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index da4e7d5286..9ac787a027 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -615,7 +615,7 @@ M:: x86 %call-gc ( gc-root-count -- ) ! Pass number of roots as second parameter param-reg-2 gc-root-count MOV ! Call GC - "inline_gc" %vm-invoke ; + "inline_gc" %vm-invoke ; ! (PHIL) TODO: vm-invoke won't work with ppc or x86.64. need %vm-invoke-3rd M: x86 %alien-global ( dst symbol library -- ) [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ; diff --git a/vm/contexts.cpp b/vm/contexts.cpp index 26197a4863..5acb7d5090 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -92,8 +92,8 @@ void factorvm::nest_stacks() void nest_stacks(factorvm *myvm) { - printf("PHIL nest_stacks %d %d\n",vm,myvm);fflush(stdout); - return vm->nest_stacks(); + ASSERTVM(); + return VM_PTR->nest_stacks(); } /* called when leaving a compiled callback */ @@ -113,8 +113,8 @@ void factorvm::unnest_stacks() void unnest_stacks(factorvm *myvm) { - printf("PHIL unnest_stacks %d %d\n",vm,myvm);fflush(stdout); - return vm->unnest_stacks(); + ASSERTVM(); + return VM_PTR->unnest_stacks(); } /* called on startup */ From 2f3cd4d23d21aa231e5c7ae17ed1cdb265323369 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 23 Aug 2009 21:04:19 +0100 Subject: [PATCH 212/345] removed some vm-> pointers --- vm/alien.cpp | 2 +- vm/os-freebsd.cpp | 2 +- vm/os-genunix.cpp | 2 +- vm/os-linux.cpp | 4 ++-- vm/os-unix.cpp | 4 ++-- vm/os-unix.hpp | 2 +- vm/quotations.cpp | 2 +- vm/tagged.hpp | 11 ----------- vm/utilities.cpp | 13 ------------- vm/utilities.hpp | 3 --- 10 files changed, 9 insertions(+), 36 deletions(-) diff --git a/vm/alien.cpp b/vm/alien.cpp index 23ca60bf6a..bed24d1037 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -232,7 +232,7 @@ char *factorvm::unbox_alien() VM_C_API char *unbox_alien() { - printf("*PHIL unbox_alien\n"); + printf("*PHIL unbox_alien\n");fflush(stdout); return vm->unbox_alien(); } diff --git a/vm/os-freebsd.cpp b/vm/os-freebsd.cpp index d259658284..64c8ac19da 100644 --- a/vm/os-freebsd.cpp +++ b/vm/os-freebsd.cpp @@ -33,7 +33,7 @@ const char *vm_executable_path() if(strcmp(path, "unknown") == 0) return NULL; - return safe_strdup(path); + return vm->safe_strdup(path); } } diff --git a/vm/os-genunix.cpp b/vm/os-genunix.cpp index 29c3e79859..9e7804caf0 100644 --- a/vm/os-genunix.cpp +++ b/vm/os-genunix.cpp @@ -31,7 +31,7 @@ const char *default_image_path() const char *iter = path; while(*iter) { len++; iter++; } - char *new_path = (char *)safe_malloc(PATH_MAX + SUFFIX_LEN + 1); + char *new_path = (char *)vm->safe_malloc(PATH_MAX + SUFFIX_LEN + 1); memcpy(new_path,path,len + 1); memcpy(new_path + len,SUFFIX,SUFFIX_LEN + 1); return new_path; diff --git a/vm/os-linux.cpp b/vm/os-linux.cpp index 2bc121ffc7..62deb70f01 100644 --- a/vm/os-linux.cpp +++ b/vm/os-linux.cpp @@ -6,7 +6,7 @@ namespace factor /* Snarfed from SBCL linux-so.c. You must free() this yourself. */ const char *vm_executable_path() { - char *path = (char *)safe_malloc(PATH_MAX + 1); + char *path = (char *)vm->safe_malloc(PATH_MAX + 1); int size = readlink("/proc/self/exe", path, PATH_MAX); if (size < 0) @@ -17,7 +17,7 @@ const char *vm_executable_path() else { path[size] = '\0'; - return safe_strdup(path); + return vm->safe_strdup(path); } } diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 3785aeda72..67327e7d30 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -58,7 +58,7 @@ void ffi_dlclose(dll *dll) PRIMITIVE(existsp) { struct stat sb; - char *path = (char *)(untag_check(dpop()) + 1); + char *path = (char *)(vm->untag_check(dpop()) + 1); box_boolean(stat(path,&sb) >= 0); } @@ -79,7 +79,7 @@ segment *alloc_segment(cell size) if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1) fatal_error("Cannot protect high guard page",(cell)array); - segment *retval = (segment *)safe_malloc(sizeof(segment)); + segment *retval = (segment *)vm->safe_malloc(sizeof(segment)); retval->start = (cell)(array + pagesize); retval->size = size; diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index 8aff18364e..52dc1bfd6e 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -40,7 +40,7 @@ typedef char symbol_char; #define OPEN_READ(path) fopen(path,"rb") #define OPEN_WRITE(path) fopen(path,"wb") -#define print_native_string(string) print_string(string) +#define print_native_string(string) vm->print_string(string) void start_thread(void *(*start_routine)(void *)); diff --git a/vm/quotations.cpp b/vm/quotations.cpp index 7b03ada175..b28fb1d3a1 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -92,7 +92,7 @@ bool quotation_jit::stack_frame_p() switch(tagged(obj).type()) { case WORD_TYPE: - if(untag(obj)->subprimitive == F) + if(myvm->untag(obj)->subprimitive == F) return true; break; case QUOTATION_TYPE: diff --git a/vm/tagged.hpp b/vm/tagged.hpp index b73f3aeb7a..2bf058212f 100755 --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -64,20 +64,9 @@ template TYPE *factorvm::untag_check(cell value) return tagged(value).untag_check(this); } -template TYPE *untag_check(cell value) -{ - return vm->untag_check(value); -} - template TYPE *factorvm::untag(cell value) { return tagged(value).untagged(); } -template TYPE *untag(cell value) -{ - return vm->untag(value); -} - - } diff --git a/vm/utilities.cpp b/vm/utilities.cpp index a1e3f30e00..4f4da9a1bc 100755 --- a/vm/utilities.cpp +++ b/vm/utilities.cpp @@ -11,11 +11,6 @@ void *factorvm::safe_malloc(size_t size) return ptr; } -void *safe_malloc(size_t size) -{ - return vm->safe_malloc(size); -} - vm_char *factorvm::safe_strdup(const vm_char *str) { vm_char *ptr = STRDUP(str); @@ -23,10 +18,6 @@ vm_char *factorvm::safe_strdup(const vm_char *str) return ptr; } -vm_char *safe_strdup(const vm_char *str) -{ - return vm->safe_strdup(str); -} /* We don't use printf directly, because format directives are not portable. Instead we define the common cases here. */ @@ -40,10 +31,6 @@ void factorvm::print_string(const char *str) fputs(str,stdout); } -void print_string(const char *str) -{ - return vm->print_string(str); -} void factorvm::print_cell(cell x) { diff --git a/vm/utilities.hpp b/vm/utilities.hpp index bc7f7d918a..412ef35bb4 100755 --- a/vm/utilities.hpp +++ b/vm/utilities.hpp @@ -1,7 +1,4 @@ namespace factor { -void *safe_malloc(size_t size); -vm_char *safe_strdup(const vm_char *str); -void print_string(const char *str); } From 700e03a6a61caa508b81a059b8508e983cef97b7 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 23 Aug 2009 21:19:35 +0100 Subject: [PATCH 213/345] removed some vm-> forwarding functions --- vm/alien.cpp | 8 +------- vm/code_heap.cpp | 6 ------ vm/code_heap.hpp | 3 --- vm/mach_signal.cpp | 2 +- vm/os-unix.cpp | 2 +- vm/os-windows-nt.cpp | 2 +- 6 files changed, 4 insertions(+), 19 deletions(-) diff --git a/vm/alien.cpp b/vm/alien.cpp index bed24d1037..282f5c2fc9 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -93,12 +93,6 @@ void *factorvm::alien_pointer() return unbox_alien() + offset; } -void *alien_pointer() -{ - return vm->alien_pointer(); -} - - /* define words to read/write values at an alien address */ #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \ PRIMITIVE(alien_##name) \ @@ -232,7 +226,7 @@ char *factorvm::unbox_alien() VM_C_API char *unbox_alien() { - printf("*PHIL unbox_alien\n");fflush(stdout); + printf("PHIL unbox_alien\n");fflush(stdout); return vm->unbox_alien(); } diff --git a/vm/code_heap.cpp b/vm/code_heap.cpp index d91e349db7..372e194cf6 100755 --- a/vm/code_heap.cpp +++ b/vm/code_heap.cpp @@ -9,17 +9,11 @@ void factorvm::init_code_heap(cell size) new_heap(&code,size); } - bool factorvm::in_code_heap_p(cell ptr) { return (ptr >= code.seg->start && ptr <= code.seg->end); } -bool in_code_heap_p(cell ptr) -{ - return vm->in_code_heap_p(ptr); // used by os specific signal handlers -} - /* Compile a word definition with the non-optimizing compiler. Allocates memory */ void factorvm::jit_compile_word(cell word_, cell def_, bool relocate) { diff --git a/vm/code_heap.hpp b/vm/code_heap.hpp index 32e1dacfee..a357699591 100755 --- a/vm/code_heap.hpp +++ b/vm/code_heap.hpp @@ -1,8 +1,5 @@ namespace factor { - -bool in_code_heap_p(cell ptr); // Used by platform specific code - struct factorvm; typedef void (*code_heap_iterator)(code_block *compiled,factorvm *myvm); diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index d8eea06f0b..df5d78d35e 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -41,7 +41,7 @@ static void call_fault_handler( a divide by zero or stack underflow in the listener */ /* Are we in compiled Factor code? Then use the current stack pointer */ - if(in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) + if(vm->in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); /* Are we in C? Then use the saved callstack top */ else diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 67327e7d30..c96addb6f4 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -107,7 +107,7 @@ static stack_frame *uap_stack_pointer(void *uap) delivered during stack frame setup/teardown or while transitioning from Factor to C is a sign of things seriously gone wrong, not just a divide by zero or stack underflow in the listener */ - if(in_code_heap_p(UAP_PROGRAM_COUNTER(uap))) + if(vm->in_code_heap_p(UAP_PROGRAM_COUNTER(uap))) { stack_frame *ptr = (stack_frame *)ucontext_stack_pointer(uap); if(!ptr) diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 9187b88986..49594ed14a 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -21,7 +21,7 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; - if(in_code_heap_p(c->EIP)) + if(vm->in_code_heap_p(c->EIP)) signal_callstack_top = (stack_frame *)c->ESP; else signal_callstack_top = NULL; From 20ef4200fb44f748e07bf3727ece63fec514144b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 23 Aug 2009 21:39:17 +0100 Subject: [PATCH 214/345] removed some error vm-> functions --- vm/errors.cpp | 28 ---------------------------- vm/errors.hpp | 9 --------- vm/mach_signal.cpp | 6 +++--- vm/os-linux-arm.cpp | 2 +- vm/os-linux.cpp | 8 ++++---- vm/os-unix.cpp | 36 ++++++++++++++++++------------------ vm/os-windows-ce.cpp | 4 ++-- vm/vm.hpp | 4 ++-- 8 files changed, 30 insertions(+), 67 deletions(-) diff --git a/vm/errors.cpp b/vm/errors.cpp index e1266cf608..09b397dd02 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -17,11 +17,6 @@ void factorvm::out_of_memory() exit(1); } -void out_of_memory() -{ - return vm->out_of_memory(); -} - void factorvm::fatal_error(const char* msg, cell tagged) { print_string("fatal_error: "); print_string(msg); @@ -29,11 +24,6 @@ void factorvm::fatal_error(const char* msg, cell tagged) exit(1); } -void fatal_error(const char* msg, cell tagged) -{ - return vm->fatal_error(msg,tagged); -} - void factorvm::critical_error(const char* msg, cell tagged) { print_string("You have triggered a bug in Factor. Please report.\n"); @@ -42,11 +32,6 @@ void factorvm::critical_error(const char* msg, cell tagged) factorbug(); } -void critical_error(const char* msg, cell tagged) -{ - return vm->critical_error(msg,tagged); -} - void factorvm::throw_error(cell error, stack_frame *callstack_top) { /* If the error handler is set, we rewind any C stack frames and @@ -98,10 +83,6 @@ void factorvm::general_error(vm_error_type error, cell arg1, cell arg2, stack_fr tag_fixnum(error),arg1,arg2),callstack_top); } -void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top) -{ - return vm->general_error(error,arg1,arg2,callstack_top); -} void factorvm::type_error(cell type, cell tagged) { @@ -113,10 +94,6 @@ void factorvm::not_implemented_error() general_error(ERROR_NOT_IMPLEMENTED,F,F,NULL); } -void not_implemented_error() -{ - return vm->not_implemented_error(); -} /* Test if 'fault' is in the guard page at the top or bottom (depending on offset being 0 or -1) of area+area_size */ @@ -155,11 +132,6 @@ void factorvm::divide_by_zero_error() general_error(ERROR_DIVIDE_BY_ZERO,F,F,NULL); } -void divide_by_zero_error() -{ - return vm->divide_by_zero_error(); -} - void factorvm::fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top) { general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),F,signal_callstack_top); diff --git a/vm/errors.hpp b/vm/errors.hpp index 69a90b70e2..cc85cfd0a8 100755 --- a/vm/errors.hpp +++ b/vm/errors.hpp @@ -23,16 +23,7 @@ enum vm_error_type ERROR_FP_TRAP, }; -void out_of_memory(); -void fatal_error(const char* msg, cell tagged); -void critical_error(const char* msg, cell tagged); - PRIMITIVE(die); - -void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *native_stack); -void not_implemented_error(); -void fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top); - PRIMITIVE(call_clear); PRIMITIVE(unimplemented); diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index df5d78d35e..7b050f72dc 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -203,13 +203,13 @@ void mach_initialize () /* Allocate a port on which the thread shall listen for exceptions. */ if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port) != KERN_SUCCESS) - fatal_error("mach_port_allocate() failed",0); + vm->fatal_error("mach_port_allocate() failed",0); /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html. */ if (mach_port_insert_right (self, our_exception_port, our_exception_port, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) - fatal_error("mach_port_insert_right() failed",0); + vm->fatal_error("mach_port_insert_right() failed",0); /* The exceptions we want to catch. */ mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC; @@ -226,7 +226,7 @@ void mach_initialize () if (task_set_exception_ports (self, mask, our_exception_port, EXCEPTION_DEFAULT, MACHINE_THREAD_STATE) != KERN_SUCCESS) - fatal_error("task_set_exception_ports() failed",0); + vm->fatal_error("task_set_exception_ports() failed",0); } } diff --git a/vm/os-linux-arm.cpp b/vm/os-linux-arm.cpp index 8e131b9011..9a9ce7ada5 100644 --- a/vm/os-linux-arm.cpp +++ b/vm/os-linux-arm.cpp @@ -25,7 +25,7 @@ void flush_icache(cell start, cell len) : "r0","r1","r2"); if(result < 0) - critical_error("flush_icache() failed",result); + vm->critical_error("flush_icache() failed",result); } } diff --git a/vm/os-linux.cpp b/vm/os-linux.cpp index 62deb70f01..c21f8142a1 100644 --- a/vm/os-linux.cpp +++ b/vm/os-linux.cpp @@ -11,7 +11,7 @@ const char *vm_executable_path() int size = readlink("/proc/self/exe", path, PATH_MAX); if (size < 0) { - fatal_error("Cannot read /proc/self/exe",0); + vm->fatal_error("Cannot read /proc/self/exe",0); return NULL; } else @@ -42,19 +42,19 @@ VM_C_API int inotify_rm_watch(int fd, u32 wd) VM_C_API int inotify_init() { - not_implemented_error(); + vm->not_implemented_error(); return -1; } VM_C_API int inotify_add_watch(int fd, const char *name, u32 mask) { - not_implemented_error(); + vm->not_implemented_error(); return -1; } VM_C_API int inotify_rm_watch(int fd, u32 wd) { - not_implemented_error(); + vm->not_implemented_error(); return -1; } diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index c96addb6f4..3cb7295996 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -9,11 +9,11 @@ void start_thread(void *(*start_routine)(void *)) pthread_t thread; if (pthread_attr_init (&attr) != 0) - fatal_error("pthread_attr_init() failed",0); + vm->fatal_error("pthread_attr_init() failed",0); if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0) - fatal_error("pthread_attr_setdetachstate() failed",0); + vm->fatal_error("pthread_attr_setdetachstate() failed",0); if (pthread_create (&thread, &attr, start_routine, NULL) != 0) - fatal_error("pthread_create() failed",0); + vm->fatal_error("pthread_create() failed",0); pthread_attr_destroy (&attr); } @@ -51,7 +51,7 @@ void *ffi_dlsym(dll *dll, symbol_char *symbol) void ffi_dlclose(dll *dll) { if(dlclose(dll->dll)) - general_error(ERROR_FFI,F,F,NULL); + vm->general_error(ERROR_FFI,F,F,NULL); dll->dll = NULL; } @@ -71,13 +71,13 @@ segment *alloc_segment(cell size) MAP_ANON | MAP_PRIVATE,-1,0); if(array == (char*)-1) - out_of_memory(); + vm->out_of_memory(); if(mprotect(array,pagesize,PROT_NONE) == -1) - fatal_error("Cannot protect low guard page",(cell)array); + vm->fatal_error("Cannot protect low guard page",(cell)array); if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1) - fatal_error("Cannot protect high guard page",(cell)array); + vm->fatal_error("Cannot protect high guard page",(cell)array); segment *retval = (segment *)vm->safe_malloc(sizeof(segment)); @@ -96,7 +96,7 @@ void dealloc_segment(segment *block) pagesize + block->size + pagesize); if(retval) - fatal_error("dealloc_segment failed",0); + vm->fatal_error("dealloc_segment failed",0); free(block); } @@ -111,7 +111,7 @@ static stack_frame *uap_stack_pointer(void *uap) { stack_frame *ptr = (stack_frame *)ucontext_stack_pointer(uap); if(!ptr) - critical_error("Invalid uap",(cell)uap); + vm->critical_error("Invalid uap",(cell)uap); return ptr; } else @@ -154,7 +154,7 @@ static void sigaction_safe(int signum, const struct sigaction *act, struct sigac while(ret == -1 && errno == EINTR); if(ret == -1) - fatal_error("sigaction failed", 0); + vm->fatal_error("sigaction failed", 0); } void unix_init_signals() @@ -216,7 +216,7 @@ extern "C" { void safe_close(int fd) { if(close(fd) < 0) - fatal_error("error closing fd",errno); + vm->fatal_error("error closing fd",errno); } bool check_write(int fd, void *data, ssize_t size) @@ -235,7 +235,7 @@ bool check_write(int fd, void *data, ssize_t size) void safe_write(int fd, void *data, ssize_t size) { if(!check_write(fd,data,size)) - fatal_error("error writing fd",errno); + vm->fatal_error("error writing fd",errno); } bool safe_read(int fd, void *data, ssize_t size) @@ -247,7 +247,7 @@ bool safe_read(int fd, void *data, ssize_t size) return safe_read(fd,data,size); else { - fatal_error("error reading fd",errno); + vm->fatal_error("error reading fd",errno); return false; } } @@ -266,7 +266,7 @@ void *stdin_loop(void *arg) break; if(buf[0] != 'X') - fatal_error("stdin_loop: bad data on control fd",buf[0]); + vm->fatal_error("stdin_loop: bad data on control fd",buf[0]); for(;;) { @@ -303,19 +303,19 @@ void open_console() int filedes[2]; if(pipe(filedes) < 0) - fatal_error("Error opening control pipe",errno); + vm->fatal_error("Error opening control pipe",errno); control_read = filedes[0]; control_write = filedes[1]; if(pipe(filedes) < 0) - fatal_error("Error opening size pipe",errno); + vm->fatal_error("Error opening size pipe",errno); size_read = filedes[0]; size_write = filedes[1]; if(pipe(filedes) < 0) - fatal_error("Error opening stdin pipe",errno); + vm->fatal_error("Error opening stdin pipe",errno); stdin_read = filedes[0]; stdin_write = filedes[1]; @@ -330,7 +330,7 @@ VM_C_API void wait_for_stdin() if(errno == EINTR) wait_for_stdin(); else - fatal_error("Error writing control fd",errno); + vm->fatal_error("Error writing control fd",errno); } } diff --git a/vm/os-windows-ce.cpp b/vm/os-windows-ce.cpp index a3192b07f5..6454535f43 100644 --- a/vm/os-windows-ce.cpp +++ b/vm/os-windows-ce.cpp @@ -26,13 +26,13 @@ void flush_icache(cell start, cell end) char *getenv(char *name) { - not_implemented_error(); + vm->not_implemented_error(); return 0; /* unreachable */ } PRIMITIVE(os_envs) { - not_implemented_error(); + vm->not_implemented_error(); } void c_to_factor_toplevel(cell quot) diff --git a/vm/vm.hpp b/vm/vm.hpp index 1b0274e0b1..50006d012a 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -68,14 +68,14 @@ struct factorvm { void memory_protection_error(cell addr, stack_frame *native_stack); void signal_error(int signal, stack_frame *native_stack); void divide_by_zero_error(); - void fp_trap_error(stack_frame *signal_callstack_top); + void fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top); inline void vmprim_call_clear(); inline void vmprim_unimplemented(); void memory_signal_handler_impl(); void misc_signal_handler_impl(); void fp_signal_handler_impl(); void type_error(cell type, cell tagged); - void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top); + void general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *native_stack); //callstack From e2d246f37158b09a0cffd64dd30f86a3f7c2c4ba Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 18:34:30 +0100 Subject: [PATCH 215/345] removed most global functions from factor.cpp --- vm/factor.cpp | 116 +++++++++++--------------------------------------- vm/factor.hpp | 10 ----- 2 files changed, 24 insertions(+), 102 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 883b4f9462..0c47e2329a 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -3,7 +3,7 @@ namespace factor { -factorvm *vm = new factorvm; +factorvm *vm; void factorvm::default_parameters(vm_parameters *p) { @@ -45,11 +45,6 @@ void factorvm::default_parameters(vm_parameters *p) p->stack_traces = true; } -VM_C_API void default_parameters(vm_parameters *p) -{ - return vm->default_parameters(p); -} - bool factorvm::factor_arg(const vm_char* str, const vm_char* arg, cell* value) { int val; @@ -62,11 +57,6 @@ bool factorvm::factor_arg(const vm_char* str, const vm_char* arg, cell* value) return false; } -bool factor_arg(const vm_char* str, const vm_char* arg, cell* value) -{ - return vm->factor_arg(str,arg,value); -} - void factorvm::init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv) { default_parameters(p); @@ -92,11 +82,6 @@ void factorvm::init_parameters_from_args(vm_parameters *p, int argc, vm_char **a } } -VM_C_API void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv) -{ - return vm->init_parameters_from_args(p,argc,argv); -} - /* Do some initialization that we do once only */ void factorvm::do_stage1_init() { @@ -110,11 +95,6 @@ void factorvm::do_stage1_init() fflush(stdout); } -void do_stage1_init() -{ - return vm->do_stage1_init(); -} - void factorvm::init_factor(vm_parameters *p) { /* Kilobytes */ @@ -171,11 +151,6 @@ void factorvm::init_factor(vm_parameters *p) } } -VM_C_API void init_factor(vm_parameters *p) -{ - return vm->init_factor(p); -} - /* May allocate memory */ void factorvm::pass_args_to_factor(int argc, vm_char **argv) { @@ -189,11 +164,6 @@ void factorvm::pass_args_to_factor(int argc, vm_char **argv) userenv[ARGS_ENV] = args.elements.value(); } -VM_C_API void pass_args_to_factor(int argc, vm_char **argv) -{ - return vm->pass_args_to_factor(argc,argv); -} - void factorvm::start_factor(vm_parameters *p) { if(p->fep) factorbug(); @@ -203,20 +173,28 @@ void factorvm::start_factor(vm_parameters *p) unnest_stacks(); } -void start_factor(vm_parameters *p) + +char *factorvm::factor_eval_string(char *string) { - return vm->start_factor(p); + char *(*callback)(char *) = (char *(*)(char *))alien_offset(userenv[EVAL_CALLBACK_ENV]); + return callback(string); } -void factorvm::start_embedded_factor(vm_parameters *p) +void factorvm::factor_eval_free(char *result) { - userenv[EMBEDDED_ENV] = T; - start_factor(p); + free(result); } -VM_C_API void start_embedded_factor(vm_parameters *p) +void factorvm::factor_yield() { - return vm->start_embedded_factor(p); + void (*callback)() = (void (*)())alien_offset(userenv[YIELD_CALLBACK_ENV]); + callback(); +} + +void factorvm::factor_sleep(long us) +{ + void (*callback)(long) = (void (*)(long))alien_offset(userenv[SLEEP_CALLBACK_ENV]); + callback(us); } void factorvm::start_standalone_factor(int argc, vm_char **argv) @@ -229,54 +207,6 @@ void factorvm::start_standalone_factor(int argc, vm_char **argv) start_factor(&p); } -VM_C_API void start_standalone_factor(int argc, vm_char **argv) -{ - return vm->start_standalone_factor(argc,argv); -} - -char *factorvm::factor_eval_string(char *string) -{ - char *(*callback)(char *) = (char *(*)(char *))alien_offset(userenv[EVAL_CALLBACK_ENV]); - return callback(string); -} - -VM_C_API char *factor_eval_string(char *string) -{ - return vm->factor_eval_string(string); -} - -void factorvm::factor_eval_free(char *result) -{ - free(result); -} - -VM_C_API void factor_eval_free(char *result) -{ - return vm->factor_eval_free(result); -} - -void factorvm::factor_yield() -{ - void (*callback)() = (void (*)())alien_offset(userenv[YIELD_CALLBACK_ENV]); - callback(); -} - -VM_C_API void factor_yield() -{ - return vm->factor_yield(); -} - -void factorvm::factor_sleep(long us) -{ - void (*callback)(long) = (void (*)(long))alien_offset(userenv[SLEEP_CALLBACK_ENV]); - callback(us); -} - -VM_C_API void factor_sleep(long us) -{ - return vm->factor_sleep(us); -} - struct startargs { int argc; vm_char **argv; @@ -286,16 +216,18 @@ void* start_standalone_factor_thread(void *arg) { factorvm *newvm = new factorvm; startargs *args = (startargs*) arg; - vm_parameters p; - newvm->default_parameters(&p); - newvm->init_parameters_from_args(&p,args->argc, args->argv); - newvm->init_factor(&p); - newvm->pass_args_to_factor(args->argc,args->argv); - newvm->start_factor(&p); + newvm->start_standalone_factor(args->argc, args->argv); return 0; } +VM_C_API void start_standalone_factor(int argc, vm_char **argv) +{ + factorvm *newvm = new factorvm; + vm = newvm; + return newvm->start_standalone_factor(argc,argv); +} + VM_C_API void start_standalone_factor_in_new_thread(int argc, vm_char **argv) { startargs *args = new startargs; // leaks startargs structure diff --git a/vm/factor.hpp b/vm/factor.hpp index b2aeccd1a6..5c5b92dff2 100644 --- a/vm/factor.hpp +++ b/vm/factor.hpp @@ -1,17 +1,7 @@ namespace factor { -VM_C_API void default_parameters(vm_parameters *p); -VM_C_API void init_parameters_from_args(vm_parameters *p, int argc, vm_char **argv); -VM_C_API void init_factor(vm_parameters *p); -VM_C_API void pass_args_to_factor(int argc, vm_char **argv); -VM_C_API void start_embedded_factor(vm_parameters *p); VM_C_API void start_standalone_factor(int argc, vm_char **argv); VM_C_API void start_standalone_factor_in_new_thread(int argc, vm_char **argv); -VM_C_API char *factor_eval_string(char *string); -VM_C_API void factor_eval_free(char *result); -VM_C_API void factor_yield(); -VM_C_API void factor_sleep(long ms); - } From e05f91f3a8bff7a7413e2d0d5bc897e6613e4d71 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 19:15:39 +0100 Subject: [PATCH 216/345] cleaning up stray vm singleton usage --- vm/code_block.cpp | 4 ++-- vm/inlineimpls.hpp | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/vm/code_block.cpp b/vm/code_block.cpp index 9eec4a2ea6..c2dfe1cac3 100755 --- a/vm/code_block.cpp +++ b/vm/code_block.cpp @@ -345,8 +345,8 @@ void copy_literal_references(code_block *compiled, factorvm *myvm) void factorvm::relocate_code_block_step(relocation_entry rel, cell index, code_block *compiled) { #ifdef FACTOR_DEBUG - tagged(compiled->literals).untag_check(vm); - tagged(compiled->relocation).untag_check(vm); + tagged(compiled->literals).untag_check(this); + tagged(compiled->relocation).untag_check(this); #endif store_address_in_code_block(relocation_class_of(rel), diff --git a/vm/inlineimpls.hpp b/vm/inlineimpls.hpp index 6b74634715..a247afa4d7 100644 --- a/vm/inlineimpls.hpp +++ b/vm/inlineimpls.hpp @@ -176,7 +176,6 @@ struct gc_root : public tagged void push() { myvm->check_tagged_pointer(tagged::value()); myvm->gc_locals.push_back((cell)this); } - //explicit gc_root(cell value_, factorvm *vm) : myvm(vm),tagged(value_) { push(); } explicit gc_root(cell value_,factorvm *vm) : tagged(value_),myvm(vm) { push(); } explicit gc_root(TYPE *value_, factorvm *vm) : tagged(value_),myvm(vm) { push(); } @@ -344,7 +343,7 @@ inline double factorvm::fixnum_to_float(cell tagged) keep the callstack in a GC root and use relative offsets */ template void factorvm::iterate_callstack_object(callstack *stack_, TYPE &iterator) { - gc_root stack(stack_,vm); + gc_root stack(stack_,this); fixnum frame_offset = untag_fixnum(stack->length) - sizeof(stack_frame); while(frame_offset >= 0) From e98f168a11e58ef4fe9d36e16b02763e8a21bb95 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 20:10:56 +0100 Subject: [PATCH 217/345] print_native_string doesn't need singleton ptr --- vm/os-unix.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index 52dc1bfd6e..8aff18364e 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -40,7 +40,7 @@ typedef char symbol_char; #define OPEN_READ(path) fopen(path,"rb") #define OPEN_WRITE(path) fopen(path,"wb") -#define print_native_string(string) vm->print_string(string) +#define print_native_string(string) print_string(string) void start_thread(void *(*start_routine)(void *)); From 12ca7bdc57c4958b6d6079a3eb3737420c1bdde6 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 20:26:10 +0100 Subject: [PATCH 218/345] added factorvm ptrs to the rest of alien functions. (Left commented debug lines to remind me that these haven't been tested yet, and some are osx specific) --- vm/alien.cpp | 30 +++++++++++++++--------------- vm/alien.hpp | 10 +++++----- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/vm/alien.cpp b/vm/alien.cpp index 282f5c2fc9..9eb6da9784 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -224,10 +224,10 @@ char *factorvm::unbox_alien() return alien_offset(dpop()); } -VM_C_API char *unbox_alien() +VM_C_API char *unbox_alien(factorvm *myvm) { - printf("PHIL unbox_alien\n");fflush(stdout); - return vm->unbox_alien(); + //printf("PHIL unbox_alien %d %d\n",vm,myvm);fflush(stdout); + return VM_PTR->unbox_alien(); } /* make an alien and push */ @@ -251,10 +251,10 @@ void factorvm::to_value_struct(cell src, void *dest, cell size) memcpy(dest,alien_offset(src),size); } -VM_C_API void to_value_struct(cell src, void *dest, cell size) +VM_C_API void to_value_struct(cell src, void *dest, cell size, factorvm *myvm) { - printf("PHIL to_value_struct\n"); - return vm->to_value_struct(src,dest,size); + //printf("PHIL to_value_struct %d %d\n",vm,myvm);fflush(stdout); + return VM_PTR->to_value_struct(src,dest,size); } /* for FFI callbacks receiving structs by value */ @@ -265,10 +265,10 @@ void factorvm::box_value_struct(void *src, cell size) dpush(tag(bytes)); } -VM_C_API void box_value_struct(void *src, cell size) +VM_C_API void box_value_struct(void *src, cell size,factorvm *myvm) { - printf("PHIL box_value_struct\n"); - return vm->box_value_struct(src,size); + //printf("PHIL box_value_struct %d %d\n",vm,myvm);fflush(stdout); + return VM_PTR->box_value_struct(src,size); } /* On some x86 OSes, structs <= 8 bytes are returned in registers. */ @@ -280,10 +280,10 @@ void factorvm::box_small_struct(cell x, cell y, cell size) box_value_struct(data,size); } -VM_C_API void box_small_struct(cell x, cell y, cell size) +VM_C_API void box_small_struct(cell x, cell y, cell size, factorvm *myvm) { - printf("PHIL box_small_struct\n"); - return vm->box_small_struct(x,y,size); + //printf("PHIL box_small_struct %d %d\n",vm,myvm);fflush(stdout); + return VM_PTR->box_small_struct(x,y,size); } /* On OS X/PPC, complex numbers are returned in registers. */ @@ -297,10 +297,10 @@ void factorvm::box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) box_value_struct(data,size); } -VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) +VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size, factorvm *myvm) { - printf("PHIL box_medium_struct\n"); - return vm->box_medium_struct(x1, x2, x3, x4, size); + //printf("PHIL box_medium_struct %d %d\n",vm,myvm);fflush(stdout); + return VM_PTR->box_medium_struct(x1, x2, x3, x4, size); } } diff --git a/vm/alien.hpp b/vm/alien.hpp index 327d791406..7b537146fd 100755 --- a/vm/alien.hpp +++ b/vm/alien.hpp @@ -37,11 +37,11 @@ PRIMITIVE(dlclose); PRIMITIVE(dll_validp); VM_C_API char *alien_offset(cell object, factorvm *vm); -VM_C_API char *unbox_alien(); +VM_C_API char *unbox_alien(factorvm *vm); VM_C_API void box_alien(void *ptr, factorvm *vm); -VM_C_API void to_value_struct(cell src, void *dest, cell size); -VM_C_API void box_value_struct(void *src, cell size); -VM_C_API void box_small_struct(cell x, cell y, cell size); -VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size); +VM_C_API void to_value_struct(cell src, void *dest, cell size, factorvm *vm); +VM_C_API void box_value_struct(void *src, cell size,factorvm *vm); +VM_C_API void box_small_struct(cell x, cell y, cell size,factorvm *vm); +VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size,factorvm *vm); } From 9cac5e8aa99cc255eac8c32e0a2759076a82e273 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 20:46:34 +0100 Subject: [PATCH 219/345] added lookup_vm and removed last usage of vm from windows code --- vm/errors.cpp | 6 +++--- vm/errors.hpp | 7 ------- vm/factor.cpp | 15 +++++++++++++++ vm/mach_signal.cpp | 8 ++++---- vm/os-unix.cpp | 8 ++++---- vm/os-unix.hpp | 1 + vm/os-windows-nt.cpp | 23 ++++++++++++----------- vm/os-windows-nt.hpp | 5 +++-- vm/vm.hpp | 8 ++++++++ 9 files changed, 50 insertions(+), 31 deletions(-) diff --git a/vm/errors.cpp b/vm/errors.cpp index 09b397dd02..09e6313f29 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -165,7 +165,7 @@ void factorvm::memory_signal_handler_impl() void memory_signal_handler_impl() { - return vm->memory_signal_handler_impl(); + SIGNAL_VM_PTR->misc_signal_handler_impl(); } void factorvm::misc_signal_handler_impl() @@ -175,7 +175,7 @@ void factorvm::misc_signal_handler_impl() void misc_signal_handler_impl() { - vm->misc_signal_handler_impl(); + SIGNAL_VM_PTR->misc_signal_handler_impl(); } void factorvm::fp_signal_handler_impl() @@ -185,7 +185,7 @@ void factorvm::fp_signal_handler_impl() void fp_signal_handler_impl() { - vm->fp_signal_handler_impl(); + SIGNAL_VM_PTR->fp_signal_handler_impl(); } } diff --git a/vm/errors.hpp b/vm/errors.hpp index cc85cfd0a8..8725941920 100755 --- a/vm/errors.hpp +++ b/vm/errors.hpp @@ -27,13 +27,6 @@ PRIMITIVE(die); PRIMITIVE(call_clear); PRIMITIVE(unimplemented); -/* Global variables used to pass fault handler state from signal handler to -user-space */ -extern cell signal_number; -extern cell signal_fault_addr; -extern unsigned int signal_fpu_status; -extern stack_frame *signal_callstack_top; - void memory_signal_handler_impl(); void fp_signal_handler_impl(); void misc_signal_handler_impl(); diff --git a/vm/factor.cpp b/vm/factor.cpp index 0c47e2329a..6e31a02cab 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -5,6 +5,19 @@ namespace factor factorvm *vm; +unordered_map thread_vms; + +factorvm *lookup_vm(long threadid) +{ + return thread_vms[threadid]; +} + +void register_vm(long threadid, factorvm *vm) +{ + thread_vms[threadid] = vm; +} + + void factorvm::default_parameters(vm_parameters *p) { p->image_path = NULL; @@ -199,6 +212,8 @@ void factorvm::factor_sleep(long us) void factorvm::start_standalone_factor(int argc, vm_char **argv) { + printf("thread id is %d\n",GetCurrentThreadId());fflush(stdout); + register_vm(GetCurrentThreadId(),this); vm_parameters p; default_parameters(&p); init_parameters_from_args(&p,argc,argv); diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index 7b050f72dc..ca3b7aa161 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -42,17 +42,17 @@ static void call_fault_handler( /* Are we in compiled Factor code? Then use the current stack pointer */ if(vm->in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) - signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); + vm->signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); /* Are we in C? Then use the saved callstack top */ else - signal_callstack_top = NULL; + vm->signal_callstack_top = NULL; MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state)); /* Now we point the program counter at the right handler function. */ if(exception == EXC_BAD_ACCESS) { - signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); + vm->signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); MACH_PROGRAM_COUNTER(thread_state) = (cell)memory_signal_handler_impl; } else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV) @@ -63,7 +63,7 @@ static void call_fault_handler( } else { - signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); + vm->signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); MACH_PROGRAM_COUNTER(thread_state) = (cell)misc_signal_handler_impl; } } diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 3cb7295996..97e1e0c04d 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -120,15 +120,15 @@ static stack_frame *uap_stack_pointer(void *uap) void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - signal_fault_addr = (cell)siginfo->si_addr; - signal_callstack_top = uap_stack_pointer(uap); + vm->signal_fault_addr = (cell)siginfo->si_addr; + vm->signal_callstack_top = uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)memory_signal_handler_impl; } void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - signal_number = signal; - signal_callstack_top = uap_stack_pointer(uap); + vm->signal_number = signal; + vm->signal_callstack_top = uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)misc_signal_handler_impl; } diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index 8aff18364e..b7e528a421 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -58,4 +58,5 @@ void sleep_micros(cell usec); void open_console(); +#define SIGNAL_VM_PTR vm } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 49594ed14a..a4cdbc66af 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -3,8 +3,8 @@ namespace factor { -void start_thread(void *(*start_routine)(void *),void *args){ - CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); +void *start_thread(void *(*start_routine)(void *),void *args){ + return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); } @@ -18,18 +18,19 @@ s64 factorvm::current_micros() FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) { + printf("exception handler %d\n",GetCurrentThreadId()); + factorvm *myvm = lookup_vm(GetCurrentThreadId()); PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; - if(vm->in_code_heap_p(c->EIP)) - signal_callstack_top = (stack_frame *)c->ESP; + if(myvm->in_code_heap_p(c->EIP)) + myvm->signal_callstack_top = (stack_frame *)c->ESP; else - signal_callstack_top = NULL; + myvm->signal_callstack_top = NULL; - switch (e->ExceptionCode) - { - case EXCEPTION_ACCESS_VIOLATION: - signal_fault_addr = e->ExceptionInformation[1]; + switch (e->ExceptionCode) { + case EXCEPTION_ACCESS_VIOLATION: + myvm->signal_fault_addr = e->ExceptionInformation[1]; c->EIP = (cell)memory_signal_handler_impl; break; @@ -42,7 +43,7 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) case STATUS_FLOAT_UNDERFLOW: case STATUS_FLOAT_MULTIPLE_FAULTS: case STATUS_FLOAT_MULTIPLE_TRAPS: - signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c)); + myvm->signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c)); X87SW(c) = 0; MXCSR(c) &= 0xffffffc0; c->EIP = (cell)fp_signal_handler_impl; @@ -56,7 +57,7 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) enabled. Don't really have any idea what this exception means. */ break; default: - signal_number = e->ExceptionCode; + myvm->signal_number = e->ExceptionCode; c->EIP = (cell)misc_signal_handler_impl; break; } diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index 27923a000a..e5c5adadf1 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -21,12 +21,13 @@ typedef char symbol_char; FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe); - // SSE traps raise these exception codes, which are defined in internal NT headers // but not winbase.h #define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4 #define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5 -void start_thread(void *(*start_routine)(void *),void *args); +void *start_thread(void *(*start_routine)(void *),void *args); + +#define SIGNAL_VM_PTR lookup_vm(GetCurrentThreadId()) } diff --git a/vm/vm.hpp b/vm/vm.hpp index 50006d012a..b3e40640b7 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -59,6 +59,12 @@ struct factorvm { inline void vmprim_profiling(); // errors + /* Global variables used to pass fault handler state from signal handler to + user-space */ + cell signal_number; + cell signal_fault_addr; + unsigned int signal_fpu_status; + stack_frame *signal_callstack_top; void out_of_memory(); void fatal_error(const char* msg, cell tagged); void critical_error(const char* msg, cell tagged); @@ -692,4 +698,6 @@ struct factorvm { extern factorvm *vm; +extern factorvm *lookup_vm(long threadid); +extern void register_vm(long threadid,factorvm *vm); } From 5c2a28173a344a185aa301739531e5d239ba7ae6 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 20:59:05 +0100 Subject: [PATCH 220/345] Start windows factor in a spawned thread (for testing) --- vm/factor.cpp | 7 ++++--- vm/factor.hpp | 2 +- vm/main-windows-nt.cpp | 2 ++ vm/os-windows-nt.cpp | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 6e31a02cab..bb30aae7ee 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -212,7 +212,7 @@ void factorvm::factor_sleep(long us) void factorvm::start_standalone_factor(int argc, vm_char **argv) { - printf("thread id is %d\n",GetCurrentThreadId());fflush(stdout); + //printf("thread id is %d\n",GetCurrentThreadId());fflush(stdout); register_vm(GetCurrentThreadId(),this); vm_parameters p; default_parameters(&p); @@ -243,11 +243,12 @@ VM_C_API void start_standalone_factor(int argc, vm_char **argv) return newvm->start_standalone_factor(argc,argv); } -VM_C_API void start_standalone_factor_in_new_thread(int argc, vm_char **argv) +VM_C_API void *start_standalone_factor_in_new_thread(int argc, vm_char **argv) { startargs *args = new startargs; // leaks startargs structure args->argc = argc; args->argv = argv; - start_thread(start_standalone_factor_thread,args); + void *handle = start_thread(start_standalone_factor_thread,args); + return handle; } } diff --git a/vm/factor.hpp b/vm/factor.hpp index 5c5b92dff2..fbd6873c28 100644 --- a/vm/factor.hpp +++ b/vm/factor.hpp @@ -2,6 +2,6 @@ namespace factor { VM_C_API void start_standalone_factor(int argc, vm_char **argv); -VM_C_API void start_standalone_factor_in_new_thread(int argc, vm_char **argv); +VM_C_API void *start_standalone_factor_in_new_thread(int argc, vm_char **argv); } diff --git a/vm/main-windows-nt.cpp b/vm/main-windows-nt.cpp index eaaad0f55b..4a0fc2ae35 100644 --- a/vm/main-windows-nt.cpp +++ b/vm/main-windows-nt.cpp @@ -17,6 +17,8 @@ int WINAPI WinMain( } factor::start_standalone_factor(nArgs,szArglist); + //HANDLE thread = factor::start_standalone_factor_in_new_thread(nArgs,szArglist); + //WaitForSingleObject(thread, INFINITE); LocalFree(szArglist); diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index a4cdbc66af..b212287e5f 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -18,7 +18,7 @@ s64 factorvm::current_micros() FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) { - printf("exception handler %d\n",GetCurrentThreadId()); + //printf("exception handler %d\n",GetCurrentThreadId());fflush(stdout); factorvm *myvm = lookup_vm(GetCurrentThreadId()); PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; From aa005c948f8aadf91f218935d9bd1cb01977787a Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 24 Aug 2009 21:10:18 +0100 Subject: [PATCH 221/345] win32 main starts factorvm in new thread --- vm/factor.cpp | 4 ++-- vm/factor.hpp | 1 - vm/os-windows-nt.cpp | 12 ++++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index bb30aae7ee..77ad60bd47 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -170,8 +170,9 @@ void factorvm::pass_args_to_factor(int argc, vm_char **argv) growable_array args(this); int i; - for(i = 1; i < argc; i++) + for(i = 1; i < argc; i++){ args.add(allot_alien(F,(cell)argv[i])); + } args.trim(); userenv[ARGS_ENV] = args.elements.value(); @@ -212,7 +213,6 @@ void factorvm::factor_sleep(long us) void factorvm::start_standalone_factor(int argc, vm_char **argv) { - //printf("thread id is %d\n",GetCurrentThreadId());fflush(stdout); register_vm(GetCurrentThreadId(),this); vm_parameters p; default_parameters(&p); diff --git a/vm/factor.hpp b/vm/factor.hpp index fbd6873c28..b824758c8a 100644 --- a/vm/factor.hpp +++ b/vm/factor.hpp @@ -3,5 +3,4 @@ namespace factor VM_C_API void start_standalone_factor(int argc, vm_char **argv); VM_C_API void *start_standalone_factor_in_new_thread(int argc, vm_char **argv); - } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index b212287e5f..c36c2f3f7e 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -18,7 +18,6 @@ s64 factorvm::current_micros() FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) { - //printf("exception handler %d\n",GetCurrentThreadId());fflush(stdout); factorvm *myvm = lookup_vm(GetCurrentThreadId()); PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; @@ -64,12 +63,17 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) return EXCEPTION_CONTINUE_EXECUTION; } +bool handler_added = 0; + void factorvm::c_to_factor_toplevel(cell quot) { - if(!AddVectoredExceptionHandler(0, (PVECTORED_EXCEPTION_HANDLER)exception_handler)) - fatal_error("AddVectoredExceptionHandler failed", 0); + if(!handler_added){ + if(!AddVectoredExceptionHandler(0, (PVECTORED_EXCEPTION_HANDLER)exception_handler)) + fatal_error("AddVectoredExceptionHandler failed", 0); + handler_added = 1; + } c_to_factor(quot,this); - RemoveVectoredExceptionHandler((void *)exception_handler); + RemoveVectoredExceptionHandler((void *)exception_handler); } void factorvm::open_console() From 6ddd3c654e5eea5bcc122076b8616855a37ead19 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 06:35:54 +0100 Subject: [PATCH 222/345] fixed up linux64 bootstrap (single threaded) --- vm/factor.cpp | 5 ++--- vm/mach_signal.cpp | 2 +- vm/os-unix.cpp | 21 ++++++++++++++++----- vm/os-unix.hpp | 2 +- vm/os-windows-nt.cpp | 4 ++++ vm/vm.hpp | 12 +++++++++--- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 77ad60bd47..a241ccf86b 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -213,7 +213,7 @@ void factorvm::factor_sleep(long us) void factorvm::start_standalone_factor(int argc, vm_char **argv) { - register_vm(GetCurrentThreadId(),this); + register_vm(thread_id(),this); vm_parameters p; default_parameters(&p); init_parameters_from_args(&p,argc,argv); @@ -247,8 +247,7 @@ VM_C_API void *start_standalone_factor_in_new_thread(int argc, vm_char **argv) { startargs *args = new startargs; // leaks startargs structure args->argc = argc; args->argv = argv; - void *handle = start_thread(start_standalone_factor_thread,args); - return handle; + return start_thread(start_standalone_factor_thread,args); } } diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index ca3b7aa161..c1d263527d 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -215,7 +215,7 @@ void mach_initialize () mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC; /* Create the thread listening on the exception port. */ - start_thread(mach_exception_thread); + start_thread(mach_exception_thread,NULL); /* Replace the exception port info for these exceptions with our own. Note that we replace the exception port for the entire task, not only diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 97e1e0c04d..4de5ede704 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -3,18 +3,18 @@ namespace factor { -void start_thread(void *(*start_routine)(void *)) +void *start_thread(void *(*start_routine)(void *),void *args) { pthread_attr_t attr; pthread_t thread; - if (pthread_attr_init (&attr) != 0) vm->fatal_error("pthread_attr_init() failed",0); if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0) vm->fatal_error("pthread_attr_setdetachstate() failed",0); - if (pthread_create (&thread, &attr, start_routine, NULL) != 0) + if (pthread_create (&thread, &attr, start_routine, args) != 0) vm->fatal_error("pthread_create() failed",0); pthread_attr_destroy (&attr); + return (void*)thread; } static void *null_dll; @@ -55,13 +55,24 @@ void ffi_dlclose(dll *dll) dll->dll = NULL; } -PRIMITIVE(existsp) + +long factorvm::thread_id(){ + return 0; // TODO fix me +} + + +inline void factorvm::vmprim_existsp() { struct stat sb; char *path = (char *)(vm->untag_check(dpop()) + 1); box_boolean(stat(path,&sb) >= 0); } +PRIMITIVE(existsp) +{ + PRIMITIVE_GETVM()->vmprim_existsp(); +} + segment *alloc_segment(cell size) { int pagesize = getpagesize(); @@ -320,7 +331,7 @@ void open_console() stdin_read = filedes[0]; stdin_write = filedes[1]; - start_thread(stdin_loop); + start_thread(stdin_loop,NULL); } VM_C_API void wait_for_stdin() diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index b7e528a421..60beb88233 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -42,7 +42,7 @@ typedef char symbol_char; #define print_native_string(string) print_string(string) -void start_thread(void *(*start_routine)(void *)); +void *start_thread(void *(*start_routine)(void *),void *args); void init_ffi(); void ffi_dlopen(dll *dll); diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index c36c2f3f7e..6085d65670 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -7,6 +7,10 @@ void *start_thread(void *(*start_routine)(void *),void *args){ return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); } +long factorvm::thread_id(){ + return GetCurrentThreadId(); +} + s64 factorvm::current_micros() { diff --git a/vm/vm.hpp b/vm/vm.hpp index b3e40640b7..384c98186a 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -669,6 +669,12 @@ struct factorvm { void print_fixnum(fixnum x); cell read_cell_hex(); + + + + // os-* + inline void vmprim_existsp(); + long thread_id(); // os-windows #if defined(WINDOWS) @@ -681,17 +687,17 @@ struct factorvm { void dealloc_segment(segment *block); segment *alloc_segment(cell size); const vm_char *vm_executable_path(); - inline void vmprim_existsp(); const vm_char *default_image_path(); void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length); bool windows_stat(vm_char *path); - + #if defined(WINNT) s64 current_micros(); void c_to_factor_toplevel(cell quot); void open_console(); - // next method here: + // next method here: #endif + #endif }; From 0bc7c0c1d0cadbead754ebaa4bbe32a981a7f374 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 06:41:37 +0100 Subject: [PATCH 223/345] separated vm-1st-arg and vm-3rd-arg asm invoke words (needed for ppc & x86.64) --- basis/compiler/codegen/codegen.factor | 4 ++-- basis/cpu/architecture/architecture.factor | 3 ++- basis/cpu/ppc/ppc.factor | 3 +++ basis/cpu/x86/32/32.factor | 5 ++++- basis/cpu/x86/64/64.factor | 4 +++- basis/cpu/x86/x86.factor | 2 +- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/basis/compiler/codegen/codegen.factor b/basis/compiler/codegen/codegen.factor index 6d43adf933..f41bc853b5 100755 --- a/basis/compiler/codegen/codegen.factor +++ b/basis/compiler/codegen/codegen.factor @@ -437,7 +437,7 @@ M: ##alien-indirect generate-insn ! Generate code for boxing input parameters in a callback. [ dup \ %save-param-reg move-parameters - "nest_stacks" %vm-invoke + "nest_stacks" %vm-invoke-1st-arg box-parameters ] with-param-regs ; @@ -475,7 +475,7 @@ TUPLE: callback-context ; [ callback-context new do-callback ] % ] [ ] make ; -: %unnest-stacks ( -- ) "unnest_stacks" %vm-invoke ; +: %unnest-stacks ( -- ) "unnest_stacks" %vm-invoke-1st-arg ; M: ##callback-return generate-insn #! All the extra book-keeping for %unwind is only for x86. diff --git a/basis/cpu/architecture/architecture.factor b/basis/cpu/architecture/architecture.factor index 6d88944881..fbec9f697a 100644 --- a/basis/cpu/architecture/architecture.factor +++ b/basis/cpu/architecture/architecture.factor @@ -298,7 +298,8 @@ M: object %prepare-var-args ; HOOK: %alien-invoke cpu ( function library -- ) -HOOK: %vm-invoke cpu ( function -- ) +HOOK: %vm-invoke-1st-arg cpu ( function -- ) +HOOK: %vm-invoke-3rd-arg cpu ( function -- ) HOOK: %cleanup cpu ( params -- ) diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index fc6a122101..83f1bc9a74 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -38,6 +38,9 @@ enable-float-intrinsics M: ppc %vm-field-ptr ( dst field -- ) %load-vm-field-addr ; +M: ppc %vm-invoke-1st-arg ( function -- ) f %alien-invoke ; +M: ppc %vm-invoke-3rd-arg ( function -- ) f %alien-invoke ; + M: ppc machine-registers { { int-regs $[ 2 12 [a,b] 15 29 [a,b] append ] } diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index 9499df7aaf..f711455fa3 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -51,11 +51,14 @@ M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ; temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument temp-reg PUSH ; -M: x86.32 %vm-invoke ( function -- ) +M: x86.32 %vm-invoke-1st-arg ( function -- ) push-vm-ptr f %alien-invoke temp-reg POP ; +M: x86.32 %vm-invoke-3rd-arg ( function -- ) + %vm-invoke-1st-arg ; ! first 2 args are regs, 3rd is stack so vm-invoke-1st-arg works here + M: x86.32 return-struct-in-registers? ( c-type -- ? ) c-type [ return-in-registers?>> ] diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 06592078d8..8c713eac8b 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -172,7 +172,9 @@ M: x86.64 %alien-invoke rc-absolute-cell rel-dlsym R11 CALL ; -M: x86.64 %vm-invoke ( function -- ) f %alien-invoke ; +M: x86.64 %vm-invoke-1st-arg ( function -- ) f %alien-invoke ; + +M: x86.64 %vm-invoke-3rd-arg ( function -- ) f %alien-invoke ; M: x86.64 %prepare-alien-indirect ( -- ) "unbox_alien" f %alien-invoke diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 9ac787a027..91705efec6 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -615,7 +615,7 @@ M:: x86 %call-gc ( gc-root-count -- ) ! Pass number of roots as second parameter param-reg-2 gc-root-count MOV ! Call GC - "inline_gc" %vm-invoke ; ! (PHIL) TODO: vm-invoke won't work with ppc or x86.64. need %vm-invoke-3rd + "inline_gc" %vm-invoke-3rd-arg ; M: x86 %alien-global ( dst symbol library -- ) [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ; From be1b079eb57501c5ca935289d12531d98ff52a3b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 06:59:26 +0100 Subject: [PATCH 224/345] Primitives now pass vm ptr on 64bit x86 --- basis/cpu/x86/64/bootstrap.factor | 3 +++ vm/primitives.hpp | 1 + 2 files changed, 4 insertions(+) diff --git a/basis/cpu/x86/64/bootstrap.factor b/basis/cpu/x86/64/bootstrap.factor index 8b0d53cda5..28309e7d97 100644 --- a/basis/cpu/x86/64/bootstrap.factor +++ b/basis/cpu/x86/64/bootstrap.factor @@ -21,6 +21,9 @@ IN: bootstrap.x86 : rex-length ( -- n ) 1 ; [ + ! HACK: stash vm pointer above the ds stack + temp0 0 MOV rc-absolute-cell rt-vm jit-rel + ds-reg bootstrap-cell [+] temp0 MOV ! load stack_chain temp0 0 MOV rc-absolute-cell rt-stack-chain jit-rel temp0 temp0 [] MOV diff --git a/vm/primitives.hpp b/vm/primitives.hpp index 8e6c3b8f51..5eefc19e01 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -13,6 +13,7 @@ namespace factor extern const primitive_type primitives[]; #define PRIMITIVE_OVERFLOW_GETVM() vm + #define VM_PTR vm #define ASSERTVM() } From 334f4c3455ad11e0a46a0cf680d6644e79801489 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 07:15:30 +0100 Subject: [PATCH 225/345] overflow functions now retrieving their vm ptr from above ds stack --- vm/primitives.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/vm/primitives.hpp b/vm/primitives.hpp index 5eefc19e01..8e6c3b8f51 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -13,7 +13,6 @@ namespace factor extern const primitive_type primitives[]; #define PRIMITIVE_OVERFLOW_GETVM() vm - #define VM_PTR vm #define ASSERTVM() } From 5cd2fbb56450817b67c8bf1039b913bd042c59e9 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 07:30:16 +0100 Subject: [PATCH 226/345] vm ptr passed to lazy_jit_compile on x86.64 --- basis/cpu/x86/64/64.factor | 8 ++++++-- vm/cpu-x86.32.S | 14 +++++++++++++- vm/cpu-x86.64.S | 11 +++++++++++ vm/cpu-x86.S | 12 +----------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 8c713eac8b..46b1e9866f 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -172,9 +172,13 @@ M: x86.64 %alien-invoke rc-absolute-cell rel-dlsym R11 CALL ; -M: x86.64 %vm-invoke-1st-arg ( function -- ) f %alien-invoke ; +M: x86.64 %vm-invoke-1st-arg ( function -- ) + param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup + f %alien-invoke ; -M: x86.64 %vm-invoke-3rd-arg ( function -- ) f %alien-invoke ; +M: x86.64 %vm-invoke-3rd-arg ( function -- ) + param-reg-3 0 MOV rc-absolute-cell rt-vm rel-fixup + f %alien-invoke ; M: x86.64 %prepare-alien-indirect ( -- ) "unbox_alien" f %alien-invoke diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index 9d2bf082d1..326ddb6eb8 100644 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -80,7 +80,7 @@ DEF(void,set_x87_env,(const void*)): ret DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to, void *vm)): - mov CELL_SIZE(STACK_REG),NV_TEMP_REG /* stash vm ptr */ + mov CELL_SIZE(STACK_REG),NV_TEMP_REG /* get vm ptr in case quot_xt = lazy_jit_compile */ /* clear x87 stack, but preserve rounding mode and exception flags */ sub $2,STACK_REG fnstcw (STACK_REG) @@ -91,6 +91,18 @@ DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to, void *vm)): mov NV_TEMP_REG,ARG1 jmp *QUOT_XT_OFFSET(ARG0) + +DEF(F_FASTCALL void,lazy_jit_compile,(CELL quot, void *vm)): + mov ARG1,NV_TEMP_REG /* stash vm ptr */ + mov STACK_REG,ARG1 /* Save stack pointer */ + sub $STACK_PADDING,STACK_REG + push NV_TEMP_REG /* push vm ptr as arg3 */ + call MANGLE(lazy_jit_compile_impl) + pop NV_TEMP_REG + mov RETURN_REG,ARG0 /* No-op on 32-bit */ + add $STACK_PADDING,STACK_REG + jmp *QUOT_XT_OFFSET(ARG0) /* Call the quotation */ + #include "cpu-x86.S" diff --git a/vm/cpu-x86.64.S b/vm/cpu-x86.64.S index 98addeb80d..4d5c70616f 100644 --- a/vm/cpu-x86.64.S +++ b/vm/cpu-x86.64.S @@ -115,6 +115,17 @@ DEF(F_FASTCALL void,throw_impl,(CELL quot, F_STACK_FRAME *rewind_to, void *vm)): fldcw (STACK_REG) /* rewind_to */ mov ARG1,STACK_REG + mov ARG2,ARG1 /* make vm ptr 2nd arg in case quot_xt = lazy_jit_compile */ jmp *QUOT_XT_OFFSET(ARG0) + +DEF(F_FASTCALL void,lazy_jit_compile,(CELL quot, void *vm)): + mov ARG1,ARG2 /* vm is 3rd arg */ + mov STACK_REG,ARG1 /* Save stack pointer */ + sub $STACK_PADDING,STACK_REG + call MANGLE(lazy_jit_compile_impl) + mov RETURN_REG,ARG0 /* No-op on 32-bit */ + add $STACK_PADDING,STACK_REG + jmp *QUOT_XT_OFFSET(ARG0) /* Call the quotation */ + #include "cpu-x86.S" diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index 03f08fdabc..2599ba3f97 100644 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -56,17 +56,6 @@ DEF(F_FASTCALL void,c_to_factor,(CELL quot, void *vm)): POP_NONVOLATILE ret -DEF(F_FASTCALL void,lazy_jit_compile,(CELL quot, void *vm)): - mov ARG1,NV_TEMP_REG /* stash vm ptr */ - mov STACK_REG,ARG1 /* Save stack pointer */ - sub $STACK_PADDING,STACK_REG - push NV_TEMP_REG /* push vm ptr as arg3 */ - call MANGLE(lazy_jit_compile_impl) - pop NV_TEMP_REG - mov RETURN_REG,ARG0 /* No-op on 32-bit */ - add $STACK_PADDING,STACK_REG - jmp *QUOT_XT_OFFSET(ARG0) /* Call the quotation */ - /* cpu.x86.features calls this */ DEF(bool,sse_version,(void)): mov $0x1,RETURN_REG @@ -103,6 +92,7 @@ sse_2: sse_1: mov $10,RETURN_REG ret + #ifdef WINDOWS .section .drectve .ascii " -export:sse_version" From b02c602a89f81cdc11f2390f9d9767ab45bce268 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 07:55:51 +0100 Subject: [PATCH 227/345] added vm passing to some alien/boxing functions and added some vm asserts --- basis/cpu/x86/64/64.factor | 13 ++++++++++--- vm/alien.cpp | 10 +++++----- vm/data_gc.cpp | 1 + vm/math.cpp | 3 +++ vm/quotations.cpp | 1 + 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 46b1e9866f..a0ca3d17c7 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -74,9 +74,14 @@ M: x86.64 %prepare-unbox ( -- ) param-reg-1 R14 [] MOV R14 cell SUB ; +: %vm-invoke-2nd-arg ( function -- ) + param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup + f %alien-invoke ; + + M:: x86.64 %unbox ( n rep func -- ) ! Call the unboxer - func f %alien-invoke + func %vm-invoke-2nd-arg ! Store the return value on the C stack if this is an ! alien-invoke, otherwise leave it the return register if ! this is the end of alien-callback @@ -92,9 +97,10 @@ M: x86.64 %unbox-long-long ( n func -- ) { float-regs [ float-regs get pop swap MOVSD ] } } case ; + M: x86.64 %unbox-small-struct ( c-type -- ) ! Alien must be in param-reg-1. - "alien_offset" f %alien-invoke + "alien_offset" %vm-invoke-2nd-arg ! Move alien_offset() return value to R11 so that we don't ! clobber it. R11 RAX MOV @@ -125,7 +131,7 @@ M:: x86.64 %box ( n rep func -- ) ] [ rep load-return-value ] if - func f %alien-invoke ; + func %vm-invoke-2nd-arg ; M: x86.64 %box-long-long ( n func -- ) [ int-rep ] dip %box ; @@ -176,6 +182,7 @@ M: x86.64 %vm-invoke-1st-arg ( function -- ) param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup f %alien-invoke ; + M: x86.64 %vm-invoke-3rd-arg ( function -- ) param-reg-3 0 MOV rc-absolute-cell rt-vm rel-fixup f %alien-invoke ; diff --git a/vm/alien.cpp b/vm/alien.cpp index 9eb6da9784..829a7efeb6 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -226,7 +226,7 @@ char *factorvm::unbox_alien() VM_C_API char *unbox_alien(factorvm *myvm) { - //printf("PHIL unbox_alien %d %d\n",vm,myvm);fflush(stdout); + ASSERTVM(); return VM_PTR->unbox_alien(); } @@ -253,7 +253,7 @@ void factorvm::to_value_struct(cell src, void *dest, cell size) VM_C_API void to_value_struct(cell src, void *dest, cell size, factorvm *myvm) { - //printf("PHIL to_value_struct %d %d\n",vm,myvm);fflush(stdout); + ASSERTVM(); return VM_PTR->to_value_struct(src,dest,size); } @@ -267,7 +267,7 @@ void factorvm::box_value_struct(void *src, cell size) VM_C_API void box_value_struct(void *src, cell size,factorvm *myvm) { - //printf("PHIL box_value_struct %d %d\n",vm,myvm);fflush(stdout); + ASSERTVM(); return VM_PTR->box_value_struct(src,size); } @@ -282,7 +282,7 @@ void factorvm::box_small_struct(cell x, cell y, cell size) VM_C_API void box_small_struct(cell x, cell y, cell size, factorvm *myvm) { - //printf("PHIL box_small_struct %d %d\n",vm,myvm);fflush(stdout); + ASSERTVM(); return VM_PTR->box_small_struct(x,y,size); } @@ -299,7 +299,7 @@ void factorvm::box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size) VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size, factorvm *myvm) { - //printf("PHIL box_medium_struct %d %d\n",vm,myvm);fflush(stdout); + ASSERTVM(); return VM_PTR->box_medium_struct(x1, x2, x3, x4, size); } diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index 5a7f34ca4b..c6cdd39853 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -707,6 +707,7 @@ void factorvm::inline_gc(cell *gc_roots_base, cell gc_roots_size) VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size, factorvm *myvm) { + ASSERTVM(); return VM_PTR->inline_gc(gc_roots_base,gc_roots_size); } diff --git a/vm/math.cpp b/vm/math.cpp index 8d213b391d..d6d824f7c0 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -620,6 +620,7 @@ fixnum factorvm::to_fixnum(cell tagged) VM_C_API fixnum to_fixnum(cell tagged,factorvm *myvm) { + ASSERTVM(); return VM_PTR->to_fixnum(tagged); } @@ -630,6 +631,7 @@ cell factorvm::to_cell(cell tagged) VM_C_API cell to_cell(cell tagged, factorvm *myvm) { + ASSERTVM(); return VM_PTR->to_cell(tagged); } @@ -807,6 +809,7 @@ float factorvm::to_float(cell value) VM_C_API float to_float(cell value,factorvm *myvm) { + ASSERTVM(); return VM_PTR->to_float(value); } diff --git a/vm/quotations.cpp b/vm/quotations.cpp index b28fb1d3a1..9c771129fc 100755 --- a/vm/quotations.cpp +++ b/vm/quotations.cpp @@ -370,6 +370,7 @@ cell factorvm::lazy_jit_compile_impl(cell quot_, stack_frame *stack) VM_ASM_API cell lazy_jit_compile_impl(cell quot_, stack_frame *stack, factorvm *myvm) { + ASSERTVM(); return VM_PTR->lazy_jit_compile_impl(quot_,stack); } From 784b8d16ae5c4c6391f57a500c1a9b7ba0f62fa0 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 08:20:58 +0100 Subject: [PATCH 228/345] moved utility functions and fatal_error out of vm struct since doesn't need state --- vm/errors.cpp | 2 +- vm/errors.hpp | 1 + vm/mach_signal.cpp | 6 +++--- vm/os-genunix.cpp | 2 +- vm/os-linux.cpp | 6 +++--- vm/os-unix.cpp | 32 ++++++++++++++++---------------- vm/utilities.cpp | 18 +++++++++--------- vm/utilities.hpp | 10 +++++++++- vm/vm.hpp | 15 --------------- 9 files changed, 43 insertions(+), 49 deletions(-) diff --git a/vm/errors.cpp b/vm/errors.cpp index 09e6313f29..f483fb4a09 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -17,7 +17,7 @@ void factorvm::out_of_memory() exit(1); } -void factorvm::fatal_error(const char* msg, cell tagged) +void fatal_error(const char* msg, cell tagged) { print_string("fatal_error: "); print_string(msg); print_string(": "); print_cell_hex(tagged); nl(); diff --git a/vm/errors.hpp b/vm/errors.hpp index 8725941920..4f45c55c73 100755 --- a/vm/errors.hpp +++ b/vm/errors.hpp @@ -27,6 +27,7 @@ PRIMITIVE(die); PRIMITIVE(call_clear); PRIMITIVE(unimplemented); +void fatal_error(const char* msg, cell tagged); void memory_signal_handler_impl(); void fp_signal_handler_impl(); void misc_signal_handler_impl(); diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index c1d263527d..ecded49c0f 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -203,13 +203,13 @@ void mach_initialize () /* Allocate a port on which the thread shall listen for exceptions. */ if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port) != KERN_SUCCESS) - vm->fatal_error("mach_port_allocate() failed",0); + fatal_error("mach_port_allocate() failed",0); /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html. */ if (mach_port_insert_right (self, our_exception_port, our_exception_port, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) - vm->fatal_error("mach_port_insert_right() failed",0); + fatal_error("mach_port_insert_right() failed",0); /* The exceptions we want to catch. */ mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC; @@ -226,7 +226,7 @@ void mach_initialize () if (task_set_exception_ports (self, mask, our_exception_port, EXCEPTION_DEFAULT, MACHINE_THREAD_STATE) != KERN_SUCCESS) - vm->fatal_error("task_set_exception_ports() failed",0); + fatal_error("task_set_exception_ports() failed",0); } } diff --git a/vm/os-genunix.cpp b/vm/os-genunix.cpp index 9e7804caf0..29c3e79859 100644 --- a/vm/os-genunix.cpp +++ b/vm/os-genunix.cpp @@ -31,7 +31,7 @@ const char *default_image_path() const char *iter = path; while(*iter) { len++; iter++; } - char *new_path = (char *)vm->safe_malloc(PATH_MAX + SUFFIX_LEN + 1); + char *new_path = (char *)safe_malloc(PATH_MAX + SUFFIX_LEN + 1); memcpy(new_path,path,len + 1); memcpy(new_path + len,SUFFIX,SUFFIX_LEN + 1); return new_path; diff --git a/vm/os-linux.cpp b/vm/os-linux.cpp index c21f8142a1..7929701d41 100644 --- a/vm/os-linux.cpp +++ b/vm/os-linux.cpp @@ -6,18 +6,18 @@ namespace factor /* Snarfed from SBCL linux-so.c. You must free() this yourself. */ const char *vm_executable_path() { - char *path = (char *)vm->safe_malloc(PATH_MAX + 1); + char *path = (char *)safe_malloc(PATH_MAX + 1); int size = readlink("/proc/self/exe", path, PATH_MAX); if (size < 0) { - vm->fatal_error("Cannot read /proc/self/exe",0); + fatal_error("Cannot read /proc/self/exe",0); return NULL; } else { path[size] = '\0'; - return vm->safe_strdup(path); + return safe_strdup(path); } } diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 4de5ede704..832b93b392 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -8,11 +8,11 @@ void *start_thread(void *(*start_routine)(void *),void *args) pthread_attr_t attr; pthread_t thread; if (pthread_attr_init (&attr) != 0) - vm->fatal_error("pthread_attr_init() failed",0); + fatal_error("pthread_attr_init() failed",0); if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0) - vm->fatal_error("pthread_attr_setdetachstate() failed",0); + fatal_error("pthread_attr_setdetachstate() failed",0); if (pthread_create (&thread, &attr, start_routine, args) != 0) - vm->fatal_error("pthread_create() failed",0); + fatal_error("pthread_create() failed",0); pthread_attr_destroy (&attr); return (void*)thread; } @@ -85,12 +85,12 @@ segment *alloc_segment(cell size) vm->out_of_memory(); if(mprotect(array,pagesize,PROT_NONE) == -1) - vm->fatal_error("Cannot protect low guard page",(cell)array); + fatal_error("Cannot protect low guard page",(cell)array); if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1) - vm->fatal_error("Cannot protect high guard page",(cell)array); + fatal_error("Cannot protect high guard page",(cell)array); - segment *retval = (segment *)vm->safe_malloc(sizeof(segment)); + segment *retval = (segment *)safe_malloc(sizeof(segment)); retval->start = (cell)(array + pagesize); retval->size = size; @@ -107,7 +107,7 @@ void dealloc_segment(segment *block) pagesize + block->size + pagesize); if(retval) - vm->fatal_error("dealloc_segment failed",0); + fatal_error("dealloc_segment failed",0); free(block); } @@ -165,7 +165,7 @@ static void sigaction_safe(int signum, const struct sigaction *act, struct sigac while(ret == -1 && errno == EINTR); if(ret == -1) - vm->fatal_error("sigaction failed", 0); + fatal_error("sigaction failed", 0); } void unix_init_signals() @@ -227,7 +227,7 @@ extern "C" { void safe_close(int fd) { if(close(fd) < 0) - vm->fatal_error("error closing fd",errno); + fatal_error("error closing fd",errno); } bool check_write(int fd, void *data, ssize_t size) @@ -246,7 +246,7 @@ bool check_write(int fd, void *data, ssize_t size) void safe_write(int fd, void *data, ssize_t size) { if(!check_write(fd,data,size)) - vm->fatal_error("error writing fd",errno); + fatal_error("error writing fd",errno); } bool safe_read(int fd, void *data, ssize_t size) @@ -258,7 +258,7 @@ bool safe_read(int fd, void *data, ssize_t size) return safe_read(fd,data,size); else { - vm->fatal_error("error reading fd",errno); + fatal_error("error reading fd",errno); return false; } } @@ -277,7 +277,7 @@ void *stdin_loop(void *arg) break; if(buf[0] != 'X') - vm->fatal_error("stdin_loop: bad data on control fd",buf[0]); + fatal_error("stdin_loop: bad data on control fd",buf[0]); for(;;) { @@ -314,19 +314,19 @@ void open_console() int filedes[2]; if(pipe(filedes) < 0) - vm->fatal_error("Error opening control pipe",errno); + fatal_error("Error opening control pipe",errno); control_read = filedes[0]; control_write = filedes[1]; if(pipe(filedes) < 0) - vm->fatal_error("Error opening size pipe",errno); + fatal_error("Error opening size pipe",errno); size_read = filedes[0]; size_write = filedes[1]; if(pipe(filedes) < 0) - vm->fatal_error("Error opening stdin pipe",errno); + fatal_error("Error opening stdin pipe",errno); stdin_read = filedes[0]; stdin_write = filedes[1]; @@ -341,7 +341,7 @@ VM_C_API void wait_for_stdin() if(errno == EINTR) wait_for_stdin(); else - vm->fatal_error("Error writing control fd",errno); + fatal_error("Error writing control fd",errno); } } diff --git a/vm/utilities.cpp b/vm/utilities.cpp index 4f4da9a1bc..94f010d050 100755 --- a/vm/utilities.cpp +++ b/vm/utilities.cpp @@ -4,14 +4,14 @@ namespace factor { /* If memory allocation fails, bail out */ -void *factorvm::safe_malloc(size_t size) +void *safe_malloc(size_t size) { void *ptr = malloc(size); if(!ptr) fatal_error("Out of memory in safe_malloc", 0); return ptr; } -vm_char *factorvm::safe_strdup(const vm_char *str) +vm_char *safe_strdup(const vm_char *str) { vm_char *ptr = STRDUP(str); if(!ptr) fatal_error("Out of memory in safe_strdup", 0); @@ -21,38 +21,38 @@ vm_char *factorvm::safe_strdup(const vm_char *str) /* We don't use printf directly, because format directives are not portable. Instead we define the common cases here. */ -void factorvm::nl() +void nl() { fputs("\n",stdout); } -void factorvm::print_string(const char *str) +void print_string(const char *str) { fputs(str,stdout); } -void factorvm::print_cell(cell x) +void print_cell(cell x) { printf(CELL_FORMAT,x); } -void factorvm::print_cell_hex(cell x) +void print_cell_hex(cell x) { printf(CELL_HEX_FORMAT,x); } -void factorvm::print_cell_hex_pad(cell x) +void print_cell_hex_pad(cell x) { printf(CELL_HEX_PAD_FORMAT,x); } -void factorvm::print_fixnum(fixnum x) +void print_fixnum(fixnum x) { printf(FIXNUM_FORMAT,x); } -cell factorvm::read_cell_hex() +cell read_cell_hex() { cell cell; if(scanf(CELL_HEX_FORMAT,&cell) < 0) exit(1); diff --git a/vm/utilities.hpp b/vm/utilities.hpp index 412ef35bb4..68e0c97b25 100755 --- a/vm/utilities.hpp +++ b/vm/utilities.hpp @@ -1,4 +1,12 @@ namespace factor { - + void *safe_malloc(size_t size); + vm_char *safe_strdup(const vm_char *str); + void print_string(const char *str); + void nl(); + void print_cell(cell x); + void print_cell_hex(cell x); + void print_cell_hex_pad(cell x); + void print_fixnum(fixnum x); + cell read_cell_hex(); } diff --git a/vm/vm.hpp b/vm/vm.hpp index 384c98186a..0a0393700c 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -66,7 +66,6 @@ struct factorvm { unsigned int signal_fpu_status; stack_frame *signal_callstack_top; void out_of_memory(); - void fatal_error(const char* msg, cell tagged); void critical_error(const char* msg, cell tagged); void throw_error(cell error, stack_frame *callstack_top); void not_implemented_error(); @@ -658,20 +657,6 @@ struct factorvm { void factor_yield(); void factor_sleep(long us); - //utilities - void *safe_malloc(size_t size); - vm_char *safe_strdup(const vm_char *str); - void nl(); - void print_string(const char *str); - void print_cell(cell x); - void print_cell_hex(cell x); - void print_cell_hex_pad(cell x); - void print_fixnum(fixnum x); - cell read_cell_hex(); - - - - // os-* inline void vmprim_existsp(); long thread_id(); From 1456fb3c97f419c58a97e176b6ce3096699d34aa Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 08:35:22 +0100 Subject: [PATCH 229/345] removed vm ptrs from unix code (still in signal handlers tho) --- vm/os-unix.cpp | 22 +++++++++++----------- vm/os-windows-nt.cpp | 2 +- vm/vm.hpp | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 832b93b392..ed007398a3 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -31,40 +31,40 @@ void sleep_micros(cell usec) usleep(usec); } -void init_ffi() +void factorvm::init_ffi() { /* NULL_DLL is "libfactor.dylib" for OS X and NULL for generic unix */ null_dll = dlopen(NULL_DLL,RTLD_LAZY); } -void ffi_dlopen(dll *dll) +void factorvm::ffi_dlopen(dll *dll) { - dll->dll = dlopen(alien_offset(dll->path,vm), RTLD_LAZY); + dll->dll = dlopen(alien_offset(dll->path), RTLD_LAZY); } -void *ffi_dlsym(dll *dll, symbol_char *symbol) +void *factorvm::ffi_dlsym(dll *dll, symbol_char *symbol) { void *handle = (dll == NULL ? null_dll : dll->dll); return dlsym(handle,symbol); } -void ffi_dlclose(dll *dll) +void factorvm::ffi_dlclose(dll *dll) { if(dlclose(dll->dll)) - vm->general_error(ERROR_FFI,F,F,NULL); + general_error(ERROR_FFI,F,F,NULL); dll->dll = NULL; } -long factorvm::thread_id(){ - return 0; // TODO fix me +cell factorvm::thread_id(){ + return pthread_self(); } inline void factorvm::vmprim_existsp() { struct stat sb; - char *path = (char *)(vm->untag_check(dpop()) + 1); + char *path = (char *)(untag_check(dpop()) + 1); box_boolean(stat(path,&sb) >= 0); } @@ -73,7 +73,7 @@ PRIMITIVE(existsp) PRIMITIVE_GETVM()->vmprim_existsp(); } -segment *alloc_segment(cell size) +segment *factorvm::alloc_segment(cell size) { int pagesize = getpagesize(); @@ -82,7 +82,7 @@ segment *alloc_segment(cell size) MAP_ANON | MAP_PRIVATE,-1,0); if(array == (char*)-1) - vm->out_of_memory(); + out_of_memory(); if(mprotect(array,pagesize,PROT_NONE) == -1) fatal_error("Cannot protect low guard page",(cell)array); diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 6085d65670..630782a946 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -7,7 +7,7 @@ void *start_thread(void *(*start_routine)(void *),void *args){ return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); } -long factorvm::thread_id(){ +cell factorvm::thread_id(){ return GetCurrentThreadId(); } diff --git a/vm/vm.hpp b/vm/vm.hpp index 0a0393700c..9af73e4c7e 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -659,18 +659,18 @@ struct factorvm { // os-* inline void vmprim_existsp(); - long thread_id(); - - // os-windows - #if defined(WINDOWS) + cell thread_id(); void init_ffi(); void ffi_dlopen(dll *dll); void *ffi_dlsym(dll *dll, symbol_char *symbol); void ffi_dlclose(dll *dll); + segment *alloc_segment(cell size); + + // os-windows + #if defined(WINDOWS) void sleep_micros(u64 usec); long getpagesize(); void dealloc_segment(segment *block); - segment *alloc_segment(cell size); const vm_char *vm_executable_path(); const vm_char *default_image_path(); void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length); From 3c139593c5b248fcf159a20500a9223fc55aa644 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 08:55:18 +0100 Subject: [PATCH 230/345] moved the thread stuff around a bit --- vm/factor.cpp | 6 +++--- vm/factor.hpp | 2 +- vm/os-unix.cpp | 12 +++++++----- vm/os-unix.hpp | 8 +++----- vm/os-windows-nt.cpp | 8 ++++---- vm/os-windows-nt.hpp | 5 ++++- vm/vm.hpp | 6 ++---- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index a241ccf86b..57bceb9cb7 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -7,12 +7,12 @@ factorvm *vm; unordered_map thread_vms; -factorvm *lookup_vm(long threadid) +factorvm *lookup_vm(unsigned long threadid) { return thread_vms[threadid]; } -void register_vm(long threadid, factorvm *vm) +void register_vm(unsigned long threadid, factorvm *vm) { thread_vms[threadid] = vm; } @@ -243,7 +243,7 @@ VM_C_API void start_standalone_factor(int argc, vm_char **argv) return newvm->start_standalone_factor(argc,argv); } -VM_C_API void *start_standalone_factor_in_new_thread(int argc, vm_char **argv) +VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv) { startargs *args = new startargs; // leaks startargs structure args->argc = argc; args->argv = argv; diff --git a/vm/factor.hpp b/vm/factor.hpp index b824758c8a..46662072b5 100644 --- a/vm/factor.hpp +++ b/vm/factor.hpp @@ -2,5 +2,5 @@ namespace factor { VM_C_API void start_standalone_factor(int argc, vm_char **argv); -VM_C_API void *start_standalone_factor_in_new_thread(int argc, vm_char **argv); +VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv); } diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index ed007398a3..7f4b070c60 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -3,7 +3,7 @@ namespace factor { -void *start_thread(void *(*start_routine)(void *),void *args) +THREADHANDLE start_thread(void *(*start_routine)(void *),void *args) { pthread_attr_t attr; pthread_t thread; @@ -14,9 +14,14 @@ void *start_thread(void *(*start_routine)(void *),void *args) if (pthread_create (&thread, &attr, start_routine, args) != 0) fatal_error("pthread_create() failed",0); pthread_attr_destroy (&attr); - return (void*)thread; + return thread; } +unsigned long thread_id(){ + return pthread_self(); +} + + static void *null_dll; s64 current_micros() @@ -56,9 +61,6 @@ void factorvm::ffi_dlclose(dll *dll) } -cell factorvm::thread_id(){ - return pthread_self(); -} inline void factorvm::vmprim_existsp() diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index 60beb88233..b37d2beeea 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -42,12 +42,10 @@ typedef char symbol_char; #define print_native_string(string) print_string(string) -void *start_thread(void *(*start_routine)(void *),void *args); +typedef pthread_t THREADHANDLE; -void init_ffi(); -void ffi_dlopen(dll *dll); -void *ffi_dlsym(dll *dll, symbol_char *symbol); -void ffi_dlclose(dll *dll); +THREADHANDLE start_thread(void *(*start_routine)(void *),void *args); +unsigned long thread_id(); void unix_init_signals(); void signal_handler(int signal, siginfo_t* siginfo, void* uap); diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index 630782a946..fbaadaaba7 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -3,16 +3,16 @@ namespace factor { -void *start_thread(void *(*start_routine)(void *),void *args){ - return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); +THREADHANDLE start_thread(void *(*start_routine)(void *),void *args){ + return (void*) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); } -cell factorvm::thread_id(){ +unsigned long thread_id(){ return GetCurrentThreadId(); } -s64 factorvm::current_micros() +s64 current_micros() { FILETIME t; GetSystemTimeAsFileTime(&t); diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index e5c5adadf1..68de8ff49e 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -26,7 +26,10 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe); #define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4 #define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5 -void *start_thread(void *(*start_routine)(void *),void *args); +typedef HANDLE THREADHANDLE; + +THREADHANDLE start_thread(void *(*start_routine)(void *),void *args); +unsigned long thread_id(); #define SIGNAL_VM_PTR lookup_vm(GetCurrentThreadId()) diff --git a/vm/vm.hpp b/vm/vm.hpp index 9af73e4c7e..76cf8c4e53 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -659,7 +659,6 @@ struct factorvm { // os-* inline void vmprim_existsp(); - cell thread_id(); void init_ffi(); void ffi_dlopen(dll *dll); void *ffi_dlsym(dll *dll, symbol_char *symbol); @@ -677,7 +676,6 @@ struct factorvm { bool windows_stat(vm_char *path); #if defined(WINNT) - s64 current_micros(); void c_to_factor_toplevel(cell quot); void open_console(); // next method here: @@ -689,6 +687,6 @@ struct factorvm { extern factorvm *vm; -extern factorvm *lookup_vm(long threadid); -extern void register_vm(long threadid,factorvm *vm); +extern factorvm *lookup_vm(unsigned long threadid); +extern void register_vm(unsigned long threadid,factorvm *vm); } From fa6d8d239bbf79ffc636de494edce2d5b2a3a859 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 18:08:45 +0100 Subject: [PATCH 231/345] removed vm singleton usage from unix stuff --- vm/os-genunix.cpp | 4 ++-- vm/os-unix.cpp | 18 ++++++++++-------- vm/vm.hpp | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/vm/os-genunix.cpp b/vm/os-genunix.cpp index 29c3e79859..6540d8d196 100644 --- a/vm/os-genunix.cpp +++ b/vm/os-genunix.cpp @@ -3,9 +3,9 @@ namespace factor { -void c_to_factor_toplevel(cell quot) +void factorvm::c_to_factor_toplevel(cell quot) { - c_to_factor(quot,vm); + c_to_factor(quot,this); } void init_signals() diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 7f4b070c60..268469a875 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -9,7 +9,7 @@ THREADHANDLE start_thread(void *(*start_routine)(void *),void *args) pthread_t thread; if (pthread_attr_init (&attr) != 0) fatal_error("pthread_attr_init() failed",0); - if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0) + if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE) != 0) fatal_error("pthread_attr_setdetachstate() failed",0); if (pthread_create (&thread, &attr, start_routine, args) != 0) fatal_error("pthread_create() failed",0); @@ -114,17 +114,17 @@ void dealloc_segment(segment *block) free(block); } -static stack_frame *uap_stack_pointer(void *uap) +stack_frame *factorvm::uap_stack_pointer(void *uap) { /* There is a race condition here, but in practice a signal delivered during stack frame setup/teardown or while transitioning from Factor to C is a sign of things seriously gone wrong, not just a divide by zero or stack underflow in the listener */ - if(vm->in_code_heap_p(UAP_PROGRAM_COUNTER(uap))) + if(in_code_heap_p(UAP_PROGRAM_COUNTER(uap))) { stack_frame *ptr = (stack_frame *)ucontext_stack_pointer(uap); if(!ptr) - vm->critical_error("Invalid uap",(cell)uap); + critical_error("Invalid uap",(cell)uap); return ptr; } else @@ -133,15 +133,17 @@ static stack_frame *uap_stack_pointer(void *uap) void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - vm->signal_fault_addr = (cell)siginfo->si_addr; - vm->signal_callstack_top = uap_stack_pointer(uap); + factorvm *myvm = lookup_vm(thread_id()); + myvm->signal_fault_addr = (cell)siginfo->si_addr; + myvm->signal_callstack_top = myvm->uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)memory_signal_handler_impl; } void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - vm->signal_number = signal; - vm->signal_callstack_top = uap_stack_pointer(uap); + factorvm *myvm = lookup_vm(thread_id()); + myvm->signal_number = signal; + myvm->signal_callstack_top = myvm->uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)misc_signal_handler_impl; } diff --git a/vm/vm.hpp b/vm/vm.hpp index 76cf8c4e53..65e41881cb 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -664,6 +664,7 @@ struct factorvm { void *ffi_dlsym(dll *dll, symbol_char *symbol); void ffi_dlclose(dll *dll); segment *alloc_segment(cell size); + void c_to_factor_toplevel(cell quot); // os-windows #if defined(WINDOWS) From ca16daa4b223342c010bfdde6a1f97fd6261b692 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 18:17:14 +0100 Subject: [PATCH 232/345] cleaned up code a bit, added multithreaded mode flags --- vm/factor.cpp | 4 ++-- vm/main-unix.cpp | 7 ++++++- vm/main-windows-nt.cpp | 7 +++++-- vm/os-unix.hpp | 2 -- vm/os-windows-nt.hpp | 2 -- vm/primitives.hpp | 5 ----- vm/tagged.hpp | 4 ++-- vm/vm.hpp | 34 ++++++++++++++++++++++++++++++++-- 8 files changed, 47 insertions(+), 18 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 57bceb9cb7..026453eae3 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -12,9 +12,9 @@ factorvm *lookup_vm(unsigned long threadid) return thread_vms[threadid]; } -void register_vm(unsigned long threadid, factorvm *vm) +void register_vm(unsigned long threadid, factorvm *thevm) { - thread_vms[threadid] = vm; + thread_vms[threadid] = thevm; } diff --git a/vm/main-unix.cpp b/vm/main-unix.cpp index bc605e3cfd..bd1549a38e 100644 --- a/vm/main-unix.cpp +++ b/vm/main-unix.cpp @@ -2,6 +2,11 @@ int main(int argc, char **argv) { - factor::start_standalone_factor(argc,argv); + #ifdef FACTOR_MULTITHREADED + factor::THREADHANDLE thread = factor::start_standalone_factor_in_new_thread(argc,argv); + pthread_join(thread,NULL); + #else + factor::start_standalone_factor(argc,argv); + #endif return 0; } diff --git a/vm/main-windows-nt.cpp b/vm/main-windows-nt.cpp index 4a0fc2ae35..2120717d86 100644 --- a/vm/main-windows-nt.cpp +++ b/vm/main-windows-nt.cpp @@ -16,9 +16,12 @@ int WINAPI WinMain( return 1; } + #ifdef FACTOR_MULTITHREADED + factor::THREADHANDLE thread = factor::start_standalone_factor_in_new_thread(nArgs,szArglist); + WaitForSingleObject(thread, INFINITE); + #else factor::start_standalone_factor(nArgs,szArglist); - //HANDLE thread = factor::start_standalone_factor_in_new_thread(nArgs,szArglist); - //WaitForSingleObject(thread, INFINITE); + #endif LocalFree(szArglist); diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index b37d2beeea..e3e207f641 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -55,6 +55,4 @@ s64 current_micros(); void sleep_micros(cell usec); void open_console(); - -#define SIGNAL_VM_PTR vm } diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index 68de8ff49e..385553e11e 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -31,6 +31,4 @@ typedef HANDLE THREADHANDLE; THREADHANDLE start_thread(void *(*start_routine)(void *),void *args); unsigned long thread_id(); -#define SIGNAL_VM_PTR lookup_vm(GetCurrentThreadId()) - } diff --git a/vm/primitives.hpp b/vm/primitives.hpp index 8e6c3b8f51..4be190d4e6 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -4,15 +4,10 @@ namespace factor #if defined(FACTOR_X86) extern "C" __attribute__ ((regparm (1))) typedef void (*primitive_type)(void *myvm); #define PRIMITIVE(name) extern "C" __attribute__ ((regparm (1))) void primitive_##name(void *myvm) - #define PRIMITIVE_GETVM() ((factorvm*)myvm) #else extern "C" typedef void (*primitive_type)(void *myvm); #define PRIMITIVE(name) extern "C" void primitive_##name(void *myvm) - #define PRIMITIVE_GETVM() vm #endif extern const primitive_type primitives[]; -#define PRIMITIVE_OVERFLOW_GETVM() vm -#define VM_PTR vm -#define ASSERTVM() } diff --git a/vm/tagged.hpp b/vm/tagged.hpp index 2bf058212f..13ddf4a047 100755 --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -37,13 +37,13 @@ struct tagged explicit tagged(cell tagged) : value_(tagged) { #ifdef FACTOR_DEBUG - untag_check(vm); + untag_check(SIGNAL_VM_PTR); #endif } explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { #ifdef FACTOR_DEBUG - untag_check(vm); + untag_check(SIGNAL_VM_PTR); #endif } diff --git a/vm/vm.hpp b/vm/vm.hpp index 65e41881cb..35011171ee 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -677,17 +677,47 @@ struct factorvm { bool windows_stat(vm_char *path); #if defined(WINNT) - void c_to_factor_toplevel(cell quot); void open_console(); // next method here: #endif + #else // UNIX + + stack_frame *uap_stack_pointer(void *uap); #endif }; -extern factorvm *vm; extern factorvm *lookup_vm(unsigned long threadid); extern void register_vm(unsigned long threadid,factorvm *vm); + +#define FACTOR_SINGLE_THREADED + +#ifdef FACTOR_SINGLE_THREADED + extern factorvm *vm; + #define PRIMITIVE_GETVM() vm + #define PRIMITIVE_OVERFLOW_GETVM() vm + #define VM_PTR vm + #define ASSERTVM() + #define SIGNAL_VM_PTR vm +#endif + +#ifdef FACTOR_TESTING_MULTITHREADED + extern factorvm *vm; + #define PRIMITIVE_GETVM() ((factorvm*)myvm) + #define PRIMITIVE_OVERFLOW_GETVM() vm + #define VM_PTR myvm + #define ASSERTVM() assert(vm==myvm) + #define SIGNAL_VM_PTR lookup_vm(thread_id()) +#endif + +#ifdef FACTOR_MULTITHREADED + #define PRIMITIVE_GETVM() ((factorvm*)myvm) + #define PRIMITIVE_OVERFLOW_GETVM() ((factorvm*)myvm) + #define VM_PTR myvm + #define ASSERTVM() + #define SIGNAL_VM_PTR lookup_vm(thread_id()) +#endif + } From 3345922330a8925c0ffe21815eb09a2e5d732418 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 25 Aug 2009 18:29:28 +0100 Subject: [PATCH 233/345] quick test vocab for mt stuff --- basis/cpu/x86/64/64.factor | 4 +++- extra/mttest/mttest.factor | 25 +++++++++++++++++++++++++ vm/errors.cpp | 4 ++-- vm/math.cpp | 2 +- vm/os-unix.cpp | 4 ++-- vm/tagged.hpp | 4 ++-- vm/vm.hpp | 6 +++--- 7 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 extra/mttest/mttest.factor diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index a0ca3d17c7..925de44c23 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -123,6 +123,8 @@ M:: x86.64 %unbox-large-struct ( n c-type -- ) [ ] tri copy-register ; + + M:: x86.64 %box ( n rep func -- ) n [ n @@ -131,7 +133,7 @@ M:: x86.64 %box ( n rep func -- ) ] [ rep load-return-value ] if - func %vm-invoke-2nd-arg ; + rep int-rep? [ func %vm-invoke-2nd-arg ] [ func %vm-invoke-1st-arg ] if ; M: x86.64 %box-long-long ( n func -- ) [ int-rep ] dip %box ; diff --git a/extra/mttest/mttest.factor b/extra/mttest/mttest.factor new file mode 100644 index 0000000000..90a398c59a --- /dev/null +++ b/extra/mttest/mttest.factor @@ -0,0 +1,25 @@ +USING: alien.syntax io io.encodings.utf16n io.encodings.utf8 io.files +kernel namespaces sequences system threads unix.utilities ; +IN: mttest + +FUNCTION: void* start_standalone_factor_in_new_thread ( int argc, char** argv ) ; + +HOOK: native-string-encoding os ( -- encoding ) +M: windows native-string-encoding utf16n ; +M: unix native-string-encoding utf8 ; + +: start-vm-in-os-thread ( args -- threadhandle ) + \ vm get-global prefix + [ length ] [ native-string-encoding strings>alien ] bi + start_standalone_factor_in_new_thread ; + +: start-tetris-in-os-thread ( -- ) + { "-run=tetris" } start-vm-in-os-thread drop ; + +: start-testthread-in-os-thread ( -- ) + { "-run=mttest" } start-vm-in-os-thread drop ; + +: testthread ( -- ) + "/tmp/hello" utf8 [ "hello!\n" write ] with-file-appender 5000000 sleep ; + +MAIN: testthread \ No newline at end of file diff --git a/vm/errors.cpp b/vm/errors.cpp index f483fb4a09..154237a8f2 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -165,7 +165,7 @@ void factorvm::memory_signal_handler_impl() void memory_signal_handler_impl() { - SIGNAL_VM_PTR->misc_signal_handler_impl(); + SIGNAL_VM_PTR()->misc_signal_handler_impl(); } void factorvm::misc_signal_handler_impl() @@ -175,7 +175,7 @@ void factorvm::misc_signal_handler_impl() void misc_signal_handler_impl() { - SIGNAL_VM_PTR->misc_signal_handler_impl(); + SIGNAL_VM_PTR()->misc_signal_handler_impl(); } void factorvm::fp_signal_handler_impl() diff --git a/vm/math.cpp b/vm/math.cpp index d6d824f7c0..40a0c20a3b 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -818,7 +818,7 @@ void factorvm::box_double(double flo) dpush(allot_float(flo)); } -VM_C_API void box_double(double flo,factorvm *myvm) // not sure if this is ever called +VM_C_API void box_double(double flo,factorvm *myvm) { ASSERTVM(); return VM_PTR->box_double(flo); diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 268469a875..4cee04a11b 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -133,7 +133,7 @@ stack_frame *factorvm::uap_stack_pointer(void *uap) void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - factorvm *myvm = lookup_vm(thread_id()); + factorvm *myvm = SIGNAL_VM_PTR(); myvm->signal_fault_addr = (cell)siginfo->si_addr; myvm->signal_callstack_top = myvm->uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)memory_signal_handler_impl; @@ -141,7 +141,7 @@ void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - factorvm *myvm = lookup_vm(thread_id()); + factorvm *myvm = SIGNAL_VM_PTR(); myvm->signal_number = signal; myvm->signal_callstack_top = myvm->uap_stack_pointer(uap); UAP_PROGRAM_COUNTER(uap) = (cell)misc_signal_handler_impl; diff --git a/vm/tagged.hpp b/vm/tagged.hpp index 13ddf4a047..8eb492a140 100755 --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -37,13 +37,13 @@ struct tagged explicit tagged(cell tagged) : value_(tagged) { #ifdef FACTOR_DEBUG - untag_check(SIGNAL_VM_PTR); + untag_check(SIGNAL_VM_PTR()); #endif } explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) { #ifdef FACTOR_DEBUG - untag_check(SIGNAL_VM_PTR); + untag_check(SIGNAL_VM_PTR()); #endif } diff --git a/vm/vm.hpp b/vm/vm.hpp index 35011171ee..c30af505f9 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -700,7 +700,7 @@ extern void register_vm(unsigned long threadid,factorvm *vm); #define PRIMITIVE_OVERFLOW_GETVM() vm #define VM_PTR vm #define ASSERTVM() - #define SIGNAL_VM_PTR vm + #define SIGNAL_VM_PTR() vm #endif #ifdef FACTOR_TESTING_MULTITHREADED @@ -709,7 +709,7 @@ extern void register_vm(unsigned long threadid,factorvm *vm); #define PRIMITIVE_OVERFLOW_GETVM() vm #define VM_PTR myvm #define ASSERTVM() assert(vm==myvm) - #define SIGNAL_VM_PTR lookup_vm(thread_id()) + #define SIGNAL_VM_PTR() lookup_vm(thread_id()) #endif #ifdef FACTOR_MULTITHREADED @@ -717,7 +717,7 @@ extern void register_vm(unsigned long threadid,factorvm *vm); #define PRIMITIVE_OVERFLOW_GETVM() ((factorvm*)myvm) #define VM_PTR myvm #define ASSERTVM() - #define SIGNAL_VM_PTR lookup_vm(thread_id()) + #define SIGNAL_VM_PTR() lookup_vm(thread_id()) #endif } From 888eae9554608adb5dcc0bfaf06e5c16de5fc05d Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 27 Aug 2009 19:49:57 +0100 Subject: [PATCH 234/345] fixed vm ptr passing to box_value_struct --- basis/cpu/x86/64/64.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 925de44c23..498a502fca 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -165,7 +165,7 @@ M: x86.64 %box-large-struct ( n c-type -- ) ! Compute destination address param-reg-1 swap struct-return@ LEA ! Copy the struct from the C stack - "box_value_struct" f %alien-invoke ; + "box_value_struct" %vm-invoke-3rd-arg ; M: x86.64 %prepare-box-struct ( -- ) ! Compute target address for value struct return From 1b92721660dce1da3302a684967b0b2b0e0e5ea7 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 27 Aug 2009 19:55:25 +0100 Subject: [PATCH 235/345] fixed vm ptr passing to box_small_struct --- basis/cpu/x86/64/64.factor | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 498a502fca..4bdabe30be 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -74,10 +74,22 @@ M: x86.64 %prepare-unbox ( -- ) param-reg-1 R14 [] MOV R14 cell SUB ; +M: x86.64 %vm-invoke-1st-arg ( function -- ) + param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup + f %alien-invoke ; + : %vm-invoke-2nd-arg ( function -- ) param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup f %alien-invoke ; +M: x86.64 %vm-invoke-3rd-arg ( function -- ) + param-reg-3 0 MOV rc-absolute-cell rt-vm rel-fixup + f %alien-invoke ; + +: %vm-invoke-4th-arg ( function -- ) + int-regs param-regs fourth 0 MOV rc-absolute-cell rt-vm rel-fixup + f %alien-invoke ; + M:: x86.64 %unbox ( n rep func -- ) ! Call the unboxer @@ -153,7 +165,7 @@ M: x86.64 %box-small-struct ( c-type -- ) [ param-reg-3 swap heap-size MOV ] bi param-reg-1 0 box-struct-field@ MOV param-reg-2 1 box-struct-field@ MOV - "box_small_struct" f %alien-invoke + "box_small_struct" %vm-invoke-4th-arg ] with-return-regs ; : struct-return@ ( n -- operand ) @@ -180,14 +192,6 @@ M: x86.64 %alien-invoke rc-absolute-cell rel-dlsym R11 CALL ; -M: x86.64 %vm-invoke-1st-arg ( function -- ) - param-reg-1 0 MOV rc-absolute-cell rt-vm rel-fixup - f %alien-invoke ; - - -M: x86.64 %vm-invoke-3rd-arg ( function -- ) - param-reg-3 0 MOV rc-absolute-cell rt-vm rel-fixup - f %alien-invoke ; M: x86.64 %prepare-alien-indirect ( -- ) "unbox_alien" f %alien-invoke From 0470b7d2c590119d4b3c6c1a0755127e5f823f31 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 27 Aug 2009 19:58:40 +0100 Subject: [PATCH 236/345] fixed vm ptr passing to to_value_struct --- basis/cpu/x86/64/64.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 4bdabe30be..5d5f30fbc2 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -127,7 +127,7 @@ M:: x86.64 %unbox-large-struct ( n c-type -- ) ! Load structure size into param-reg-3 param-reg-3 c-type heap-size MOV ! Copy the struct to the C stack - "to_value_struct" f %alien-invoke ; + "to_value_struct" %vm-invoke-4th-arg ; : load-return-value ( rep -- ) [ [ 0 ] dip reg-class-of param-reg ] @@ -194,7 +194,7 @@ M: x86.64 %alien-invoke M: x86.64 %prepare-alien-indirect ( -- ) - "unbox_alien" f %alien-invoke + "unbox_alien" %vm-invoke-1st-arg RBP RAX MOV ; M: x86.64 %alien-indirect ( -- ) From 2e50da6beb8db4b86a1cdaea58078eca0fd00cbf Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Wed, 2 Sep 2009 10:43:21 +0100 Subject: [PATCH 237/345] added vm-ptr primitive --- core/bootstrap/primitives.factor | 2 ++ vm/alien.cpp | 10 ++++++++++ vm/alien.hpp | 2 ++ vm/primitives.cpp | 1 + vm/vm.hpp | 1 + 5 files changed, 16 insertions(+) diff --git a/core/bootstrap/primitives.factor b/core/bootstrap/primitives.factor index 355fa8ed58..fc071cc566 100644 --- a/core/bootstrap/primitives.factor +++ b/core/bootstrap/primitives.factor @@ -103,6 +103,7 @@ bootstrapping? on "words" "vectors" "vectors.private" + "vm" } [ create-vocab drop ] each ! Builtin classes @@ -518,6 +519,7 @@ tuple { "inline-cache-stats" "generic.single" (( -- stats )) } { "optimized?" "words" (( word -- ? )) } { "quot-compiled?" "quotations" (( quot -- ? )) } + { "vm-ptr" "vm" (( -- ptr )) } } [ [ first3 ] dip swap make-primitive ] each-index ! Bump build number diff --git a/vm/alien.cpp b/vm/alien.cpp index 829a7efeb6..ea8d0a6026 100755 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -303,4 +303,14 @@ VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size, f return VM_PTR->box_medium_struct(x1, x2, x3, x4, size); } +inline void factorvm::vmprim_vm_ptr() +{ + box_alien(this); +} + +PRIMITIVE(vm_ptr) +{ + PRIMITIVE_GETVM()->vmprim_vm_ptr(); +} + } diff --git a/vm/alien.hpp b/vm/alien.hpp index 7b537146fd..ca3601f51e 100755 --- a/vm/alien.hpp +++ b/vm/alien.hpp @@ -36,6 +36,8 @@ PRIMITIVE(dlsym); PRIMITIVE(dlclose); PRIMITIVE(dll_validp); +PRIMITIVE(vm_ptr); + VM_C_API char *alien_offset(cell object, factorvm *vm); VM_C_API char *unbox_alien(factorvm *vm); VM_C_API void box_alien(void *ptr, factorvm *vm); diff --git a/vm/primitives.cpp b/vm/primitives.cpp index 6dbe281d0c..1cbad03001 100644 --- a/vm/primitives.cpp +++ b/vm/primitives.cpp @@ -162,6 +162,7 @@ const primitive_type primitives[] = { primitive_inline_cache_stats, primitive_optimized_p, primitive_quot_compiled_p, + primitive_vm_ptr, }; } diff --git a/vm/vm.hpp b/vm/vm.hpp index c30af505f9..a16ff21121 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -585,6 +585,7 @@ struct factorvm { inline void vmprim_dlsym(); inline void vmprim_dlclose(); inline void vmprim_dll_validp(); + inline void vmprim_vm_ptr(); char *alien_offset(cell obj); char *unbox_alien(); void box_alien(void *ptr); From 4af25578d8ce5cf06973aab8ba9a1f6ee7d71d23 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Wed, 2 Sep 2009 10:45:03 +0100 Subject: [PATCH 238/345] fixed up some alien boxing (x86 32 & 64) --- basis/cpu/x86/32/32.factor | 29 ++++++++++++++++++----------- basis/cpu/x86/64/64.factor | 4 ++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/basis/cpu/x86/32/32.factor b/basis/cpu/x86/32/32.factor index f711455fa3..85db5fb09c 100755 --- a/basis/cpu/x86/32/32.factor +++ b/basis/cpu/x86/32/32.factor @@ -133,7 +133,8 @@ M:: x86.32 %box ( n rep func -- ) M: x86.32 %box-long-long ( n func -- ) [ (%box-long-long) ] dip - 8 [ + 8 vm-ptr-size + [ + push-vm-ptr EDX PUSH EAX PUSH f %alien-invoke @@ -141,12 +142,13 @@ M: x86.32 %box-long-long ( n func -- ) M:: x86.32 %box-large-struct ( n c-type -- ) ! Compute destination address - ECX n struct-return@ LEA - 8 [ + EDX n struct-return@ LEA + 8 vm-ptr-size + [ + push-vm-ptr ! Push struct size c-type heap-size PUSH ! Push destination address - ECX PUSH + EDX PUSH ! Copy the struct from the C stack "box_value_struct" f %alien-invoke ] with-aligned-stack ; @@ -159,7 +161,8 @@ M: x86.32 %prepare-box-struct ( -- ) M: x86.32 %box-small-struct ( c-type -- ) #! Box a <= 8-byte struct returned in EAX:EDX. OS X only. - 12 [ + 12 vm-ptr-size + [ + push-vm-ptr heap-size PUSH EDX PUSH EAX PUSH @@ -200,7 +203,8 @@ M: x86.32 %unbox-long-long ( n func -- ) : %unbox-struct-1 ( -- ) #! Alien must be in EAX. - 4 [ + 4 vm-ptr-size + [ + push-vm-ptr EAX PUSH "alien_offset" f %alien-invoke ! Load first cell @@ -209,7 +213,8 @@ M: x86.32 %unbox-long-long ( n func -- ) : %unbox-struct-2 ( -- ) #! Alien must be in EAX. - 4 [ + 4 vm-ptr-size + [ + push-vm-ptr EAX PUSH "alien_offset" f %alien-invoke ! Load second cell @@ -228,12 +233,13 @@ M: x86 %unbox-small-struct ( size -- ) M:: x86.32 %unbox-large-struct ( n c-type -- ) ! Alien must be in EAX. ! Compute destination address - ECX n stack@ LEA - 12 [ + EDX n stack@ LEA + 12 vm-ptr-size + [ + push-vm-ptr ! Push struct size c-type heap-size PUSH ! Push destination address - ECX PUSH + EDX PUSH ! Push source address EAX PUSH ! Copy the struct to the stack @@ -241,7 +247,8 @@ M:: x86.32 %unbox-large-struct ( n c-type -- ) ] with-aligned-stack ; M: x86.32 %prepare-alien-indirect ( -- ) - "unbox_alien" f %alien-invoke + push-vm-ptr "unbox_alien" f %alien-invoke + temp-reg POP EBP EAX MOV ; M: x86.32 %alien-indirect ( -- ) diff --git a/basis/cpu/x86/64/64.factor b/basis/cpu/x86/64/64.factor index 5d5f30fbc2..0528733af1 100644 --- a/basis/cpu/x86/64/64.factor +++ b/basis/cpu/x86/64/64.factor @@ -202,7 +202,7 @@ M: x86.64 %alien-indirect ( -- ) M: x86.64 %alien-callback ( quot -- ) param-reg-1 swap %load-reference - "c_to_factor" f %alien-invoke ; + "c_to_factor" %vm-invoke-2nd-arg ; M: x86.64 %callback-value ( ctype -- ) ! Save top of data stack @@ -211,7 +211,7 @@ M: x86.64 %callback-value ( ctype -- ) RSP 8 SUB param-reg-1 PUSH ! Restore data/call/retain stacks - "unnest_stacks" f %alien-invoke + "unnest_stacks" %vm-invoke-1st-arg ! Put former top of data stack in param-reg-1 param-reg-1 POP RSP 8 ADD From 3a3154797ccc5a95abe5fdc1b8ef336ce9648b73 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 28 Aug 2009 20:17:21 +0100 Subject: [PATCH 239/345] fixed stupid signal handler bug --- vm/errors.cpp | 2 +- vm/factor.cpp | 6 +++++- vm/main-unix.cpp | 7 +------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/vm/errors.cpp b/vm/errors.cpp index 154237a8f2..8372edf0d2 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -165,7 +165,7 @@ void factorvm::memory_signal_handler_impl() void memory_signal_handler_impl() { - SIGNAL_VM_PTR()->misc_signal_handler_impl(); + SIGNAL_VM_PTR()->memory_signal_handler_impl(); } void factorvm::misc_signal_handler_impl() diff --git a/vm/factor.cpp b/vm/factor.cpp index 026453eae3..741800f8d1 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -52,7 +52,11 @@ void factorvm::default_parameters(vm_parameters *p) #ifdef WINDOWS p->console = false; #else - p->console = true; + if (this == vm) + p->console = true; + else + p->console = false; + #endif p->stack_traces = true; diff --git a/vm/main-unix.cpp b/vm/main-unix.cpp index bd1549a38e..bc605e3cfd 100644 --- a/vm/main-unix.cpp +++ b/vm/main-unix.cpp @@ -2,11 +2,6 @@ int main(int argc, char **argv) { - #ifdef FACTOR_MULTITHREADED - factor::THREADHANDLE thread = factor::start_standalone_factor_in_new_thread(argc,argv); - pthread_join(thread,NULL); - #else - factor::start_standalone_factor(argc,argv); - #endif + factor::start_standalone_factor(argc,argv); return 0; } From b1c68d92b7ece61074f17635355dbcf9c86f48e4 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 28 Aug 2009 20:23:01 +0100 Subject: [PATCH 240/345] added threadsafe defines. Dunno if they do much --- vm/master.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vm/master.hpp b/vm/master.hpp index bf60d1e4f6..00ee181b8f 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -1,6 +1,9 @@ #ifndef __FACTOR_MASTER_H__ #define __FACTOR_MASTER_H__ +#define _THREAD_SAFE +#define _REENTRANT + #ifndef WINCE #include #endif From f4af39b60e09faae16acc99d3f389c79afb1b115 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 28 Aug 2009 21:46:47 +0100 Subject: [PATCH 241/345] thread_id is a pthread_t on unix --- vm/factor.cpp | 15 ++++----------- vm/factor.hpp | 2 ++ vm/main-unix.cpp | 1 + vm/main-windows-nt.cpp | 1 + vm/os-macosx.mm | 2 +- vm/os-unix.cpp | 20 ++++++++++++++++++-- vm/os-unix.hpp | 6 +++++- vm/os-windows-nt.cpp | 25 ++++++++++++++++++++++--- vm/os-windows-nt.hpp | 6 +++++- vm/vm.hpp | 16 ++++++++-------- 10 files changed, 67 insertions(+), 27 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 741800f8d1..4ef4d11796 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -5,19 +5,11 @@ namespace factor factorvm *vm; -unordered_map thread_vms; - -factorvm *lookup_vm(unsigned long threadid) +void init_globals() { - return thread_vms[threadid]; + init_platform_globals(); } -void register_vm(unsigned long threadid, factorvm *thevm) -{ - thread_vms[threadid] = thevm; -} - - void factorvm::default_parameters(vm_parameters *p) { p->image_path = NULL; @@ -217,7 +209,6 @@ void factorvm::factor_sleep(long us) void factorvm::start_standalone_factor(int argc, vm_char **argv) { - register_vm(thread_id(),this); vm_parameters p; default_parameters(&p); init_parameters_from_args(&p,argc,argv); @@ -234,6 +225,7 @@ struct startargs { void* start_standalone_factor_thread(void *arg) { factorvm *newvm = new factorvm; + register_vm_with_thread(newvm); startargs *args = (startargs*) arg; newvm->start_standalone_factor(args->argc, args->argv); return 0; @@ -244,6 +236,7 @@ VM_C_API void start_standalone_factor(int argc, vm_char **argv) { factorvm *newvm = new factorvm; vm = newvm; + register_vm_with_thread(newvm); return newvm->start_standalone_factor(argc,argv); } diff --git a/vm/factor.hpp b/vm/factor.hpp index 46662072b5..5f41c952e1 100644 --- a/vm/factor.hpp +++ b/vm/factor.hpp @@ -1,6 +1,8 @@ namespace factor { +VM_C_API void init_globals(); + VM_C_API void start_standalone_factor(int argc, vm_char **argv); VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char **argv); } diff --git a/vm/main-unix.cpp b/vm/main-unix.cpp index bc605e3cfd..b8914e2bd3 100644 --- a/vm/main-unix.cpp +++ b/vm/main-unix.cpp @@ -2,6 +2,7 @@ int main(int argc, char **argv) { + factor::init_globals(); factor::start_standalone_factor(argc,argv); return 0; } diff --git a/vm/main-windows-nt.cpp b/vm/main-windows-nt.cpp index 2120717d86..df4a1172f1 100644 --- a/vm/main-windows-nt.cpp +++ b/vm/main-windows-nt.cpp @@ -16,6 +16,7 @@ int WINAPI WinMain( return 1; } + factor::init_globals(); #ifdef FACTOR_MULTITHREADED factor::THREADHANDLE thread = factor::start_standalone_factor_in_new_thread(nArgs,szArglist); WaitForSingleObject(thread, INFINITE); diff --git a/vm/os-macosx.mm b/vm/os-macosx.mm index e4da7b2221..7f692def06 100644 --- a/vm/os-macosx.mm +++ b/vm/os-macosx.mm @@ -5,7 +5,7 @@ namespace factor { -void c_to_factor_toplevel(cell quot) +void factorvm::c_to_factor_toplevel(cell quot) { for(;;) { diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 4cee04a11b..7913647f3e 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -17,10 +17,26 @@ THREADHANDLE start_thread(void *(*start_routine)(void *),void *args) return thread; } -unsigned long thread_id(){ - return pthread_self(); + +pthread_key_t tlsKey = 0; + +void init_platform_globals() +{ + if (pthread_key_create(&tlsKey, NULL) != 0){ + fatal_error("pthread_key_create() failed",0); + } + } +void register_vm_with_thread(factorvm *vm) +{ + pthread_setspecific(tlsKey,vm); +} + +factorvm *tls_vm() +{ + return (factorvm*)pthread_getspecific(tlsKey); +} static void *null_dll; diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index e3e207f641..5f84106f97 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -45,7 +45,7 @@ typedef char symbol_char; typedef pthread_t THREADHANDLE; THREADHANDLE start_thread(void *(*start_routine)(void *),void *args); -unsigned long thread_id(); +pthread_t thread_id(); void unix_init_signals(); void signal_handler(int signal, siginfo_t* siginfo, void* uap); @@ -54,5 +54,9 @@ void dump_stack_signal(int signal, siginfo_t* siginfo, void* uap); s64 current_micros(); void sleep_micros(cell usec); +void init_platform_globals(); +struct factorvm; +void register_vm_with_thread(factorvm *vm); +factorvm *tls_vm(); void open_console(); } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index fbaadaaba7..ee00e1434a 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -3,12 +3,31 @@ namespace factor { + THREADHANDLE start_thread(void *(*start_routine)(void *),void *args){ return (void*) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, args, 0, 0); } -unsigned long thread_id(){ - return GetCurrentThreadId(); + +DWORD dwTlsIndex; + +void init_platform_globals() +{ + if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) { + fatal_error("TlsAlloc failed - out of indexes",0); + } +} + +void register_vm_with_thread(factorvm *vm) +{ + if (! TlsSetValue(dwTlsIndex, vm)) { + fatal_error("TlsSetValue failed",0); + } +} + +factorvm *tls_vm() +{ + return (factorvm*)TlsGetValue(dwTlsIndex); } @@ -22,7 +41,7 @@ s64 current_micros() FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) { - factorvm *myvm = lookup_vm(GetCurrentThreadId()); + factorvm *myvm = SIGNAL_VM_PTR(); PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; diff --git a/vm/os-windows-nt.hpp b/vm/os-windows-nt.hpp index 385553e11e..366348a898 100755 --- a/vm/os-windows-nt.hpp +++ b/vm/os-windows-nt.hpp @@ -29,6 +29,10 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe); typedef HANDLE THREADHANDLE; THREADHANDLE start_thread(void *(*start_routine)(void *),void *args); -unsigned long thread_id(); + +void init_platform_globals(); +struct factorvm; +void register_vm_with_thread(factorvm *vm); +factorvm *tls_vm(); } diff --git a/vm/vm.hpp b/vm/vm.hpp index a16ff21121..b94ba16e00 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -690,12 +690,11 @@ struct factorvm { }; -extern factorvm *lookup_vm(unsigned long threadid); -extern void register_vm(unsigned long threadid,factorvm *vm); +// #define FACTOR_SINGLE_THREADED_SINGLETON +#define FACTOR_SINGLE_THREADED_TESTING -#define FACTOR_SINGLE_THREADED - -#ifdef FACTOR_SINGLE_THREADED +#ifdef FACTOR_SINGLE_THREADED_SINGLETON +/* calls are dispatched using the singleton */ extern factorvm *vm; #define PRIMITIVE_GETVM() vm #define PRIMITIVE_OVERFLOW_GETVM() vm @@ -704,13 +703,14 @@ extern void register_vm(unsigned long threadid,factorvm *vm); #define SIGNAL_VM_PTR() vm #endif -#ifdef FACTOR_TESTING_MULTITHREADED +#ifdef FACTOR_SINGLE_THREADED_TESTING +/* calls are dispatched as per multithreaded, but checked against singleton */ extern factorvm *vm; #define PRIMITIVE_GETVM() ((factorvm*)myvm) #define PRIMITIVE_OVERFLOW_GETVM() vm #define VM_PTR myvm #define ASSERTVM() assert(vm==myvm) - #define SIGNAL_VM_PTR() lookup_vm(thread_id()) + #define SIGNAL_VM_PTR() tls_vm() #endif #ifdef FACTOR_MULTITHREADED @@ -718,7 +718,7 @@ extern void register_vm(unsigned long threadid,factorvm *vm); #define PRIMITIVE_OVERFLOW_GETVM() ((factorvm*)myvm) #define VM_PTR myvm #define ASSERTVM() - #define SIGNAL_VM_PTR() lookup_vm(thread_id()) + #define SIGNAL_VM_PTR() tls_vm() #endif } From e49fa4109d1950a2acb041b4c39df02c7509668b Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 1 Sep 2009 19:08:27 +0100 Subject: [PATCH 242/345] added FACTOR_MULTITHREADED_TLS option --- vm/vm.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/vm/vm.hpp b/vm/vm.hpp index b94ba16e00..6570cf0c04 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -690,11 +690,10 @@ struct factorvm { }; -// #define FACTOR_SINGLE_THREADED_SINGLETON -#define FACTOR_SINGLE_THREADED_TESTING +#define FACTOR_MULTITHREADED_TLS #ifdef FACTOR_SINGLE_THREADED_SINGLETON -/* calls are dispatched using the singleton */ +/* calls are dispatched using the singleton vm ptr */ extern factorvm *vm; #define PRIMITIVE_GETVM() vm #define PRIMITIVE_OVERFLOW_GETVM() vm @@ -713,6 +712,15 @@ struct factorvm { #define SIGNAL_VM_PTR() tls_vm() #endif +#ifdef FACTOR_MULTITHREADED_TLS +/* uses thread local storage to obtain vm ptr */ + #define PRIMITIVE_GETVM() tls_vm() + #define PRIMITIVE_OVERFLOW_GETVM() tls_vm() + #define VM_PTR tls_vm() + #define ASSERTVM() + #define SIGNAL_VM_PTR() tls_vm() +#endif + #ifdef FACTOR_MULTITHREADED #define PRIMITIVE_GETVM() ((factorvm*)myvm) #define PRIMITIVE_OVERFLOW_GETVM() ((factorvm*)myvm) From 544bc3cd336e9fefbc5ec3cf2b2a66e939332aef Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 1 Sep 2009 19:17:34 +0100 Subject: [PATCH 243/345] removed vm ptr from os-macosx.mm --- vm/os-macosx.mm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/os-macosx.mm b/vm/os-macosx.mm index 7f692def06..872e0b8b48 100644 --- a/vm/os-macosx.mm +++ b/vm/os-macosx.mm @@ -10,11 +10,11 @@ void factorvm::c_to_factor_toplevel(cell quot) for(;;) { NS_DURING - c_to_factor(quot,vm); + c_to_factor(quot,this); NS_VOIDRETURN; NS_HANDLER - dpush(vm->allot_alien(F,(cell)localException)); - quot = vm->userenv[COCOA_EXCEPTION_ENV]; + dpush(allot_alien(F,(cell)localException)); + quot = userenv[COCOA_EXCEPTION_ENV]; if(!tagged(quot).type_p(QUOTATION_TYPE)) { /* No Cocoa exception handler was registered, so From 9e460f6decdc801be87423b81ca7f24146fe0af8 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 1 Sep 2009 19:40:03 +0100 Subject: [PATCH 244/345] removed vm ptr from mach_signal and some other places --- vm/mach_signal.cpp | 10 +++++----- vm/os-freebsd.cpp | 2 +- vm/os-linux-arm.cpp | 2 +- vm/os-linux.cpp | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index ecded49c0f..03a6ca2b72 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -41,18 +41,18 @@ static void call_fault_handler( a divide by zero or stack underflow in the listener */ /* Are we in compiled Factor code? Then use the current stack pointer */ - if(vm->in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) - vm->signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); + if(SIGNAL_VM_PTR()->in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) + SIGNAL_VM_PTR()->signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); /* Are we in C? Then use the saved callstack top */ else - vm->signal_callstack_top = NULL; + SIGNAL_VM_PTR()->signal_callstack_top = NULL; MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state)); /* Now we point the program counter at the right handler function. */ if(exception == EXC_BAD_ACCESS) { - vm->signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); + SIGNAL_VM_PTR()->signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); MACH_PROGRAM_COUNTER(thread_state) = (cell)memory_signal_handler_impl; } else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV) @@ -63,7 +63,7 @@ static void call_fault_handler( } else { - vm->signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); + SIGNAL_VM_PTR()->signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); MACH_PROGRAM_COUNTER(thread_state) = (cell)misc_signal_handler_impl; } } diff --git a/vm/os-freebsd.cpp b/vm/os-freebsd.cpp index 64c8ac19da..d259658284 100644 --- a/vm/os-freebsd.cpp +++ b/vm/os-freebsd.cpp @@ -33,7 +33,7 @@ const char *vm_executable_path() if(strcmp(path, "unknown") == 0) return NULL; - return vm->safe_strdup(path); + return safe_strdup(path); } } diff --git a/vm/os-linux-arm.cpp b/vm/os-linux-arm.cpp index 9a9ce7ada5..0f459d5ec5 100644 --- a/vm/os-linux-arm.cpp +++ b/vm/os-linux-arm.cpp @@ -25,7 +25,7 @@ void flush_icache(cell start, cell len) : "r0","r1","r2"); if(result < 0) - vm->critical_error("flush_icache() failed",result); + SIGNAL_VM_PTR->critical_error("flush_icache() failed",result); } } diff --git a/vm/os-linux.cpp b/vm/os-linux.cpp index 7929701d41..66b197e7c9 100644 --- a/vm/os-linux.cpp +++ b/vm/os-linux.cpp @@ -42,19 +42,19 @@ VM_C_API int inotify_rm_watch(int fd, u32 wd) VM_C_API int inotify_init() { - vm->not_implemented_error(); + VM_PTR->not_implemented_error(); return -1; } VM_C_API int inotify_add_watch(int fd, const char *name, u32 mask) { - vm->not_implemented_error(); + VM_PTR->not_implemented_error(); return -1; } VM_C_API int inotify_rm_watch(int fd, u32 wd) { - vm->not_implemented_error(); + VM_PTR->not_implemented_error(); return -1; } From 9bf6f97e35f6e6ca8c48d52b106b3963f9bc28b3 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Tue, 1 Sep 2009 20:47:18 +0100 Subject: [PATCH 245/345] Switched on singleton flag --- vm/vm.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/vm.hpp b/vm/vm.hpp index 6570cf0c04..91a832aa10 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -690,7 +690,7 @@ struct factorvm { }; -#define FACTOR_MULTITHREADED_TLS +#define FACTOR_SINGLE_THREADED_SINGLETON #ifdef FACTOR_SINGLE_THREADED_SINGLETON /* calls are dispatched using the singleton vm ptr */ From b07550620f9d7f005a03f9e275afa445da98aed0 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 3 Sep 2009 19:41:19 +0100 Subject: [PATCH 246/345] Dev checkpoint --- basis/vm/vm.factor | 1 - vm/errors.cpp | 2 +- vm/vm.hpp | 175 ++++++++++++++++++++++++--------------------- 3 files changed, 96 insertions(+), 82 deletions(-) diff --git a/basis/vm/vm.factor b/basis/vm/vm.factor index e2e7fc879c..ab5a98ab3c 100644 --- a/basis/vm/vm.factor +++ b/basis/vm/vm.factor @@ -17,7 +17,6 @@ C-STRUCT: vm { "zone" "nursery" } { "cell" "cards_offset" } { "cell" "decks_offset" } - { "cell" "__padding__" } { "cell[70]" "userenv" } ; diff --git a/vm/errors.cpp b/vm/errors.cpp index 8372edf0d2..c137782f81 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -185,7 +185,7 @@ void factorvm::fp_signal_handler_impl() void fp_signal_handler_impl() { - SIGNAL_VM_PTR->fp_signal_handler_impl(); + SIGNAL_VM_PTR()->fp_signal_handler_impl(); } } diff --git a/vm/vm.hpp b/vm/vm.hpp index 91a832aa10..b6508c7560 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -1,27 +1,112 @@ namespace factor { -struct factorvm { - +struct factorvmdata { // if you change this struct, also change vm.factor k-------- context *stack_chain; zone nursery; /* new objects are allocated here */ cell cards_offset; cell decks_offset; -#ifndef FACTOR_64 - cell __padding__ ; // align to 8 byte boundary -#endif cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ -#ifndef FACTOR_64 - cell __padding2__; // not sure why we need this, bootstrap doesn't work without it -#endif + + // ------------------------------- + + // contexts + cell ds_size, rs_size; + context *unused_contexts; + + // run + cell T; /* Canonical T object. It's just a word */ + + // profiler + bool profiling_p; + + // errors + /* Global variables used to pass fault handler state from signal handler to + user-space */ + cell signal_number; + cell signal_fault_addr; + unsigned int signal_fpu_status; + stack_frame *signal_callstack_top; + //data_heap + bool secure_gc; /* Set by the -securegc command line argument */ + bool gc_off; /* GC is off during heap walking */ + data_heap *data; + /* A heap walk allows useful things to be done, like finding all + references to an object for debugging purposes. */ + cell heap_scan_ptr; + //write barrier + cell allot_markers_offset; + //data_gc + /* used during garbage collection only */ + zone *newspace; + bool performing_gc; + bool performing_compaction; + cell collecting_gen; + /* if true, we are collecting aging space for the second time, so if it is still + full, we go on to collect tenured */ + bool collecting_aging_again; + /* in case a generation fills up in the middle of a gc, we jump back + up to try collecting the next generation. */ + jmp_buf gc_jmp; + gc_stats stats[max_gen_count]; + u64 cards_scanned; + u64 decks_scanned; + u64 card_scan_time; + cell code_heap_scans; + /* What generation was being collected when copy_code_heap_roots() was last + called? Until the next call to add_code_block(), future + collections of younger generations don't have to touch the code + heap. */ + cell last_code_heap_scan; + /* sometimes we grow the heap */ + bool growing_data_heap; + data_heap *old_data_heap; + + // local roots + /* If a runtime function needs to call another function which potentially + allocates memory, it must wrap any local variable references to Factor + objects in gc_root instances */ + std::vector gc_locals; + std::vector gc_bignums; + + //debug + bool fep_disabled; + bool full_output; + cell look_for; + cell obj; + + //math + cell bignum_zero; + cell bignum_pos_one; + cell bignum_neg_one; + + //code_heap + heap code; + unordered_map forwarding; + + //image + cell code_relocation_base; + cell data_relocation_base; + + //dispatch + cell megamorphic_cache_hits; + cell megamorphic_cache_misses; + + //inline cache + cell max_pic_size; + cell cold_call_to_ic_transitions; + cell ic_to_pic_transitions; + cell pic_to_mega_transitions; + cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ +}; + +struct factorvm : factorvmdata { // segments inline cell align_page(cell a); // contexts - cell ds_size, rs_size; - context *unused_contexts; void reset_datastack(); void reset_retainstack(); void fix_stacks(); @@ -40,7 +125,6 @@ struct factorvm { inline void vmprim_check_datastack(); // run - cell T; /* Canonical T object. It's just a word */ inline void vmprim_getenv(); inline void vmprim_setenv(); inline void vmprim_exit(); @@ -52,19 +136,12 @@ struct factorvm { inline void vmprim_clone(); // profiler - bool profiling_p; void init_profiler(); code_block *compile_profiling_stub(cell word_); void set_profiling(bool profiling); inline void vmprim_profiling(); // errors - /* Global variables used to pass fault handler state from signal handler to - user-space */ - cell signal_number; - cell signal_fault_addr; - unsigned int signal_fpu_status; - stack_frame *signal_callstack_top; void out_of_memory(); void critical_error(const char* msg, cell tagged); void throw_error(cell error, stack_frame *callstack_top); @@ -148,12 +225,6 @@ struct factorvm { bignum *digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm *), unsigned int radix, int negative_p); //data_heap - bool secure_gc; /* Set by the -securegc command line argument */ - bool gc_off; /* GC is off during heap walking */ - data_heap *data; - /* A heap walk allows useful things to be done, like finding all - references to an object for debugging purposes. */ - cell heap_scan_ptr; cell init_zone(zone *z, cell size, cell start); void init_card_decks(); data_heap *alloc_data_heap(cell gens, cell young_size,cell aging_size,cell tenured_size); @@ -183,7 +254,6 @@ struct factorvm { //write barrier - cell allot_markers_offset; inline card *addr_to_card(cell a); inline cell card_to_addr(card *c); inline cell card_offset(card *c); @@ -196,36 +266,6 @@ struct factorvm { //data_gc - /* used during garbage collection only */ - zone *newspace; - bool performing_gc; - bool performing_compaction; - cell collecting_gen; - - /* if true, we are collecting aging space for the second time, so if it is still - full, we go on to collect tenured */ - bool collecting_aging_again; - - /* in case a generation fills up in the middle of a gc, we jump back - up to try collecting the next generation. */ - jmp_buf gc_jmp; - - gc_stats stats[max_gen_count]; - u64 cards_scanned; - u64 decks_scanned; - u64 card_scan_time; - cell code_heap_scans; - - /* What generation was being collected when copy_code_heap_roots() was last - called? Until the next call to add_code_block(), future - collections of younger generations don't have to touch the code - heap. */ - cell last_code_heap_scan; - - /* sometimes we grow the heap */ - bool growing_data_heap; - data_heap *old_data_heap; - void init_data_gc(); object *copy_untagged_object_impl(object *pointer, cell size); object *copy_object_impl(object *untagged); @@ -263,23 +303,12 @@ struct factorvm { inline void check_tagged_pointer(cell tagged); inline void vmprim_clear_gc_stats(); - // local roots - /* If a runtime function needs to call another function which potentially - allocates memory, it must wrap any local variable references to Factor - objects in gc_root instances */ - std::vector gc_locals; - std::vector gc_bignums; - // generic arrays template T *allot_array_internal(cell capacity); template bool reallot_array_in_place_p(T *array, cell capacity); template TYPE *reallot_array(TYPE *array_, cell capacity); //debug - bool fep_disabled; - bool full_output; - cell look_for; - cell obj; void print_chars(string* str); void print_word(word* word, cell nesting); void print_factor_string(string* str); @@ -353,9 +382,6 @@ struct factorvm { inline void vmprim_wrapper(); //math - cell bignum_zero; - cell bignum_pos_one; - cell bignum_neg_one; inline void vmprim_bignum_to_fixnum(); inline void vmprim_float_to_fixnum(); inline void vmprim_fixnum_divint(); @@ -511,8 +537,6 @@ struct factorvm { } //code_heap - heap code; - unordered_map forwarding; void init_code_heap(cell size); bool in_code_heap_p(cell ptr); void jit_compile_word(cell word_, cell def_, bool relocate); @@ -530,8 +554,6 @@ struct factorvm { //image - cell code_relocation_base; - cell data_relocation_base; void init_objects(image_header *h); void load_data_heap(FILE *file, image_header *h, vm_parameters *p); void load_code_heap(FILE *file, image_header *h, vm_parameters *p); @@ -606,8 +628,6 @@ struct factorvm { inline void vmprim_quot_compiled_p(); //dispatch - cell megamorphic_cache_hits; - cell megamorphic_cache_misses; cell search_lookup_alist(cell table, cell klass); cell search_lookup_hash(cell table, cell klass, cell hashcode); cell nth_superclass(tuple_layout *layout, fixnum echelon); @@ -625,11 +645,6 @@ struct factorvm { inline void vmprim_dispatch_stats(); //inline cache - cell max_pic_size; - cell cold_call_to_ic_transitions; - cell ic_to_pic_transitions; - cell pic_to_mega_transitions; - cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ void init_inline_caching(int max_size); void deallocate_inline_cache(cell return_address); cell determine_inline_cache_type(array *cache_entries); From e8d1612e8e1c30ccf52c384d145fd854a8535885 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Thu, 3 Sep 2009 20:32:39 +0100 Subject: [PATCH 247/345] Split data out into separate vm-data struct --- vm/bignum.cpp | 2574 +++++++++++++++++++++--------------------- vm/errors.cpp | 7 - vm/factor.cpp | 156 +++ vm/os-windows-nt.cpp | 31 +- vm/vm-data-dummy.hpp | 116 ++ vm/vm-data.hpp | 105 ++ vm/vm.hpp | 105 +- 7 files changed, 1687 insertions(+), 1407 deletions(-) create mode 100644 vm/vm-data-dummy.hpp create mode 100644 vm/vm-data.hpp diff --git a/vm/bignum.cpp b/vm/bignum.cpp index 4c01b12820..3e754c2ab5 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -1,36 +1,36 @@ /* :tabSize=2:indentSize=2:noTabs=true: -Copyright (C) 1989-94 Massachusetts Institute of Technology -Portions copyright (C) 2004-2008 Slava Pestov + Copyright (C) 1989-94 Massachusetts Institute of Technology + Portions copyright (C) 2004-2008 Slava Pestov -This material was developed by the Scheme project at the Massachusetts -Institute of Technology, Department of Electrical Engineering and -Computer Science. Permission to copy and modify this software, to -redistribute either the original software or a modified version, and -to use this software for any purpose is granted, subject to the -following restrictions and understandings. + This material was developed by the Scheme project at the Massachusetts + Institute of Technology, Department of Electrical Engineering and + Computer Science. Permission to copy and modify this software, to + redistribute either the original software or a modified version, and + to use this software for any purpose is granted, subject to the + following restrictions and understandings. -1. Any copy made of this software must include this copyright notice -in full. + 1. Any copy made of this software must include this copyright notice + in full. -2. Users of this software agree to make their best efforts (a) to -return to the MIT Scheme project any improvements or extensions that -they make, so that these may be included in future releases; and (b) -to inform MIT of noteworthy uses of this software. + 2. Users of this software agree to make their best efforts (a) to + return to the MIT Scheme project any improvements or extensions that + they make, so that these may be included in future releases; and (b) + to inform MIT of noteworthy uses of this software. -3. All materials developed as a consequence of the use of this -software shall duly acknowledge such use, in accordance with the usual -standards of acknowledging credit in academic research. + 3. All materials developed as a consequence of the use of this + software shall duly acknowledge such use, in accordance with the usual + standards of acknowledging credit in academic research. -4. MIT has made no warrantee or representation that the operation of -this software will be error-free, and MIT is under no obligation to -provide any services, by way of maintenance, update, or otherwise. + 4. MIT has made no warrantee or representation that the operation of + this software will be error-free, and MIT is under no obligation to + provide any services, by way of maintenance, update, or otherwise. -5. In conjunction with products arising from the use of this material, -there shall be no use of the name of the Massachusetts Institute of -Technology nor of any adaptation thereof in any advertising, -promotional, or sales literature without prior written consent from -MIT in each case. */ + 5. In conjunction with products arising from the use of this material, + there shall be no use of the name of the Massachusetts Institute of + Technology nor of any adaptation thereof in any advertising, + promotional, or sales literature without prior written consent from + MIT in each case. */ /* Changes for Scheme 48: * - Converted to ANSI. @@ -63,309 +63,309 @@ namespace factor int factorvm::bignum_equal_p(bignum * x, bignum * y) { - return - ((BIGNUM_ZERO_P (x)) - ? (BIGNUM_ZERO_P (y)) - : ((! (BIGNUM_ZERO_P (y))) - && ((BIGNUM_NEGATIVE_P (x)) - ? (BIGNUM_NEGATIVE_P (y)) - : (! (BIGNUM_NEGATIVE_P (y)))) - && (bignum_equal_p_unsigned (x, y)))); + return + ((BIGNUM_ZERO_P (x)) + ? (BIGNUM_ZERO_P (y)) + : ((! (BIGNUM_ZERO_P (y))) + && ((BIGNUM_NEGATIVE_P (x)) + ? (BIGNUM_NEGATIVE_P (y)) + : (! (BIGNUM_NEGATIVE_P (y)))) + && (bignum_equal_p_unsigned (x, y)))); } enum bignum_comparison factorvm::bignum_compare(bignum * x, bignum * y) { - return - ((BIGNUM_ZERO_P (x)) - ? ((BIGNUM_ZERO_P (y)) - ? bignum_comparison_equal - : (BIGNUM_NEGATIVE_P (y)) - ? bignum_comparison_greater - : bignum_comparison_less) - : (BIGNUM_ZERO_P (y)) - ? ((BIGNUM_NEGATIVE_P (x)) - ? bignum_comparison_less - : bignum_comparison_greater) - : (BIGNUM_NEGATIVE_P (x)) - ? ((BIGNUM_NEGATIVE_P (y)) - ? (bignum_compare_unsigned (y, x)) - : (bignum_comparison_less)) - : ((BIGNUM_NEGATIVE_P (y)) - ? (bignum_comparison_greater) - : (bignum_compare_unsigned (x, y)))); + return + ((BIGNUM_ZERO_P (x)) + ? ((BIGNUM_ZERO_P (y)) + ? bignum_comparison_equal + : (BIGNUM_NEGATIVE_P (y)) + ? bignum_comparison_greater + : bignum_comparison_less) + : (BIGNUM_ZERO_P (y)) + ? ((BIGNUM_NEGATIVE_P (x)) + ? bignum_comparison_less + : bignum_comparison_greater) + : (BIGNUM_NEGATIVE_P (x)) + ? ((BIGNUM_NEGATIVE_P (y)) + ? (bignum_compare_unsigned (y, x)) + : (bignum_comparison_less)) + : ((BIGNUM_NEGATIVE_P (y)) + ? (bignum_comparison_greater) + : (bignum_compare_unsigned (x, y)))); } /* allocates memory */ bignum *factorvm::bignum_add(bignum * x, bignum * y) { - return - ((BIGNUM_ZERO_P (x)) - ? (y) - : (BIGNUM_ZERO_P (y)) - ? (x) - : ((BIGNUM_NEGATIVE_P (x)) - ? ((BIGNUM_NEGATIVE_P (y)) - ? (bignum_add_unsigned (x, y, 1)) - : (bignum_subtract_unsigned (y, x))) - : ((BIGNUM_NEGATIVE_P (y)) - ? (bignum_subtract_unsigned (x, y)) - : (bignum_add_unsigned (x, y, 0))))); + return + ((BIGNUM_ZERO_P (x)) + ? (y) + : (BIGNUM_ZERO_P (y)) + ? (x) + : ((BIGNUM_NEGATIVE_P (x)) + ? ((BIGNUM_NEGATIVE_P (y)) + ? (bignum_add_unsigned (x, y, 1)) + : (bignum_subtract_unsigned (y, x))) + : ((BIGNUM_NEGATIVE_P (y)) + ? (bignum_subtract_unsigned (x, y)) + : (bignum_add_unsigned (x, y, 0))))); } /* allocates memory */ bignum *factorvm::bignum_subtract(bignum * x, bignum * y) { - return - ((BIGNUM_ZERO_P (x)) - ? ((BIGNUM_ZERO_P (y)) - ? (y) - : (bignum_new_sign (y, (! (BIGNUM_NEGATIVE_P (y)))))) - : ((BIGNUM_ZERO_P (y)) - ? (x) - : ((BIGNUM_NEGATIVE_P (x)) - ? ((BIGNUM_NEGATIVE_P (y)) - ? (bignum_subtract_unsigned (y, x)) - : (bignum_add_unsigned (x, y, 1))) - : ((BIGNUM_NEGATIVE_P (y)) - ? (bignum_add_unsigned (x, y, 0)) - : (bignum_subtract_unsigned (x, y)))))); + return + ((BIGNUM_ZERO_P (x)) + ? ((BIGNUM_ZERO_P (y)) + ? (y) + : (bignum_new_sign (y, (! (BIGNUM_NEGATIVE_P (y)))))) + : ((BIGNUM_ZERO_P (y)) + ? (x) + : ((BIGNUM_NEGATIVE_P (x)) + ? ((BIGNUM_NEGATIVE_P (y)) + ? (bignum_subtract_unsigned (y, x)) + : (bignum_add_unsigned (x, y, 1))) + : ((BIGNUM_NEGATIVE_P (y)) + ? (bignum_add_unsigned (x, y, 0)) + : (bignum_subtract_unsigned (x, y)))))); } /* allocates memory */ bignum *factorvm::bignum_multiply(bignum * x, bignum * y) { - bignum_length_type x_length = (BIGNUM_LENGTH (x)); - bignum_length_type y_length = (BIGNUM_LENGTH (y)); - int negative_p = - ((BIGNUM_NEGATIVE_P (x)) - ? (! (BIGNUM_NEGATIVE_P (y))) - : (BIGNUM_NEGATIVE_P (y))); - if (BIGNUM_ZERO_P (x)) - return (x); - if (BIGNUM_ZERO_P (y)) - return (y); - if (x_length == 1) - { - bignum_digit_type digit = (BIGNUM_REF (x, 0)); - if (digit == 1) - return (bignum_maybe_new_sign (y, negative_p)); - if (digit < BIGNUM_RADIX_ROOT) - return (bignum_multiply_unsigned_small_factor (y, digit, negative_p)); - } - if (y_length == 1) - { - bignum_digit_type digit = (BIGNUM_REF (y, 0)); - if (digit == 1) - return (bignum_maybe_new_sign (x, negative_p)); - if (digit < BIGNUM_RADIX_ROOT) - return (bignum_multiply_unsigned_small_factor (x, digit, negative_p)); - } - return (bignum_multiply_unsigned (x, y, negative_p)); + bignum_length_type x_length = (BIGNUM_LENGTH (x)); + bignum_length_type y_length = (BIGNUM_LENGTH (y)); + int negative_p = + ((BIGNUM_NEGATIVE_P (x)) + ? (! (BIGNUM_NEGATIVE_P (y))) + : (BIGNUM_NEGATIVE_P (y))); + if (BIGNUM_ZERO_P (x)) + return (x); + if (BIGNUM_ZERO_P (y)) + return (y); + if (x_length == 1) + { + bignum_digit_type digit = (BIGNUM_REF (x, 0)); + if (digit == 1) + return (bignum_maybe_new_sign (y, negative_p)); + if (digit < BIGNUM_RADIX_ROOT) + return (bignum_multiply_unsigned_small_factor (y, digit, negative_p)); + } + if (y_length == 1) + { + bignum_digit_type digit = (BIGNUM_REF (y, 0)); + if (digit == 1) + return (bignum_maybe_new_sign (x, negative_p)); + if (digit < BIGNUM_RADIX_ROOT) + return (bignum_multiply_unsigned_small_factor (x, digit, negative_p)); + } + return (bignum_multiply_unsigned (x, y, negative_p)); } /* allocates memory */ void factorvm::bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder) { - if (BIGNUM_ZERO_P (denominator)) - { - divide_by_zero_error(); - return; - } - if (BIGNUM_ZERO_P (numerator)) - { - (*quotient) = numerator; - (*remainder) = numerator; - } - else - { - int r_negative_p = (BIGNUM_NEGATIVE_P (numerator)); - int q_negative_p = - ((BIGNUM_NEGATIVE_P (denominator)) ? (! r_negative_p) : r_negative_p); - switch (bignum_compare_unsigned (numerator, denominator)) - { - case bignum_comparison_equal: - { - (*quotient) = (BIGNUM_ONE (q_negative_p)); - (*remainder) = (BIGNUM_ZERO ()); - break; - } - case bignum_comparison_less: - { - (*quotient) = (BIGNUM_ZERO ()); - (*remainder) = numerator; - break; - } - case bignum_comparison_greater: - { - if ((BIGNUM_LENGTH (denominator)) == 1) - { - bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); - if (digit == 1) - { - (*quotient) = - (bignum_maybe_new_sign (numerator, q_negative_p)); - (*remainder) = (BIGNUM_ZERO ()); - break; - } - else if (digit < BIGNUM_RADIX_ROOT) - { - bignum_divide_unsigned_small_denominator - (numerator, digit, - quotient, remainder, - q_negative_p, r_negative_p); - break; - } - else - { - bignum_divide_unsigned_medium_denominator - (numerator, digit, - quotient, remainder, - q_negative_p, r_negative_p); - break; - } - } - bignum_divide_unsigned_large_denominator - (numerator, denominator, - quotient, remainder, - q_negative_p, r_negative_p); - break; - } - } - } + if (BIGNUM_ZERO_P (denominator)) + { + divide_by_zero_error(); + return; + } + if (BIGNUM_ZERO_P (numerator)) + { + (*quotient) = numerator; + (*remainder) = numerator; + } + else + { + int r_negative_p = (BIGNUM_NEGATIVE_P (numerator)); + int q_negative_p = + ((BIGNUM_NEGATIVE_P (denominator)) ? (! r_negative_p) : r_negative_p); + switch (bignum_compare_unsigned (numerator, denominator)) + { + case bignum_comparison_equal: + { + (*quotient) = (BIGNUM_ONE (q_negative_p)); + (*remainder) = (BIGNUM_ZERO ()); + break; + } + case bignum_comparison_less: + { + (*quotient) = (BIGNUM_ZERO ()); + (*remainder) = numerator; + break; + } + case bignum_comparison_greater: + { + if ((BIGNUM_LENGTH (denominator)) == 1) + { + bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); + if (digit == 1) + { + (*quotient) = + (bignum_maybe_new_sign (numerator, q_negative_p)); + (*remainder) = (BIGNUM_ZERO ()); + break; + } + else if (digit < BIGNUM_RADIX_ROOT) + { + bignum_divide_unsigned_small_denominator + (numerator, digit, + quotient, remainder, + q_negative_p, r_negative_p); + break; + } + else + { + bignum_divide_unsigned_medium_denominator + (numerator, digit, + quotient, remainder, + q_negative_p, r_negative_p); + break; + } + } + bignum_divide_unsigned_large_denominator + (numerator, denominator, + quotient, remainder, + q_negative_p, r_negative_p); + break; + } + } + } } /* allocates memory */ bignum *factorvm::bignum_quotient(bignum * numerator, bignum * denominator) { - if (BIGNUM_ZERO_P (denominator)) - { - divide_by_zero_error(); - return (BIGNUM_OUT_OF_BAND); - } - if (BIGNUM_ZERO_P (numerator)) - return numerator; - { - int q_negative_p = - ((BIGNUM_NEGATIVE_P (denominator)) - ? (! (BIGNUM_NEGATIVE_P (numerator))) - : (BIGNUM_NEGATIVE_P (numerator))); - switch (bignum_compare_unsigned (numerator, denominator)) - { - case bignum_comparison_equal: - return (BIGNUM_ONE (q_negative_p)); - case bignum_comparison_less: - return (BIGNUM_ZERO ()); - case bignum_comparison_greater: - default: /* to appease gcc -Wall */ - { - bignum * quotient; - if ((BIGNUM_LENGTH (denominator)) == 1) - { - bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); - if (digit == 1) - return (bignum_maybe_new_sign (numerator, q_negative_p)); - if (digit < BIGNUM_RADIX_ROOT) - bignum_divide_unsigned_small_denominator - (numerator, digit, - ("ient), ((bignum * *) 0), - q_negative_p, 0); - else - bignum_divide_unsigned_medium_denominator - (numerator, digit, - ("ient), ((bignum * *) 0), - q_negative_p, 0); - } - else - bignum_divide_unsigned_large_denominator - (numerator, denominator, - ("ient), ((bignum * *) 0), - q_negative_p, 0); - return (quotient); - } - } - } + if (BIGNUM_ZERO_P (denominator)) + { + divide_by_zero_error(); + return (BIGNUM_OUT_OF_BAND); + } + if (BIGNUM_ZERO_P (numerator)) + return numerator; + { + int q_negative_p = + ((BIGNUM_NEGATIVE_P (denominator)) + ? (! (BIGNUM_NEGATIVE_P (numerator))) + : (BIGNUM_NEGATIVE_P (numerator))); + switch (bignum_compare_unsigned (numerator, denominator)) + { + case bignum_comparison_equal: + return (BIGNUM_ONE (q_negative_p)); + case bignum_comparison_less: + return (BIGNUM_ZERO ()); + case bignum_comparison_greater: + default: /* to appease gcc -Wall */ + { + bignum * quotient; + if ((BIGNUM_LENGTH (denominator)) == 1) + { + bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); + if (digit == 1) + return (bignum_maybe_new_sign (numerator, q_negative_p)); + if (digit < BIGNUM_RADIX_ROOT) + bignum_divide_unsigned_small_denominator + (numerator, digit, + ("ient), ((bignum * *) 0), + q_negative_p, 0); + else + bignum_divide_unsigned_medium_denominator + (numerator, digit, + ("ient), ((bignum * *) 0), + q_negative_p, 0); + } + else + bignum_divide_unsigned_large_denominator + (numerator, denominator, + ("ient), ((bignum * *) 0), + q_negative_p, 0); + return (quotient); + } + } + } } /* allocates memory */ bignum *factorvm::bignum_remainder(bignum * numerator, bignum * denominator) { - if (BIGNUM_ZERO_P (denominator)) - { - divide_by_zero_error(); - return (BIGNUM_OUT_OF_BAND); - } - if (BIGNUM_ZERO_P (numerator)) - return numerator; - switch (bignum_compare_unsigned (numerator, denominator)) - { - case bignum_comparison_equal: - return (BIGNUM_ZERO ()); - case bignum_comparison_less: - return numerator; - case bignum_comparison_greater: - default: /* to appease gcc -Wall */ - { - bignum * remainder; - if ((BIGNUM_LENGTH (denominator)) == 1) - { - bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); - if (digit == 1) - return (BIGNUM_ZERO ()); - if (digit < BIGNUM_RADIX_ROOT) - return - (bignum_remainder_unsigned_small_denominator - (numerator, digit, (BIGNUM_NEGATIVE_P (numerator)))); - bignum_divide_unsigned_medium_denominator - (numerator, digit, - ((bignum * *) 0), (&remainder), - 0, (BIGNUM_NEGATIVE_P (numerator))); - } - else - bignum_divide_unsigned_large_denominator - (numerator, denominator, - ((bignum * *) 0), (&remainder), - 0, (BIGNUM_NEGATIVE_P (numerator))); - return (remainder); - } - } + if (BIGNUM_ZERO_P (denominator)) + { + divide_by_zero_error(); + return (BIGNUM_OUT_OF_BAND); + } + if (BIGNUM_ZERO_P (numerator)) + return numerator; + switch (bignum_compare_unsigned (numerator, denominator)) + { + case bignum_comparison_equal: + return (BIGNUM_ZERO ()); + case bignum_comparison_less: + return numerator; + case bignum_comparison_greater: + default: /* to appease gcc -Wall */ + { + bignum * remainder; + if ((BIGNUM_LENGTH (denominator)) == 1) + { + bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); + if (digit == 1) + return (BIGNUM_ZERO ()); + if (digit < BIGNUM_RADIX_ROOT) + return + (bignum_remainder_unsigned_small_denominator + (numerator, digit, (BIGNUM_NEGATIVE_P (numerator)))); + bignum_divide_unsigned_medium_denominator + (numerator, digit, + ((bignum * *) 0), (&remainder), + 0, (BIGNUM_NEGATIVE_P (numerator))); + } + else + bignum_divide_unsigned_large_denominator + (numerator, denominator, + ((bignum * *) 0), (&remainder), + 0, (BIGNUM_NEGATIVE_P (numerator))); + return (remainder); + } + } } -#define FOO_TO_BIGNUM(name,type,utype) \ - bignum * factorvm::name##_to_bignum(type n) \ - { \ - int negative_p; \ - bignum_digit_type result_digits [BIGNUM_DIGITS_FOR(type)]; \ - bignum_digit_type * end_digits = result_digits; \ - /* Special cases win when these small constants are cached. */ \ - if (n == 0) return (BIGNUM_ZERO ()); \ - if (n == 1) return (BIGNUM_ONE (0)); \ - if (n < (type)0 && n == (type)-1) return (BIGNUM_ONE (1)); \ - { \ - utype accumulator = ((negative_p = (n < (type)0)) ? (-n) : n); \ - do \ - { \ - (*end_digits++) = (accumulator & BIGNUM_DIGIT_MASK); \ - accumulator >>= BIGNUM_DIGIT_LENGTH; \ - } \ - while (accumulator != 0); \ - } \ - { \ - bignum * result = \ - (allot_bignum ((end_digits - result_digits), negative_p)); \ - bignum_digit_type * scan_digits = result_digits; \ - bignum_digit_type * scan_result = (BIGNUM_START_PTR (result)); \ - while (scan_digits < end_digits) \ - (*scan_result++) = (*scan_digits++); \ - return (result); \ - } \ - } +#define FOO_TO_BIGNUM(name,type,utype) \ +bignum * factorvm::name##_to_bignum(type n) \ +{ \ + int negative_p; \ + bignum_digit_type result_digits [BIGNUM_DIGITS_FOR(type)]; \ + bignum_digit_type * end_digits = result_digits; \ + /* Special cases win when these small constants are cached. */ \ + if (n == 0) return (BIGNUM_ZERO ()); \ + if (n == 1) return (BIGNUM_ONE (0)); \ + if (n < (type)0 && n == (type)-1) return (BIGNUM_ONE (1)); \ + { \ + utype accumulator = ((negative_p = (n < (type)0)) ? (-n) : n); \ + do \ + { \ + (*end_digits++) = (accumulator & BIGNUM_DIGIT_MASK); \ + accumulator >>= BIGNUM_DIGIT_LENGTH; \ + } \ + while (accumulator != 0); \ + } \ + { \ + bignum * result = \ + (allot_bignum ((end_digits - result_digits), negative_p)); \ + bignum_digit_type * scan_digits = result_digits; \ + bignum_digit_type * scan_result = (BIGNUM_START_PTR (result)); \ + while (scan_digits < end_digits) \ + (*scan_result++) = (*scan_digits++); \ + return (result); \ + } \ +} /* all below allocate memory */ FOO_TO_BIGNUM(cell,cell,cell) @@ -373,20 +373,20 @@ FOO_TO_BIGNUM(fixnum,fixnum,cell) FOO_TO_BIGNUM(long_long,s64,u64) FOO_TO_BIGNUM(ulong_long,u64,u64) -#define BIGNUM_TO_FOO(name,type,utype) \ -type factorvm::bignum_to_##name(bignum * bignum) \ - { \ - if (BIGNUM_ZERO_P (bignum)) \ - return (0); \ - { \ - utype accumulator = 0; \ - bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); \ - bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); \ - while (start < scan) \ - accumulator = ((accumulator << BIGNUM_DIGIT_LENGTH) + (*--scan)); \ - return ((BIGNUM_NEGATIVE_P (bignum)) ? (-((type)accumulator)) : accumulator); \ - } \ - } +#define BIGNUM_TO_FOO(name,type,utype) \ + type factorvm::bignum_to_##name(bignum * bignum) \ + { \ + if (BIGNUM_ZERO_P (bignum)) \ + return (0); \ + { \ + utype accumulator = 0; \ + bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); \ + bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); \ + while (start < scan) \ + accumulator = ((accumulator << BIGNUM_DIGIT_LENGTH) + (*--scan)); \ + return ((BIGNUM_NEGATIVE_P (bignum)) ? (-((type)accumulator)) : accumulator); \ + } \ + } /* all of the below allocate memory */ BIGNUM_TO_FOO(cell,cell,cell); @@ -396,25 +396,25 @@ BIGNUM_TO_FOO(ulong_long,u64,u64) double factorvm::bignum_to_double(bignum * bignum) { - if (BIGNUM_ZERO_P (bignum)) - return (0); - { - double accumulator = 0; - bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); - bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); - while (start < scan) - accumulator = ((accumulator * BIGNUM_RADIX) + (*--scan)); - return ((BIGNUM_NEGATIVE_P (bignum)) ? (-accumulator) : accumulator); - } + if (BIGNUM_ZERO_P (bignum)) + return (0); + { + double accumulator = 0; + bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); + bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); + while (start < scan) + accumulator = ((accumulator * BIGNUM_RADIX) + (*--scan)); + return ((BIGNUM_NEGATIVE_P (bignum)) ? (-accumulator) : accumulator); + } } -#define DTB_WRITE_DIGIT(factor) \ -{ \ - significand *= (factor); \ - digit = ((bignum_digit_type) significand); \ - (*--scan) = digit; \ - significand -= ((double) digit); \ +#define DTB_WRITE_DIGIT(factor) \ +{ \ + significand *= (factor); \ + digit = ((bignum_digit_type) significand); \ + (*--scan) = digit; \ + significand -= ((double) digit); \ } /* allocates memory */ @@ -422,33 +422,33 @@ double factorvm::bignum_to_double(bignum * bignum) bignum *factorvm::double_to_bignum(double x) { - if (x == inf || x == -inf || x != x) return (BIGNUM_ZERO ()); - int exponent; - double significand = (frexp (x, (&exponent))); - if (exponent <= 0) return (BIGNUM_ZERO ()); - if (exponent == 1) return (BIGNUM_ONE (x < 0)); - if (significand < 0) significand = (-significand); - { - bignum_length_type length = (BIGNUM_BITS_TO_DIGITS (exponent)); - bignum * result = (allot_bignum (length, (x < 0))); - bignum_digit_type * start = (BIGNUM_START_PTR (result)); - bignum_digit_type * scan = (start + length); - bignum_digit_type digit; - int odd_bits = (exponent % BIGNUM_DIGIT_LENGTH); - if (odd_bits > 0) - DTB_WRITE_DIGIT ((fixnum)1 << odd_bits); - while (start < scan) - { - if (significand == 0) - { - while (start < scan) - (*--scan) = 0; - break; - } - DTB_WRITE_DIGIT (BIGNUM_RADIX); - } - return (result); - } + if (x == inf || x == -inf || x != x) return (BIGNUM_ZERO ()); + int exponent; + double significand = (frexp (x, (&exponent))); + if (exponent <= 0) return (BIGNUM_ZERO ()); + if (exponent == 1) return (BIGNUM_ONE (x < 0)); + if (significand < 0) significand = (-significand); + { + bignum_length_type length = (BIGNUM_BITS_TO_DIGITS (exponent)); + bignum * result = (allot_bignum (length, (x < 0))); + bignum_digit_type * start = (BIGNUM_START_PTR (result)); + bignum_digit_type * scan = (start + length); + bignum_digit_type digit; + int odd_bits = (exponent % BIGNUM_DIGIT_LENGTH); + if (odd_bits > 0) + DTB_WRITE_DIGIT ((fixnum)1 << odd_bits); + while (start < scan) + { + if (significand == 0) + { + while (start < scan) + (*--scan) = 0; + break; + } + DTB_WRITE_DIGIT (BIGNUM_RADIX); + } + return (result); + } } @@ -458,45 +458,45 @@ bignum *factorvm::double_to_bignum(double x) int factorvm::bignum_equal_p_unsigned(bignum * x, bignum * y) { - bignum_length_type length = (BIGNUM_LENGTH (x)); - if (length != (BIGNUM_LENGTH (y))) - return (0); - else - { - bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); - bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); - bignum_digit_type * end_x = (scan_x + length); - while (scan_x < end_x) - if ((*scan_x++) != (*scan_y++)) - return (0); - return (1); - } + bignum_length_type length = (BIGNUM_LENGTH (x)); + if (length != (BIGNUM_LENGTH (y))) + return (0); + else + { + bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); + bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); + bignum_digit_type * end_x = (scan_x + length); + while (scan_x < end_x) + if ((*scan_x++) != (*scan_y++)) + return (0); + return (1); + } } enum bignum_comparison factorvm::bignum_compare_unsigned(bignum * x, bignum * y) { - bignum_length_type x_length = (BIGNUM_LENGTH (x)); - bignum_length_type y_length = (BIGNUM_LENGTH (y)); - if (x_length < y_length) - return (bignum_comparison_less); - if (x_length > y_length) - return (bignum_comparison_greater); - { - bignum_digit_type * start_x = (BIGNUM_START_PTR (x)); - bignum_digit_type * scan_x = (start_x + x_length); - bignum_digit_type * scan_y = ((BIGNUM_START_PTR (y)) + y_length); - while (start_x < scan_x) - { - bignum_digit_type digit_x = (*--scan_x); - bignum_digit_type digit_y = (*--scan_y); - if (digit_x < digit_y) - return (bignum_comparison_less); - if (digit_x > digit_y) - return (bignum_comparison_greater); - } - } - return (bignum_comparison_equal); + bignum_length_type x_length = (BIGNUM_LENGTH (x)); + bignum_length_type y_length = (BIGNUM_LENGTH (y)); + if (x_length < y_length) + return (bignum_comparison_less); + if (x_length > y_length) + return (bignum_comparison_greater); + { + bignum_digit_type * start_x = (BIGNUM_START_PTR (x)); + bignum_digit_type * scan_x = (start_x + x_length); + bignum_digit_type * scan_y = ((BIGNUM_START_PTR (y)) + y_length); + while (start_x < scan_x) + { + bignum_digit_type digit_x = (*--scan_x); + bignum_digit_type digit_y = (*--scan_y); + if (digit_x < digit_y) + return (bignum_comparison_less); + if (digit_x > digit_y) + return (bignum_comparison_greater); + } + } + return (bignum_comparison_equal); } @@ -507,64 +507,64 @@ bignum *factorvm::bignum_add_unsigned(bignum * x, bignum * y, int negative_p) { GC_BIGNUM(x,this); GC_BIGNUM(y,this); - if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) - { - bignum * z = x; - x = y; - y = z; - } - { - bignum_length_type x_length = (BIGNUM_LENGTH (x)); + if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) + { + bignum * z = x; + x = y; + y = z; + } + { + bignum_length_type x_length = (BIGNUM_LENGTH (x)); - bignum * r = (allot_bignum ((x_length + 1), negative_p)); + bignum * r = (allot_bignum ((x_length + 1), negative_p)); - bignum_digit_type sum; - bignum_digit_type carry = 0; - bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); - bignum_digit_type * scan_r = (BIGNUM_START_PTR (r)); - { - bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); - bignum_digit_type * end_y = (scan_y + (BIGNUM_LENGTH (y))); - while (scan_y < end_y) - { - sum = ((*scan_x++) + (*scan_y++) + carry); - if (sum < BIGNUM_RADIX) - { - (*scan_r++) = sum; - carry = 0; - } - else - { - (*scan_r++) = (sum - BIGNUM_RADIX); - carry = 1; - } - } - } - { - bignum_digit_type * end_x = ((BIGNUM_START_PTR (x)) + x_length); - if (carry != 0) - while (scan_x < end_x) - { - sum = ((*scan_x++) + 1); - if (sum < BIGNUM_RADIX) - { - (*scan_r++) = sum; - carry = 0; - break; - } - else - (*scan_r++) = (sum - BIGNUM_RADIX); - } - while (scan_x < end_x) - (*scan_r++) = (*scan_x++); - } - if (carry != 0) - { - (*scan_r) = 1; - return (r); - } - return (bignum_shorten_length (r, x_length)); - } + bignum_digit_type sum; + bignum_digit_type carry = 0; + bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); + bignum_digit_type * scan_r = (BIGNUM_START_PTR (r)); + { + bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); + bignum_digit_type * end_y = (scan_y + (BIGNUM_LENGTH (y))); + while (scan_y < end_y) + { + sum = ((*scan_x++) + (*scan_y++) + carry); + if (sum < BIGNUM_RADIX) + { + (*scan_r++) = sum; + carry = 0; + } + else + { + (*scan_r++) = (sum - BIGNUM_RADIX); + carry = 1; + } + } + } + { + bignum_digit_type * end_x = ((BIGNUM_START_PTR (x)) + x_length); + if (carry != 0) + while (scan_x < end_x) + { + sum = ((*scan_x++) + 1); + if (sum < BIGNUM_RADIX) + { + (*scan_r++) = sum; + carry = 0; + break; + } + else + (*scan_r++) = (sum - BIGNUM_RADIX); + } + while (scan_x < end_x) + (*scan_r++) = (*scan_x++); + } + if (carry != 0) + { + (*scan_r) = 1; + return (r); + } + return (bignum_shorten_length (r, x_length)); + } } @@ -575,149 +575,149 @@ bignum *factorvm::bignum_subtract_unsigned(bignum * x, bignum * y) { GC_BIGNUM(x,this); GC_BIGNUM(y,this); - int negative_p = 0; - switch (bignum_compare_unsigned (x, y)) - { - case bignum_comparison_equal: - return (BIGNUM_ZERO ()); - case bignum_comparison_less: - { - bignum * z = x; - x = y; - y = z; - } - negative_p = 1; - break; - case bignum_comparison_greater: - negative_p = 0; - break; - } - { - bignum_length_type x_length = (BIGNUM_LENGTH (x)); + int negative_p = 0; + switch (bignum_compare_unsigned (x, y)) + { + case bignum_comparison_equal: + return (BIGNUM_ZERO ()); + case bignum_comparison_less: + { + bignum * z = x; + x = y; + y = z; + } + negative_p = 1; + break; + case bignum_comparison_greater: + negative_p = 0; + break; + } + { + bignum_length_type x_length = (BIGNUM_LENGTH (x)); - bignum * r = (allot_bignum (x_length, negative_p)); + bignum * r = (allot_bignum (x_length, negative_p)); - bignum_digit_type difference; - bignum_digit_type borrow = 0; - bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); - bignum_digit_type * scan_r = (BIGNUM_START_PTR (r)); - { - bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); - bignum_digit_type * end_y = (scan_y + (BIGNUM_LENGTH (y))); - while (scan_y < end_y) - { - difference = (((*scan_x++) - (*scan_y++)) - borrow); - if (difference < 0) - { - (*scan_r++) = (difference + BIGNUM_RADIX); - borrow = 1; - } - else - { - (*scan_r++) = difference; - borrow = 0; - } - } - } - { - bignum_digit_type * end_x = ((BIGNUM_START_PTR (x)) + x_length); - if (borrow != 0) - while (scan_x < end_x) - { - difference = ((*scan_x++) - borrow); - if (difference < 0) - (*scan_r++) = (difference + BIGNUM_RADIX); - else - { - (*scan_r++) = difference; - borrow = 0; - break; - } - } - BIGNUM_ASSERT (borrow == 0); - while (scan_x < end_x) - (*scan_r++) = (*scan_x++); - } - return (bignum_trim (r)); - } + bignum_digit_type difference; + bignum_digit_type borrow = 0; + bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); + bignum_digit_type * scan_r = (BIGNUM_START_PTR (r)); + { + bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); + bignum_digit_type * end_y = (scan_y + (BIGNUM_LENGTH (y))); + while (scan_y < end_y) + { + difference = (((*scan_x++) - (*scan_y++)) - borrow); + if (difference < 0) + { + (*scan_r++) = (difference + BIGNUM_RADIX); + borrow = 1; + } + else + { + (*scan_r++) = difference; + borrow = 0; + } + } + } + { + bignum_digit_type * end_x = ((BIGNUM_START_PTR (x)) + x_length); + if (borrow != 0) + while (scan_x < end_x) + { + difference = ((*scan_x++) - borrow); + if (difference < 0) + (*scan_r++) = (difference + BIGNUM_RADIX); + else + { + (*scan_r++) = difference; + borrow = 0; + break; + } + } + BIGNUM_ASSERT (borrow == 0); + while (scan_x < end_x) + (*scan_r++) = (*scan_x++); + } + return (bignum_trim (r)); + } } /* Multiplication Maximum value for product_low or product_high: - ((R * R) + (R * (R - 2)) + (R - 1)) + ((R * R) + (R * (R - 2)) + (R - 1)) Maximum value for carry: ((R * (R - 1)) + (R - 1)) - where R == BIGNUM_RADIX_ROOT */ + where R == BIGNUM_RADIX_ROOT */ /* allocates memory */ bignum *factorvm::bignum_multiply_unsigned(bignum * x, bignum * y, int negative_p) { GC_BIGNUM(x,this); GC_BIGNUM(y,this); - if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) - { - bignum * z = x; - x = y; - y = z; - } - { - bignum_digit_type carry; - bignum_digit_type y_digit_low; - bignum_digit_type y_digit_high; - bignum_digit_type x_digit_low; - bignum_digit_type x_digit_high; - bignum_digit_type product_low; - bignum_digit_type * scan_r; - bignum_digit_type * scan_y; - bignum_length_type x_length = (BIGNUM_LENGTH (x)); - bignum_length_type y_length = (BIGNUM_LENGTH (y)); + if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) + { + bignum * z = x; + x = y; + y = z; + } + { + bignum_digit_type carry; + bignum_digit_type y_digit_low; + bignum_digit_type y_digit_high; + bignum_digit_type x_digit_low; + bignum_digit_type x_digit_high; + bignum_digit_type product_low; + bignum_digit_type * scan_r; + bignum_digit_type * scan_y; + bignum_length_type x_length = (BIGNUM_LENGTH (x)); + bignum_length_type y_length = (BIGNUM_LENGTH (y)); - bignum * r = - (allot_bignum_zeroed ((x_length + y_length), negative_p)); + bignum * r = + (allot_bignum_zeroed ((x_length + y_length), negative_p)); - bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); - bignum_digit_type * end_x = (scan_x + x_length); - bignum_digit_type * start_y = (BIGNUM_START_PTR (y)); - bignum_digit_type * end_y = (start_y + y_length); - bignum_digit_type * start_r = (BIGNUM_START_PTR (r)); + bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); + bignum_digit_type * end_x = (scan_x + x_length); + bignum_digit_type * start_y = (BIGNUM_START_PTR (y)); + bignum_digit_type * end_y = (start_y + y_length); + bignum_digit_type * start_r = (BIGNUM_START_PTR (r)); #define x_digit x_digit_high #define y_digit y_digit_high #define product_high carry - while (scan_x < end_x) - { - x_digit = (*scan_x++); - x_digit_low = (HD_LOW (x_digit)); - x_digit_high = (HD_HIGH (x_digit)); - carry = 0; - scan_y = start_y; - scan_r = (start_r++); - while (scan_y < end_y) - { - y_digit = (*scan_y++); - y_digit_low = (HD_LOW (y_digit)); - y_digit_high = (HD_HIGH (y_digit)); - product_low = - ((*scan_r) + - (x_digit_low * y_digit_low) + - (HD_LOW (carry))); - product_high = - ((x_digit_high * y_digit_low) + - (x_digit_low * y_digit_high) + - (HD_HIGH (product_low)) + - (HD_HIGH (carry))); - (*scan_r++) = - (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); - carry = - ((x_digit_high * y_digit_high) + - (HD_HIGH (product_high))); - } - (*scan_r) += carry; - } - return (bignum_trim (r)); + while (scan_x < end_x) + { + x_digit = (*scan_x++); + x_digit_low = (HD_LOW (x_digit)); + x_digit_high = (HD_HIGH (x_digit)); + carry = 0; + scan_y = start_y; + scan_r = (start_r++); + while (scan_y < end_y) + { + y_digit = (*scan_y++); + y_digit_low = (HD_LOW (y_digit)); + y_digit_high = (HD_HIGH (y_digit)); + product_low = + ((*scan_r) + + (x_digit_low * y_digit_low) + + (HD_LOW (carry))); + product_high = + ((x_digit_high * y_digit_low) + + (x_digit_low * y_digit_high) + + (HD_HIGH (product_low)) + + (HD_HIGH (carry))); + (*scan_r++) = + (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); + carry = + ((x_digit_high * y_digit_high) + + (HD_HIGH (product_high))); + } + (*scan_r) += carry; + } + return (bignum_trim (r)); #undef x_digit #undef y_digit #undef product_high - } + } } @@ -726,67 +726,67 @@ bignum *factorvm::bignum_multiply_unsigned_small_factor(bignum * x, bignum_digit { GC_BIGNUM(x,this); - bignum_length_type length_x = (BIGNUM_LENGTH (x)); + bignum_length_type length_x = (BIGNUM_LENGTH (x)); - bignum * p = (allot_bignum ((length_x + 1), negative_p)); + bignum * p = (allot_bignum ((length_x + 1), negative_p)); - bignum_destructive_copy (x, p); - (BIGNUM_REF (p, length_x)) = 0; - bignum_destructive_scale_up (p, y); - return (bignum_trim (p)); + bignum_destructive_copy (x, p); + (BIGNUM_REF (p, length_x)) = 0; + bignum_destructive_scale_up (p, y); + return (bignum_trim (p)); } void factorvm::bignum_destructive_add(bignum * bignum, bignum_digit_type n) { - bignum_digit_type * scan = (BIGNUM_START_PTR (bignum)); - bignum_digit_type digit; - digit = ((*scan) + n); - if (digit < BIGNUM_RADIX) - { - (*scan) = digit; - return; - } - (*scan++) = (digit - BIGNUM_RADIX); - while (1) - { - digit = ((*scan) + 1); - if (digit < BIGNUM_RADIX) - { - (*scan) = digit; - return; - } - (*scan++) = (digit - BIGNUM_RADIX); - } + bignum_digit_type * scan = (BIGNUM_START_PTR (bignum)); + bignum_digit_type digit; + digit = ((*scan) + n); + if (digit < BIGNUM_RADIX) + { + (*scan) = digit; + return; + } + (*scan++) = (digit - BIGNUM_RADIX); + while (1) + { + digit = ((*scan) + 1); + if (digit < BIGNUM_RADIX) + { + (*scan) = digit; + return; + } + (*scan++) = (digit - BIGNUM_RADIX); + } } void factorvm::bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) { - bignum_digit_type carry = 0; - bignum_digit_type * scan = (BIGNUM_START_PTR (bignum)); - bignum_digit_type two_digits; - bignum_digit_type product_low; + bignum_digit_type carry = 0; + bignum_digit_type * scan = (BIGNUM_START_PTR (bignum)); + bignum_digit_type two_digits; + bignum_digit_type product_low; #define product_high carry - bignum_digit_type * end = (scan + (BIGNUM_LENGTH (bignum))); - BIGNUM_ASSERT ((factor > 1) && (factor < BIGNUM_RADIX_ROOT)); - while (scan < end) - { - two_digits = (*scan); - product_low = ((factor * (HD_LOW (two_digits))) + (HD_LOW (carry))); - product_high = - ((factor * (HD_HIGH (two_digits))) + - (HD_HIGH (product_low)) + - (HD_HIGH (carry))); - (*scan++) = (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); - carry = (HD_HIGH (product_high)); - } - /* A carry here would be an overflow, i.e. it would not fit. - Hopefully the callers allocate enough space that this will - never happen. - */ - BIGNUM_ASSERT (carry == 0); - return; + bignum_digit_type * end = (scan + (BIGNUM_LENGTH (bignum))); + BIGNUM_ASSERT ((factor > 1) && (factor < BIGNUM_RADIX_ROOT)); + while (scan < end) + { + two_digits = (*scan); + product_low = ((factor * (HD_LOW (two_digits))) + (HD_LOW (carry))); + product_high = + ((factor * (HD_HIGH (two_digits))) + + (HD_HIGH (product_low)) + + (HD_HIGH (carry))); + (*scan++) = (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); + carry = (HD_HIGH (product_high)); + } + /* A carry here would be an overflow, i.e. it would not fit. + Hopefully the callers allocate enough space that this will + never happen. + */ + BIGNUM_ASSERT (carry == 0); + return; #undef product_high } @@ -803,129 +803,129 @@ void factorvm::bignum_divide_unsigned_large_denominator(bignum * numerator, bign { GC_BIGNUM(numerator,this); GC_BIGNUM(denominator,this); - bignum_length_type length_n = ((BIGNUM_LENGTH (numerator)) + 1); - bignum_length_type length_d = (BIGNUM_LENGTH (denominator)); + bignum_length_type length_n = ((BIGNUM_LENGTH (numerator)) + 1); + bignum_length_type length_d = (BIGNUM_LENGTH (denominator)); - bignum * q = - ((quotient != ((bignum * *) 0)) - ? (allot_bignum ((length_n - length_d), q_negative_p)) - : BIGNUM_OUT_OF_BAND); - GC_BIGNUM(q,this); + bignum * q = + ((quotient != ((bignum * *) 0)) + ? (allot_bignum ((length_n - length_d), q_negative_p)) + : BIGNUM_OUT_OF_BAND); + GC_BIGNUM(q,this); - bignum * u = (allot_bignum (length_n, r_negative_p)); - GC_BIGNUM(u,this); + bignum * u = (allot_bignum (length_n, r_negative_p)); + GC_BIGNUM(u,this); - int shift = 0; - BIGNUM_ASSERT (length_d > 1); - { - bignum_digit_type v1 = (BIGNUM_REF ((denominator), (length_d - 1))); - while (v1 < (BIGNUM_RADIX / 2)) - { - v1 <<= 1; - shift += 1; - } - } - if (shift == 0) - { - bignum_destructive_copy (numerator, u); - (BIGNUM_REF (u, (length_n - 1))) = 0; - bignum_divide_unsigned_normalized (u, denominator, q); - } - else - { - bignum * v = (allot_bignum (length_d, 0)); + int shift = 0; + BIGNUM_ASSERT (length_d > 1); + { + bignum_digit_type v1 = (BIGNUM_REF ((denominator), (length_d - 1))); + while (v1 < (BIGNUM_RADIX / 2)) + { + v1 <<= 1; + shift += 1; + } + } + if (shift == 0) + { + bignum_destructive_copy (numerator, u); + (BIGNUM_REF (u, (length_n - 1))) = 0; + bignum_divide_unsigned_normalized (u, denominator, q); + } + else + { + bignum * v = (allot_bignum (length_d, 0)); - bignum_destructive_normalization (numerator, u, shift); - bignum_destructive_normalization (denominator, v, shift); - bignum_divide_unsigned_normalized (u, v, q); - if (remainder != ((bignum * *) 0)) - bignum_destructive_unnormalization (u, shift); - } + bignum_destructive_normalization (numerator, u, shift); + bignum_destructive_normalization (denominator, v, shift); + bignum_divide_unsigned_normalized (u, v, q); + if (remainder != ((bignum * *) 0)) + bignum_destructive_unnormalization (u, shift); + } - if(q) - q = bignum_trim (q); + if(q) + q = bignum_trim (q); - u = bignum_trim (u); + u = bignum_trim (u); - if (quotient != ((bignum * *) 0)) - (*quotient) = q; + if (quotient != ((bignum * *) 0)) + (*quotient) = q; - if (remainder != ((bignum * *) 0)) - (*remainder) = u; + if (remainder != ((bignum * *) 0)) + (*remainder) = u; - return; + return; } void factorvm::bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum * q) { - bignum_length_type u_length = (BIGNUM_LENGTH (u)); - bignum_length_type v_length = (BIGNUM_LENGTH (v)); - bignum_digit_type * u_start = (BIGNUM_START_PTR (u)); - bignum_digit_type * u_scan = (u_start + u_length); - bignum_digit_type * u_scan_limit = (u_start + v_length); - bignum_digit_type * u_scan_start = (u_scan - v_length); - bignum_digit_type * v_start = (BIGNUM_START_PTR (v)); - bignum_digit_type * v_end = (v_start + v_length); - bignum_digit_type * q_scan = NULL; - bignum_digit_type v1 = (v_end[-1]); - bignum_digit_type v2 = (v_end[-2]); - bignum_digit_type ph; /* high half of double-digit product */ - bignum_digit_type pl; /* low half of double-digit product */ - bignum_digit_type guess; - bignum_digit_type gh; /* high half-digit of guess */ - bignum_digit_type ch; /* high half of double-digit comparand */ - bignum_digit_type v2l = (HD_LOW (v2)); - bignum_digit_type v2h = (HD_HIGH (v2)); - bignum_digit_type cl; /* low half of double-digit comparand */ + bignum_length_type u_length = (BIGNUM_LENGTH (u)); + bignum_length_type v_length = (BIGNUM_LENGTH (v)); + bignum_digit_type * u_start = (BIGNUM_START_PTR (u)); + bignum_digit_type * u_scan = (u_start + u_length); + bignum_digit_type * u_scan_limit = (u_start + v_length); + bignum_digit_type * u_scan_start = (u_scan - v_length); + bignum_digit_type * v_start = (BIGNUM_START_PTR (v)); + bignum_digit_type * v_end = (v_start + v_length); + bignum_digit_type * q_scan = NULL; + bignum_digit_type v1 = (v_end[-1]); + bignum_digit_type v2 = (v_end[-2]); + bignum_digit_type ph; /* high half of double-digit product */ + bignum_digit_type pl; /* low half of double-digit product */ + bignum_digit_type guess; + bignum_digit_type gh; /* high half-digit of guess */ + bignum_digit_type ch; /* high half of double-digit comparand */ + bignum_digit_type v2l = (HD_LOW (v2)); + bignum_digit_type v2h = (HD_HIGH (v2)); + bignum_digit_type cl; /* low half of double-digit comparand */ #define gl ph /* low half-digit of guess */ #define uj pl #define qj ph - bignum_digit_type gm; /* memory loc for reference parameter */ - if (q != BIGNUM_OUT_OF_BAND) - q_scan = ((BIGNUM_START_PTR (q)) + (BIGNUM_LENGTH (q))); - while (u_scan_limit < u_scan) - { - uj = (*--u_scan); - if (uj != v1) - { - /* comparand = - (((((uj * BIGNUM_RADIX) + uj1) % v1) * BIGNUM_RADIX) + uj2); - guess = (((uj * BIGNUM_RADIX) + uj1) / v1); */ - cl = (u_scan[-2]); - ch = (bignum_digit_divide (uj, (u_scan[-1]), v1, (&gm))); - guess = gm; - } - else - { - cl = (u_scan[-2]); - ch = ((u_scan[-1]) + v1); - guess = (BIGNUM_RADIX - 1); - } - while (1) - { - /* product = (guess * v2); */ - gl = (HD_LOW (guess)); - gh = (HD_HIGH (guess)); - pl = (v2l * gl); - ph = ((v2l * gh) + (v2h * gl) + (HD_HIGH (pl))); - pl = (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl)))); - ph = ((v2h * gh) + (HD_HIGH (ph))); - /* if (comparand >= product) */ - if ((ch > ph) || ((ch == ph) && (cl >= pl))) - break; - guess -= 1; - /* comparand += (v1 << BIGNUM_DIGIT_LENGTH) */ - ch += v1; - /* if (comparand >= (BIGNUM_RADIX * BIGNUM_RADIX)) */ - if (ch >= BIGNUM_RADIX) - break; - } - qj = (bignum_divide_subtract (v_start, v_end, guess, (--u_scan_start))); - if (q != BIGNUM_OUT_OF_BAND) - (*--q_scan) = qj; - } - return; + bignum_digit_type gm; /* memory loc for reference parameter */ + if (q != BIGNUM_OUT_OF_BAND) + q_scan = ((BIGNUM_START_PTR (q)) + (BIGNUM_LENGTH (q))); + while (u_scan_limit < u_scan) + { + uj = (*--u_scan); + if (uj != v1) + { + /* comparand = + (((((uj * BIGNUM_RADIX) + uj1) % v1) * BIGNUM_RADIX) + uj2); + guess = (((uj * BIGNUM_RADIX) + uj1) / v1); */ + cl = (u_scan[-2]); + ch = (bignum_digit_divide (uj, (u_scan[-1]), v1, (&gm))); + guess = gm; + } + else + { + cl = (u_scan[-2]); + ch = ((u_scan[-1]) + v1); + guess = (BIGNUM_RADIX - 1); + } + while (1) + { + /* product = (guess * v2); */ + gl = (HD_LOW (guess)); + gh = (HD_HIGH (guess)); + pl = (v2l * gl); + ph = ((v2l * gh) + (v2h * gl) + (HD_HIGH (pl))); + pl = (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl)))); + ph = ((v2h * gh) + (HD_HIGH (ph))); + /* if (comparand >= product) */ + if ((ch > ph) || ((ch == ph) && (cl >= pl))) + break; + guess -= 1; + /* comparand += (v1 << BIGNUM_DIGIT_LENGTH) */ + ch += v1; + /* if (comparand >= (BIGNUM_RADIX * BIGNUM_RADIX)) */ + if (ch >= BIGNUM_RADIX) + break; + } + qj = (bignum_divide_subtract (v_start, v_end, guess, (--u_scan_start))); + if (q != BIGNUM_OUT_OF_BAND) + (*--q_scan) = qj; + } + return; #undef gl #undef uj #undef qj @@ -934,77 +934,77 @@ void factorvm::bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum bignum_digit_type factorvm::bignum_divide_subtract(bignum_digit_type * v_start, bignum_digit_type * v_end, bignum_digit_type guess, bignum_digit_type * u_start) { - bignum_digit_type * v_scan = v_start; - bignum_digit_type * u_scan = u_start; - bignum_digit_type carry = 0; - if (guess == 0) return (0); - { - bignum_digit_type gl = (HD_LOW (guess)); - bignum_digit_type gh = (HD_HIGH (guess)); - bignum_digit_type v; - bignum_digit_type pl; - bignum_digit_type vl; + bignum_digit_type * v_scan = v_start; + bignum_digit_type * u_scan = u_start; + bignum_digit_type carry = 0; + if (guess == 0) return (0); + { + bignum_digit_type gl = (HD_LOW (guess)); + bignum_digit_type gh = (HD_HIGH (guess)); + bignum_digit_type v; + bignum_digit_type pl; + bignum_digit_type vl; #define vh v #define ph carry #define diff pl - while (v_scan < v_end) - { - v = (*v_scan++); - vl = (HD_LOW (v)); - vh = (HD_HIGH (v)); - pl = ((vl * gl) + (HD_LOW (carry))); - ph = ((vl * gh) + (vh * gl) + (HD_HIGH (pl)) + (HD_HIGH (carry))); - diff = ((*u_scan) - (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl))))); - if (diff < 0) - { - (*u_scan++) = (diff + BIGNUM_RADIX); - carry = ((vh * gh) + (HD_HIGH (ph)) + 1); - } - else - { - (*u_scan++) = diff; - carry = ((vh * gh) + (HD_HIGH (ph))); - } - } - if (carry == 0) - return (guess); - diff = ((*u_scan) - carry); - if (diff < 0) - (*u_scan) = (diff + BIGNUM_RADIX); - else - { - (*u_scan) = diff; - return (guess); - } + while (v_scan < v_end) + { + v = (*v_scan++); + vl = (HD_LOW (v)); + vh = (HD_HIGH (v)); + pl = ((vl * gl) + (HD_LOW (carry))); + ph = ((vl * gh) + (vh * gl) + (HD_HIGH (pl)) + (HD_HIGH (carry))); + diff = ((*u_scan) - (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl))))); + if (diff < 0) + { + (*u_scan++) = (diff + BIGNUM_RADIX); + carry = ((vh * gh) + (HD_HIGH (ph)) + 1); + } + else + { + (*u_scan++) = diff; + carry = ((vh * gh) + (HD_HIGH (ph))); + } + } + if (carry == 0) + return (guess); + diff = ((*u_scan) - carry); + if (diff < 0) + (*u_scan) = (diff + BIGNUM_RADIX); + else + { + (*u_scan) = diff; + return (guess); + } #undef vh #undef ph #undef diff - } - /* Subtraction generated carry, implying guess is one too large. - Add v back in to bring it back down. */ - v_scan = v_start; - u_scan = u_start; - carry = 0; - while (v_scan < v_end) - { - bignum_digit_type sum = ((*v_scan++) + (*u_scan) + carry); - if (sum < BIGNUM_RADIX) - { - (*u_scan++) = sum; - carry = 0; - } - else - { - (*u_scan++) = (sum - BIGNUM_RADIX); - carry = 1; - } - } - if (carry == 1) - { - bignum_digit_type sum = ((*u_scan) + carry); - (*u_scan) = ((sum < BIGNUM_RADIX) ? sum : (sum - BIGNUM_RADIX)); - } - return (guess - 1); + } + /* Subtraction generated carry, implying guess is one too large. + Add v back in to bring it back down. */ + v_scan = v_start; + u_scan = u_start; + carry = 0; + while (v_scan < v_end) + { + bignum_digit_type sum = ((*v_scan++) + (*u_scan) + carry); + if (sum < BIGNUM_RADIX) + { + (*u_scan++) = sum; + carry = 0; + } + else + { + (*u_scan++) = (sum - BIGNUM_RADIX); + carry = 1; + } + } + if (carry == 1) + { + bignum_digit_type sum = ((*u_scan) + carry); + (*u_scan) = ((sum < BIGNUM_RADIX) ? sum : (sum - BIGNUM_RADIX)); + } + return (guess - 1); } @@ -1013,101 +1013,101 @@ void factorvm::bignum_divide_unsigned_medium_denominator(bignum * numerator,bign { GC_BIGNUM(numerator,this); - bignum_length_type length_n = (BIGNUM_LENGTH (numerator)); - bignum_length_type length_q; - bignum * q = NULL; - GC_BIGNUM(q,this); + bignum_length_type length_n = (BIGNUM_LENGTH (numerator)); + bignum_length_type length_q; + bignum * q = NULL; + GC_BIGNUM(q,this); - int shift = 0; - /* Because `bignum_digit_divide' requires a normalized denominator. */ - while (denominator < (BIGNUM_RADIX / 2)) - { - denominator <<= 1; - shift += 1; - } - if (shift == 0) - { - length_q = length_n; + int shift = 0; + /* Because `bignum_digit_divide' requires a normalized denominator. */ + while (denominator < (BIGNUM_RADIX / 2)) + { + denominator <<= 1; + shift += 1; + } + if (shift == 0) + { + length_q = length_n; - q = (allot_bignum (length_q, q_negative_p)); - bignum_destructive_copy (numerator, q); - } - else - { - length_q = (length_n + 1); + q = (allot_bignum (length_q, q_negative_p)); + bignum_destructive_copy (numerator, q); + } + else + { + length_q = (length_n + 1); - q = (allot_bignum (length_q, q_negative_p)); - bignum_destructive_normalization (numerator, q, shift); - } - { - bignum_digit_type r = 0; - bignum_digit_type * start = (BIGNUM_START_PTR (q)); - bignum_digit_type * scan = (start + length_q); - bignum_digit_type qj; + q = (allot_bignum (length_q, q_negative_p)); + bignum_destructive_normalization (numerator, q, shift); + } + { + bignum_digit_type r = 0; + bignum_digit_type * start = (BIGNUM_START_PTR (q)); + bignum_digit_type * scan = (start + length_q); + bignum_digit_type qj; - while (start < scan) - { - r = (bignum_digit_divide (r, (*--scan), denominator, (&qj))); - (*scan) = qj; - } + while (start < scan) + { + r = (bignum_digit_divide (r, (*--scan), denominator, (&qj))); + (*scan) = qj; + } - q = bignum_trim (q); + q = bignum_trim (q); - if (remainder != ((bignum * *) 0)) - { - if (shift != 0) - r >>= shift; + if (remainder != ((bignum * *) 0)) + { + if (shift != 0) + r >>= shift; - (*remainder) = (bignum_digit_to_bignum (r, r_negative_p)); - } + (*remainder) = (bignum_digit_to_bignum (r, r_negative_p)); + } - if (quotient != ((bignum * *) 0)) - (*quotient) = q; - } - return; + if (quotient != ((bignum * *) 0)) + (*quotient) = q; + } + return; } void factorvm::bignum_destructive_normalization(bignum * source, bignum * target, int shift_left) { - bignum_digit_type digit; - bignum_digit_type * scan_source = (BIGNUM_START_PTR (source)); - bignum_digit_type carry = 0; - bignum_digit_type * scan_target = (BIGNUM_START_PTR (target)); - bignum_digit_type * end_source = (scan_source + (BIGNUM_LENGTH (source))); - bignum_digit_type * end_target = (scan_target + (BIGNUM_LENGTH (target))); - int shift_right = (BIGNUM_DIGIT_LENGTH - shift_left); - bignum_digit_type mask = (((cell)1 << shift_right) - 1); - while (scan_source < end_source) - { - digit = (*scan_source++); - (*scan_target++) = (((digit & mask) << shift_left) | carry); - carry = (digit >> shift_right); - } - if (scan_target < end_target) - (*scan_target) = carry; - else - BIGNUM_ASSERT (carry == 0); - return; + bignum_digit_type digit; + bignum_digit_type * scan_source = (BIGNUM_START_PTR (source)); + bignum_digit_type carry = 0; + bignum_digit_type * scan_target = (BIGNUM_START_PTR (target)); + bignum_digit_type * end_source = (scan_source + (BIGNUM_LENGTH (source))); + bignum_digit_type * end_target = (scan_target + (BIGNUM_LENGTH (target))); + int shift_right = (BIGNUM_DIGIT_LENGTH - shift_left); + bignum_digit_type mask = (((cell)1 << shift_right) - 1); + while (scan_source < end_source) + { + digit = (*scan_source++); + (*scan_target++) = (((digit & mask) << shift_left) | carry); + carry = (digit >> shift_right); + } + if (scan_target < end_target) + (*scan_target) = carry; + else + BIGNUM_ASSERT (carry == 0); + return; } void factorvm::bignum_destructive_unnormalization(bignum * bignum, int shift_right) { - bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); - bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); - bignum_digit_type digit; - bignum_digit_type carry = 0; - int shift_left = (BIGNUM_DIGIT_LENGTH - shift_right); - bignum_digit_type mask = (((fixnum)1 << shift_right) - 1); - while (start < scan) - { - digit = (*--scan); - (*scan) = ((digit >> shift_right) | carry); - carry = ((digit & mask) << shift_left); - } - BIGNUM_ASSERT (carry == 0); - return; + bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); + bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); + bignum_digit_type digit; + bignum_digit_type carry = 0; + int shift_left = (BIGNUM_DIGIT_LENGTH - shift_right); + bignum_digit_type mask = (((fixnum)1 << shift_right) - 1); + while (start < scan) + { + digit = (*--scan); + (*scan) = ((digit >> shift_right) | carry); + carry = ((digit & mask) << shift_left); + } + BIGNUM_ASSERT (carry == 0); + return; } @@ -1115,128 +1115,128 @@ void factorvm::bignum_destructive_unnormalization(bignum * bignum, int shift_rig case of dividing two bignum digits by one bignum digit. It is assumed that the numerator, denominator are normalized. */ -#define BDD_STEP(qn, j) \ -{ \ - uj = (u[j]); \ - if (uj != v1) \ - { \ - uj_uj1 = (HD_CONS (uj, (u[j + 1]))); \ - guess = (uj_uj1 / v1); \ - comparand = (HD_CONS ((uj_uj1 % v1), (u[j + 2]))); \ - } \ - else \ - { \ - guess = (BIGNUM_RADIX_ROOT - 1); \ - comparand = (HD_CONS (((u[j + 1]) + v1), (u[j + 2]))); \ - } \ - while ((guess * v2) > comparand) \ - { \ - guess -= 1; \ - comparand += (v1 << BIGNUM_HALF_DIGIT_LENGTH); \ - if (comparand >= BIGNUM_RADIX) \ - break; \ - } \ - qn = (bignum_digit_divide_subtract (v1, v2, guess, (&u[j]))); \ +#define BDD_STEP(qn, j) \ +{ \ + uj = (u[j]); \ + if (uj != v1) \ + { \ + uj_uj1 = (HD_CONS (uj, (u[j + 1]))); \ + guess = (uj_uj1 / v1); \ + comparand = (HD_CONS ((uj_uj1 % v1), (u[j + 2]))); \ + } \ + else \ + { \ + guess = (BIGNUM_RADIX_ROOT - 1); \ + comparand = (HD_CONS (((u[j + 1]) + v1), (u[j + 2]))); \ + } \ + while ((guess * v2) > comparand) \ + { \ + guess -= 1; \ + comparand += (v1 << BIGNUM_HALF_DIGIT_LENGTH); \ + if (comparand >= BIGNUM_RADIX) \ + break; \ + } \ + qn = (bignum_digit_divide_subtract (v1, v2, guess, (&u[j]))); \ } bignum_digit_type factorvm::bignum_digit_divide(bignum_digit_type uh, bignum_digit_type ul, bignum_digit_type v, bignum_digit_type * q) /* return value */ { - bignum_digit_type guess; - bignum_digit_type comparand; - bignum_digit_type v1 = (HD_HIGH (v)); - bignum_digit_type v2 = (HD_LOW (v)); - bignum_digit_type uj; - bignum_digit_type uj_uj1; - bignum_digit_type q1; - bignum_digit_type q2; - bignum_digit_type u [4]; - if (uh == 0) - { - if (ul < v) - { - (*q) = 0; - return (ul); - } - else if (ul == v) - { - (*q) = 1; - return (0); - } - } - (u[0]) = (HD_HIGH (uh)); - (u[1]) = (HD_LOW (uh)); - (u[2]) = (HD_HIGH (ul)); - (u[3]) = (HD_LOW (ul)); - v1 = (HD_HIGH (v)); - v2 = (HD_LOW (v)); - BDD_STEP (q1, 0); - BDD_STEP (q2, 1); - (*q) = (HD_CONS (q1, q2)); - return (HD_CONS ((u[2]), (u[3]))); + bignum_digit_type guess; + bignum_digit_type comparand; + bignum_digit_type v1 = (HD_HIGH (v)); + bignum_digit_type v2 = (HD_LOW (v)); + bignum_digit_type uj; + bignum_digit_type uj_uj1; + bignum_digit_type q1; + bignum_digit_type q2; + bignum_digit_type u [4]; + if (uh == 0) + { + if (ul < v) + { + (*q) = 0; + return (ul); + } + else if (ul == v) + { + (*q) = 1; + return (0); + } + } + (u[0]) = (HD_HIGH (uh)); + (u[1]) = (HD_LOW (uh)); + (u[2]) = (HD_HIGH (ul)); + (u[3]) = (HD_LOW (ul)); + v1 = (HD_HIGH (v)); + v2 = (HD_LOW (v)); + BDD_STEP (q1, 0); + BDD_STEP (q2, 1); + (*q) = (HD_CONS (q1, q2)); + return (HD_CONS ((u[2]), (u[3]))); } #undef BDD_STEP -#define BDDS_MULSUB(vn, un, carry_in) \ -{ \ - product = ((vn * guess) + carry_in); \ - diff = (un - (HD_LOW (product))); \ - if (diff < 0) \ - { \ - un = (diff + BIGNUM_RADIX_ROOT); \ - carry = ((HD_HIGH (product)) + 1); \ - } \ - else \ - { \ - un = diff; \ - carry = (HD_HIGH (product)); \ - } \ +#define BDDS_MULSUB(vn, un, carry_in) \ +{ \ + product = ((vn * guess) + carry_in); \ + diff = (un - (HD_LOW (product))); \ + if (diff < 0) \ + { \ + un = (diff + BIGNUM_RADIX_ROOT); \ + carry = ((HD_HIGH (product)) + 1); \ + } \ + else \ + { \ + un = diff; \ + carry = (HD_HIGH (product)); \ + } \ } -#define BDDS_ADD(vn, un, carry_in) \ -{ \ - sum = (vn + un + carry_in); \ - if (sum < BIGNUM_RADIX_ROOT) \ - { \ - un = sum; \ - carry = 0; \ - } \ - else \ - { \ - un = (sum - BIGNUM_RADIX_ROOT); \ - carry = 1; \ - } \ +#define BDDS_ADD(vn, un, carry_in) \ +{ \ + sum = (vn + un + carry_in); \ + if (sum < BIGNUM_RADIX_ROOT) \ + { \ + un = sum; \ + carry = 0; \ + } \ + else \ + { \ + un = (sum - BIGNUM_RADIX_ROOT); \ + carry = 1; \ + } \ } bignum_digit_type factorvm::bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, bignum_digit_type guess, bignum_digit_type * u) { - { - bignum_digit_type product; - bignum_digit_type diff; - bignum_digit_type carry; - BDDS_MULSUB (v2, (u[2]), 0); - BDDS_MULSUB (v1, (u[1]), carry); - if (carry == 0) - return (guess); - diff = ((u[0]) - carry); - if (diff < 0) - (u[0]) = (diff + BIGNUM_RADIX); - else - { - (u[0]) = diff; - return (guess); - } - } - { - bignum_digit_type sum; - bignum_digit_type carry; - BDDS_ADD(v2, (u[2]), 0); - BDDS_ADD(v1, (u[1]), carry); - if (carry == 1) - (u[0]) += 1; - } - return (guess - 1); + { + bignum_digit_type product; + bignum_digit_type diff; + bignum_digit_type carry; + BDDS_MULSUB (v2, (u[2]), 0); + BDDS_MULSUB (v1, (u[1]), carry); + if (carry == 0) + return (guess); + diff = ((u[0]) - carry); + if (diff < 0) + (u[0]) = (diff + BIGNUM_RADIX); + else + { + (u[0]) = diff; + return (guess); + } + } + { + bignum_digit_type sum; + bignum_digit_type carry; + BDDS_ADD(v2, (u[2]), 0); + BDDS_ADD(v1, (u[1]), carry); + if (carry == 1) + (u[0]) += 1; + } + return (guess - 1); } @@ -1248,19 +1248,19 @@ void factorvm::bignum_divide_unsigned_small_denominator(bignum * numerator, bign { GC_BIGNUM(numerator,this); - bignum * q = (bignum_new_sign (numerator, q_negative_p)); - GC_BIGNUM(q,this); + bignum * q = (bignum_new_sign (numerator, q_negative_p)); + GC_BIGNUM(q,this); - bignum_digit_type r = (bignum_destructive_scale_down (q, denominator)); + bignum_digit_type r = (bignum_destructive_scale_down (q, denominator)); - q = (bignum_trim (q)); + q = (bignum_trim (q)); - if (remainder != ((bignum * *) 0)) - (*remainder) = (bignum_digit_to_bignum (r, r_negative_p)); + if (remainder != ((bignum * *) 0)) + (*remainder) = (bignum_digit_to_bignum (r, r_negative_p)); - (*quotient) = q; + (*quotient) = q; - return; + return; } @@ -1270,23 +1270,23 @@ void factorvm::bignum_divide_unsigned_small_denominator(bignum * numerator, bign bignum_digit_type factorvm::bignum_destructive_scale_down(bignum * bignum, bignum_digit_type denominator) { - bignum_digit_type numerator; - bignum_digit_type remainder = 0; - bignum_digit_type two_digits; + bignum_digit_type numerator; + bignum_digit_type remainder = 0; + bignum_digit_type two_digits; #define quotient_high remainder - bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); - bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); - BIGNUM_ASSERT ((denominator > 1) && (denominator < BIGNUM_RADIX_ROOT)); - while (start < scan) - { - two_digits = (*--scan); - numerator = (HD_CONS (remainder, (HD_HIGH (two_digits)))); - quotient_high = (numerator / denominator); - numerator = (HD_CONS ((numerator % denominator), (HD_LOW (two_digits)))); - (*scan) = (HD_CONS (quotient_high, (numerator / denominator))); - remainder = (numerator % denominator); - } - return (remainder); + bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); + bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); + BIGNUM_ASSERT ((denominator > 1) && (denominator < BIGNUM_RADIX_ROOT)); + while (start < scan) + { + two_digits = (*--scan); + numerator = (HD_CONS (remainder, (HD_HIGH (two_digits)))); + quotient_high = (numerator / denominator); + numerator = (HD_CONS ((numerator % denominator), (HD_LOW (two_digits)))); + (*scan) = (HD_CONS (quotient_high, (numerator / denominator))); + remainder = (numerator % denominator); + } + return (remainder); #undef quotient_high } @@ -1294,92 +1294,92 @@ bignum_digit_type factorvm::bignum_destructive_scale_down(bignum * bignum, bignu /* allocates memory */ bignum * factorvm::bignum_remainder_unsigned_small_denominator(bignum * n, bignum_digit_type d, int negative_p) { - bignum_digit_type two_digits; - bignum_digit_type * start = (BIGNUM_START_PTR (n)); - bignum_digit_type * scan = (start + (BIGNUM_LENGTH (n))); - bignum_digit_type r = 0; - BIGNUM_ASSERT ((d > 1) && (d < BIGNUM_RADIX_ROOT)); - while (start < scan) - { - two_digits = (*--scan); - r = - ((HD_CONS (((HD_CONS (r, (HD_HIGH (two_digits)))) % d), - (HD_LOW (two_digits)))) - % d); - } - return (bignum_digit_to_bignum (r, negative_p)); + bignum_digit_type two_digits; + bignum_digit_type * start = (BIGNUM_START_PTR (n)); + bignum_digit_type * scan = (start + (BIGNUM_LENGTH (n))); + bignum_digit_type r = 0; + BIGNUM_ASSERT ((d > 1) && (d < BIGNUM_RADIX_ROOT)); + while (start < scan) + { + two_digits = (*--scan); + r = + ((HD_CONS (((HD_CONS (r, (HD_HIGH (two_digits)))) % d), + (HD_LOW (two_digits)))) + % d); + } + return (bignum_digit_to_bignum (r, negative_p)); } /* allocates memory */ bignum *factorvm::bignum_digit_to_bignum(bignum_digit_type digit, int negative_p) { - if (digit == 0) - return (BIGNUM_ZERO ()); - else - { - bignum * result = (allot_bignum (1, negative_p)); - (BIGNUM_REF (result, 0)) = digit; - return (result); - } + if (digit == 0) + return (BIGNUM_ZERO ()); + else + { + bignum * result = (allot_bignum (1, negative_p)); + (BIGNUM_REF (result, 0)) = digit; + return (result); + } } /* allocates memory */ bignum *factorvm::allot_bignum(bignum_length_type length, int negative_p) { - BIGNUM_ASSERT ((length >= 0) || (length < BIGNUM_RADIX)); - bignum * result = allot_array_internal(length + 1); - BIGNUM_SET_NEGATIVE_P (result, negative_p); - return (result); + BIGNUM_ASSERT ((length >= 0) || (length < BIGNUM_RADIX)); + bignum * result = allot_array_internal(length + 1); + BIGNUM_SET_NEGATIVE_P (result, negative_p); + return (result); } /* allocates memory */ bignum * factorvm::allot_bignum_zeroed(bignum_length_type length, int negative_p) { - bignum * result = allot_bignum(length,negative_p); - bignum_digit_type * scan = (BIGNUM_START_PTR (result)); - bignum_digit_type * end = (scan + length); - while (scan < end) - (*scan++) = 0; - return (result); + bignum * result = allot_bignum(length,negative_p); + bignum_digit_type * scan = (BIGNUM_START_PTR (result)); + bignum_digit_type * end = (scan + length); + while (scan < end) + (*scan++) = 0; + return (result); } -#define BIGNUM_REDUCE_LENGTH(source, length) \ - source = reallot_array(source,length + 1) +#define BIGNUM_REDUCE_LENGTH(source, length) \ +source = reallot_array(source,length + 1) /* allocates memory */ bignum *factorvm::bignum_shorten_length(bignum * bignum, bignum_length_type length) { - bignum_length_type current_length = (BIGNUM_LENGTH (bignum)); - BIGNUM_ASSERT ((length >= 0) || (length <= current_length)); - if (length < current_length) - { - BIGNUM_REDUCE_LENGTH (bignum, length); - BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); - } - return (bignum); + bignum_length_type current_length = (BIGNUM_LENGTH (bignum)); + BIGNUM_ASSERT ((length >= 0) || (length <= current_length)); + if (length < current_length) + { + BIGNUM_REDUCE_LENGTH (bignum, length); + BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); + } + return (bignum); } /* allocates memory */ bignum *factorvm::bignum_trim(bignum * bignum) { - bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); - bignum_digit_type * end = (start + (BIGNUM_LENGTH (bignum))); - bignum_digit_type * scan = end; - while ((start <= scan) && ((*--scan) == 0)) - ; - scan += 1; - if (scan < end) - { - bignum_length_type length = (scan - start); - BIGNUM_REDUCE_LENGTH (bignum, length); - BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); - } - return (bignum); + bignum_digit_type * start = (BIGNUM_START_PTR (bignum)); + bignum_digit_type * end = (start + (BIGNUM_LENGTH (bignum))); + bignum_digit_type * scan = end; + while ((start <= scan) && ((*--scan) == 0)) + ; + scan += 1; + if (scan < end) + { + bignum_length_type length = (scan - start); + BIGNUM_REDUCE_LENGTH (bignum, length); + BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); + } + return (bignum); } @@ -1389,37 +1389,37 @@ bignum *factorvm::bignum_trim(bignum * bignum) bignum *factorvm::bignum_new_sign(bignum * x, int negative_p) { GC_BIGNUM(x,this); - bignum * result = (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); + bignum * result = (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); - bignum_destructive_copy (x, result); - return (result); + bignum_destructive_copy (x, result); + return (result); } /* allocates memory */ bignum *factorvm::bignum_maybe_new_sign(bignum * x, int negative_p) { - if ((BIGNUM_NEGATIVE_P (x)) ? negative_p : (! negative_p)) - return (x); - else - { - bignum * result = - (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); - bignum_destructive_copy (x, result); - return (result); - } + if ((BIGNUM_NEGATIVE_P (x)) ? negative_p : (! negative_p)) + return (x); + else + { + bignum * result = + (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); + bignum_destructive_copy (x, result); + return (result); + } } void factorvm::bignum_destructive_copy(bignum * source, bignum * target) { - bignum_digit_type * scan_source = (BIGNUM_START_PTR (source)); - bignum_digit_type * end_source = - (scan_source + (BIGNUM_LENGTH (source))); - bignum_digit_type * scan_target = (BIGNUM_START_PTR (target)); - while (scan_source < end_source) - (*scan_target++) = (*scan_source++); - return; + bignum_digit_type * scan_source = (BIGNUM_START_PTR (source)); + bignum_digit_type * end_source = + (scan_source + (BIGNUM_LENGTH (source))); + bignum_digit_type * scan_target = (BIGNUM_START_PTR (target)); + while (scan_source < end_source) + (*scan_target++) = (*scan_source++); + return; } @@ -1430,17 +1430,17 @@ void factorvm::bignum_destructive_copy(bignum * source, bignum * target) /* allocates memory */ bignum *factorvm::bignum_bitwise_not(bignum * x) { - return bignum_subtract(BIGNUM_ONE(1), x); + return bignum_subtract(BIGNUM_ONE(1), x); } /* allocates memory */ bignum *factorvm::bignum_arithmetic_shift(bignum * arg1, fixnum n) { - if (BIGNUM_NEGATIVE_P(arg1) && n < 0) - return bignum_bitwise_not(bignum_magnitude_ash(bignum_bitwise_not(arg1), n)); - else - return bignum_magnitude_ash(arg1, n); + if (BIGNUM_NEGATIVE_P(arg1) && n < 0) + return bignum_bitwise_not(bignum_magnitude_ash(bignum_bitwise_not(arg1), n)); + else + return bignum_magnitude_ash(arg1, n); } @@ -1451,45 +1451,45 @@ bignum *factorvm::bignum_arithmetic_shift(bignum * arg1, fixnum n) /* allocates memory */ bignum *factorvm::bignum_bitwise_and(bignum * arg1, bignum * arg2) { - return( - (BIGNUM_NEGATIVE_P (arg1)) - ? (BIGNUM_NEGATIVE_P (arg2)) + return( + (BIGNUM_NEGATIVE_P (arg1)) + ? (BIGNUM_NEGATIVE_P (arg2)) ? bignum_negneg_bitwise_op(AND_OP, arg1, arg2) : bignum_posneg_bitwise_op(AND_OP, arg2, arg1) - : (BIGNUM_NEGATIVE_P (arg2)) + : (BIGNUM_NEGATIVE_P (arg2)) ? bignum_posneg_bitwise_op(AND_OP, arg1, arg2) : bignum_pospos_bitwise_op(AND_OP, arg1, arg2) - ); + ); } /* allocates memory */ bignum *factorvm::bignum_bitwise_ior(bignum * arg1, bignum * arg2) { - return( - (BIGNUM_NEGATIVE_P (arg1)) - ? (BIGNUM_NEGATIVE_P (arg2)) + return( + (BIGNUM_NEGATIVE_P (arg1)) + ? (BIGNUM_NEGATIVE_P (arg2)) ? bignum_negneg_bitwise_op(IOR_OP, arg1, arg2) : bignum_posneg_bitwise_op(IOR_OP, arg2, arg1) - : (BIGNUM_NEGATIVE_P (arg2)) + : (BIGNUM_NEGATIVE_P (arg2)) ? bignum_posneg_bitwise_op(IOR_OP, arg1, arg2) : bignum_pospos_bitwise_op(IOR_OP, arg1, arg2) - ); + ); } /* allocates memory */ bignum *factorvm::bignum_bitwise_xor(bignum * arg1, bignum * arg2) { - return( - (BIGNUM_NEGATIVE_P (arg1)) - ? (BIGNUM_NEGATIVE_P (arg2)) + return( + (BIGNUM_NEGATIVE_P (arg1)) + ? (BIGNUM_NEGATIVE_P (arg2)) ? bignum_negneg_bitwise_op(XOR_OP, arg1, arg2) : bignum_posneg_bitwise_op(XOR_OP, arg2, arg1) - : (BIGNUM_NEGATIVE_P (arg2)) + : (BIGNUM_NEGATIVE_P (arg2)) ? bignum_posneg_bitwise_op(XOR_OP, arg1, arg2) : bignum_pospos_bitwise_op(XOR_OP, arg1, arg2) - ); + ); } @@ -1500,60 +1500,60 @@ bignum *factorvm::bignum_magnitude_ash(bignum * arg1, fixnum n) { GC_BIGNUM(arg1,this); - bignum * result = NULL; - bignum_digit_type *scan1; - bignum_digit_type *scanr; - bignum_digit_type *end; + bignum * result = NULL; + bignum_digit_type *scan1; + bignum_digit_type *scanr; + bignum_digit_type *end; - fixnum digit_offset,bit_offset; + fixnum digit_offset,bit_offset; - if (BIGNUM_ZERO_P (arg1)) return (arg1); + if (BIGNUM_ZERO_P (arg1)) return (arg1); - if (n > 0) { - digit_offset = n / BIGNUM_DIGIT_LENGTH; - bit_offset = n % BIGNUM_DIGIT_LENGTH; + if (n > 0) { + digit_offset = n / BIGNUM_DIGIT_LENGTH; + bit_offset = n % BIGNUM_DIGIT_LENGTH; - result = allot_bignum_zeroed (BIGNUM_LENGTH (arg1) + digit_offset + 1, - BIGNUM_NEGATIVE_P(arg1)); + result = allot_bignum_zeroed (BIGNUM_LENGTH (arg1) + digit_offset + 1, + BIGNUM_NEGATIVE_P(arg1)); - scanr = BIGNUM_START_PTR (result) + digit_offset; - scan1 = BIGNUM_START_PTR (arg1); - end = scan1 + BIGNUM_LENGTH (arg1); + scanr = BIGNUM_START_PTR (result) + digit_offset; + scan1 = BIGNUM_START_PTR (arg1); + end = scan1 + BIGNUM_LENGTH (arg1); - while (scan1 < end) { - *scanr = *scanr | (*scan1 & BIGNUM_DIGIT_MASK) << bit_offset; - *scanr = *scanr & BIGNUM_DIGIT_MASK; - scanr++; - *scanr = *scan1++ >> (BIGNUM_DIGIT_LENGTH - bit_offset); - *scanr = *scanr & BIGNUM_DIGIT_MASK; - } - } - else if (n < 0 - && (-n >= (BIGNUM_LENGTH (arg1) * (bignum_length_type) BIGNUM_DIGIT_LENGTH))) - result = BIGNUM_ZERO (); + while (scan1 < end) { + *scanr = *scanr | (*scan1 & BIGNUM_DIGIT_MASK) << bit_offset; + *scanr = *scanr & BIGNUM_DIGIT_MASK; + scanr++; + *scanr = *scan1++ >> (BIGNUM_DIGIT_LENGTH - bit_offset); + *scanr = *scanr & BIGNUM_DIGIT_MASK; + } + } + else if (n < 0 + && (-n >= (BIGNUM_LENGTH (arg1) * (bignum_length_type) BIGNUM_DIGIT_LENGTH))) + result = BIGNUM_ZERO (); - else if (n < 0) { - digit_offset = -n / BIGNUM_DIGIT_LENGTH; - bit_offset = -n % BIGNUM_DIGIT_LENGTH; + else if (n < 0) { + digit_offset = -n / BIGNUM_DIGIT_LENGTH; + bit_offset = -n % BIGNUM_DIGIT_LENGTH; - result = allot_bignum_zeroed (BIGNUM_LENGTH (arg1) - digit_offset, - BIGNUM_NEGATIVE_P(arg1)); + result = allot_bignum_zeroed (BIGNUM_LENGTH (arg1) - digit_offset, + BIGNUM_NEGATIVE_P(arg1)); - scanr = BIGNUM_START_PTR (result); - scan1 = BIGNUM_START_PTR (arg1) + digit_offset; - end = scanr + BIGNUM_LENGTH (result) - 1; + scanr = BIGNUM_START_PTR (result); + scan1 = BIGNUM_START_PTR (arg1) + digit_offset; + end = scanr + BIGNUM_LENGTH (result) - 1; - while (scanr < end) { - *scanr = (*scan1++ & BIGNUM_DIGIT_MASK) >> bit_offset ; - *scanr = (*scanr | - *scan1 << (BIGNUM_DIGIT_LENGTH - bit_offset)) & BIGNUM_DIGIT_MASK; - scanr++; - } - *scanr = (*scan1++ & BIGNUM_DIGIT_MASK) >> bit_offset ; - } - else if (n == 0) result = arg1; + while (scanr < end) { + *scanr = (*scan1++ & BIGNUM_DIGIT_MASK) >> bit_offset ; + *scanr = (*scanr | + *scan1 << (BIGNUM_DIGIT_LENGTH - bit_offset)) & BIGNUM_DIGIT_MASK; + scanr++; + } + *scanr = (*scan1++ & BIGNUM_DIGIT_MASK) >> bit_offset ; + } + else if (n == 0) result = arg1; - return (bignum_trim (result)); + return (bignum_trim (result)); } @@ -1562,33 +1562,33 @@ bignum *factorvm::bignum_pospos_bitwise_op(int op, bignum * arg1, bignum * arg2) { GC_BIGNUM(arg1,this); GC_BIGNUM(arg2,this); - bignum * result; - bignum_length_type max_length; + bignum * result; + bignum_length_type max_length; - bignum_digit_type *scan1, *end1, digit1; - bignum_digit_type *scan2, *end2, digit2; - bignum_digit_type *scanr, *endr; + bignum_digit_type *scan1, *end1, digit1; + bignum_digit_type *scan2, *end2, digit2; + bignum_digit_type *scanr, *endr; - max_length = (BIGNUM_LENGTH(arg1) > BIGNUM_LENGTH(arg2)) - ? BIGNUM_LENGTH(arg1) : BIGNUM_LENGTH(arg2); + max_length = (BIGNUM_LENGTH(arg1) > BIGNUM_LENGTH(arg2)) + ? BIGNUM_LENGTH(arg1) : BIGNUM_LENGTH(arg2); - result = allot_bignum(max_length, 0); + result = allot_bignum(max_length, 0); - scanr = BIGNUM_START_PTR(result); - scan1 = BIGNUM_START_PTR(arg1); - scan2 = BIGNUM_START_PTR(arg2); - endr = scanr + max_length; - end1 = scan1 + BIGNUM_LENGTH(arg1); - end2 = scan2 + BIGNUM_LENGTH(arg2); + scanr = BIGNUM_START_PTR(result); + scan1 = BIGNUM_START_PTR(arg1); + scan2 = BIGNUM_START_PTR(arg2); + endr = scanr + max_length; + end1 = scan1 + BIGNUM_LENGTH(arg1); + end2 = scan2 + BIGNUM_LENGTH(arg2); - while (scanr < endr) { - digit1 = (scan1 < end1) ? *scan1++ : 0; - digit2 = (scan2 < end2) ? *scan2++ : 0; - *scanr++ = (op == AND_OP) ? digit1 & digit2 : - (op == IOR_OP) ? digit1 | digit2 : - digit1 ^ digit2; - } - return bignum_trim(result); + while (scanr < endr) { + digit1 = (scan1 < end1) ? *scan1++ : 0; + digit2 = (scan2 < end2) ? *scan2++ : 0; + *scanr++ = (op == AND_OP) ? digit1 & digit2 : + (op == IOR_OP) ? digit1 | digit2 : + digit1 ^ digit2; + } + return bignum_trim(result); } @@ -1597,51 +1597,51 @@ bignum *factorvm::bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) { GC_BIGNUM(arg1,this); GC_BIGNUM(arg2,this); - bignum * result; - bignum_length_type max_length; + bignum * result; + bignum_length_type max_length; - bignum_digit_type *scan1, *end1, digit1; - bignum_digit_type *scan2, *end2, digit2, carry2; - bignum_digit_type *scanr, *endr; + bignum_digit_type *scan1, *end1, digit1; + bignum_digit_type *scan2, *end2, digit2, carry2; + bignum_digit_type *scanr, *endr; - char neg_p = op == IOR_OP || op == XOR_OP; + char neg_p = op == IOR_OP || op == XOR_OP; - max_length = (BIGNUM_LENGTH(arg1) > BIGNUM_LENGTH(arg2) + 1) - ? BIGNUM_LENGTH(arg1) : BIGNUM_LENGTH(arg2) + 1; + max_length = (BIGNUM_LENGTH(arg1) > BIGNUM_LENGTH(arg2) + 1) + ? BIGNUM_LENGTH(arg1) : BIGNUM_LENGTH(arg2) + 1; - result = allot_bignum(max_length, neg_p); + result = allot_bignum(max_length, neg_p); - scanr = BIGNUM_START_PTR(result); - scan1 = BIGNUM_START_PTR(arg1); - scan2 = BIGNUM_START_PTR(arg2); - endr = scanr + max_length; - end1 = scan1 + BIGNUM_LENGTH(arg1); - end2 = scan2 + BIGNUM_LENGTH(arg2); + scanr = BIGNUM_START_PTR(result); + scan1 = BIGNUM_START_PTR(arg1); + scan2 = BIGNUM_START_PTR(arg2); + endr = scanr + max_length; + end1 = scan1 + BIGNUM_LENGTH(arg1); + end2 = scan2 + BIGNUM_LENGTH(arg2); - carry2 = 1; + carry2 = 1; - while (scanr < endr) { - digit1 = (scan1 < end1) ? *scan1++ : 0; - digit2 = (~((scan2 < end2) ? *scan2++ : 0) & BIGNUM_DIGIT_MASK) - + carry2; + while (scanr < endr) { + digit1 = (scan1 < end1) ? *scan1++ : 0; + digit2 = (~((scan2 < end2) ? *scan2++ : 0) & BIGNUM_DIGIT_MASK) + + carry2; - if (digit2 < BIGNUM_RADIX) - carry2 = 0; - else - { - digit2 = (digit2 - BIGNUM_RADIX); - carry2 = 1; - } + if (digit2 < BIGNUM_RADIX) + carry2 = 0; + else + { + digit2 = (digit2 - BIGNUM_RADIX); + carry2 = 1; + } - *scanr++ = (op == AND_OP) ? digit1 & digit2 : - (op == IOR_OP) ? digit1 | digit2 : - digit1 ^ digit2; - } + *scanr++ = (op == AND_OP) ? digit1 & digit2 : + (op == IOR_OP) ? digit1 | digit2 : + digit1 ^ digit2; + } - if (neg_p) - bignum_negate_magnitude(result); + if (neg_p) + bignum_negate_magnitude(result); - return bignum_trim(result); + return bignum_trim(result); } @@ -1650,87 +1650,87 @@ bignum *factorvm::bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) { GC_BIGNUM(arg1,this); GC_BIGNUM(arg2,this); - bignum * result; - bignum_length_type max_length; + bignum * result; + bignum_length_type max_length; - bignum_digit_type *scan1, *end1, digit1, carry1; - bignum_digit_type *scan2, *end2, digit2, carry2; - bignum_digit_type *scanr, *endr; + bignum_digit_type *scan1, *end1, digit1, carry1; + bignum_digit_type *scan2, *end2, digit2, carry2; + bignum_digit_type *scanr, *endr; - char neg_p = op == AND_OP || op == IOR_OP; + char neg_p = op == AND_OP || op == IOR_OP; - max_length = (BIGNUM_LENGTH(arg1) > BIGNUM_LENGTH(arg2)) - ? BIGNUM_LENGTH(arg1) + 1 : BIGNUM_LENGTH(arg2) + 1; + max_length = (BIGNUM_LENGTH(arg1) > BIGNUM_LENGTH(arg2)) + ? BIGNUM_LENGTH(arg1) + 1 : BIGNUM_LENGTH(arg2) + 1; - result = allot_bignum(max_length, neg_p); + result = allot_bignum(max_length, neg_p); - scanr = BIGNUM_START_PTR(result); - scan1 = BIGNUM_START_PTR(arg1); - scan2 = BIGNUM_START_PTR(arg2); - endr = scanr + max_length; - end1 = scan1 + BIGNUM_LENGTH(arg1); - end2 = scan2 + BIGNUM_LENGTH(arg2); + scanr = BIGNUM_START_PTR(result); + scan1 = BIGNUM_START_PTR(arg1); + scan2 = BIGNUM_START_PTR(arg2); + endr = scanr + max_length; + end1 = scan1 + BIGNUM_LENGTH(arg1); + end2 = scan2 + BIGNUM_LENGTH(arg2); - carry1 = 1; - carry2 = 1; + carry1 = 1; + carry2 = 1; - while (scanr < endr) { - digit1 = (~((scan1 < end1) ? *scan1++ : 0) & BIGNUM_DIGIT_MASK) + carry1; - digit2 = (~((scan2 < end2) ? *scan2++ : 0) & BIGNUM_DIGIT_MASK) + carry2; + while (scanr < endr) { + digit1 = (~((scan1 < end1) ? *scan1++ : 0) & BIGNUM_DIGIT_MASK) + carry1; + digit2 = (~((scan2 < end2) ? *scan2++ : 0) & BIGNUM_DIGIT_MASK) + carry2; - if (digit1 < BIGNUM_RADIX) - carry1 = 0; - else - { - digit1 = (digit1 - BIGNUM_RADIX); - carry1 = 1; - } + if (digit1 < BIGNUM_RADIX) + carry1 = 0; + else + { + digit1 = (digit1 - BIGNUM_RADIX); + carry1 = 1; + } - if (digit2 < BIGNUM_RADIX) - carry2 = 0; - else - { - digit2 = (digit2 - BIGNUM_RADIX); - carry2 = 1; - } + if (digit2 < BIGNUM_RADIX) + carry2 = 0; + else + { + digit2 = (digit2 - BIGNUM_RADIX); + carry2 = 1; + } - *scanr++ = (op == AND_OP) ? digit1 & digit2 : - (op == IOR_OP) ? digit1 | digit2 : - digit1 ^ digit2; - } + *scanr++ = (op == AND_OP) ? digit1 & digit2 : + (op == IOR_OP) ? digit1 | digit2 : + digit1 ^ digit2; + } - if (neg_p) - bignum_negate_magnitude(result); + if (neg_p) + bignum_negate_magnitude(result); - return bignum_trim(result); + return bignum_trim(result); } void factorvm::bignum_negate_magnitude(bignum * arg) { - bignum_digit_type *scan; - bignum_digit_type *end; - bignum_digit_type digit; - bignum_digit_type carry; + bignum_digit_type *scan; + bignum_digit_type *end; + bignum_digit_type digit; + bignum_digit_type carry; - scan = BIGNUM_START_PTR(arg); - end = scan + BIGNUM_LENGTH(arg); + scan = BIGNUM_START_PTR(arg); + end = scan + BIGNUM_LENGTH(arg); - carry = 1; + carry = 1; - while (scan < end) { - digit = (~*scan & BIGNUM_DIGIT_MASK) + carry; + while (scan < end) { + digit = (~*scan & BIGNUM_DIGIT_MASK) + carry; - if (digit < BIGNUM_RADIX) - carry = 0; - else - { - digit = (digit - BIGNUM_RADIX); - carry = 1; - } + if (digit < BIGNUM_RADIX) + carry = 0; + else + { + digit = (digit - BIGNUM_RADIX); + carry = 1; + } - *scan++ = digit; - } + *scan++ = digit; + } } @@ -1739,80 +1739,80 @@ bignum *factorvm::bignum_integer_length(bignum * x) { GC_BIGNUM(x,this); - bignum_length_type index = ((BIGNUM_LENGTH (x)) - 1); - bignum_digit_type digit = (BIGNUM_REF (x, index)); + bignum_length_type index = ((BIGNUM_LENGTH (x)) - 1); + bignum_digit_type digit = (BIGNUM_REF (x, index)); - bignum * result = (allot_bignum (2, 0)); + bignum * result = (allot_bignum (2, 0)); - (BIGNUM_REF (result, 0)) = index; - (BIGNUM_REF (result, 1)) = 0; - bignum_destructive_scale_up (result, BIGNUM_DIGIT_LENGTH); - while (digit > 1) - { - bignum_destructive_add (result, ((bignum_digit_type) 1)); - digit >>= 1; - } - return (bignum_trim (result)); + (BIGNUM_REF (result, 0)) = index; + (BIGNUM_REF (result, 1)) = 0; + bignum_destructive_scale_up (result, BIGNUM_DIGIT_LENGTH); + while (digit > 1) + { + bignum_destructive_add (result, ((bignum_digit_type) 1)); + digit >>= 1; + } + return (bignum_trim (result)); } /* Allocates memory */ int factorvm::bignum_logbitp(int shift, bignum * arg) { - return((BIGNUM_NEGATIVE_P (arg)) - ? !bignum_unsigned_logbitp (shift, bignum_bitwise_not (arg)) - : bignum_unsigned_logbitp (shift,arg)); + return((BIGNUM_NEGATIVE_P (arg)) + ? !bignum_unsigned_logbitp (shift, bignum_bitwise_not (arg)) + : bignum_unsigned_logbitp (shift,arg)); } int factorvm::bignum_unsigned_logbitp(int shift, bignum * bignum) { - bignum_length_type len = (BIGNUM_LENGTH (bignum)); - int index = shift / BIGNUM_DIGIT_LENGTH; - if (index >= len) - return 0; - bignum_digit_type digit = (BIGNUM_REF (bignum, index)); - int p = shift % BIGNUM_DIGIT_LENGTH; - bignum_digit_type mask = ((fixnum)1) << p; - return (digit & mask) ? 1 : 0; + bignum_length_type len = (BIGNUM_LENGTH (bignum)); + int index = shift / BIGNUM_DIGIT_LENGTH; + if (index >= len) + return 0; + bignum_digit_type digit = (BIGNUM_REF (bignum, index)); + int p = shift % BIGNUM_DIGIT_LENGTH; + bignum_digit_type mask = ((fixnum)1) << p; + return (digit & mask) ? 1 : 0; } /* Allocates memory */ bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*producer)(unsigned int, factorvm*), unsigned int radix, int negative_p) { - BIGNUM_ASSERT ((radix > 1) && (radix <= BIGNUM_RADIX_ROOT)); - if (n_digits == 0) - return (BIGNUM_ZERO ()); - if (n_digits == 1) - { - fixnum digit = ((fixnum) ((*producer) (0,this))); - return (fixnum_to_bignum (negative_p ? (- digit) : digit)); - } - { - bignum_length_type length; - { - unsigned int radix_copy = radix; - unsigned int log_radix = 0; - while (radix_copy > 0) - { - radix_copy >>= 1; - log_radix += 1; - } - /* This length will be at least as large as needed. */ - length = (BIGNUM_BITS_TO_DIGITS (n_digits * log_radix)); - } - { - bignum * result = (allot_bignum_zeroed (length, negative_p)); - while ((n_digits--) > 0) - { - bignum_destructive_scale_up (result, ((bignum_digit_type) radix)); - bignum_destructive_add - (result, ((bignum_digit_type) ((*producer) (n_digits,this)))); - } - return (bignum_trim (result)); - } - } + BIGNUM_ASSERT ((radix > 1) && (radix <= BIGNUM_RADIX_ROOT)); + if (n_digits == 0) + return (BIGNUM_ZERO ()); + if (n_digits == 1) + { + fixnum digit = ((fixnum) ((*producer) (0,this))); + return (fixnum_to_bignum (negative_p ? (- digit) : digit)); + } + { + bignum_length_type length; + { + unsigned int radix_copy = radix; + unsigned int log_radix = 0; + while (radix_copy > 0) + { + radix_copy >>= 1; + log_radix += 1; + } + /* This length will be at least as large as needed. */ + length = (BIGNUM_BITS_TO_DIGITS (n_digits * log_radix)); + } + { + bignum * result = (allot_bignum_zeroed (length, negative_p)); + while ((n_digits--) > 0) + { + bignum_destructive_scale_up (result, ((bignum_digit_type) radix)); + bignum_destructive_add + (result, ((bignum_digit_type) ((*producer) (n_digits,this)))); + } + return (bignum_trim (result)); + } + } } diff --git a/vm/errors.cpp b/vm/errors.cpp index c137782f81..b3e9543b13 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -3,13 +3,6 @@ namespace factor { -/* Global variables used to pass fault handler state from signal handler to -user-space */ -cell signal_number; -cell signal_fault_addr; -unsigned int signal_fpu_status; -stack_frame *signal_callstack_top; - void factorvm::out_of_memory() { print_string("Out of memory\n\n"); diff --git a/vm/factor.cpp b/vm/factor.cpp index 4ef4d11796..34e2267a88 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -235,6 +235,8 @@ void* start_standalone_factor_thread(void *arg) VM_C_API void start_standalone_factor(int argc, vm_char **argv) { factorvm *newvm = new factorvm; + newvm->print_vm_data(); + printf("PHIL YEAH: %d %d %d %d\n",(void*)(newvm),(void*)(newvm+1),sizeof(newvm), sizeof(factorvm)); vm = newvm; register_vm_with_thread(newvm); return newvm->start_standalone_factor(argc,argv); @@ -247,4 +249,158 @@ VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char ** return start_thread(start_standalone_factor_thread,args); } + +void factorvm::print_vm_data() { + printf("PHIL: stack_chain %d\n",&stack_chain); + printf("PHIL: nursery %d\n",&nursery); + printf("PHIL: cards_offset %d\n",&cards_offset); + printf("PHIL: decks_offset %d\n",&decks_offset); + printf("PHIL: userenv %d\n",&userenv); + printf("PHIL: ds_size %d\n",&ds_size); + printf("PHIL: rs_size %d\n",&rs_size); + printf("PHIL: unused_contexts %d\n",&unused_contexts); + printf("PHIL: T %d\n",&T); + printf("PHIL: profiling_p %d\n",&profiling_p); + printf("PHIL: signal_number %d\n",&signal_number); + printf("PHIL: signal_fault_addr %d\n",&signal_fault_addr); + printf("PHIL: signal_callstack_top %d\n",&signal_callstack_top); + printf("PHIL: secure_gc %d\n",&secure_gc); + printf("PHIL: gc_off %d\n",&gc_off); + printf("PHIL: data %d\n",&data); + printf("PHIL: heap_scan_ptr %d\n",&heap_scan_ptr); + printf("PHIL: allot_markers_offset %d\n",&allot_markers_offset); + printf("PHIL: newspace %d\n",&newspace); + printf("PHIL: performing_gc %d\n",&performing_gc); + printf("PHIL: performing_compaction %d\n",&performing_compaction); + printf("PHIL: collecting_gen %d\n",&collecting_gen); + printf("PHIL: collecting_aging_again %d\n",&collecting_aging_again); + printf("PHIL: gc_jmp %d\n",&gc_jmp); + printf("PHIL: stats %d\n",&stats); + printf("PHIL: cards_scanned %d\n",&cards_scanned); + printf("PHIL: decks_scanned %d\n",&decks_scanned); + printf("PHIL: card_scan_time %d\n",&card_scan_time); + printf("PHIL: code_heap_scans %d\n",&code_heap_scans); + printf("PHIL: last_code_heap_scan %d\n",&last_code_heap_scan); + printf("PHIL: growing_data_heap %d\n",&growing_data_heap); + printf("PHIL: old_data_heap %d\n",&old_data_heap); + printf("PHIL: gc_locals %d\n",&gc_locals); + printf("PHIL: gc_bignums %d\n",&gc_bignums); + printf("PHIL: fep_disabled %d\n",&fep_disabled); + printf("PHIL: full_output %d\n",&full_output); + printf("PHIL: look_for %d\n",&look_for); + printf("PHIL: obj %d\n",&obj); + printf("PHIL: bignum_zero %d\n",&bignum_zero); + printf("PHIL: bignum_pos_one %d\n",&bignum_pos_one); + printf("PHIL: bignum_neg_one %d\n",&bignum_neg_one); + printf("PHIL: code %d\n",&code); + printf("PHIL: forwarding %d\n",&forwarding); + printf("PHIL: code_relocation_base %d\n",&code_relocation_base); + printf("PHIL: data_relocation_base %d\n",&data_relocation_base); + printf("PHIL: megamorphic_cache_hits %d\n",&megamorphic_cache_hits); + printf("PHIL: megamorphic_cache_misses %d\n",&megamorphic_cache_misses); + printf("PHIL: max_pic_size %d\n",&max_pic_size); + printf("PHIL: cold_call_to_ic_transitions %d\n",&cold_call_to_ic_transitions); + printf("PHIL: ic_to_pic_transitions %d\n",&ic_to_pic_transitions); + printf("PHIL: pic_to_mega_transitions %d\n",&pic_to_mega_transitions); + printf("PHIL: pic_counts %d\n",&pic_counts); +} + + // if you change this struct, also change vm.factor k-------- + context *stack_chain; + zone nursery; /* new objects are allocated here */ + cell cards_offset; + cell decks_offset; + cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ + + // ------------------------------- + + // contexts + cell ds_size, rs_size; + context *unused_contexts; + + // run + cell T; /* Canonical T object. It's just a word */ + + // profiler + bool profiling_p; + + // errors + /* Global variables used to pass fault handler state from signal handler to + user-space */ + cell signal_number; + cell signal_fault_addr; + unsigned int signal_fpu_status; + stack_frame *signal_callstack_top; + + //data_heap + bool secure_gc; /* Set by the -securegc command line argument */ + bool gc_off; /* GC is off during heap walking */ + data_heap *data; + /* A heap walk allows useful things to be done, like finding all + references to an object for debugging purposes. */ + cell heap_scan_ptr; + //write barrier + cell allot_markers_offset; + //data_gc + /* used during garbage collection only */ + zone *newspace; + bool performing_gc; + bool performing_compaction; + cell collecting_gen; + /* if true, we are collecting aging space for the second time, so if it is still + full, we go on to collect tenured */ + bool collecting_aging_again; + /* in case a generation fills up in the middle of a gc, we jump back + up to try collecting the next generation. */ + jmp_buf gc_jmp; + gc_stats stats[max_gen_count]; + u64 cards_scanned; + u64 decks_scanned; + u64 card_scan_time; + cell code_heap_scans; + /* What generation was being collected when copy_code_heap_roots() was last + called? Until the next call to add_code_block(), future + collections of younger generations don't have to touch the code + heap. */ + cell last_code_heap_scan; + /* sometimes we grow the heap */ + bool growing_data_heap; + data_heap *old_data_heap; + + // local roots + /* If a runtime function needs to call another function which potentially + allocates memory, it must wrap any local variable references to Factor + objects in gc_root instances */ + std::vector gc_locals; + std::vector gc_bignums; + + //debug + bool fep_disabled; + bool full_output; + cell look_for; + cell obj; + + //math + cell bignum_zero; + cell bignum_pos_one; + cell bignum_neg_one; + + //code_heap + heap code; + unordered_map forwarding; + + //image + cell code_relocation_base; + cell data_relocation_base; + + //dispatch + cell megamorphic_cache_hits; + cell megamorphic_cache_misses; + + //inline cache + cell max_pic_size; + cell cold_call_to_ic_transitions; + cell ic_to_pic_transitions; + cell pic_to_mega_transitions; + cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ } diff --git a/vm/os-windows-nt.cpp b/vm/os-windows-nt.cpp index ee00e1434a..988ce60a8a 100755 --- a/vm/os-windows-nt.cpp +++ b/vm/os-windows-nt.cpp @@ -39,21 +39,20 @@ s64 current_micros() - EPOCH_OFFSET) / 10; } -FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) +LONG factorvm::exception_handler(PEXCEPTION_POINTERS pe) { - factorvm *myvm = SIGNAL_VM_PTR(); PEXCEPTION_RECORD e = (PEXCEPTION_RECORD)pe->ExceptionRecord; CONTEXT *c = (CONTEXT*)pe->ContextRecord; - if(myvm->in_code_heap_p(c->EIP)) - myvm->signal_callstack_top = (stack_frame *)c->ESP; + if(in_code_heap_p(c->EIP)) + signal_callstack_top = (stack_frame *)c->ESP; else - myvm->signal_callstack_top = NULL; + signal_callstack_top = NULL; switch (e->ExceptionCode) { case EXCEPTION_ACCESS_VIOLATION: - myvm->signal_fault_addr = e->ExceptionInformation[1]; - c->EIP = (cell)memory_signal_handler_impl; + signal_fault_addr = e->ExceptionInformation[1]; + c->EIP = (cell)factor::memory_signal_handler_impl; break; case STATUS_FLOAT_DENORMAL_OPERAND: @@ -65,10 +64,10 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) case STATUS_FLOAT_UNDERFLOW: case STATUS_FLOAT_MULTIPLE_FAULTS: case STATUS_FLOAT_MULTIPLE_TRAPS: - myvm->signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c)); + signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c)); X87SW(c) = 0; MXCSR(c) &= 0xffffffc0; - c->EIP = (cell)fp_signal_handler_impl; + c->EIP = (cell)factor::fp_signal_handler_impl; break; case 0x40010006: /* If the Widcomm bluetooth stack is installed, the BTTray.exe @@ -79,24 +78,30 @@ FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) enabled. Don't really have any idea what this exception means. */ break; default: - myvm->signal_number = e->ExceptionCode; - c->EIP = (cell)misc_signal_handler_impl; + signal_number = e->ExceptionCode; + c->EIP = (cell)factor::misc_signal_handler_impl; break; } return EXCEPTION_CONTINUE_EXECUTION; } + +FACTOR_STDCALL LONG exception_handler(PEXCEPTION_POINTERS pe) +{ + return SIGNAL_VM_PTR()->exception_handler(pe); +} + bool handler_added = 0; void factorvm::c_to_factor_toplevel(cell quot) { if(!handler_added){ - if(!AddVectoredExceptionHandler(0, (PVECTORED_EXCEPTION_HANDLER)exception_handler)) + if(!AddVectoredExceptionHandler(0, (PVECTORED_EXCEPTION_HANDLER)factor::exception_handler)) fatal_error("AddVectoredExceptionHandler failed", 0); handler_added = 1; } c_to_factor(quot,this); - RemoveVectoredExceptionHandler((void *)exception_handler); + RemoveVectoredExceptionHandler((void *)factor::exception_handler); } void factorvm::open_console() diff --git a/vm/vm-data-dummy.hpp b/vm/vm-data-dummy.hpp new file mode 100644 index 0000000000..c028a61503 --- /dev/null +++ b/vm/vm-data-dummy.hpp @@ -0,0 +1,116 @@ +namespace factor +{ + + // if you change this struct, also change vm.factor k-------- + extern "C" context *stack_chain; + extern "C" zone nursery; /* new objects are allocated here */ + extern "C" cell cards_offset; + extern "C" cell decks_offset; +//extern "C" cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ + + // ------------------------------- + + // contexts + extern "C" cell ds_size, rs_size; + extern "C" context *unused_contexts; + + + // profiler + extern "C" bool profiling_p; + + // errors + /* Global variables used to pass fault handler state from signal handler to + user-space */ + extern "C" cell signal_number; + extern "C" cell signal_fault_addr; + extern "C" unsigned int signal_fpu_status; + extern "C" stack_frame *signal_callstack_top; + + //data_heap + extern "C" bool secure_gc; /* Set by the -securegc command line argument */ + extern "C" bool gc_off; /* GC is off during heap walking */ + extern "C" data_heap *data; + /* A heap walk allows useful things to be done, like finding all + references to an object for debugging purposes. */ + extern "C" cell heap_scan_ptr; + //write barrier + extern "C" cell allot_markers_offset; + //data_gc + /* used during garbage collection only */ + extern "C" zone *newspace; + extern "C" bool performing_gc; + extern "C" bool performing_compaction; + extern "C" cell collecting_gen; + /* if true, we are collecting aging space for the second time, so if it is still + full, we go on to collect tenured */ + extern "C" bool collecting_aging_again; + /* in case a generation fills up in the middle of a gc, we jump back + up to try collecting the next generation. */ + extern "C" jmp_buf gc_jmp; + extern "C" gc_stats stats[max_gen_count]; + extern "C" u64 cards_scanned; + extern "C" u64 decks_scanned; + extern "C" u64 card_scan_time; + extern "C" cell code_heap_scans; + /* What generation was being collected when copy_code_heap_roots() was last + called? Until the next call to add_code_block(), future + collections of younger generations don't have to touch the code + heap. */ + extern "C" cell last_code_heap_scan; + /* sometimes we grow the heap */ + extern "C" bool growing_data_heap; + extern "C" data_heap *old_data_heap; + + // local roots + /* If a runtime function needs to call another function which potentially + allocates memory, it must wrap any local variable references to Factor + objects in gc_root instances */ + //extern "C" segment *gc_locals_region; + //extern "C" cell gc_locals; + //extern "C" segment *gc_bignums_region; + //extern "C" cell gc_bignums; + + //debug + extern "C" bool fep_disabled; + extern "C" bool full_output; + extern "C" cell look_for; + extern "C" cell obj; + + //math + extern "C" cell bignum_zero; + extern "C" cell bignum_pos_one; + extern "C" cell bignum_neg_one; + + //code_heap + extern "C" heap code; + extern "C" unordered_map forwarding; + + //image + extern "C" cell code_relocation_base; + extern "C" cell data_relocation_base; + + //dispatch + extern "C" cell megamorphic_cache_hits; + extern "C" cell megamorphic_cache_misses; + + //inline cache + extern "C" cell max_pic_size; + extern "C" cell cold_call_to_ic_transitions; + extern "C" cell ic_to_pic_transitions; + extern "C" cell pic_to_mega_transitions; + extern "C" cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ + +struct factorvmdata { + cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ + + // run + cell T; /* Canonical T object. It's just a word */ + + // local roots + /* If a runtime function needs to call another function which potentially + allocates memory, it must wrap any local variable references to Factor + objects in gc_root instances */ + std::vector gc_locals; + std::vector gc_bignums; +}; +} diff --git a/vm/vm-data.hpp b/vm/vm-data.hpp new file mode 100644 index 0000000000..701e35da9d --- /dev/null +++ b/vm/vm-data.hpp @@ -0,0 +1,105 @@ +namespace factor +{ + +struct factorvmdata { + // if you change this struct, also change vm.factor k-------- + context *stack_chain; + zone nursery; /* new objects are allocated here */ + cell cards_offset; + cell decks_offset; + cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ + + // ------------------------------- + + // contexts + cell ds_size, rs_size; + context *unused_contexts; + + // run + cell T; /* Canonical T object. It's just a word */ + + // profiler + bool profiling_p; + + // errors + /* Global variables used to pass fault handler state from signal handler to + user-space */ + cell signal_number; + cell signal_fault_addr; + unsigned int signal_fpu_status; + stack_frame *signal_callstack_top; + + //data_heap + bool secure_gc; /* Set by the -securegc command line argument */ + bool gc_off; /* GC is off during heap walking */ + data_heap *data; + /* A heap walk allows useful things to be done, like finding all + references to an object for debugging purposes. */ + cell heap_scan_ptr; + //write barrier + cell allot_markers_offset; + //data_gc + /* used during garbage collection only */ + zone *newspace; + bool performing_gc; + bool performing_compaction; + cell collecting_gen; + /* if true, we are collecting aging space for the second time, so if it is still + full, we go on to collect tenured */ + bool collecting_aging_again; + /* in case a generation fills up in the middle of a gc, we jump back + up to try collecting the next generation. */ + jmp_buf gc_jmp; + gc_stats stats[max_gen_count]; + u64 cards_scanned; + u64 decks_scanned; + u64 card_scan_time; + cell code_heap_scans; + /* What generation was being collected when copy_code_heap_roots() was last + called? Until the next call to add_code_block(), future + collections of younger generations don't have to touch the code + heap. */ + cell last_code_heap_scan; + /* sometimes we grow the heap */ + bool growing_data_heap; + data_heap *old_data_heap; + + // local roots + /* If a runtime function needs to call another function which potentially + allocates memory, it must wrap any local variable references to Factor + objects in gc_root instances */ + std::vector gc_locals; + std::vector gc_bignums; + + //debug + bool fep_disabled; + bool full_output; + cell look_for; + cell obj; + + //math + cell bignum_zero; + cell bignum_pos_one; + cell bignum_neg_one; + + //code_heap + heap code; + unordered_map forwarding; + + //image + cell code_relocation_base; + cell data_relocation_base; + + //dispatch + cell megamorphic_cache_hits; + cell megamorphic_cache_misses; + + //inline cache + cell max_pic_size; + cell cold_call_to_ic_transitions; + cell ic_to_pic_transitions; + cell pic_to_mega_transitions; + cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ +}; + +} diff --git a/vm/vm.hpp b/vm/vm.hpp index b6508c7560..baa67deae8 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -1,106 +1,8 @@ +#include "vm-data-dummy.hpp" + namespace factor { -struct factorvmdata { - // if you change this struct, also change vm.factor k-------- - context *stack_chain; - zone nursery; /* new objects are allocated here */ - cell cards_offset; - cell decks_offset; - cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ - - // ------------------------------- - - // contexts - cell ds_size, rs_size; - context *unused_contexts; - - // run - cell T; /* Canonical T object. It's just a word */ - - // profiler - bool profiling_p; - - // errors - /* Global variables used to pass fault handler state from signal handler to - user-space */ - cell signal_number; - cell signal_fault_addr; - unsigned int signal_fpu_status; - stack_frame *signal_callstack_top; - //data_heap - bool secure_gc; /* Set by the -securegc command line argument */ - bool gc_off; /* GC is off during heap walking */ - data_heap *data; - /* A heap walk allows useful things to be done, like finding all - references to an object for debugging purposes. */ - cell heap_scan_ptr; - //write barrier - cell allot_markers_offset; - //data_gc - /* used during garbage collection only */ - zone *newspace; - bool performing_gc; - bool performing_compaction; - cell collecting_gen; - /* if true, we are collecting aging space for the second time, so if it is still - full, we go on to collect tenured */ - bool collecting_aging_again; - /* in case a generation fills up in the middle of a gc, we jump back - up to try collecting the next generation. */ - jmp_buf gc_jmp; - gc_stats stats[max_gen_count]; - u64 cards_scanned; - u64 decks_scanned; - u64 card_scan_time; - cell code_heap_scans; - /* What generation was being collected when copy_code_heap_roots() was last - called? Until the next call to add_code_block(), future - collections of younger generations don't have to touch the code - heap. */ - cell last_code_heap_scan; - /* sometimes we grow the heap */ - bool growing_data_heap; - data_heap *old_data_heap; - - // local roots - /* If a runtime function needs to call another function which potentially - allocates memory, it must wrap any local variable references to Factor - objects in gc_root instances */ - std::vector gc_locals; - std::vector gc_bignums; - - //debug - bool fep_disabled; - bool full_output; - cell look_for; - cell obj; - - //math - cell bignum_zero; - cell bignum_pos_one; - cell bignum_neg_one; - - //code_heap - heap code; - unordered_map forwarding; - - //image - cell code_relocation_base; - cell data_relocation_base; - - //dispatch - cell megamorphic_cache_hits; - cell megamorphic_cache_misses; - - //inline cache - cell max_pic_size; - cell cold_call_to_ic_transitions; - cell ic_to_pic_transitions; - cell pic_to_mega_transitions; - cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ -}; - struct factorvm : factorvmdata { // segments @@ -694,6 +596,7 @@ struct factorvm : factorvmdata { #if defined(WINNT) void open_console(); + LONG exception_handler(PEXCEPTION_POINTERS pe); // next method here: #endif #else // UNIX @@ -702,9 +605,11 @@ struct factorvm : factorvmdata { #endif + void print_vm_data(); }; + #define FACTOR_SINGLE_THREADED_SINGLETON #ifdef FACTOR_SINGLE_THREADED_SINGLETON From eee1de23c834db5ac2c02d807b5967665ed91c44 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 19:21:00 +0100 Subject: [PATCH 248/345] removed debugging --- vm/factor.cpp | 58 --------------------------------------------------- 1 file changed, 58 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index 34e2267a88..7b95304887 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -235,8 +235,6 @@ void* start_standalone_factor_thread(void *arg) VM_C_API void start_standalone_factor(int argc, vm_char **argv) { factorvm *newvm = new factorvm; - newvm->print_vm_data(); - printf("PHIL YEAH: %d %d %d %d\n",(void*)(newvm),(void*)(newvm+1),sizeof(newvm), sizeof(factorvm)); vm = newvm; register_vm_with_thread(newvm); return newvm->start_standalone_factor(argc,argv); @@ -249,62 +247,6 @@ VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char ** return start_thread(start_standalone_factor_thread,args); } - -void factorvm::print_vm_data() { - printf("PHIL: stack_chain %d\n",&stack_chain); - printf("PHIL: nursery %d\n",&nursery); - printf("PHIL: cards_offset %d\n",&cards_offset); - printf("PHIL: decks_offset %d\n",&decks_offset); - printf("PHIL: userenv %d\n",&userenv); - printf("PHIL: ds_size %d\n",&ds_size); - printf("PHIL: rs_size %d\n",&rs_size); - printf("PHIL: unused_contexts %d\n",&unused_contexts); - printf("PHIL: T %d\n",&T); - printf("PHIL: profiling_p %d\n",&profiling_p); - printf("PHIL: signal_number %d\n",&signal_number); - printf("PHIL: signal_fault_addr %d\n",&signal_fault_addr); - printf("PHIL: signal_callstack_top %d\n",&signal_callstack_top); - printf("PHIL: secure_gc %d\n",&secure_gc); - printf("PHIL: gc_off %d\n",&gc_off); - printf("PHIL: data %d\n",&data); - printf("PHIL: heap_scan_ptr %d\n",&heap_scan_ptr); - printf("PHIL: allot_markers_offset %d\n",&allot_markers_offset); - printf("PHIL: newspace %d\n",&newspace); - printf("PHIL: performing_gc %d\n",&performing_gc); - printf("PHIL: performing_compaction %d\n",&performing_compaction); - printf("PHIL: collecting_gen %d\n",&collecting_gen); - printf("PHIL: collecting_aging_again %d\n",&collecting_aging_again); - printf("PHIL: gc_jmp %d\n",&gc_jmp); - printf("PHIL: stats %d\n",&stats); - printf("PHIL: cards_scanned %d\n",&cards_scanned); - printf("PHIL: decks_scanned %d\n",&decks_scanned); - printf("PHIL: card_scan_time %d\n",&card_scan_time); - printf("PHIL: code_heap_scans %d\n",&code_heap_scans); - printf("PHIL: last_code_heap_scan %d\n",&last_code_heap_scan); - printf("PHIL: growing_data_heap %d\n",&growing_data_heap); - printf("PHIL: old_data_heap %d\n",&old_data_heap); - printf("PHIL: gc_locals %d\n",&gc_locals); - printf("PHIL: gc_bignums %d\n",&gc_bignums); - printf("PHIL: fep_disabled %d\n",&fep_disabled); - printf("PHIL: full_output %d\n",&full_output); - printf("PHIL: look_for %d\n",&look_for); - printf("PHIL: obj %d\n",&obj); - printf("PHIL: bignum_zero %d\n",&bignum_zero); - printf("PHIL: bignum_pos_one %d\n",&bignum_pos_one); - printf("PHIL: bignum_neg_one %d\n",&bignum_neg_one); - printf("PHIL: code %d\n",&code); - printf("PHIL: forwarding %d\n",&forwarding); - printf("PHIL: code_relocation_base %d\n",&code_relocation_base); - printf("PHIL: data_relocation_base %d\n",&data_relocation_base); - printf("PHIL: megamorphic_cache_hits %d\n",&megamorphic_cache_hits); - printf("PHIL: megamorphic_cache_misses %d\n",&megamorphic_cache_misses); - printf("PHIL: max_pic_size %d\n",&max_pic_size); - printf("PHIL: cold_call_to_ic_transitions %d\n",&cold_call_to_ic_transitions); - printf("PHIL: ic_to_pic_transitions %d\n",&ic_to_pic_transitions); - printf("PHIL: pic_to_mega_transitions %d\n",&pic_to_mega_transitions); - printf("PHIL: pic_counts %d\n",&pic_counts); -} - // if you change this struct, also change vm.factor k-------- context *stack_chain; zone nursery; /* new objects are allocated here */ From b02944c6d5f4e204e07b06806d80da8129149fb9 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 19:23:20 +0100 Subject: [PATCH 249/345] moved signal handlers into vm object --- vm/os-unix.cpp | 27 +++++++++++++++++++-------- vm/vm.hpp | 9 +++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 7913647f3e..3f4e1b29f7 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -147,20 +147,31 @@ stack_frame *factorvm::uap_stack_pointer(void *uap) return NULL; } + + +void factorvm::memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) +{ + signal_fault_addr = (cell)siginfo->si_addr; + signal_callstack_top = uap_stack_pointer(uap); + UAP_PROGRAM_COUNTER(uap) = (cell)factor::memory_signal_handler_impl; +} + void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - factorvm *myvm = SIGNAL_VM_PTR(); - myvm->signal_fault_addr = (cell)siginfo->si_addr; - myvm->signal_callstack_top = myvm->uap_stack_pointer(uap); - UAP_PROGRAM_COUNTER(uap) = (cell)memory_signal_handler_impl; + SIGNAL_VM_PTR()->memory_signal_handler(signal,siginfo,uap); +} + + +void factorvm::misc_signal_handler(int signal, siginfo_t *siginfo, void *uap) +{ + signal_number = signal; + signal_callstack_top = uap_stack_pointer(uap); + UAP_PROGRAM_COUNTER(uap) = (cell)factor::misc_signal_handler_impl; } void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap) { - factorvm *myvm = SIGNAL_VM_PTR(); - myvm->signal_number = signal; - myvm->signal_callstack_top = myvm->uap_stack_pointer(uap); - UAP_PROGRAM_COUNTER(uap) = (cell)misc_signal_handler_impl; + SIGNAL_VM_PTR()->misc_signal_handler(signal,siginfo,uap); } void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap) diff --git a/vm/vm.hpp b/vm/vm.hpp index baa67deae8..efb0fa48e9 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -355,9 +355,9 @@ struct factorvm : factorvmdata { float to_float(cell value); void box_double(double flo); double to_double(cell value); - void overflow_fixnum_add(fixnum x, fixnum y); - void overflow_fixnum_subtract(fixnum x, fixnum y); - void overflow_fixnum_multiply(fixnum x, fixnum y); + inline void overflow_fixnum_add(fixnum x, fixnum y); + inline void overflow_fixnum_subtract(fixnum x, fixnum y); + inline void overflow_fixnum_multiply(fixnum x, fixnum y); inline cell allot_integer(fixnum x); inline cell allot_cell(cell x); inline cell allot_float(double n); @@ -600,7 +600,8 @@ struct factorvm : factorvmdata { // next method here: #endif #else // UNIX - + void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap); + void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap); stack_frame *uap_stack_pointer(void *uap); #endif From 67ac514a3b4abf8ab3b0eb4c96744c8b40de5262 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 19:24:30 +0100 Subject: [PATCH 250/345] Added vm ptr to math overflow functions --- vm/math.cpp | 12 ++++++------ vm/math.hpp | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/vm/math.cpp b/vm/math.cpp index 40a0c20a3b..fbb946e19f 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -836,29 +836,29 @@ VM_C_API double to_double(cell value,factorvm *myvm) /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On overflow, they call these functions. */ -void factorvm::overflow_fixnum_add(fixnum x, fixnum y) +inline void factorvm::overflow_fixnum_add(fixnum x, fixnum y) { drepl(tag(fixnum_to_bignum( untag_fixnum(x) + untag_fixnum(y)))); } -VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y) +VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factorvm *myvm) { return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y); } -void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) +inline void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) { drepl(tag(fixnum_to_bignum( untag_fixnum(x) - untag_fixnum(y)))); } -VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y) +VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factorvm *myvm) { return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y); } -void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) +inline void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) { bignum *bx = fixnum_to_bignum(x); GC_BIGNUM(bx,this); @@ -867,7 +867,7 @@ void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) drepl(tag(bignum_multiply(bx,by))); } -VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y) +VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factorvm *myvm) { return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y); } diff --git a/vm/math.hpp b/vm/math.hpp index b45284f693..5183f85f45 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -83,8 +83,8 @@ VM_C_API u64 to_unsigned_8(cell obj, factorvm *vm); VM_C_API fixnum to_fixnum(cell tagged, factorvm *vm); VM_C_API cell to_cell(cell tagged, factorvm *vm); -VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y); -VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y); -VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y); +VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factorvm *vm); +VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factorvm *vm); +VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factorvm *vm); } From 34ce33431784af6395f55264517cc06b54dc62a4 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 19:25:22 +0100 Subject: [PATCH 251/345] Added data constructor to initialize bools in factorvmdata struct --- vm/vm-data.hpp | 13 +++++++++++++ vm/vm.hpp | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/vm/vm-data.hpp b/vm/vm-data.hpp index 701e35da9d..de5cf36632 100644 --- a/vm/vm-data.hpp +++ b/vm/vm-data.hpp @@ -100,6 +100,19 @@ struct factorvmdata { cell ic_to_pic_transitions; cell pic_to_mega_transitions; cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ + + factorvmdata() + : profiling_p(false), + secure_gc(false), + gc_off(false), + performing_gc(false), + performing_compaction(false), + collecting_aging_again(false), + growing_data_heap(false), + fep_disabled(false), + full_output(false) + {} + }; } diff --git a/vm/vm.hpp b/vm/vm.hpp index efb0fa48e9..a199885f49 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -1,4 +1,4 @@ -#include "vm-data-dummy.hpp" +#include "vm-data.hpp" namespace factor { From 3ecff2c0ebb6705b5aba0cfaf29a63bb049a794a Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 19:52:05 +0100 Subject: [PATCH 252/345] fixed bug where vm_char being treated as 1byte type --- vm/os-windows.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/os-windows.cpp b/vm/os-windows.cpp index 38c8b944a4..bd7e573dcc 100644 --- a/vm/os-windows.cpp +++ b/vm/os-windows.cpp @@ -60,7 +60,7 @@ bool factorvm::windows_stat(vm_char *path) void factorvm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length) { snwprintf(temp_path, length-1, L"%s.image", full_path); - temp_path[sizeof(temp_path) - 1] = 0; + temp_path[length - 1] = 0; } /* You must free() this yourself. */ @@ -76,8 +76,8 @@ const vm_char *factorvm::default_image_path() if((ptr = wcsrchr(full_path, '.'))) *ptr = 0; - snwprintf(temp_path, sizeof(temp_path)-1, L"%s.image", full_path); - temp_path[sizeof(temp_path) - 1] = 0; + snwprintf(temp_path, MAX_UNICODE_PATH-1, L"%s.image", full_path); + temp_path[MAX_UNICODE_PATH - 1] = 0; return safe_strdup(temp_path); } From deb7af70bb42bdf3bf9837d1f7232671b8d41a70 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 20:13:44 +0100 Subject: [PATCH 253/345] asm math functions pass vm ptr to overflow function in 3rd arg (X86.32) --- vm/cpu-ppc.hpp | 1 + vm/cpu-x86.32.S | 1 + vm/cpu-x86.32.hpp | 1 + vm/cpu-x86.64.hpp | 2 +- vm/cpu-x86.S | 10 +++++++--- vm/math.cpp | 6 +++--- vm/math.hpp | 6 +++--- vm/vm.hpp | 6 +++--- 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/vm/cpu-ppc.hpp b/vm/cpu-ppc.hpp index 495eb375ec..d0036fb84f 100644 --- a/vm/cpu-ppc.hpp +++ b/vm/cpu-ppc.hpp @@ -3,6 +3,7 @@ namespace factor #define FACTOR_CPU_STRING "ppc" #define VM_ASM_API VM_C_API +#define VM_ASM_API_OVERFLOW VM_C_API register cell ds asm("r13"); register cell rs asm("r14"); diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index 326ddb6eb8..1618171b5b 100644 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -2,6 +2,7 @@ #define ARG0 %eax #define ARG1 %edx +#define ARG2 %ecx #define STACK_REG %esp #define DS_REG %esi #define RETURN_REG %eax diff --git a/vm/cpu-x86.32.hpp b/vm/cpu-x86.32.hpp index 351865f776..a95179a49b 100644 --- a/vm/cpu-x86.32.hpp +++ b/vm/cpu-x86.32.hpp @@ -7,4 +7,5 @@ register cell ds asm("esi"); register cell rs asm("edi"); #define VM_ASM_API VM_C_API __attribute__ ((regparm (2))) +#define VM_ASM_API_OVERFLOW VM_C_API __attribute__ ((regparm (3))) } diff --git a/vm/cpu-x86.64.hpp b/vm/cpu-x86.64.hpp index 679c301548..841705c171 100644 --- a/vm/cpu-x86.64.hpp +++ b/vm/cpu-x86.64.hpp @@ -7,5 +7,5 @@ register cell ds asm("r14"); register cell rs asm("r15"); #define VM_ASM_API VM_C_API - +#define VM_ASM_API_OVERFLOW VM_C_API } diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index 2599ba3f97..a18d1483fb 100644 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -1,4 +1,5 @@ -DEF(void,primitive_fixnum_add,(void)): +DEF(void,primitive_fixnum_add,(void *myvm)): + mov ARG0, ARG2 /* save vm ptr for overflow */ mov (DS_REG),ARG0 mov -CELL_SIZE(DS_REG),ARG1 sub $CELL_SIZE,DS_REG @@ -8,7 +9,8 @@ DEF(void,primitive_fixnum_add,(void)): mov ARITH_TEMP_1,(DS_REG) ret -DEF(void,primitive_fixnum_subtract,(void)): +DEF(void,primitive_fixnum_subtract,(void *myvm)): + mov ARG0, ARG2 /* save vm ptr for overflow */ mov (DS_REG),ARG1 mov -CELL_SIZE(DS_REG),ARG0 sub $CELL_SIZE,DS_REG @@ -18,7 +20,8 @@ DEF(void,primitive_fixnum_subtract,(void)): mov ARITH_TEMP_1,(DS_REG) ret -DEF(void,primitive_fixnum_multiply,(void)): +DEF(void,primitive_fixnum_multiply,(void *myvm)): + mov ARG0, ARG2 /* save vm ptr for overflow */ mov (DS_REG),ARITH_TEMP_1 mov ARITH_TEMP_1,DIV_RESULT mov -CELL_SIZE(DS_REG),ARITH_TEMP_2 @@ -28,6 +31,7 @@ DEF(void,primitive_fixnum_multiply,(void)): jo multiply_overflow mov DIV_RESULT,(DS_REG) ret + multiply_overflow: sar $3,ARITH_TEMP_1 mov ARITH_TEMP_1,ARG0 diff --git a/vm/math.cpp b/vm/math.cpp index fbb946e19f..be285872f5 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -842,7 +842,7 @@ inline void factorvm::overflow_fixnum_add(fixnum x, fixnum y) untag_fixnum(x) + untag_fixnum(y)))); } -VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factorvm *myvm) +VM_ASM_API_OVERFLOW void overflow_fixnum_add(fixnum x, fixnum y, factorvm *myvm) { return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y); } @@ -853,7 +853,7 @@ inline void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) untag_fixnum(x) - untag_fixnum(y)))); } -VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factorvm *myvm) +VM_ASM_API_OVERFLOW void overflow_fixnum_subtract(fixnum x, fixnum y, factorvm *myvm) { return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y); } @@ -867,7 +867,7 @@ inline void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) drepl(tag(bignum_multiply(bx,by))); } -VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factorvm *myvm) +VM_ASM_API_OVERFLOW void overflow_fixnum_multiply(fixnum x, fixnum y, factorvm *myvm) { return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y); } diff --git a/vm/math.hpp b/vm/math.hpp index 5183f85f45..5e6121afb2 100644 --- a/vm/math.hpp +++ b/vm/math.hpp @@ -83,8 +83,8 @@ VM_C_API u64 to_unsigned_8(cell obj, factorvm *vm); VM_C_API fixnum to_fixnum(cell tagged, factorvm *vm); VM_C_API cell to_cell(cell tagged, factorvm *vm); -VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factorvm *vm); -VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factorvm *vm); -VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factorvm *vm); +VM_ASM_API_OVERFLOW void overflow_fixnum_add(fixnum x, fixnum y, factorvm *vm); +VM_ASM_API_OVERFLOW void overflow_fixnum_subtract(fixnum x, fixnum y, factorvm *vm); +VM_ASM_API_OVERFLOW void overflow_fixnum_multiply(fixnum x, fixnum y, factorvm *vm); } diff --git a/vm/vm.hpp b/vm/vm.hpp index a199885f49..3c0f4a246b 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -626,10 +626,10 @@ struct factorvm : factorvmdata { #ifdef FACTOR_SINGLE_THREADED_TESTING /* calls are dispatched as per multithreaded, but checked against singleton */ extern factorvm *vm; - #define PRIMITIVE_GETVM() ((factorvm*)myvm) - #define PRIMITIVE_OVERFLOW_GETVM() vm - #define VM_PTR myvm #define ASSERTVM() assert(vm==myvm) + #define PRIMITIVE_GETVM() ((factorvm*)myvm) + #define PRIMITIVE_OVERFLOW_GETVM() ASSERTVM(); myvm + #define VM_PTR myvm #define SIGNAL_VM_PTR() tls_vm() #endif From 3343723ee3277fa2028678f73bc04253ba351128 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 20:37:23 +0100 Subject: [PATCH 254/345] Don't return functions returning void. -O3 seems to optimize them out! --- vm/data_gc.cpp | 2 +- vm/math.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/vm/data_gc.cpp b/vm/data_gc.cpp index c6cdd39853..c192d5714e 100755 --- a/vm/data_gc.cpp +++ b/vm/data_gc.cpp @@ -708,7 +708,7 @@ void factorvm::inline_gc(cell *gc_roots_base, cell gc_roots_size) VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size, factorvm *myvm) { ASSERTVM(); - return VM_PTR->inline_gc(gc_roots_base,gc_roots_size); + VM_PTR->inline_gc(gc_roots_base,gc_roots_size); } } diff --git a/vm/math.cpp b/vm/math.cpp index be285872f5..4b595f85a3 100755 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -831,6 +831,7 @@ double factorvm::to_double(cell value) VM_C_API double to_double(cell value,factorvm *myvm) { + ASSERTVM(); return VM_PTR->to_double(value); } @@ -844,7 +845,7 @@ inline void factorvm::overflow_fixnum_add(fixnum x, fixnum y) VM_ASM_API_OVERFLOW void overflow_fixnum_add(fixnum x, fixnum y, factorvm *myvm) { - return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y); + PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y); } inline void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) @@ -855,7 +856,7 @@ inline void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y) VM_ASM_API_OVERFLOW void overflow_fixnum_subtract(fixnum x, fixnum y, factorvm *myvm) { - return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y); + PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y); } inline void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) @@ -869,7 +870,7 @@ inline void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y) VM_ASM_API_OVERFLOW void overflow_fixnum_multiply(fixnum x, fixnum y, factorvm *myvm) { - return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y); + PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y); } } From 480a15c2c3934bc9efbf29bdc7132df69a8de899 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Fri, 4 Sep 2009 21:11:13 +0100 Subject: [PATCH 255/345] Added vm passing to inline_cache_miss x86.32 asm --- vm/cpu-x86.32.S | 7 ++++--- vm/inline_cache.cpp | 5 +++-- vm/inline_cache.hpp | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index 1618171b5b..a486fed29d 100644 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -49,13 +49,14 @@ DEF(long long,read_timestamp_counter,(void)): rdtsc ret -DEF(void,primitive_inline_cache_miss,(void)): +DEF(void,primitive_inline_cache_miss,(void *vm)): mov (%esp),%ebx -DEF(void,primitive_inline_cache_miss_tail,(void)): +DEF(void,primitive_inline_cache_miss_tail,(void *vm)): sub $8,%esp + push %eax /* push vm ptr */ push %ebx call MANGLE(inline_cache_miss) - add $12,%esp + add $16,%esp jmp *%eax DEF(void,get_sse_env,(void*)): diff --git a/vm/inline_cache.cpp b/vm/inline_cache.cpp index 35479c29f5..4c77a83a93 100755 --- a/vm/inline_cache.cpp +++ b/vm/inline_cache.cpp @@ -245,9 +245,10 @@ void *factorvm::inline_cache_miss(cell return_address) return xt; } -VM_C_API void *inline_cache_miss(cell return_address) +VM_C_API void *inline_cache_miss(cell return_address, factorvm *myvm) { - return vm->inline_cache_miss(return_address); + ASSERTVM(); + return VM_PTR->inline_cache_miss(return_address); } diff --git a/vm/inline_cache.hpp b/vm/inline_cache.hpp index 5b1bbdf516..02ac43dce8 100644 --- a/vm/inline_cache.hpp +++ b/vm/inline_cache.hpp @@ -5,6 +5,6 @@ PRIMITIVE(inline_cache_stats); PRIMITIVE(inline_cache_miss); PRIMITIVE(inline_cache_miss_tail); -VM_C_API void *inline_cache_miss(cell return_address); +VM_C_API void *inline_cache_miss(cell return_address, factorvm *vm); } From 8a65f35c3175660fa84d402c0326f34c35737184 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 7 Sep 2009 07:15:10 +0100 Subject: [PATCH 256/345] vm passed in primitives as arg0 for x86.64 --- basis/cpu/x86/64/bootstrap.factor | 6 +++--- vm/cpu-x86.64.S | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/basis/cpu/x86/64/bootstrap.factor b/basis/cpu/x86/64/bootstrap.factor index 28309e7d97..aa7a5dcd67 100644 --- a/basis/cpu/x86/64/bootstrap.factor +++ b/basis/cpu/x86/64/bootstrap.factor @@ -21,9 +21,7 @@ IN: bootstrap.x86 : rex-length ( -- n ) 1 ; [ - ! HACK: stash vm pointer above the ds stack - temp0 0 MOV rc-absolute-cell rt-vm jit-rel - ds-reg bootstrap-cell [+] temp0 MOV + ! load stack_chain temp0 0 MOV rc-absolute-cell rt-stack-chain jit-rel temp0 temp0 [] MOV @@ -31,6 +29,8 @@ IN: bootstrap.x86 temp0 [] stack-reg MOV ! load XT temp1 0 MOV rc-absolute-cell rt-primitive jit-rel + ! load vm ptr + arg 0 MOV rc-absolute-cell rt-vm jit-rel ! go temp1 JMP ] jit-primitive jit-define diff --git a/vm/cpu-x86.64.S b/vm/cpu-x86.64.S index 4d5c70616f..704cebe804 100644 --- a/vm/cpu-x86.64.S +++ b/vm/cpu-x86.64.S @@ -79,10 +79,11 @@ DEF(long long,read_timestamp_counter,(void)): or %rdx,%rax ret -DEF(void,primitive_inline_cache_miss,(void)): +DEF(void,primitive_inline_cache_miss,(void *vm)): mov (%rsp),%rbx -DEF(void,primitive_inline_cache_miss_tail,(void)): +DEF(void,primitive_inline_cache_miss_tail,(void *vm)): sub $STACK_PADDING,%rsp + mov ARG0,ARG1 mov %rbx,ARG0 call MANGLE(inline_cache_miss) add $STACK_PADDING,%rsp From c7b7517f36dbcdf2db47de04c685e87273775944 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 7 Sep 2009 07:23:29 +0100 Subject: [PATCH 257/345] small x86 asm cleanup --- basis/cpu/x86/32/bootstrap.factor | 2 +- vm/cpu-x86.32.S | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/cpu/x86/32/bootstrap.factor b/basis/cpu/x86/32/bootstrap.factor index afa7c245a0..e2096987da 100644 --- a/basis/cpu/x86/32/bootstrap.factor +++ b/basis/cpu/x86/32/bootstrap.factor @@ -29,7 +29,7 @@ IN: bootstrap.x86 ! save stack pointer temp0 [] stack-reg MOV ! pass vm ptr to primitive - EAX 0 MOV rc-absolute-cell rt-vm jit-rel + arg 0 MOV rc-absolute-cell rt-vm jit-rel ! call the primitive 0 JMP rc-relative rt-primitive jit-rel ] jit-primitive jit-define diff --git a/vm/cpu-x86.32.S b/vm/cpu-x86.32.S index a486fed29d..042924ca4f 100644 --- a/vm/cpu-x86.32.S +++ b/vm/cpu-x86.32.S @@ -53,7 +53,7 @@ DEF(void,primitive_inline_cache_miss,(void *vm)): mov (%esp),%ebx DEF(void,primitive_inline_cache_miss_tail,(void *vm)): sub $8,%esp - push %eax /* push vm ptr */ + push ARG0 /* push vm ptr */ push %ebx call MANGLE(inline_cache_miss) add $16,%esp From 617a7cbd6561c08f2c778c7fbac9e04fd748fc46 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 7 Sep 2009 07:41:11 +0100 Subject: [PATCH 258/345] Added more init code to vm constructor. Also removed dummy variables file as have fixed that problem now --- vm/factor.cpp | 98 ------------------------------------ vm/vm-data-dummy.hpp | 116 ------------------------------------------- vm/vm-data.hpp | 7 ++- 3 files changed, 5 insertions(+), 216 deletions(-) delete mode 100644 vm/vm-data-dummy.hpp diff --git a/vm/factor.cpp b/vm/factor.cpp index 7b95304887..4ef4d11796 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -247,102 +247,4 @@ VM_C_API THREADHANDLE start_standalone_factor_in_new_thread(int argc, vm_char ** return start_thread(start_standalone_factor_thread,args); } - // if you change this struct, also change vm.factor k-------- - context *stack_chain; - zone nursery; /* new objects are allocated here */ - cell cards_offset; - cell decks_offset; - cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ - - // ------------------------------- - - // contexts - cell ds_size, rs_size; - context *unused_contexts; - - // run - cell T; /* Canonical T object. It's just a word */ - - // profiler - bool profiling_p; - - // errors - /* Global variables used to pass fault handler state from signal handler to - user-space */ - cell signal_number; - cell signal_fault_addr; - unsigned int signal_fpu_status; - stack_frame *signal_callstack_top; - - //data_heap - bool secure_gc; /* Set by the -securegc command line argument */ - bool gc_off; /* GC is off during heap walking */ - data_heap *data; - /* A heap walk allows useful things to be done, like finding all - references to an object for debugging purposes. */ - cell heap_scan_ptr; - //write barrier - cell allot_markers_offset; - //data_gc - /* used during garbage collection only */ - zone *newspace; - bool performing_gc; - bool performing_compaction; - cell collecting_gen; - /* if true, we are collecting aging space for the second time, so if it is still - full, we go on to collect tenured */ - bool collecting_aging_again; - /* in case a generation fills up in the middle of a gc, we jump back - up to try collecting the next generation. */ - jmp_buf gc_jmp; - gc_stats stats[max_gen_count]; - u64 cards_scanned; - u64 decks_scanned; - u64 card_scan_time; - cell code_heap_scans; - /* What generation was being collected when copy_code_heap_roots() was last - called? Until the next call to add_code_block(), future - collections of younger generations don't have to touch the code - heap. */ - cell last_code_heap_scan; - /* sometimes we grow the heap */ - bool growing_data_heap; - data_heap *old_data_heap; - - // local roots - /* If a runtime function needs to call another function which potentially - allocates memory, it must wrap any local variable references to Factor - objects in gc_root instances */ - std::vector gc_locals; - std::vector gc_bignums; - - //debug - bool fep_disabled; - bool full_output; - cell look_for; - cell obj; - - //math - cell bignum_zero; - cell bignum_pos_one; - cell bignum_neg_one; - - //code_heap - heap code; - unordered_map forwarding; - - //image - cell code_relocation_base; - cell data_relocation_base; - - //dispatch - cell megamorphic_cache_hits; - cell megamorphic_cache_misses; - - //inline cache - cell max_pic_size; - cell cold_call_to_ic_transitions; - cell ic_to_pic_transitions; - cell pic_to_mega_transitions; - cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ } diff --git a/vm/vm-data-dummy.hpp b/vm/vm-data-dummy.hpp deleted file mode 100644 index c028a61503..0000000000 --- a/vm/vm-data-dummy.hpp +++ /dev/null @@ -1,116 +0,0 @@ -namespace factor -{ - - // if you change this struct, also change vm.factor k-------- - extern "C" context *stack_chain; - extern "C" zone nursery; /* new objects are allocated here */ - extern "C" cell cards_offset; - extern "C" cell decks_offset; -//extern "C" cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ - - // ------------------------------- - - // contexts - extern "C" cell ds_size, rs_size; - extern "C" context *unused_contexts; - - - // profiler - extern "C" bool profiling_p; - - // errors - /* Global variables used to pass fault handler state from signal handler to - user-space */ - extern "C" cell signal_number; - extern "C" cell signal_fault_addr; - extern "C" unsigned int signal_fpu_status; - extern "C" stack_frame *signal_callstack_top; - - //data_heap - extern "C" bool secure_gc; /* Set by the -securegc command line argument */ - extern "C" bool gc_off; /* GC is off during heap walking */ - extern "C" data_heap *data; - /* A heap walk allows useful things to be done, like finding all - references to an object for debugging purposes. */ - extern "C" cell heap_scan_ptr; - //write barrier - extern "C" cell allot_markers_offset; - //data_gc - /* used during garbage collection only */ - extern "C" zone *newspace; - extern "C" bool performing_gc; - extern "C" bool performing_compaction; - extern "C" cell collecting_gen; - /* if true, we are collecting aging space for the second time, so if it is still - full, we go on to collect tenured */ - extern "C" bool collecting_aging_again; - /* in case a generation fills up in the middle of a gc, we jump back - up to try collecting the next generation. */ - extern "C" jmp_buf gc_jmp; - extern "C" gc_stats stats[max_gen_count]; - extern "C" u64 cards_scanned; - extern "C" u64 decks_scanned; - extern "C" u64 card_scan_time; - extern "C" cell code_heap_scans; - /* What generation was being collected when copy_code_heap_roots() was last - called? Until the next call to add_code_block(), future - collections of younger generations don't have to touch the code - heap. */ - extern "C" cell last_code_heap_scan; - /* sometimes we grow the heap */ - extern "C" bool growing_data_heap; - extern "C" data_heap *old_data_heap; - - // local roots - /* If a runtime function needs to call another function which potentially - allocates memory, it must wrap any local variable references to Factor - objects in gc_root instances */ - //extern "C" segment *gc_locals_region; - //extern "C" cell gc_locals; - //extern "C" segment *gc_bignums_region; - //extern "C" cell gc_bignums; - - //debug - extern "C" bool fep_disabled; - extern "C" bool full_output; - extern "C" cell look_for; - extern "C" cell obj; - - //math - extern "C" cell bignum_zero; - extern "C" cell bignum_pos_one; - extern "C" cell bignum_neg_one; - - //code_heap - extern "C" heap code; - extern "C" unordered_map forwarding; - - //image - extern "C" cell code_relocation_base; - extern "C" cell data_relocation_base; - - //dispatch - extern "C" cell megamorphic_cache_hits; - extern "C" cell megamorphic_cache_misses; - - //inline cache - extern "C" cell max_pic_size; - extern "C" cell cold_call_to_ic_transitions; - extern "C" cell ic_to_pic_transitions; - extern "C" cell pic_to_mega_transitions; - extern "C" cell pic_counts[4]; /* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */ - -struct factorvmdata { - cell userenv[USER_ENV]; /* TAGGED user environment data; see getenv/setenv prims */ - - // run - cell T; /* Canonical T object. It's just a word */ - - // local roots - /* If a runtime function needs to call another function which potentially - allocates memory, it must wrap any local variable references to Factor - objects in gc_root instances */ - std::vector gc_locals; - std::vector gc_bignums; -}; -} diff --git a/vm/vm-data.hpp b/vm/vm-data.hpp index de5cf36632..f5ecdc5f62 100644 --- a/vm/vm-data.hpp +++ b/vm/vm-data.hpp @@ -110,8 +110,11 @@ struct factorvmdata { collecting_aging_again(false), growing_data_heap(false), fep_disabled(false), - full_output(false) - {} + full_output(false), + max_pic_size(0) + { + memset(this,0,sizeof(this)); // just to make sure + } }; From a8d1e5187d98cf5d0e776415ae16e73a5475fbc9 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 7 Sep 2009 08:18:41 +0100 Subject: [PATCH 259/345] Added -DREENTRANT option to Makefile Also renamed FACTOR_MULTITHREADED to FACTOR_REENTRANT --- Makefile | 4 ++++ vm/vm.hpp | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index bfaa6ec7a3..10efe34d34 100755 --- a/Makefile +++ b/Makefile @@ -18,6 +18,10 @@ else CFLAGS += -O3 endif +ifdef REENTRANT + CFLAGS += -DFACTOR_REENTRANT +endif + CFLAGS += $(SITE_CFLAGS) ENGINE = $(DLL_PREFIX)factor$(DLL_SUFFIX)$(DLL_EXTENSION) diff --git a/vm/vm.hpp b/vm/vm.hpp index 3c0f4a246b..e4cd633fb1 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -610,8 +610,9 @@ struct factorvm : factorvmdata { }; - -#define FACTOR_SINGLE_THREADED_SINGLETON +#ifndef FACTOR_REENTRANT + #define FACTOR_SINGLE_THREADED_SINGLETON +#endif #ifdef FACTOR_SINGLE_THREADED_SINGLETON /* calls are dispatched using the singleton vm ptr */ @@ -633,7 +634,7 @@ struct factorvm : factorvmdata { #define SIGNAL_VM_PTR() tls_vm() #endif -#ifdef FACTOR_MULTITHREADED_TLS +#ifdef FACTOR_REENTRANT_TLS /* uses thread local storage to obtain vm ptr */ #define PRIMITIVE_GETVM() tls_vm() #define PRIMITIVE_OVERFLOW_GETVM() tls_vm() @@ -642,7 +643,7 @@ struct factorvm : factorvmdata { #define SIGNAL_VM_PTR() tls_vm() #endif -#ifdef FACTOR_MULTITHREADED +#ifdef FACTOR_REENTRANT #define PRIMITIVE_GETVM() ((factorvm*)myvm) #define PRIMITIVE_OVERFLOW_GETVM() ((factorvm*)myvm) #define VM_PTR myvm From 8049b441c2ccdc1571ea7cbc134d3865c008816d Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 7 Sep 2009 18:20:43 +0100 Subject: [PATCH 260/345] imul clobbers arg2 on x86.64, so stashing vm ptr on the stack --- vm/cpu-x86.S | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vm/cpu-x86.S b/vm/cpu-x86.S index a18d1483fb..5360d6c227 100644 --- a/vm/cpu-x86.S +++ b/vm/cpu-x86.S @@ -21,7 +21,7 @@ DEF(void,primitive_fixnum_subtract,(void *myvm)): ret DEF(void,primitive_fixnum_multiply,(void *myvm)): - mov ARG0, ARG2 /* save vm ptr for overflow */ + push ARG0 /* save vm ptr for overflow */ mov (DS_REG),ARITH_TEMP_1 mov ARITH_TEMP_1,DIV_RESULT mov -CELL_SIZE(DS_REG),ARITH_TEMP_2 @@ -30,14 +30,16 @@ DEF(void,primitive_fixnum_multiply,(void *myvm)): imul ARITH_TEMP_2 jo multiply_overflow mov DIV_RESULT,(DS_REG) + pop ARG2 ret - multiply_overflow: sar $3,ARITH_TEMP_1 mov ARITH_TEMP_1,ARG0 mov ARITH_TEMP_2,ARG1 + pop ARG2 jmp MANGLE(overflow_fixnum_multiply) + DEF(F_FASTCALL void,c_to_factor,(CELL quot, void *vm)): PUSH_NONVOLATILE mov ARG0,NV_TEMP_REG From 2cf2dab48e26431772b615e252bcc17034b6a390 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sun, 13 Sep 2009 21:50:20 +0100 Subject: [PATCH 261/345] fpe signals working on unix again --- vm/os-unix.cpp | 17 +++++++++++------ vm/vm.hpp | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/vm/os-unix.cpp b/vm/os-unix.cpp index 3f4e1b29f7..65b32066e5 100644 --- a/vm/os-unix.cpp +++ b/vm/os-unix.cpp @@ -174,16 +174,21 @@ void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap) SIGNAL_VM_PTR()->misc_signal_handler(signal,siginfo,uap); } -void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap) +void factorvm::fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap) { signal_number = signal; signal_callstack_top = uap_stack_pointer(uap); - signal_fpu_status = fpu_status(uap_fpu_status(uap)); - uap_clear_fpu_status(uap); + signal_fpu_status = fpu_status(uap_fpu_status(uap)); + uap_clear_fpu_status(uap); UAP_PROGRAM_COUNTER(uap) = - (siginfo->si_code == FPE_INTDIV || siginfo->si_code == FPE_INTOVF) - ? (cell)misc_signal_handler_impl - : (cell)fp_signal_handler_impl; + (siginfo->si_code == FPE_INTDIV || siginfo->si_code == FPE_INTOVF) + ? (cell)factor::misc_signal_handler_impl + : (cell)factor::fp_signal_handler_impl; +} + +void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap) +{ + SIGNAL_VM_PTR()->fpe_signal_handler(signal, siginfo, uap); } static void sigaction_safe(int signum, const struct sigaction *act, struct sigaction *oldact) diff --git a/vm/vm.hpp b/vm/vm.hpp index e4cd633fb1..b426ff67d8 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -602,6 +602,7 @@ struct factorvm : factorvmdata { #else // UNIX void memory_signal_handler(int signal, siginfo_t *siginfo, void *uap); void misc_signal_handler(int signal, siginfo_t *siginfo, void *uap); + void fpe_signal_handler(int signal, siginfo_t *siginfo, void *uap); stack_frame *uap_stack_pointer(void *uap); #endif From d2afb4b3442c074f8959b1f59b7f32c83e910254 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 14 Sep 2009 08:00:28 +0100 Subject: [PATCH 262/345] put mach call_fault_handler in the vm --- vm/mach_signal.cpp | 31 ++++++++++++++++++++----------- vm/vm.hpp | 4 ++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/vm/mach_signal.cpp b/vm/mach_signal.cpp index 03a6ca2b72..08b0d00f1c 100644 --- a/vm/mach_signal.cpp +++ b/vm/mach_signal.cpp @@ -28,7 +28,7 @@ http://www.wodeveloper.com/omniLists/macosx-dev/2000/June/msg00137.html */ /* Modify a suspended thread's thread_state so that when the thread resumes executing, the call frame of the current C primitive (if any) is rewound, and the appropriate Factor error is thrown from the top-most Factor frame. */ -static void call_fault_handler( +void factorvm::call_fault_handler( exception_type_t exception, exception_data_type_t code, MACH_EXC_STATE_TYPE *exc_state, @@ -41,33 +41,42 @@ static void call_fault_handler( a divide by zero or stack underflow in the listener */ /* Are we in compiled Factor code? Then use the current stack pointer */ - if(SIGNAL_VM_PTR()->in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) - SIGNAL_VM_PTR()->signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); + if(in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state))) + signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state); /* Are we in C? Then use the saved callstack top */ else - SIGNAL_VM_PTR()->signal_callstack_top = NULL; + signal_callstack_top = NULL; MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state)); /* Now we point the program counter at the right handler function. */ if(exception == EXC_BAD_ACCESS) { - SIGNAL_VM_PTR()->signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); - MACH_PROGRAM_COUNTER(thread_state) = (cell)memory_signal_handler_impl; + signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state); + MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::memory_signal_handler_impl; } else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV) { - signal_fpu_status = fpu_status(mach_fpu_status(float_state)); - mach_clear_fpu_status(float_state); - MACH_PROGRAM_COUNTER(thread_state) = (cell)fp_signal_handler_impl; + signal_fpu_status = fpu_status(mach_fpu_status(float_state)); + mach_clear_fpu_status(float_state); + MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::fp_signal_handler_impl; } else { - SIGNAL_VM_PTR()->signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); - MACH_PROGRAM_COUNTER(thread_state) = (cell)misc_signal_handler_impl; + signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT); + MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::misc_signal_handler_impl; } } +static void call_fault_handler(exception_type_t exception, + exception_data_type_t code, + MACH_EXC_STATE_TYPE *exc_state, + MACH_THREAD_STATE_TYPE *thread_state, + MACH_FLOAT_STATE_TYPE *float_state) +{ + SIGNAL_VM_PTR()->call_fault_handler(exception,code,exc_state,thread_state,float_state); +} + /* Handle an exception by invoking the user's fault handler and/or forwarding the duty to the previously installed handlers. */ extern "C" diff --git a/vm/vm.hpp b/vm/vm.hpp index b426ff67d8..76a2adb9c6 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -607,6 +607,10 @@ struct factorvm : factorvmdata { #endif + #ifdef __APPLE__ + void call_fault_handler(exception_type_t exception, exception_data_type_t code, MACH_EXC_STATE_TYPE *exc_state, MACH_THREAD_STATE_TYPE *thread_state, MACH_FLOAT_STATE_TYPE *float_state); + #endif + void print_vm_data(); }; From 01d2ef415ac71210614ea6fff0d032edef67d59b Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 09:20:47 -0500 Subject: [PATCH 263/345] get compiler tests loading --- basis/classes/struct/struct.factor | 2 +- basis/compiler/tests/alien.factor | 1 + basis/compiler/tests/codegen.factor | 3 ++- basis/compiler/tests/intrinsics.factor | 1 + basis/compiler/tree/cleanup/cleanup-tests.factor | 1 + basis/compiler/tree/propagation/propagation-tests.factor | 1 + basis/cpu/x86/64/unix/unix.factor | 2 +- 7 files changed, 8 insertions(+), 3 deletions(-) diff --git a/basis/classes/struct/struct.factor b/basis/classes/struct/struct.factor index a96a74d2ac..dabdead10c 100755 --- a/basis/classes/struct/struct.factor +++ b/basis/classes/struct/struct.factor @@ -126,7 +126,7 @@ M: struct-c-type c-type ; M: struct-c-type c-type-stack-align? drop f ; : if-value-struct ( ctype true false -- ) - [ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline + [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline M: struct-c-type unbox-parameter [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ; diff --git a/basis/compiler/tests/alien.factor b/basis/compiler/tests/alien.factor index 484b1f4f2f..e21e13dc13 100755 --- a/basis/compiler/tests/alien.factor +++ b/basis/compiler/tests/alien.factor @@ -5,6 +5,7 @@ io.streams.string kernel math memory namespaces namespaces.private parser quotations sequences specialized-arrays stack-checker stack-checker.errors system threads tools.test words ; +FROM: alien.c-types => float short ; SPECIALIZED-ARRAY: float SPECIALIZED-ARRAY: char IN: compiler.tests.alien diff --git a/basis/compiler/tests/codegen.factor b/basis/compiler/tests/codegen.factor index fcbac30444..56e368e320 100644 --- a/basis/compiler/tests/codegen.factor +++ b/basis/compiler/tests/codegen.factor @@ -4,6 +4,7 @@ namespaces.private slots.private sequences.private byte-arrays alien alien.accessors layouts words definitions compiler.units io combinators vectors grouping make alien.c-types combinators.short-circuit math.order math.libm math.parser ; +FROM: math => float ; QUALIFIED: namespaces.private IN: compiler.tests.codegen @@ -414,4 +415,4 @@ cell 4 = [ [ "0.169967142900241" "0.9854497299884601" ] [ 1.4 [ [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test [ 1 "0.169967142900241" "0.9854497299884601" ] [ 1.4 1 [ swap >float [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test -[ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test \ No newline at end of file +[ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test diff --git a/basis/compiler/tests/intrinsics.factor b/basis/compiler/tests/intrinsics.factor index ad2d2c8be5..dc2f5d9257 100644 --- a/basis/compiler/tests/intrinsics.factor +++ b/basis/compiler/tests/intrinsics.factor @@ -5,6 +5,7 @@ hashtables.private byte-arrays system random layouts vectors sbufs strings.private slots.private alien math.order alien.accessors alien.c-types alien.syntax alien.strings namespaces libc io.encodings.ascii classes compiler ; +FROM: math => float ; IN: compiler.tests.intrinsics ! Make sure that intrinsic ops compile to correct code. diff --git a/basis/compiler/tree/cleanup/cleanup-tests.factor b/basis/compiler/tree/cleanup/cleanup-tests.factor index faf6968670..02e7409c24 100755 --- a/basis/compiler/tree/cleanup/cleanup-tests.factor +++ b/basis/compiler/tree/cleanup/cleanup-tests.factor @@ -16,6 +16,7 @@ compiler.tree.propagation compiler.tree.propagation.info compiler.tree.checker compiler.tree.debugger ; +FROM: math => float ; IN: compiler.tree.cleanup.tests [ t ] [ [ [ 1 ] [ 2 ] if ] cleaned-up-tree [ #if? ] contains-node? ] unit-test diff --git a/basis/compiler/tree/propagation/propagation-tests.factor b/basis/compiler/tree/propagation/propagation-tests.factor index 0c220542ca..0da234791b 100644 --- a/basis/compiler/tree/propagation/propagation-tests.factor +++ b/basis/compiler/tree/propagation/propagation-tests.factor @@ -10,6 +10,7 @@ compiler.tree.debugger compiler.tree.checker slots.private words hashtables classes assocs locals specialized-arrays system sorting math.libm math.intervals quotations effects alien ; +FROM: math => float ; SPECIALIZED-ARRAY: double IN: compiler.tree.propagation.tests diff --git a/basis/cpu/x86/64/unix/unix.factor b/basis/cpu/x86/64/unix/unix.factor index 2f8a01f0fe..13e91a87a4 100644 --- a/basis/cpu/x86/64/unix/unix.factor +++ b/basis/cpu/x86/64/unix/unix.factor @@ -34,7 +34,7 @@ stack-params \ (stack-value) c-type (>>rep) >> : flatten-small-struct ( c-type -- seq ) struct-types&offset split-struct [ [ c-type c-type-rep reg-class-of ] map - int-regs swap member? "void*" "double" ? c-type + int-regs swap member? void* double ? c-type ] map ; : flatten-large-struct ( c-type -- seq ) From 21c09ab97ad2b4cf5c18e3c29eef6f4e3fe76605 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 09:56:07 -0500 Subject: [PATCH 264/345] fix struct class see --- basis/classes/struct/prettyprint/prettyprint.factor | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/basis/classes/struct/prettyprint/prettyprint.factor b/basis/classes/struct/prettyprint/prettyprint.factor index 2c969531e8..7f57e8568a 100644 --- a/basis/classes/struct/prettyprint/prettyprint.factor +++ b/basis/classes/struct/prettyprint/prettyprint.factor @@ -1,9 +1,9 @@ ! (c)Joe Groff bsd license -USING: accessors alien alien.c-types arrays assocs classes -classes.struct combinators combinators.short-circuit continuations -fry kernel libc make math math.parser mirrors prettyprint.backend -prettyprint.custom prettyprint.sections see.private sequences -slots strings summary words ; +USING: accessors alien alien.c-types alien.prettyprint arrays +assocs classes classes.struct combinators combinators.short-circuit +continuations fry kernel libc make math math.parser mirrors +prettyprint.backend prettyprint.custom prettyprint.sections +see.private sequences slots strings summary words ; IN: classes.struct.prettyprint > text ] - [ type>> dup string? [ text ] [ pprint* ] if ] + [ type>> pprint-c-type ] [ read-only>> [ \ read-only pprint-word ] when ] [ initial>> [ \ initial: pprint-word pprint* ] when* ] } cleave block> From b403ba5c1755dda9682d3ba7d696cd4d9879a6c0 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 10:24:03 -0500 Subject: [PATCH 265/345] fix FUNCTION: prettyprint when function has no arguments --- basis/alien/prettyprint/prettyprint.factor | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/basis/alien/prettyprint/prettyprint.factor b/basis/alien/prettyprint/prettyprint.factor index 096a5547c5..4586c08542 100644 --- a/basis/alien/prettyprint/prettyprint.factor +++ b/basis/alien/prettyprint/prettyprint.factor @@ -36,9 +36,11 @@ M: typedef-word synopsis* [ pprint-c-type ] [ text ] bi* ; : pprint-function-args ( word -- ) - [ def>> fourth ] [ stack-effect in>> ] bi zip unclip-last - [ [ first2 "," append pprint-function-arg ] each ] dip - first2 pprint-function-arg ; + [ def>> fourth ] [ stack-effect in>> ] bi zip [ ] [ + unclip-last + [ [ first2 "," append pprint-function-arg ] each ] dip + first2 pprint-function-arg + ] if-empty ; M: alien-function-word definer drop \ FUNCTION: \ ; ; From fc5500a0dcfad986222f41085f151f92802763c5 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 13:11:53 -0500 Subject: [PATCH 266/345] oops... word c-types can be structs too --- basis/alien/c-types/c-types.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index 7dc00333b8..f147810cd2 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -94,7 +94,7 @@ GENERIC: c-struct? ( type -- ? ) M: object c-struct? drop f ; -M: string c-struct? +M: c-type-name c-struct? dup void? [ drop f ] [ c-type c-struct? ] if ; ! These words being foldable means that words need to be @@ -123,7 +123,7 @@ M: c-type-name GENERIC: (c-array) ( len c-type -- array ) -M: string (c-array) +M: c-type-name (c-array) c-(array)-constructor execute( len -- array ) ; inline GENERIC: ( alien len c-type -- array ) From 123a962596345e2c29b297010fcdf8753fa36cdc Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 13:42:40 -0500 Subject: [PATCH 267/345] fix button background (from Keith Lazuka ) --- basis/ui/gadgets/buttons/buttons.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/ui/gadgets/buttons/buttons.factor b/basis/ui/gadgets/buttons/buttons.factor index 26cbafc0d5..fb6f8153e9 100644 --- a/basis/ui/gadgets/buttons/buttons.factor +++ b/basis/ui/gadgets/buttons/buttons.factor @@ -119,7 +119,7 @@ PRIVATE> [ append theme-image ] tri-curry@ tri ] 2dip ; -CONSTANT: button-background COLOR: FactorLightTan +CONSTANT: button-background COLOR: FactorTan CONSTANT: button-clicked-background COLOR: FactorDarkSlateBlue : ( -- pen ) From e70c063e61a65be2e208ed2db7d313cbb6201bd8 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 14:17:13 -0500 Subject: [PATCH 268/345] fix alien-function-word predicate --- basis/alien/parser/parser.factor | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/basis/alien/parser/parser.factor b/basis/alien/parser/parser.factor index 662139810e..ab09383d7c 100644 --- a/basis/alien/parser/parser.factor +++ b/basis/alien/parser/parser.factor @@ -1,9 +1,9 @@ ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: accessors alien alien.c-types arrays assocs -combinators effects grouping kernel parser sequences -splitting words fry locals lexer namespaces summary -math vocabs.parser ; +combinators combinators.short-circuit effects grouping +kernel parser sequences splitting words fry locals lexer +namespaces summary math vocabs.parser ; IN: alien.parser : parse-c-type-name ( name -- word/string ) @@ -58,4 +58,7 @@ IN: alien.parser make-function define-declared ; PREDICATE: alien-function-word < word - def>> [ length 5 = ] [ last \ alien-invoke eq? ] bi and ; + def>> { + [ length 5 = ] + [ last \ alien-invoke eq? ] + } 1&& ; From 53abf7e15d3b95e27aa632c2494263f137119de9 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 16 Sep 2009 13:17:15 -0700 Subject: [PATCH 269/345] add recaptcha vocabulary --- .../furnace/chloe-tags/recaptcha/authors.txt | 1 + .../recaptcha/recaptcha-docs.factor | 77 +++++++++++++++++++ .../chloe-tags/recaptcha/recaptcha.factor | 75 ++++++++++++++++++ .../chloe-tags/recaptcha/recaptcha.xml | 7 ++ .../furnace/chloe-tags/recaptcha/summary.txt | 1 + basis/furnace/chloe-tags/recaptcha/tags.txt | 1 + 6 files changed, 162 insertions(+) create mode 100644 basis/furnace/chloe-tags/recaptcha/authors.txt create mode 100644 basis/furnace/chloe-tags/recaptcha/recaptcha-docs.factor create mode 100644 basis/furnace/chloe-tags/recaptcha/recaptcha.factor create mode 100644 basis/furnace/chloe-tags/recaptcha/recaptcha.xml create mode 100644 basis/furnace/chloe-tags/recaptcha/summary.txt create mode 100644 basis/furnace/chloe-tags/recaptcha/tags.txt diff --git a/basis/furnace/chloe-tags/recaptcha/authors.txt b/basis/furnace/chloe-tags/recaptcha/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/basis/furnace/chloe-tags/recaptcha/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/basis/furnace/chloe-tags/recaptcha/recaptcha-docs.factor b/basis/furnace/chloe-tags/recaptcha/recaptcha-docs.factor new file mode 100644 index 0000000000..0d93949f53 --- /dev/null +++ b/basis/furnace/chloe-tags/recaptcha/recaptcha-docs.factor @@ -0,0 +1,77 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: help.markup help.syntax http.server.filters kernel +multiline furnace.actions ; +IN: furnace.chloe-tags.recaptcha + +HELP: +{ $values + { "responder" "a responder" } + { "obj" object } +} +{ $description "A " { $link filter-responder } " wrapping another responder. Set the domain, public, and private keys using the key you get by registering with Recaptcha." } ; + +HELP: recaptcha-error +{ $var-description "Set to the error string returned by the Recaptcha server." } ; + +HELP: recaptcha-valid? +{ $var-description "Set to " { $link t } " if the user solved the last Recaptcha correctly." } ; + +HELP: validate-recaptcha +{ $description "Validates a Recaptcha using the Recaptcha web service API." } ; + +ARTICLE: "recaptcha-example" "Recaptcha example" +"There are several steps to using the Recaptcha library." +{ $list + { "Wrap the responder in a " { $link } } + { "Add a handler calling " { $link validate-recaptcha } " in the " { $slot "submit" } " of the " { $link page-action } } + { "Put the chloe tag " { $snippet "" } " in the template for your " { $link action } } +} +"An example follows:" +{ $code +HEREDOC: RECAPTCHA-TUTORIAL +TUPLE: recaptcha-app < dispatcher recaptcha ; + +: ( -- obj ) + + [ + validate-recaptcha + recaptcha-valid? get "?good" "?bad" ? + ] >>submit + [ + +{" + + +"} >>body + ] >>display ; + +: ( -- obj ) + \ recaptcha-app new-dispatcher + "" add-responder + + "concatenative.org" >>domain + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" >>public-key + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" >>private-key ; + + main-responder set-global +RECAPTCHA-TUTORIAL +} + +; + +ARTICLE: "furnace.chloe-tags.recaptcha" "Recaptcha chloe tag" +"The " { $vocab-link "furnace.chloe-tags.recaptcha" } " vocabulary implements support for the Recaptcha. Recaptcha is a web service that provides the user with a captcha, a test that is easy to solve by visual inspection, but hard to solve by writing a computer program. Use a captcha to protect forms from abusive users." $nl + +"The recaptcha responder is a " { $link filter-responder } " that wraps another responder. Set the " { $slot "domain" } ", " { $slot "public-key" } ", and " { $slot "private-key" } " slots of this responder to your Recaptcha account information." $nl + +"Wrapping a responder with Recaptcha:" +{ $subsection } +"Validating recaptcha:" +{ $subsection validate-recaptcha } +"Symbols set after validation:" +{ $subsection recaptcha-valid? } +{ $subsection recaptcha-error } +{ $subsection "recaptcha-example" } ; + +ABOUT: "furnace.chloe-tags.recaptcha" diff --git a/basis/furnace/chloe-tags/recaptcha/recaptcha.factor b/basis/furnace/chloe-tags/recaptcha/recaptcha.factor new file mode 100644 index 0000000000..81744dc0e0 --- /dev/null +++ b/basis/furnace/chloe-tags/recaptcha/recaptcha.factor @@ -0,0 +1,75 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors furnace.actions furnace.redirection html.forms +html.templates.chloe.compiler html.templates.chloe.syntax +http.client http.server http.server.filters io.sockets kernel +locals namespaces sequences splitting urls validators +xml.syntax ; +IN: furnace.chloe-tags.recaptcha + +TUPLE: recaptcha < filter-responder domain public-key private-key ; + +SYMBOLS: recaptcha-valid? recaptcha-error ; + +: ( responder -- obj ) + recaptcha new + swap >>responder ; + +M: recaptcha call-responder* + dup \ recaptcha set + responder>> call-responder ; + +> + + + +XML] ; + +: recaptcha-url ( secure? -- ? ) + [ "https://api.recaptcha.net/challenge" >url ] + [ "http://api.recaptcha.net/challenge" >url ] if ; + +: render-recaptcha ( -- xml ) + secure-connection? recaptcha-url + recaptcha get public-key>> "k" set-query-param (render-recaptcha) ; + +: parse-recaptcha-response ( string -- valid? error ) + "\n" split first2 [ "true" = ] dip ; + +:: (validate-recaptcha) ( challenge response recaptcha -- valid? error ) + recaptcha private-key>> :> private-key + remote-address get host>> :> remote-ip + H{ + { "challenge" challenge } + { "response" response } + { "privatekey" private-key } + { "remoteip" remote-ip } + } URL" http://api-verify.recaptcha.net/verify" + http-request nip parse-recaptcha-response ; + +CHLOE: recaptcha + drop [ render-recaptcha ] [xml-code] ; + +PRIVATE> + +: validate-recaptcha ( -- ) + { + { "recaptcha_challenge_field" [ v-required ] } + { "recaptcha_response_field" [ v-required ] } + } validate-params + "recaptcha_challenge_field" value + "recaptcha_response_field" value + \ recaptcha get (validate-recaptcha) + [ recaptcha-valid? set ] [ recaptcha-error set ] bi* ; diff --git a/basis/furnace/chloe-tags/recaptcha/recaptcha.xml b/basis/furnace/chloe-tags/recaptcha/recaptcha.xml new file mode 100644 index 0000000000..6cbf795310 --- /dev/null +++ b/basis/furnace/chloe-tags/recaptcha/recaptcha.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/basis/furnace/chloe-tags/recaptcha/summary.txt b/basis/furnace/chloe-tags/recaptcha/summary.txt new file mode 100644 index 0000000000..909566f3cc --- /dev/null +++ b/basis/furnace/chloe-tags/recaptcha/summary.txt @@ -0,0 +1 @@ +Recaptcha library diff --git a/basis/furnace/chloe-tags/recaptcha/tags.txt b/basis/furnace/chloe-tags/recaptcha/tags.txt new file mode 100644 index 0000000000..c0772185a0 --- /dev/null +++ b/basis/furnace/chloe-tags/recaptcha/tags.txt @@ -0,0 +1 @@ +web From c880d3fff3af76ecfb1da228cf1f8cba611e90b3 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 15:41:38 -0500 Subject: [PATCH 270/345] update functors tests --- basis/functors/functors-tests.factor | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/basis/functors/functors-tests.factor b/basis/functors/functors-tests.factor index 5f2e32ad71..58da96aa17 100644 --- a/basis/functors/functors-tests.factor +++ b/basis/functors/functors-tests.factor @@ -161,15 +161,15 @@ T-class DEFINES-CLASS ${T} WHERE STRUCT: T-class - { NAME int } + { NAME c:int } { x { TYPE 4 } } - { y { short N } } + { y { c:short N } } { z TYPE initial: 5 } { float { c:float 2 } } ; ;FUNCTOR -"a-struct" "nemo" "char" 2 define-a-struct +"a-struct" "nemo" c:char 2 define-a-struct >> @@ -180,35 +180,35 @@ STRUCT: T-class { offset 0 } { class integer } { initial 0 } - { c-type int } + { type c:int } } T{ struct-slot-spec { name "x" } { offset 4 } { class object } { initial f } - { c-type { char 4 } } + { type { c:char 4 } } } T{ struct-slot-spec { name "y" } { offset 8 } { class object } { initial f } - { c-type { short 2 } } + { type { c:short 2 } } } T{ struct-slot-spec { name "z" } { offset 12 } { class fixnum } { initial 5 } - { c-type char } + { type c:char } } T{ struct-slot-spec { name "float" } { offset 16 } { class object } { initial f } - { c-type { c:float 2 } } + { type { c:float 2 } } } } ] [ a-struct struct-slots ] unit-test From 40620d470fc50175aa709687c8633bff09b1810b Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 17:18:19 -0500 Subject: [PATCH 271/345] allow word c-types and definitions to coexist --- basis/alien/parser/parser.factor | 6 ++++++ basis/alien/syntax/syntax.factor | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/basis/alien/parser/parser.factor b/basis/alien/parser/parser.factor index ab09383d7c..9a24f7cd4d 100644 --- a/basis/alien/parser/parser.factor +++ b/basis/alien/parser/parser.factor @@ -24,6 +24,12 @@ IN: alien.parser [ drop \ } parse-until >array ] [ parse-c-type ] if ; +: reset-c-type ( word -- ) + { "c-type" "pointer-c-type" } reset-props ; + +: CREATE-C-TYPE ( -- word ) + scan current-vocab create dup reset-c-type ; + : normalize-c-arg ( type name -- type' name' ) [ length ] [ diff --git a/basis/alien/syntax/syntax.factor b/basis/alien/syntax/syntax.factor index fac45176a3..0e3b569fff 100644 --- a/basis/alien/syntax/syntax.factor +++ b/basis/alien/syntax/syntax.factor @@ -19,7 +19,7 @@ SYNTAX: FUNCTION: (FUNCTION:) define-declared ; SYNTAX: TYPEDEF: - scan-c-type CREATE typedef ; + scan-c-type CREATE-C-TYPE typedef ; SYNTAX: C-STRUCT: scan current-vocab parse-definition define-struct ; deprecated From 58756c27c53029af3b810247cc003f8cee53c745 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 17:36:50 -0500 Subject: [PATCH 272/345] have typedefs take on the old type's pointer type even when the new type is a word and the old a string --- basis/alien/c-types/c-types.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index f147810cd2..ecdc926b62 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -318,8 +318,8 @@ M: word typedef ( old new -- ) [ name>> typedef ] [ swap "c-type" set-word-prop ] [ - swap dup word? [ - "pointer-c-type" word-prop + swap dup c-type-name? [ + resolve-pointer-type "pointer-c-type" set-word-prop ] [ 2drop ] if ] From d1c69efc0f4843dafa4fc201fdfb68523d9613dc Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 17:38:01 -0500 Subject: [PATCH 273/345] fix functors tests --- basis/functors/functors-tests.factor | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/basis/functors/functors-tests.factor b/basis/functors/functors-tests.factor index bcdc1bae74..32d578d05d 100644 --- a/basis/functors/functors-tests.factor +++ b/basis/functors/functors-tests.factor @@ -179,35 +179,35 @@ STRUCT: T-class { offset 0 } { class integer } { initial 0 } - { c-type "int" } + { type "int" } } T{ struct-slot-spec { name "x" } { offset 4 } { class object } { initial f } - { c-type { "char" 4 } } + { type { "char" 4 } } } T{ struct-slot-spec { name "y" } { offset 8 } { class object } { initial f } - { c-type { "short" 2 } } + { type { "short" 2 } } } T{ struct-slot-spec { name "z" } { offset 12 } { class fixnum } { initial 5 } - { c-type "char" } + { type "char" } } T{ struct-slot-spec { name "float" } { offset 16 } { class object } { initial f } - { c-type { "float" 2 } } + { type { "float" 2 } } } } ] [ a-struct struct-slots ] unit-test From ef98f1de68a3452ceedb7c6e91de75f71ec50d4a Mon Sep 17 00:00:00 2001 From: Bruno Deferrari Date: Wed, 16 Sep 2009 19:58:45 -0300 Subject: [PATCH 274/345] irc.client: Fix detach chat (wasn't sending a PART message) --- extra/irc/client/internals/internals-tests.factor | 8 +++++++- extra/irc/client/internals/internals.factor | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/extra/irc/client/internals/internals-tests.factor b/extra/irc/client/internals/internals-tests.factor index a591fe9ce0..84510fb67e 100644 --- a/extra/irc/client/internals/internals-tests.factor +++ b/extra/irc/client/internals/internals-tests.factor @@ -99,7 +99,13 @@ M: mb-writer dispose drop ; ! Test join [ { "JOIN #factortest" } [ - "#factortest" %join %pop-output-line + "#factortest" %join %pop-output-line + ] unit-test +] spawning-irc + +[ { "PART #factortest" } [ + "#factortest" %join %pop-output-line drop + "#factortest" chat> remove-chat %pop-output-line ] unit-test ] spawning-irc diff --git a/extra/irc/client/internals/internals.factor b/extra/irc/client/internals/internals.factor index 6ce851e7dd..ef1695f563 100644 --- a/extra/irc/client/internals/internals.factor +++ b/extra/irc/client/internals/internals.factor @@ -172,7 +172,7 @@ M: irc-nick-chat remove-chat name>> unregister-chat ; M: irc-server-chat remove-chat drop +server-chat+ unregister-chat ; M: irc-channel-chat remove-chat - [ part new annotate-message irc-send ] + [ name>> "PART " prepend string>irc-message irc-send ] [ name>> unregister-chat ] bi ; : (speak) ( message irc-chat -- ) swap annotate-message irc-send ; From 748ba4b833f2912b24d8140714a714cc73a1b551 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 18:07:39 -0500 Subject: [PATCH 275/345] give a better error message when an invalid slot name is used in a tuple/struct literal --- basis/debugger/debugger.factor | 2 ++ core/classes/tuple/parser/parser.factor | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/basis/debugger/debugger.factor b/basis/debugger/debugger.factor index 2fad0e4c2e..1e08896e8d 100644 --- a/basis/debugger/debugger.factor +++ b/basis/debugger/debugger.factor @@ -174,6 +174,8 @@ M: no-method error. M: bad-slot-value summary drop "Bad store to specialized slot" ; +M: bad-slot-name summary drop "Bad slot name in object literal" ; + M: no-math-method summary drop "No suitable arithmetic method" ; diff --git a/core/classes/tuple/parser/parser.factor b/core/classes/tuple/parser/parser.factor index 0a57ad34f3..626cbd63df 100644 --- a/core/classes/tuple/parser/parser.factor +++ b/core/classes/tuple/parser/parser.factor @@ -99,9 +99,17 @@ GENERIC# boa>object 1 ( class slots -- tuple ) M: tuple-class boa>object swap prefix >tuple ; +ERROR: bad-slot-name class slot ; + +: check-slot-exists ( class initials slot-spec/f index/f name -- class initials slot-spec index ) + over [ drop ] [ nip nip nip bad-slot-name ] if ; + +: slot-named-checked ( class initials name slots -- class initials slot-spec ) + over [ slot-named* ] dip check-slot-exists drop ; + : assoc>object ( class slots values -- tuple ) [ [ [ initial>> ] map ] keep ] dip - swap [ [ slot-named* drop ] curry dip ] curry assoc-map + swap [ [ slot-named-checked ] curry dip ] curry assoc-map [ dup ] dip update boa>object ; : parse-tuple-literal-slots ( class slots -- tuple ) From d79b6d590eff83e22152587f5c163bfb74b42f82 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 16 Sep 2009 16:11:05 -0700 Subject: [PATCH 276/345] html.templates.chloe: minor doc fix --- basis/html/templates/chloe/chloe-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/html/templates/chloe/chloe-docs.factor b/basis/html/templates/chloe/chloe-docs.factor index 9716407de8..61121bd769 100644 --- a/basis/html/templates/chloe/chloe-docs.factor +++ b/basis/html/templates/chloe/chloe-docs.factor @@ -24,7 +24,7 @@ HELP: compile-attr { $description "Compiles code which pushes an attribute value previously extracted by " { $link required-attr } " or " { $link optional-attr } " on the stack. If the attribute value begins with " { $snippet "@" } ", compiles into code which pushes the a form value." } ; HELP: CHLOE: -{ $syntax "name definition... ;" } +{ $syntax "CHLOE: name definition... ;" } { $values { "name" "the tag name" } { "definition" { $quotation "( tag -- )" } } } { $description "Defines compilation semantics for the Chloe tag named " { $snippet "tag" } ". The definition body receives a " { $link tag } " on the stack." } ; From 8d83824bb8415bc203dcc102cc6233822305535c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 16 Sep 2009 16:13:55 -0700 Subject: [PATCH 277/345] benchmark.spectral-norm: take out unsafe sequence access since it doesn't make it faster, and replace tuck/2bi* with bi-curry bi* --- extra/benchmark/spectral-norm/spectral-norm.factor | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/extra/benchmark/spectral-norm/spectral-norm.factor b/extra/benchmark/spectral-norm/spectral-norm.factor index 4f93367b8a..41ae5b3578 100644 --- a/extra/benchmark/spectral-norm/spectral-norm.factor +++ b/extra/benchmark/spectral-norm/spectral-norm.factor @@ -1,8 +1,7 @@ ! Factor port of ! http://shootout.alioth.debian.org/gp4/benchmark.php?test=spectralnorm&lang=all USING: specialized-arrays kernel math math.functions -math.vectors sequences sequences.private prettyprint words hints -locals ; +math.vectors sequences prettyprint words hints locals ; SPECIALIZED-ARRAY: double IN: benchmark.spectral-norm @@ -19,13 +18,13 @@ IN: benchmark.spectral-norm + 1 + recip ; inline : (eval-A-times-u) ( u i j -- x ) - tuck [ swap nth-unsafe ] [ eval-A ] 2bi* * ; inline + [ swap nth ] [ eval-A ] bi-curry bi* * ; inline : eval-A-times-u ( n u -- seq ) [ (eval-A-times-u) ] inner-loop ; inline : (eval-At-times-u) ( u i j -- x ) - tuck [ swap nth-unsafe ] [ swap eval-A ] 2bi* * ; inline + [ swap nth ] [ swap eval-A ] bi-curry bi* * ; inline : eval-At-times-u ( u n -- seq ) [ (eval-At-times-u) ] inner-loop ; inline From 9479fb4099f4404978e8ed6f709d9dbdc32b02b2 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 20:54:22 -0500 Subject: [PATCH 278/345] have SPECIALIZED-ARRAY: scan in a c-type rather than a string --- basis/specialized-arrays/specialized-arrays.factor | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/basis/specialized-arrays/specialized-arrays.factor b/basis/specialized-arrays/specialized-arrays.factor index bca85a25db..0490ede304 100755 --- a/basis/specialized-arrays/specialized-arrays.factor +++ b/basis/specialized-arrays/specialized-arrays.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2008, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien alien.c-types assocs byte-arrays classes -compiler.units functors kernel lexer libc math +USING: accessors alien alien.c-types alien.parser assocs +byte-arrays classes compiler.units functors kernel lexer libc math math.vectors.specialization namespaces parser prettyprint.custom sequences sequences.private strings summary vocabs vocabs.loader vocabs.parser words fry combinators ; @@ -157,7 +157,7 @@ M: c-type-name c-direct-array-constructor [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable SYNTAX: SPECIALIZED-ARRAY: - scan define-array-vocab use-vocab ; + scan-c-type define-array-vocab use-vocab ; "prettyprint" vocab [ "specialized-arrays.prettyprint" require From 263ce45932f6926a9654d21f9cf2296d1a82cd1c Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 20:54:57 -0500 Subject: [PATCH 279/345] fix resolve-pointer-type --- basis/alien/c-types/c-types.factor | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index ecdc926b62..6d63987265 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -65,8 +65,12 @@ M: word resolve-pointer-type dup "pointer-c-type" word-prop [ ] [ drop void* ] ?if ; M: string resolve-pointer-type - c-types get at dup c-type-name? - [ resolve-pointer-type ] [ drop void* ] if ; + dup "*" append dup c-types get at + [ nip ] [ + drop + c-types get at dup c-type-name? + [ resolve-pointer-type ] [ drop void* ] if + ] if ; : resolve-typedef ( name -- type ) dup c-type-name? [ c-type ] when ; From 31264538e3f9dfcbf77329d92dae7b8869b9fe34 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 20:55:14 -0500 Subject: [PATCH 280/345] get gpu vocabs to load with c-type changes --- extra/gpu/demos/bunny/bunny.factor | 1 + extra/gpu/render/render.factor | 4 +++- extra/gpu/state/state.factor | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/extra/gpu/demos/bunny/bunny.factor b/extra/gpu/demos/bunny/bunny.factor index 10e49984a1..d6c7456d63 100755 --- a/extra/gpu/demos/bunny/bunny.factor +++ b/extra/gpu/demos/bunny/bunny.factor @@ -7,6 +7,7 @@ io io.encodings.ascii io.files io.files.temp kernel math math.matrices math.parser math.vectors method-chains sequences splitting threads ui ui.gadgets ui.gadgets.worlds ui.pixel-formats specialized-arrays specialized-vectors ; +FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float SPECIALIZED-VECTOR: uint IN: gpu.demos.bunny diff --git a/extra/gpu/render/render.factor b/extra/gpu/render/render.factor index 0ee9ab78c5..9d8c15ab7a 100644 --- a/extra/gpu/render/render.factor +++ b/extra/gpu/render/render.factor @@ -9,7 +9,9 @@ lexer locals math math.order math.parser namespaces opengl opengl.gl parser quotations sequences slots sorting specialized-arrays strings ui.gadgets.worlds variants vocabs.parser words ; -SPECIALIZED-ARRAY: float +FROM: math => float ; +QUALIFIED-WITH: alien.c-types c +SPECIALIZED-ARRAY: c:float SPECIALIZED-ARRAY: int SPECIALIZED-ARRAY: uint SPECIALIZED-ARRAY: void* diff --git a/extra/gpu/state/state.factor b/extra/gpu/state/state.factor index 02d6046722..2bca8f72fc 100755 --- a/extra/gpu/state/state.factor +++ b/extra/gpu/state/state.factor @@ -2,8 +2,10 @@ USING: accessors alien.c-types arrays byte-arrays combinators gpu kernel literals math math.rectangles opengl opengl.gl sequences variants specialized-arrays ; +QUALIFIED-WITH: alien.c-types c +FROM: math => float ; SPECIALIZED-ARRAY: int -SPECIALIZED-ARRAY: float +SPECIALIZED-ARRAY: c:float IN: gpu.state UNION: ?rect rect POSTPONE: f ; From 8f336b4ec00bfd1dd08cb798d5dcae8cf8f7da6f Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 21:24:10 -0500 Subject: [PATCH 281/345] alien.fortran can't piggyback the alien.parser arg parser anymore --- basis/alien/fortran/fortran.factor | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/basis/alien/fortran/fortran.factor b/basis/alien/fortran/fortran.factor index 52d69fd193..3670a376e1 100644 --- a/basis/alien/fortran/fortran.factor +++ b/basis/alien/fortran/fortran.factor @@ -1,5 +1,5 @@ ! (c) 2009 Joe Groff, see BSD license -USING: accessors alien alien.c-types alien.complex alien.parser +USING: accessors alien alien.c-types alien.complex grouping alien.strings alien.syntax arrays ascii assocs byte-arrays combinators combinators.short-circuit fry generalizations kernel lexer macros math math.parser namespaces parser sequences @@ -429,6 +429,11 @@ PRIVATE> MACRO: fortran-invoke ( return library function parameters -- ) { [ 2drop nip set-fortran-abi ] [ (fortran-invoke) ] } 4 ncleave ; +: parse-arglist ( parameters return -- types effect ) + [ 2 group unzip [ "," ?tail drop ] map ] + [ [ { } ] [ 1array ] if-void ] + bi* ; + :: define-fortran-function ( return library function parameters -- ) function create-in dup reset-generic return library function parameters return [ "void" ] unless* parse-arglist From fa60d96ae47006a7071f2c8599f2656e56c98843 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 16 Sep 2009 21:25:46 -0500 Subject: [PATCH 282/345] fix "float" ambiguities in math.blas, opengl vocabs --- basis/math/blas/matrices/matrices.factor | 1 + basis/math/blas/vectors/vectors.factor | 1 + basis/opengl/opengl.factor | 1 + 3 files changed, 3 insertions(+) diff --git a/basis/math/blas/matrices/matrices.factor b/basis/math/blas/matrices/matrices.factor index a051fb250d..4212f32b2d 100755 --- a/basis/math/blas/matrices/matrices.factor +++ b/basis/math/blas/matrices/matrices.factor @@ -5,6 +5,7 @@ math.complex math.functions math.order functors words sequences sequences.merged sequences.private shuffle parser prettyprint.backend prettyprint.custom ascii specialized-arrays ; +FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float SPECIALIZED-ARRAY: double SPECIALIZED-ARRAY: complex-float diff --git a/basis/math/blas/vectors/vectors.factor b/basis/math/blas/vectors/vectors.factor index c08fdb6120..20ee7925b0 100755 --- a/basis/math/blas/vectors/vectors.factor +++ b/basis/math/blas/vectors/vectors.factor @@ -3,6 +3,7 @@ combinators.short-circuit fry kernel math math.blas.ffi math.complex math.functions math.order sequences sequences.private functors words locals parser prettyprint.backend prettyprint.custom specialized-arrays ; +FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float SPECIALIZED-ARRAY: double SPECIALIZED-ARRAY: complex-float diff --git a/basis/opengl/opengl.factor b/basis/opengl/opengl.factor index 75f327664d..cdf68cebd3 100755 --- a/basis/opengl/opengl.factor +++ b/basis/opengl/opengl.factor @@ -8,6 +8,7 @@ math.parser opengl.gl combinators combinators.smart arrays sequences splitting words byte-arrays assocs vocabs colors colors.constants accessors generalizations locals fry specialized-arrays ; +FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float SPECIALIZED-ARRAY: uint IN: opengl From 1f04ed01feda73e467055fe58e842ea267d90ad6 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 09:29:23 -0500 Subject: [PATCH 283/345] fix more ambiguities --- basis/math/vectors/simd/functor/functor.factor | 12 ++++++++---- basis/math/vectors/simd/simd.factor | 14 ++++++++------ .../specialized-arrays-tests.factor | 1 + basis/x11/xlib/xlib.factor | 1 + extra/alien/marshall/marshall.factor | 1 + extra/bunny/model/model.factor | 5 +++-- extra/gpu/textures/textures.factor | 1 + extra/openal/openal.factor | 1 + 8 files changed, 24 insertions(+), 12 deletions(-) diff --git a/basis/math/vectors/simd/functor/functor.factor b/basis/math/vectors/simd/functor/functor.factor index cabb731fef..641585a5d7 100644 --- a/basis/math/vectors/simd/functor/functor.factor +++ b/basis/math/vectors/simd/functor/functor.factor @@ -9,14 +9,16 @@ ERROR: bad-length got expected ; FUNCTOR: define-simd-128 ( T -- ) -N [ 16 T heap-size /i ] +T-TYPE IS ${T} + +N [ 16 T-TYPE heap-size /i ] A DEFINES-CLASS ${T}-${N} >A DEFINES >${A} A{ DEFINES ${A}{ -NTH [ T dup c-type-getter-boxer array-accessor ] -SET-NTH [ T dup c-setter array-accessor ] +NTH [ T-TYPE dup c-type-getter-boxer array-accessor ] +SET-NTH [ T-TYPE dup c-setter array-accessor ] A-rep IS ${A}-rep A-vv->v-op DEFINES-PRIVATE ${A}-vv->v-op @@ -74,7 +76,9 @@ PRIVATE> ! Synthesize 256-bit vectors from a pair of 128-bit vectors FUNCTOR: define-simd-256 ( T -- ) -N [ 32 T heap-size /i ] +T-TYPE IS ${T} + +N [ 32 T-TYPE heap-size /i ] N/2 [ N 2 / ] A/2 IS ${T}-${N/2} diff --git a/basis/math/vectors/simd/simd.factor b/basis/math/vectors/simd/simd.factor index 7df9b2d8d2..a3c99ae217 100644 --- a/basis/math/vectors/simd/simd.factor +++ b/basis/math/vectors/simd/simd.factor @@ -5,6 +5,8 @@ kernel math math.functions math.vectors math.vectors.simd.functor math.vectors.simd.intrinsics math.vectors.specialization parser prettyprint.custom sequences sequences.private locals assocs words fry ; +FROM: alien.c-types => float ; +QUALIFIED-WITH: math m IN: math.vectors.simd << @@ -15,9 +17,9 @@ DEFER: float-8 DEFER: double-4 "double" define-simd-128 -"float" define-simd-128 +"float" define-simd-128 "double" define-simd-256 -"float" define-simd-256 +"float" define-simd-256 >> @@ -136,7 +138,7 @@ DEFER: double-4 PRIVATE> -\ float-4 \ float-4-with float H{ +\ float-4 \ float-4-with m:float H{ { v+ [ [ (simd-v+) ] float-4-vv->v-op ] } { v- [ [ (simd-v-) ] float-4-vv->v-op ] } { v* [ [ (simd-v*) ] float-4-vv->v-op ] } @@ -146,7 +148,7 @@ PRIVATE> { sum [ [ (simd-sum) ] float-4-v->n-op ] } } simd-vector-words -\ double-2 \ double-2-with float H{ +\ double-2 \ double-2-with m:float H{ { v+ [ [ (simd-v+) ] double-2-vv->v-op ] } { v- [ [ (simd-v-) ] double-2-vv->v-op ] } { v* [ [ (simd-v*) ] double-2-vv->v-op ] } @@ -156,7 +158,7 @@ PRIVATE> { sum [ [ (simd-sum) ] double-2-v->n-op ] } } simd-vector-words -\ float-8 \ float-8-with float H{ +\ float-8 \ float-8-with m:float H{ { v+ [ [ (simd-v+) ] float-8-vv->v-op ] } { v- [ [ (simd-v-) ] float-8-vv->v-op ] } { v* [ [ (simd-v*) ] float-8-vv->v-op ] } @@ -166,7 +168,7 @@ PRIVATE> { sum [ [ (simd-sum) ] [ + ] float-8-v->n-op ] } } simd-vector-words -\ double-4 \ double-4-with float H{ +\ double-4 \ double-4-with m:float H{ { v+ [ [ (simd-v+) ] double-4-vv->v-op ] } { v- [ [ (simd-v-) ] double-4-vv->v-op ] } { v* [ [ (simd-v*) ] double-4-vv->v-op ] } diff --git a/basis/specialized-arrays/specialized-arrays-tests.factor b/basis/specialized-arrays/specialized-arrays-tests.factor index 2698149bac..e289efb077 100755 --- a/basis/specialized-arrays/specialized-arrays-tests.factor +++ b/basis/specialized-arrays/specialized-arrays-tests.factor @@ -5,6 +5,7 @@ kernel arrays combinators compiler compiler.units classes.struct combinators.smart compiler.tree.debugger math libc destructors sequences.private multiline eval words vocabs namespaces assocs prettyprint ; +FROM: alien.c-types => float ; SPECIALIZED-ARRAY: int SPECIALIZED-ARRAY: bool diff --git a/basis/x11/xlib/xlib.factor b/basis/x11/xlib/xlib.factor index 48d556de1d..98305e8304 100644 --- a/basis/x11/xlib/xlib.factor +++ b/basis/x11/xlib/xlib.factor @@ -13,6 +13,7 @@ USING: accessors kernel arrays alien alien.c-types alien.strings alien.syntax classes.struct math math.bitwise words sequences namespaces continuations io io.encodings.ascii x11.syntax ; +FROM: alien.c-types => short ; IN: x11.xlib LIBRARY: xlib diff --git a/extra/alien/marshall/marshall.factor b/extra/alien/marshall/marshall.factor index 2cae122641..e8ea0d3754 100644 --- a/extra/alien/marshall/marshall.factor +++ b/extra/alien/marshall/marshall.factor @@ -6,6 +6,7 @@ combinators combinators.short-circuit destructors fry io.encodings.utf8 kernel libc sequences specialized-arrays strings unix.utilities vocabs.parser words libc.private locals generalizations math ; +FROM: alien.c-types => float short ; SPECIALIZED-ARRAY: bool SPECIALIZED-ARRAY: char SPECIALIZED-ARRAY: double diff --git a/extra/bunny/model/model.factor b/extra/bunny/model/model.factor index dd6730b57f..d80f3aa98a 100755 --- a/extra/bunny/model/model.factor +++ b/extra/bunny/model/model.factor @@ -3,8 +3,9 @@ http.client io io.encodings.ascii io.files io.files.temp kernel math math.matrices math.parser math.vectors opengl opengl.capabilities opengl.gl opengl.demo-support sequences splitting vectors words specialized-arrays ; -SPECIALIZED-ARRAY: float -SPECIALIZED-ARRAY: uint +QUALIFIED-WITH: alien.c-types c +SPECIALIZED-ARRAY: c:float +SPECIALIZED-ARRAY: c:uint IN: bunny.model : numbers ( str -- seq ) diff --git a/extra/gpu/textures/textures.factor b/extra/gpu/textures/textures.factor index 8015ff9a9b..2649f7c586 100644 --- a/extra/gpu/textures/textures.factor +++ b/extra/gpu/textures/textures.factor @@ -3,6 +3,7 @@ USING: accessors alien.c-types arrays byte-arrays combinators destructors fry gpu gpu.buffers images kernel locals math opengl opengl.gl opengl.textures sequences specialized-arrays ui.gadgets.worlds variants ; +FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float IN: gpu.textures diff --git a/extra/openal/openal.factor b/extra/openal/openal.factor index 81a6621eff..bccdec1420 100644 --- a/extra/openal/openal.factor +++ b/extra/openal/openal.factor @@ -4,6 +4,7 @@ USING: kernel accessors arrays alien system combinators alien.syntax namespaces alien.c-types sequences vocabs.loader shuffle openal.backend alien.libraries generalizations specialized-arrays ; +FROM: alien.c-types => float short ; SPECIALIZED-ARRAY: uint IN: openal From e02d480b43ad6dc0a99d307351c6ed0584734063 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 09:40:37 -0500 Subject: [PATCH 284/345] fix alien.inline tests --- extra/alien/inline/types/types.factor | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/alien/inline/types/types.factor b/extra/alien/inline/types/types.factor index 070febc324..ac7f6ae17f 100644 --- a/extra/alien/inline/types/types.factor +++ b/extra/alien/inline/types/types.factor @@ -2,10 +2,11 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors alien.c-types assocs combinators.short-circuit continuations effects fry kernel math memoize sequences -splitting strings peg.ebnf make ; +splitting strings peg.ebnf make words ; IN: alien.inline.types : cify-type ( str -- str' ) + dup word? [ name>> ] when { { CHAR: - CHAR: space } } substitute ; : factorize-type ( str -- str' ) From c3f0688164f2cf18b08c3c830fe533ff6c88003b Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 09:55:09 -0500 Subject: [PATCH 285/345] more loading fixes --- extra/alien/marshall/marshall.factor | 2 +- extra/freetype/freetype.factor | 2 +- extra/jamshred/gl/gl.factor | 1 + extra/synth/buffers/buffers.factor | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/extra/alien/marshall/marshall.factor b/extra/alien/marshall/marshall.factor index e8ea0d3754..d343da0fe0 100644 --- a/extra/alien/marshall/marshall.factor +++ b/extra/alien/marshall/marshall.factor @@ -23,7 +23,7 @@ SPECIALIZED-ARRAY: ushort SPECIALIZED-ARRAY: void* IN: alien.marshall -<< primitive-types [ [ "void*" = ] [ "bool" = ] bi or not ] +<< primitive-types [ [ void* = ] [ bool = ] bi or not ] filter [ define-primitive-marshallers ] each >> TUPLE: alien-wrapper { underlying alien } ; diff --git a/extra/freetype/freetype.factor b/extra/freetype/freetype.factor index c45475cefa..0bfaae9853 100644 --- a/extra/freetype/freetype.factor +++ b/extra/freetype/freetype.factor @@ -23,7 +23,7 @@ TYPEDEF: ushort FT_UShort TYPEDEF: long FT_Long TYPEDEF: ulong FT_ULong TYPEDEF: uchar FT_Bool -TYPEDEF: cell FT_Offset +TYPEDEF: ulong FT_Offset TYPEDEF: int FT_PtrDist TYPEDEF: char FT_String TYPEDEF: int FT_Tag diff --git a/extra/jamshred/gl/gl.factor b/extra/jamshred/gl/gl.factor index 1a03a2c941..60e9e39d9f 100644 --- a/extra/jamshred/gl/gl.factor +++ b/extra/jamshred/gl/gl.factor @@ -4,6 +4,7 @@ USING: accessors alien.c-types jamshred.game jamshred.oint jamshred.player jamshred.tunnel kernel math math.constants math.functions math.vectors opengl opengl.gl opengl.glu opengl.demo-support sequences specialized-arrays ; +FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float IN: jamshred.gl diff --git a/extra/synth/buffers/buffers.factor b/extra/synth/buffers/buffers.factor index 71b05ac642..978fb32d42 100644 --- a/extra/synth/buffers/buffers.factor +++ b/extra/synth/buffers/buffers.factor @@ -2,6 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors alien.c-types combinators kernel locals math math.ranges openal sequences sequences.merged specialized-arrays ; +FROM: alien.c-types => short ; SPECIALIZED-ARRAY: uchar SPECIALIZED-ARRAY: short IN: synth.buffers From d9c6230f43bad0d3db82d761f2c81026726b418e Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 11:10:06 -0500 Subject: [PATCH 286/345] fix more alien.inline tests --- extra/alien/inline/inline.factor | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extra/alien/inline/inline.factor b/extra/alien/inline/inline.factor index 84c3450102..ee69d954ea 100644 --- a/extra/alien/inline/inline.factor +++ b/extra/alien/inline/inline.factor @@ -41,6 +41,11 @@ SYMBOL: c-strings [ current-vocab name>> % "_" % % ] "" make ; PRIVATE> +: parse-arglist ( parameters return -- types effect ) + [ 2 group unzip [ "," ?tail drop ] map ] + [ [ { } ] [ 1array ] if-void ] + bi* ; + : append-function-body ( prototype-str body -- str ) [ swap % " {\n" % % "\n}\n" % ] "" make ; From 17008bd2754daa61383288497afc47c15d6aab0f Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 17 Sep 2009 09:13:33 -0700 Subject: [PATCH 287/345] fix recaptcha, move to furnace.recaptcha --- .../{chloe-tags => }/recaptcha/authors.txt | 0 .../recaptcha/recaptcha-docs.factor | 32 ++++++++++++------- .../recaptcha/recaptcha.factor | 11 ++++--- .../{chloe-tags => }/recaptcha/recaptcha.xml | 0 .../{chloe-tags => }/recaptcha/summary.txt | 0 .../{chloe-tags => }/recaptcha/tags.txt | 0 6 files changed, 26 insertions(+), 17 deletions(-) rename basis/furnace/{chloe-tags => }/recaptcha/authors.txt (100%) rename basis/furnace/{chloe-tags => }/recaptcha/recaptcha-docs.factor (71%) rename basis/furnace/{chloe-tags => }/recaptcha/recaptcha.factor (88%) rename basis/furnace/{chloe-tags => }/recaptcha/recaptcha.xml (100%) rename basis/furnace/{chloe-tags => }/recaptcha/summary.txt (100%) rename basis/furnace/{chloe-tags => }/recaptcha/tags.txt (100%) diff --git a/basis/furnace/chloe-tags/recaptcha/authors.txt b/basis/furnace/recaptcha/authors.txt similarity index 100% rename from basis/furnace/chloe-tags/recaptcha/authors.txt rename to basis/furnace/recaptcha/authors.txt diff --git a/basis/furnace/chloe-tags/recaptcha/recaptcha-docs.factor b/basis/furnace/recaptcha/recaptcha-docs.factor similarity index 71% rename from basis/furnace/chloe-tags/recaptcha/recaptcha-docs.factor rename to basis/furnace/recaptcha/recaptcha-docs.factor index 0d93949f53..90d4a8195c 100644 --- a/basis/furnace/chloe-tags/recaptcha/recaptcha-docs.factor +++ b/basis/furnace/recaptcha/recaptcha-docs.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: help.markup help.syntax http.server.filters kernel -multiline furnace.actions ; -IN: furnace.chloe-tags.recaptcha +multiline furnace.actions furnace.alloy ; +IN: furnace.recaptcha HELP: { $values @@ -30,20 +30,27 @@ ARTICLE: "recaptcha-example" "Recaptcha example" "An example follows:" { $code HEREDOC: RECAPTCHA-TUTORIAL +USING: db.sqlite kernel http.server.dispatchers db.sqlite +furnace.actions furnace.recaptcha furnace.conversations +furnace.redirection xml.syntax html.templates.chloe.compiler +io.streams.string http.server.responses furnace.alloy http.server ; TUPLE: recaptcha-app < dispatcher recaptcha ; +: recaptcha-db ( -- obj ) + "recaptcha-example" ; + : ( -- obj ) [ + begin-conversation validate-recaptcha - recaptcha-valid? get "?good" "?bad" ? + recaptcha-valid? cget "?good" "?bad" ? >url ] >>submit [ - -{" - - -"} >>body + +
+ XML> + compile-template [ call( -- ) ] with-string-writer "text/html" ] >>display ; : ( -- obj ) @@ -51,8 +58,9 @@ TUPLE: recaptcha-app < dispatcher recaptcha ; "" add-responder "concatenative.org" >>domain - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" >>public-key - "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" >>private-key ; + "6LeJWQgAAAAAAFlYV7SuBClE9uSpGtV_ZS-qVON7" >>public-key + "6LeJWQgAAAAAALh-XJgSSQ6xKygRgJ8-029Ip2Xv" >>private-key + recaptcha-db ; main-responder set-global RECAPTCHA-TUTORIAL @@ -60,7 +68,7 @@ RECAPTCHA-TUTORIAL ; -ARTICLE: "furnace.chloe-tags.recaptcha" "Recaptcha chloe tag" +ARTICLE: "furnace.recaptcha" "Recaptcha" "The " { $vocab-link "furnace.chloe-tags.recaptcha" } " vocabulary implements support for the Recaptcha. Recaptcha is a web service that provides the user with a captcha, a test that is easy to solve by visual inspection, but hard to solve by writing a computer program. Use a captcha to protect forms from abusive users." $nl "The recaptcha responder is a " { $link filter-responder } " that wraps another responder. Set the " { $slot "domain" } ", " { $slot "public-key" } ", and " { $slot "private-key" } " slots of this responder to your Recaptcha account information." $nl @@ -74,4 +82,4 @@ ARTICLE: "furnace.chloe-tags.recaptcha" "Recaptcha chloe tag" { $subsection recaptcha-error } { $subsection "recaptcha-example" } ; -ABOUT: "furnace.chloe-tags.recaptcha" +ABOUT: "furnace.recaptcha" diff --git a/basis/furnace/chloe-tags/recaptcha/recaptcha.factor b/basis/furnace/recaptcha/recaptcha.factor similarity index 88% rename from basis/furnace/chloe-tags/recaptcha/recaptcha.factor rename to basis/furnace/recaptcha/recaptcha.factor index 81744dc0e0..99b223b8e3 100644 --- a/basis/furnace/chloe-tags/recaptcha/recaptcha.factor +++ b/basis/furnace/recaptcha/recaptcha.factor @@ -4,8 +4,8 @@ USING: accessors furnace.actions furnace.redirection html.forms html.templates.chloe.compiler html.templates.chloe.syntax http.client http.server http.server.filters io.sockets kernel locals namespaces sequences splitting urls validators -xml.syntax ; -IN: furnace.chloe-tags.recaptcha +xml.syntax furnace.conversations ; +IN: furnace.recaptcha TUPLE: recaptcha < filter-responder domain public-key private-key ; @@ -38,8 +38,9 @@ M: recaptcha call-responder* XML] ; : recaptcha-url ( secure? -- ? ) - [ "https://api.recaptcha.net/challenge" >url ] - [ "http://api.recaptcha.net/challenge" >url ] if ; + [ "https://api.recaptcha.net/challenge" ] + [ "http://api.recaptcha.net/challenge" ] if + recaptcha-error cget [ "?error=" glue ] when* >url ; : render-recaptcha ( -- xml ) secure-connection? recaptcha-url @@ -72,4 +73,4 @@ PRIVATE> "recaptcha_challenge_field" value "recaptcha_response_field" value \ recaptcha get (validate-recaptcha) - [ recaptcha-valid? set ] [ recaptcha-error set ] bi* ; + [ recaptcha-valid? cset ] [ recaptcha-error cset ] bi* ; diff --git a/basis/furnace/chloe-tags/recaptcha/recaptcha.xml b/basis/furnace/recaptcha/recaptcha.xml similarity index 100% rename from basis/furnace/chloe-tags/recaptcha/recaptcha.xml rename to basis/furnace/recaptcha/recaptcha.xml diff --git a/basis/furnace/chloe-tags/recaptcha/summary.txt b/basis/furnace/recaptcha/summary.txt similarity index 100% rename from basis/furnace/chloe-tags/recaptcha/summary.txt rename to basis/furnace/recaptcha/summary.txt diff --git a/basis/furnace/chloe-tags/recaptcha/tags.txt b/basis/furnace/recaptcha/tags.txt similarity index 100% rename from basis/furnace/chloe-tags/recaptcha/tags.txt rename to basis/furnace/recaptcha/tags.txt From 97d4ac27988946e6a64e9c8f80a56b960a662f0c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 17 Sep 2009 09:14:56 -0700 Subject: [PATCH 288/345] bootstrap.stage1: decent error message if stage2.factor can't be found --- core/bootstrap/stage1.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/bootstrap/stage1.factor b/core/bootstrap/stage1.factor index c7be17e38d..9c84904ff7 100644 --- a/core/bootstrap/stage1.factor +++ b/core/bootstrap/stage1.factor @@ -40,7 +40,7 @@ load-help? off "bootstrap.layouts" require [ - "vocab:bootstrap/stage2.factor" + "resource:basis/bootstrap/stage2.factor" dup exists? [ run-file ] [ From ce97e7328bbd4995aa481bcb76a3d3bb3809d190 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 17 Sep 2009 09:48:49 -0700 Subject: [PATCH 289/345] move recaptcha example to a vocabulary on disk --- basis/furnace/recaptcha/example/authors.txt | 1 + .../furnace/recaptcha/example/example.factor | 31 +++++++++++ basis/furnace/recaptcha/example/example.xml | 4 ++ basis/furnace/recaptcha/recaptcha-docs.factor | 52 ++++--------------- 4 files changed, 47 insertions(+), 41 deletions(-) create mode 100644 basis/furnace/recaptcha/example/authors.txt create mode 100644 basis/furnace/recaptcha/example/example.factor create mode 100644 basis/furnace/recaptcha/example/example.xml diff --git a/basis/furnace/recaptcha/example/authors.txt b/basis/furnace/recaptcha/example/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/basis/furnace/recaptcha/example/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/basis/furnace/recaptcha/example/example.factor b/basis/furnace/recaptcha/example/example.factor new file mode 100644 index 0000000000..264be678ae --- /dev/null +++ b/basis/furnace/recaptcha/example/example.factor @@ -0,0 +1,31 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors db.sqlite furnace.actions furnace.alloy +furnace.conversations furnace.recaptcha furnace.redirection +html.templates.chloe.compiler http.server +http.server.dispatchers http.server.responses io.streams.string +kernel urls xml.syntax ; +IN: furnace.recaptcha.example + +TUPLE: recaptcha-app < dispatcher recaptcha ; + +: recaptcha-db ( -- obj ) "recaptcha-example" ; + +: ( -- obj ) + + [ + begin-conversation + validate-recaptcha + recaptcha-valid? cget + "?good" "?bad" ? >url + ] >>submit + { recaptcha-app "example" } >>template ; + +: ( -- obj ) + \ recaptcha-app new-dispatcher + "" add-responder + + "concatenative.org" >>domain + "6LeJWQgAAAAAAFlYV7SuBClE9uSpGtV_ZS-qVON7" >>public-key + "6LeJWQgAAAAAALh-XJgSSQ6xKygRgJ8-029Ip2Xv" >>private-key + recaptcha-db ; diff --git a/basis/furnace/recaptcha/example/example.xml b/basis/furnace/recaptcha/example/example.xml new file mode 100644 index 0000000000..e59f441f7f --- /dev/null +++ b/basis/furnace/recaptcha/example/example.xml @@ -0,0 +1,4 @@ + + +
+
diff --git a/basis/furnace/recaptcha/recaptcha-docs.factor b/basis/furnace/recaptcha/recaptcha-docs.factor index 90d4a8195c..d416dd9474 100644 --- a/basis/furnace/recaptcha/recaptcha-docs.factor +++ b/basis/furnace/recaptcha/recaptcha-docs.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: help.markup help.syntax http.server.filters kernel -multiline furnace.actions furnace.alloy ; +multiline furnace.actions furnace.alloy furnace.conversations ; IN: furnace.recaptcha HELP: @@ -24,49 +24,19 @@ ARTICLE: "recaptcha-example" "Recaptcha example" "There are several steps to using the Recaptcha library." { $list { "Wrap the responder in a " { $link } } + { "Wrap the responder in a " { $link } " if it is not already" } + { "Ensure that there is a database connected, with the " { $link } " word" } + { "Start a conversation to move values between requests" } { "Add a handler calling " { $link validate-recaptcha } " in the " { $slot "submit" } " of the " { $link page-action } } - { "Put the chloe tag " { $snippet "" } " in the template for your " { $link action } } + { "Pass the conversation from your submit action using " { $link } } + { "Put the chloe tag " { $snippet "" } " inside a form tag in the template for your " { $link page-action } } } -"An example follows:" +$nl +"Run this example vocabulary:" { $code -HEREDOC: RECAPTCHA-TUTORIAL -USING: db.sqlite kernel http.server.dispatchers db.sqlite -furnace.actions furnace.recaptcha furnace.conversations -furnace.redirection xml.syntax html.templates.chloe.compiler -io.streams.string http.server.responses furnace.alloy http.server ; -TUPLE: recaptcha-app < dispatcher recaptcha ; - -: recaptcha-db ( -- obj ) - "recaptcha-example" ; - -: ( -- obj ) - - [ - begin-conversation - validate-recaptcha - recaptcha-valid? cget "?good" "?bad" ? >url - ] >>submit - [ - -
- XML> - compile-template [ call( -- ) ] with-string-writer "text/html" - ] >>display ; - -: ( -- obj ) - \ recaptcha-app new-dispatcher - "" add-responder - - "concatenative.org" >>domain - "6LeJWQgAAAAAAFlYV7SuBClE9uSpGtV_ZS-qVON7" >>public-key - "6LeJWQgAAAAAALh-XJgSSQ6xKygRgJ8-029Ip2Xv" >>private-key - recaptcha-db ; - - main-responder set-global -RECAPTCHA-TUTORIAL -} - -; + "USE: furnace.recaptcha.example" + " main-responder set-global" +} ; ARTICLE: "furnace.recaptcha" "Recaptcha" "The " { $vocab-link "furnace.chloe-tags.recaptcha" } " vocabulary implements support for the Recaptcha. Recaptcha is a web service that provides the user with a captcha, a test that is easy to solve by visual inspection, but hard to solve by writing a computer program. Use a captcha to protect forms from abusive users." $nl From eeebf6c7514768e65d3e8c6d32aa7b633dba5fc0 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 14:01:41 -0500 Subject: [PATCH 290/345] fix loading issues in windows vocabs --- basis/windows/dinput/constants/constants.factor | 9 +++++++-- basis/windows/types/types.factor | 4 +++- basis/windows/winsock/winsock.factor | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/basis/windows/dinput/constants/constants.factor b/basis/windows/dinput/constants/constants.factor index b67b5fa08f..270c2fa3dd 100755 --- a/basis/windows/dinput/constants/constants.factor +++ b/basis/windows/dinput/constants/constants.factor @@ -2,7 +2,7 @@ USING: windows.dinput windows.kernel32 windows.ole32 windows.com windows.com.syntax alien alien.c-types alien.syntax kernel system namespaces combinators sequences fry math accessors macros words quotations libc continuations generalizations splitting locals assocs init -specialized-arrays memoize classes.struct ; +specialized-arrays memoize classes.struct strings arrays ; SPECIALIZED-ARRAY: DIOBJECTDATAFORMAT IN: windows.dinput.constants @@ -22,12 +22,17 @@ SYMBOLS: MEMO: c-type* ( name -- c-type ) c-type ; MEMO: heap-size* ( c-type -- n ) heap-size ; +GENERIC: array-base-type ( c-type -- c-type' ) +M: object array-base-type ; +M: string array-base-type "[" split1 drop ; +M: array array-base-type first ; + : (field-spec-of) ( field struct -- field-spec ) c-type* fields>> [ name>> = ] with find nip ; : (offsetof) ( field struct -- offset ) [ (field-spec-of) offset>> ] [ drop 0 ] if* ; : (sizeof) ( field struct -- size ) - [ (field-spec-of) type>> "[" split1 drop heap-size* ] [ drop 1 ] if* ; + [ (field-spec-of) type>> array-base-type heap-size* ] [ drop 1 ] if* ; : (flag) ( thing -- integer ) { diff --git a/basis/windows/types/types.factor b/basis/windows/types/types.factor index c882ba2e7f..544abb69a8 100755 --- a/basis/windows/types/types.factor +++ b/basis/windows/types/types.factor @@ -3,6 +3,7 @@ USING: alien alien.c-types alien.syntax namespaces kernel words sequences math math.bitwise math.vectors colors io.encodings.utf16n classes.struct accessors ; +FROM: alien.c-types => float short ; IN: windows.types TYPEDEF: char CHAR @@ -69,7 +70,8 @@ TYPEDEF: ulonglong ULARGE_INTEGER TYPEDEF: LARGE_INTEGER* PLARGE_INTEGER TYPEDEF: ULARGE_INTEGER* PULARGE_INTEGER -<< { "char*" utf16n } "wchar_t*" typedef >> +SYMBOL: wchar_t* +<< { char* utf16n } \ wchar_t* typedef >> TYPEDEF: wchar_t* LPCSTR TYPEDEF: wchar_t* LPWSTR diff --git a/basis/windows/winsock/winsock.factor b/basis/windows/winsock/winsock.factor index 87b8970b02..e29eb3e090 100755 --- a/basis/windows/winsock/winsock.factor +++ b/basis/windows/winsock/winsock.factor @@ -4,6 +4,7 @@ USING: alien alien.c-types alien.strings alien.syntax arrays byte-arrays kernel literals math sequences windows.types windows.kernel32 windows.errors math.bitwise io.encodings.utf16n classes.struct windows.com.syntax init ; +FROM: alien.c-types => short ; IN: windows.winsock TYPEDEF: void* SOCKET From 4758cca22319f5c84a2ef623dde3e797fafd1a8b Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 14:22:49 -0500 Subject: [PATCH 291/345] fix dinput device hotplug support --- basis/game-input/dinput/dinput.factor | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/basis/game-input/dinput/dinput.factor b/basis/game-input/dinput/dinput.factor index ea3100f95f..a7489f26a2 100755 --- a/basis/game-input/dinput/dinput.factor +++ b/basis/game-input/dinput/dinput.factor @@ -160,19 +160,24 @@ SYMBOLS: +dinput+ +keyboard-device+ +keyboard-state+ [ device-attached? not ] filter [ remove-controller ] each ; -: device-interface? ( dbt-broadcast-hdr -- ? ) - dbch_devicetype>> DBT_DEVTYP_DEVICEINTERFACE = ; +: ?device-interface ( dbt-broadcast-hdr -- ? ) + dup dbch_devicetype>> DBT_DEVTYP_DEVICEINTERFACE = + [ >c-ptr DEV_BROADCAST_DEVICEW memory>struct ] + [ drop f ] if ; inline : device-arrived ( dbt-broadcast-hdr -- ) - device-interface? [ find-controllers ] when ; + ?device-interface [ find-controllers ] when ; inline : device-removed ( dbt-broadcast-hdr -- ) - device-interface? [ find-and-remove-detached-devices ] when ; + ?device-interface [ find-and-remove-detached-devices ] when ; inline + +: ( wParam -- struct ) + DEV_BROADCAST_HDR memory>struct ; : handle-wm-devicechange ( hWnd uMsg wParam lParam -- ) [ 2drop ] 2dip swap { - { [ dup DBT_DEVICEARRIVAL = ] [ drop device-arrived ] } - { [ dup DBT_DEVICEREMOVECOMPLETE = ] [ drop device-removed ] } + { [ dup DBT_DEVICEARRIVAL = ] [ drop device-arrived ] } + { [ dup DBT_DEVICEREMOVECOMPLETE = ] [ drop device-removed ] } [ 2drop ] } cond ; From 086d4a87b4425dc4f717335ebc3d967cd40cef6d Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 17 Sep 2009 13:09:45 -0700 Subject: [PATCH 292/345] fix recaptcha docs --- basis/furnace/recaptcha/recaptcha-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/furnace/recaptcha/recaptcha-docs.factor b/basis/furnace/recaptcha/recaptcha-docs.factor index d416dd9474..e6473a4bf9 100644 --- a/basis/furnace/recaptcha/recaptcha-docs.factor +++ b/basis/furnace/recaptcha/recaptcha-docs.factor @@ -39,7 +39,7 @@ $nl } ; ARTICLE: "furnace.recaptcha" "Recaptcha" -"The " { $vocab-link "furnace.chloe-tags.recaptcha" } " vocabulary implements support for the Recaptcha. Recaptcha is a web service that provides the user with a captcha, a test that is easy to solve by visual inspection, but hard to solve by writing a computer program. Use a captcha to protect forms from abusive users." $nl +"The " { $vocab-link "furnace.recaptcha" } " vocabulary implements support for the Recaptcha. Recaptcha is a web service that provides the user with a captcha, a test that is easy to solve by visual inspection, but hard to solve by writing a computer program. Use a captcha to protect forms from abusive users." $nl "The recaptcha responder is a " { $link filter-responder } " that wraps another responder. Set the " { $slot "domain" } ", " { $slot "public-key" } ", and " { $slot "private-key" } " slots of this responder to your Recaptcha account information." $nl From 7f1c33f363459329ecea66c837201fdd85b11879 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 17 Sep 2009 13:27:15 -0700 Subject: [PATCH 293/345] stack-checker.errors: improve documentation a bit --- basis/stack-checker/errors/errors-docs.factor | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/basis/stack-checker/errors/errors-docs.factor b/basis/stack-checker/errors/errors-docs.factor index 6a67b815cd..e451c53c71 100755 --- a/basis/stack-checker/errors/errors-docs.factor +++ b/basis/stack-checker/errors/errors-docs.factor @@ -1,14 +1,43 @@ USING: help.markup help.syntax kernel effects sequences -sequences.private words ; +sequences.private words combinators ; IN: stack-checker.errors +HELP: do-not-compile +{ $error-description "Thrown when inference encounters a macro being applied to a value which is not known to be a literal. Such code needs changes before it can compile and run. See " { $link "inference-combinators" } " and " { $link "inference-escape" } " for details." } +{ $examples + "In this example, " { $link cleave } " is being applied to an array that is constructed on the fly. This is not allowed and fails to compile with a " { $link do-not-compile } " error:" + { $code + ": cannot-compile-call-example ( x -- y z )" + " [ 1 + ] [ 1 - ] 2array cleave ;" + } +} ; + HELP: literal-expected { $error-description "Thrown when inference encounters a combinator or macro being applied to a value which is not known to be a literal, or constructed in a manner which can be analyzed statically. Such code needs changes before it can compile and run. See " { $link "inference-combinators" } " and " { $link "inference-escape" } " for details." } { $examples - "In this example, words calling " { $snippet "literal-expected-example" } " will have a static stac keffect, even if " { $snippet "literal-expected-example" } " does not:" + "In this example, the words being defined cannot be called, because they fail to compile with a " { $link literal-expected } " error:" { $code - ": literal-expected-example ( quot -- )" + ": bad-example ( quot -- )" + " [ call ] [ call ] bi ;" + "" + ": usage ( -- )" + " 10 [ 2 * ] bad-example . ;" + } + "One fix is to declare the combinator as inline:" + { $code + ": good-example ( quot -- )" " [ call ] [ call ] bi ; inline" + "" + ": usage ( -- )" + " 10 [ 2 * ] good-example . ;" + } + "Another fix is to use " { $link POSTPONE: call( } ":" + { $code + ": good-example ( quot -- )" + " [ call( x -- y ) ] [ call( x -- y ) ] bi ;" + "" + ": usage ( -- )" + " 10 [ 2 * ] good-example . ;" } } ; @@ -89,7 +118,8 @@ ARTICLE: "inference-errors" "Stack checker errors" { { $link "tools.inference" } " throws them as errors" } { "The " { $link "compiler" } " reports them via " { $link "tools.errors" } } } -"Error thrown when insufficient information is available to calculate the stack effect of a combinator call (see " { $link "inference-combinators" } "):" +"Errors thrown when insufficient information is available to calculate the stack effect of a call to a combinator or macro (see " { $link "inference-combinators" } "):" +{ $subsection do-not-compile } { $subsection literal-expected } "Error thrown when a word's stack effect declaration does not match the composition of the stack effects of its factors:" { $subsection effect-error } From 6b502f6fe56d4ec63a08d5bb469dc0eec9bfc587 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 17 Sep 2009 14:07:08 -0700 Subject: [PATCH 294/345] combinators: clarify docs --- core/combinators/combinators-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/combinators/combinators-docs.factor b/core/combinators/combinators-docs.factor index 4a7fcea0e6..5d778ba1e4 100755 --- a/core/combinators/combinators-docs.factor +++ b/core/combinators/combinators-docs.factor @@ -85,7 +85,7 @@ $nl } ; ARTICLE: "spread-combinators" "Spread combinators" -"The spread combinators apply multiple quotations to multiple values. The " { $snippet "*" } " suffix signifies spreading." +"The spread combinators apply multiple quotations to multiple values. In this case, " { $snippet "*" } " suffix signify spreading." $nl "Two quotations:" { $subsection bi* } From aa1edad0788d91f89965e3c82b51c1d8f6fbf698 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 19:10:40 -0500 Subject: [PATCH 295/345] disambiguate math:float in cpu.ppc --- basis/cpu/ppc/ppc.factor | 1 + 1 file changed, 1 insertion(+) diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index f881ff5f91..063f3b37b1 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -9,6 +9,7 @@ compiler.codegen.fixup compiler.cfg.intrinsics compiler.cfg.stack-frame compiler.cfg.build-stack-frame compiler.units compiler.constants compiler.codegen ; FROM: cpu.ppc.assembler => B ; +FROM: math => float ; IN: cpu.ppc ! PowerPC register assignments: From 076ab42dc37caaa946769ec79faee57af0865bb3 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 17 Sep 2009 22:07:21 -0500 Subject: [PATCH 296/345] move some allocation words that don't really have much to do with c types out of alien.c-types into a new alien.data vocab --- basis/alien/arrays/arrays-docs.factor | 2 +- basis/alien/arrays/arrays.factor | 4 +- basis/alien/c-types/c-types-docs.factor | 181 ++---------------- basis/alien/c-types/c-types.factor | 105 ++-------- basis/alien/data/authors.txt | 1 + basis/alien/data/data-docs.factor | 148 ++++++++++++++ basis/alien/data/data.factor | 83 ++++++++ basis/alien/data/summary.txt | 1 + basis/alien/fortran/fortran-tests.factor | 4 +- basis/alien/fortran/fortran.factor | 2 +- .../remote-control/remote-control.factor | 2 +- basis/alien/structs/structs-docs.factor | 2 +- basis/alien/structs/structs-tests.factor | 2 +- basis/bit-arrays/bit-arrays.factor | 2 +- basis/checksums/openssl/openssl.factor | 6 +- .../struct/prettyprint/prettyprint.factor | 2 +- basis/classes/struct/struct-tests.factor | 2 +- basis/classes/struct/struct.factor | 2 +- basis/cocoa/enumeration/enumeration.factor | 11 +- basis/cocoa/plists/plists.factor | 4 +- basis/compiler/tests/intrinsics.factor | 2 +- basis/cpu/ppc/ppc.factor | 2 +- basis/db/postgresql/lib/lib.factor | 10 +- basis/db/sqlite/lib/lib.factor | 2 +- basis/environment/unix/unix.factor | 7 +- basis/environment/winnt/winnt.factor | 11 +- basis/game-input/dinput/dinput.factor | 2 +- .../dinput/keys-array/keys-array.factor | 4 +- basis/game-input/iokit/iokit.factor | 3 +- basis/images/memory/memory.factor | 6 +- .../windows/nt/privileges/privileges.factor | 2 +- basis/io/buffers/buffers-tests.factor | 6 +- basis/io/buffers/buffers.factor | 4 +- basis/io/files/info/windows/windows.factor | 2 +- basis/io/files/windows/windows.factor | 2 +- basis/io/mmap/mmap.factor | 2 +- basis/io/monitors/windows/nt/nt.factor | 4 +- .../io/sockets/secure/openssl/openssl.factor | 8 +- basis/io/sockets/sockets.factor | 2 +- basis/io/sockets/unix/unix.factor | 2 +- basis/io/sockets/windows/nt/nt.factor | 2 +- basis/libc/libc.factor | 20 +- basis/math/blas/matrices/matrices.factor | 14 +- .../vectors/simd/intrinsics/intrinsics.factor | 2 +- basis/opengl/shaders/shaders.factor | 2 +- basis/random/windows/windows.factor | 2 +- .../specialized-arrays-tests.factor | 2 +- .../specialized-arrays.factor | 2 +- basis/tools/deploy/config/config-docs.factor | 2 +- basis/tools/disassembler/disassembler.factor | 2 +- basis/tools/disassembler/udis/udis.factor | 2 +- basis/ui/backend/cocoa/views/views.factor | 10 +- basis/ui/backend/windows/windows.factor | 2 +- basis/unix/process/process.factor | 6 +- basis/unix/utilities/utilities.factor | 2 +- basis/unix/utmpx/utmpx.factor | 8 +- basis/windows/com/com.factor | 2 +- basis/windows/com/wrapper/wrapper.factor | 12 +- .../windows/dinput/constants/constants.factor | 9 +- .../dragdrop-listener.factor | 9 +- basis/windows/errors/errors.factor | 11 +- basis/windows/offscreen/offscreen.factor | 6 +- basis/windows/ole32/ole32.factor | 4 +- basis/x11/xlib/xlib.factor | 6 +- core/alien/strings/strings-tests.factor | 2 +- extra/alien/inline/syntax/syntax-tests.factor | 2 +- extra/alien/marshall/marshall-docs.factor | 2 +- extra/alien/marshall/marshall.factor | 2 +- extra/alien/marshall/private/private.factor | 2 +- extra/alien/marshall/structs/structs.factor | 2 +- extra/audio/wav/wav.factor | 2 +- extra/benchmark/yuv-to-rgb/yuv-to-rgb.factor | 2 +- extra/ecdsa/ecdsa.factor | 2 +- extra/gpu/render/render.factor | 2 +- extra/gpu/shaders/shaders.factor | 14 +- extra/gpu/state/state.factor | 6 +- extra/half-floats/half-floats-tests.factor | 2 +- extra/half-floats/half-floats.factor | 2 +- extra/io/serial/unix/unix.factor | 7 +- extra/memory/piles/piles.factor | 2 +- extra/system-info/windows/ce/ce.factor | 2 +- 81 files changed, 427 insertions(+), 416 deletions(-) create mode 100644 basis/alien/data/authors.txt create mode 100644 basis/alien/data/data-docs.factor create mode 100644 basis/alien/data/data.factor create mode 100644 basis/alien/data/summary.txt diff --git a/basis/alien/arrays/arrays-docs.factor b/basis/alien/arrays/arrays-docs.factor index db4a7bf595..74174485fe 100755 --- a/basis/alien/arrays/arrays-docs.factor +++ b/basis/alien/arrays/arrays-docs.factor @@ -1,5 +1,5 @@ +USING: help.syntax help.markup byte-arrays alien.c-types alien.data ; IN: alien.arrays -USING: help.syntax help.markup byte-arrays alien.c-types ; ARTICLE: "c-arrays" "C arrays" "C arrays are allocated in the same manner as other C data; see " { $link "c-byte-arrays" } " and " { $link "malloc" } "." diff --git a/basis/alien/arrays/arrays.factor b/basis/alien/arrays/arrays.factor index 52c6afa4df..ee75d22c2c 100755 --- a/basis/alien/arrays/arrays.factor +++ b/basis/alien/arrays/arrays.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2008, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.strings alien.c-types alien.accessors -arrays words sequences math kernel namespaces fry libc cpu.architecture +USING: alien alien.strings alien.c-types alien.data alien.accessors +arrays words sequences math kernel namespaces fry cpu.architecture io.encodings.utf8 accessors ; IN: alien.arrays diff --git a/basis/alien/c-types/c-types-docs.factor b/basis/alien/c-types/c-types-docs.factor index d9e1f7124a..a9613d2c9f 100755 --- a/basis/alien/c-types/c-types-docs.factor +++ b/basis/alien/c-types/c-types-docs.factor @@ -1,7 +1,25 @@ -IN: alien.c-types USING: alien help.syntax help.markup libc kernel.private byte-arrays math strings hashtables alien.syntax alien.strings sequences io.encodings.string debugger destructors vocabs.loader ; +IN: alien.c-types + +HELP: byte-length +{ $values { "seq" "A byte array or float array" } { "n" "a non-negative integer" } } +{ $contract "Outputs the size of the byte array, struct, or specialized array data in bytes." } ; + +HELP: heap-size +{ $values { "type" string } { "size" integer } } +{ $description "Outputs the number of bytes needed for a heap-allocated value of this C type." } +{ $examples + "On a 32-bit system, you will get the following output:" + { $unchecked-example "USE: alien\n\"void*\" heap-size ." "4" } +} +{ $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ; + +HELP: stack-size +{ $values { "type" string } { "size" integer } } +{ $description "Outputs the number of bytes to reserve on the C stack by a value of this C type. In most cases this is equal to " { $link heap-size } ", except on some platforms where C structs are passed by invisible reference, in which case a C struct type only uses as much space as a pointer on the C stack." } +{ $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ; HELP: { $values { "type" hashtable } } @@ -20,24 +38,6 @@ HELP: c-type { $description "Looks up a C type by name." } { $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ; -HELP: heap-size -{ $values { "type" string } { "size" integer } } -{ $description "Outputs the number of bytes needed for a heap-allocated value of this C type." } -{ $examples - "On a 32-bit system, you will get the following output:" - { $unchecked-example "USE: alien\n\"void*\" heap-size ." "4" } -} -{ $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ; - -HELP: stack-size -{ $values { "type" string } { "size" integer } } -{ $description "Outputs the number of bytes to reserve on the C stack by a value of this C type. In most cases this is equal to " { $link heap-size } ", except on some platforms where C structs are passed by invisible reference, in which case a C struct type only uses as much space as a pointer on the C stack." } -{ $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ; - -HELP: byte-length -{ $values { "seq" "A byte array or float array" } { "n" "a non-negative integer" } } -{ $contract "Outputs the size of the byte array or float array data in bytes as presented to the C library interface." } ; - HELP: c-getter { $values { "name" string } { "quot" { $quotation "( c-ptr n -- obj )" } } } { $description "Outputs a quotation which reads values of this C type from a C structure." } @@ -48,49 +48,6 @@ HELP: c-setter { $description "Outputs a quotation which writes values of this C type to a C structure." } { $errors "Throws an error if the type does not exist." } ; -HELP: -{ $values { "len" "a non-negative integer" } { "c-type" "a C type" } { "array" byte-array } } -{ $description "Creates a byte array large enough to hold " { $snippet "n" } " values of a C type." } -{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." } -{ $errors "Throws an error if the type does not exist, the necessary specialized array vocabulary is not loaded, or the requested size is negative." } ; - -HELP: -{ $values { "type" "a C type" } { "array" byte-array } } -{ $description "Creates a byte array suitable for holding a value with the given C type." } -{ $errors "Throws an " { $link no-c-type } " error if the type does not exist." } ; - -{ malloc-object } related-words - -HELP: memory>byte-array -{ $values { "alien" c-ptr } { "len" "a non-negative integer" } { "byte-array" byte-array } } -{ $description "Reads " { $snippet "len" } " bytes starting from " { $snippet "base" } " and stores them in a new byte array." } ; - -HELP: byte-array>memory -{ $values { "byte-array" byte-array } { "base" c-ptr } } -{ $description "Writes a byte array to memory starting from the " { $snippet "base" } " address." } -{ $warning "This word is unsafe. Improper use can corrupt memory." } ; - -HELP: malloc-array -{ $values { "n" "a non-negative integer" } { "type" "a C type" } { "alien" alien } } -{ $description "Allocates an unmanaged memory block large enough to hold " { $snippet "n" } " values of a C type, then wraps the memory in a sequence object using " { $link } "." } -{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." } -{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } -{ $errors "Throws an error if the type does not exist, if the requested size is negative, if a direct specialized array class appropriate to the type is not loaded, or if memory allocation fails." } ; - -HELP: malloc-object -{ $values { "type" "a C type" } { "alien" alien } } -{ $description "Allocates an unmanaged memory block large enough to hold a value of a C type." } -{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } -{ $errors "Throws an error if the type does not exist or if memory allocation fails." } ; - -HELP: malloc-byte-array -{ $values { "byte-array" byte-array } { "alien" alien } } -{ $description "Allocates an unmanaged memory block of the same size as the byte array, and copies the contents of the byte array there." } -{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } -{ $errors "Throws an error if memory allocation fails." } ; - -{ malloc-array } related-words - HELP: box-parameter { $values { "n" integer } { "ctype" string } } { $description "Generates code for converting a C value stored at offset " { $snippet "n" } " from the top of the stack into a Factor object to be pushed on the data stack." } @@ -116,48 +73,6 @@ HELP: define-out { $description "Defines a word " { $snippet "<" { $emphasis "name" } ">" } " with stack effect " { $snippet "( value -- array )" } ". This word allocates a byte array large enough to hold a value with C type " { $snippet "name" } ", and writes the value at the top of the stack to the array." } { $notes "This is an internal word called when defining C types, there is no need to call it on your own." } ; -{ string>alien alien>string malloc-string } related-words - -HELP: malloc-string -{ $values { "string" string } { "encoding" "an encoding descriptor" } { "alien" c-ptr } } -{ $description "Encodes a string together with a trailing null code point using the given encoding, and stores the resulting bytes in a freshly-allocated unmanaged memory block." } -{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } -{ $errors "Throws an error if one of the following conditions occurs:" - { $list - "the string contains null code points" - "the string contains characters not representable using the encoding specified" - "memory allocation fails" - } -} ; - -HELP: require-c-array -{ $values { "c-type" "a C type" } } -{ $description "Generates a specialized array of " { $snippet "c-type" } " using the " { $link } " or " { $link } " vocabularies." } -{ $notes "This word must be called inside a compilation unit. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence types loaded." } ; - -HELP: -{ $values { "alien" c-ptr } { "len" integer } { "c-type" "a C type" } { "array" "a specialized direct array" } } -{ $description "Constructs a new specialized array of length " { $snippet "len" } " and element type " { $snippet "c-type" } " over the range of memory referenced by " { $snippet "alien" } "." } -{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." } ; - -ARTICLE: "c-strings" "C strings" -"C string types are arrays with shape " { $snippet "{ \"char*\" encoding }" } ", where " { $snippet "encoding" } " is an encoding descriptor. The type " { $snippet "\"char*\"" } " is an alias for " { $snippet "{ \"char*\" utf8 }" } ". See " { $link "encodings-descriptors" } " for information about encoding descriptors." -$nl -"Passing a Factor string to a C function expecting a C string allocates a " { $link byte-array } " in the Factor heap; the string is then converted to the requested format and a raw pointer is passed to the function." -$nl -"If the conversion fails, for example if the string contains null bytes or characters with values higher than 255, a " { $link c-string-error. } " is thrown." -$nl -"Care must be taken if the C function expects a " { $snippet "char*" } " with a length in bytes, rather than a null-terminated " { $snippet "char*" } "; passing the result of calling " { $link length } " on the string object will not suffice. This is because a Factor string of " { $emphasis "n" } " characters will not necessarily encode to " { $emphasis "n" } " bytes. The correct idiom for C functions which take a string with a length is to first encode the string using " { $link encode } ", and then pass the resulting byte array together with the length of this byte array." -$nl -"Sometimes a C function has a parameter type of " { $snippet "void*" } ", and various data types, among them strings, can be passed in. In this case, strings are not automatically converted to aliens, and instead you must call one of these words:" -{ $subsection string>alien } -{ $subsection malloc-string } -"The first allocates " { $link byte-array } "s, and the latter allocates manually-managed memory which is not moved by the garbage collector and has to be explicitly freed by calling " { $link free } ". See " { $link "byte-arrays-gc" } " for a discussion of the two approaches." -$nl -"A word to read strings from arbitrary addresses:" -{ $subsection alien>string } -"For example, if a C function returns a " { $snippet "char*" } " but stipulates that the caller must deallocate the memory afterward, you must define the function as returning " { $snippet "void*" } ", and call one of the above words before passing the pointer to " { $link free } "." ; - ARTICLE: "byte-arrays-gc" "Byte arrays and the garbage collector" "The Factor garbage collector can move byte arrays around, and it is only safe to pass byte arrays to C functions if the garbage collector will not run while C code still has a reference to the data." $nl @@ -234,61 +149,3 @@ $nl "Fixed-size arrays differ from pointers in that they are allocated inside structures and unions; however when used as function parameters they behave exactly like pointers and thus the dimensions only serve as documentation." $nl "Structure and union types are specified by the name of the structure or union." ; - -ARTICLE: "c-byte-arrays" "Passing data in byte arrays" -"Instances of the " { $link byte-array } " class can be passed to C functions; the C function receives a pointer to the first element of the array." -$nl -"Byte arrays can be allocated directly with a byte count using the " { $link } " word. However in most cases, instead of computing a size in bytes directly, it is easier to use a higher-level word which expects C type and outputs a byte array large enough to hold that type:" -{ $subsection } -{ $subsection } -{ $warning -"The Factor garbage collector can move byte arrays around, and code passing byte arrays to C must obey important guidelines. See " { $link "byte-arrays-gc" } "." } -{ $see-also "c-arrays" } ; - -ARTICLE: "malloc" "Manual memory management" -"Sometimes data passed to C functions must be allocated at a fixed address. See " { $link "byte-arrays-gc" } " for an explanation of when this is the case." -$nl -"Allocating a C datum with a fixed address:" -{ $subsection malloc-object } -{ $subsection malloc-array } -{ $subsection malloc-byte-array } -"There is a set of words in the " { $vocab-link "libc" } " vocabulary which directly call C standard library memory management functions:" -{ $subsection malloc } -{ $subsection calloc } -{ $subsection realloc } -"You must always free pointers returned by any of the above words when the block of memory is no longer in use:" -{ $subsection free } -"Utilities for automatically freeing memory in conjunction with " { $link with-destructors } ":" -{ $subsection &free } -{ $subsection |free } -"The " { $link &free } " and " { $link |free } " words are generated using " { $link "alien.destructors" } "." -$nl -"You can unsafely copy a range of bytes from one memory location to another:" -{ $subsection memcpy } -"You can copy a range of bytes from memory into a byte array:" -{ $subsection memory>byte-array } -"You can copy a byte array to memory unsafely:" -{ $subsection byte-array>memory } ; - -ARTICLE: "c-data" "Passing data between Factor and C" -"Two defining characteristics of Factor are dynamic typing and automatic memory management, which are somewhat incompatible with the machine-level data model exposed by C. Factor's C library interface defines its own set of C data types, distinct from Factor language types, together with automatic conversion between Factor values and C types. For example, C integer types must be declared and are fixed-width, whereas Factor supports arbitrary-precision integers." -$nl -"Furthermore, Factor's garbage collector can move objects in memory; for a discussion of the consequences, see " { $link "byte-arrays-gc" } "." -{ $subsection "c-types-specs" } -{ $subsection "c-byte-arrays" } -{ $subsection "malloc" } -{ $subsection "c-strings" } -{ $subsection "c-arrays" } -{ $subsection "c-out-params" } -"Important guidelines for passing data in byte arrays:" -{ $subsection "byte-arrays-gc" } -"C-style enumerated types are supported:" -{ $subsection POSTPONE: C-ENUM: } -"C types can be aliased for convenience and consitency with native library documentation:" -{ $subsection POSTPONE: TYPEDEF: } -"New C types can be defined:" -{ $subsection "c-structs" } -{ $subsection "c-unions" } -"A utility for defining " { $link "destructors" } " for deallocating memory:" -{ $subsection "alien.destructors" } -{ $see-also "aliens" } ; diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index 6d63987265..fa27e29c04 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2004, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: byte-arrays arrays assocs kernel kernel.private libc math +USING: byte-arrays arrays assocs kernel kernel.private math namespaces make parser sequences strings words splitting math.parser cpu.architecture alien alien.accessors alien.strings quotations layouts system compiler.units io io.files io.encodings.binary @@ -22,8 +22,6 @@ SYMBOLS: DEFER: DEFER: *char -: little-endian? ( -- ? ) 1 *char 1 = ; foldable - TUPLE: abstract-c-type { class class initial: object } { boxed-class class initial: object } @@ -104,43 +102,6 @@ M: c-type-name c-struct? ! These words being foldable means that words need to be ! recompiled if a C type is redefined. Even so, folding the ! size facilitates some optimizations. -GENERIC: heap-size ( type -- size ) foldable - -M: c-type-name heap-size c-type heap-size ; - -M: abstract-c-type heap-size size>> ; - -GENERIC: require-c-array ( c-type -- ) - -M: array require-c-array first require-c-array ; - -GENERIC: c-array-constructor ( c-type -- word ) - -GENERIC: c-(array)-constructor ( c-type -- word ) - -GENERIC: c-direct-array-constructor ( c-type -- word ) - -GENERIC: ( len c-type -- array ) - -M: c-type-name - c-array-constructor execute( len -- array ) ; inline - -GENERIC: (c-array) ( len c-type -- array ) - -M: c-type-name (c-array) - c-(array)-constructor execute( len -- array ) ; inline - -GENERIC: ( alien len c-type -- array ) - -M: c-type-name - c-direct-array-constructor execute( alien len -- array ) ; inline - -: malloc-array ( n type -- alien ) - [ heap-size calloc ] [ ] 2bi ; inline - -: (malloc-array) ( n type -- alien ) - [ heap-size * malloc ] [ ] 2bi ; inline - GENERIC: c-type-class ( name -- class ) M: abstract-c-type c-type-class class>> ; @@ -239,29 +200,28 @@ M: c-type unbox-return f swap c-type-unbox ; M: c-type-name unbox-return c-type unbox-return ; +: little-endian? ( -- ? ) 1 *char 1 = ; foldable + +GENERIC: heap-size ( type -- size ) foldable + +M: c-type-name heap-size c-type heap-size ; + +M: abstract-c-type heap-size size>> ; + GENERIC: stack-size ( type -- size ) foldable M: c-type-name stack-size c-type stack-size ; M: c-type stack-size size>> cell align ; -MIXIN: value-type - -M: value-type c-type-rep drop int-rep ; - -M: value-type c-type-getter - drop [ swap ] ; - -M: value-type c-type-setter ( type -- quot ) - [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri - '[ @ swap @ _ memcpy ] ; - GENERIC: byte-length ( seq -- n ) flushable M: byte-array byte-length length ; inline M: f byte-length drop 0 ; inline +MIXIN: value-type + : c-getter ( name -- quot ) c-type-getter [ [ "Cannot read struct fields with this type" throw ] @@ -275,36 +235,6 @@ M: f byte-length drop 0 ; inline [ "Cannot write struct fields with this type" throw ] ] unless* ; -: ( type -- array ) - heap-size ; inline - -: (c-object) ( type -- array ) - heap-size (byte-array) ; inline - -: malloc-object ( type -- alien ) - 1 swap heap-size calloc ; inline - -: (malloc-object) ( type -- alien ) - heap-size malloc ; inline - -: malloc-byte-array ( byte-array -- alien ) - dup byte-length [ nip malloc dup ] 2keep memcpy ; - -: memory>byte-array ( alien len -- byte-array ) - [ nip (byte-array) dup ] 2keep memcpy ; - -: malloc-string ( string encoding -- alien ) - string>alien malloc-byte-array ; - -M: memory-stream stream-read - [ - [ index>> ] [ alien>> ] bi - swap memory>byte-array - ] [ [ + ] change-index drop ] 2bi ; - -: byte-array>memory ( byte-array base -- ) - swap dup byte-length memcpy ; inline - : array-accessor ( type quot -- def ) [ \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi* @@ -352,22 +282,15 @@ M: long-long-type box-return ( type -- ) : define-out ( name -- ) [ "alien.c-types" constructor-word ] - [ dup c-setter '[ _ [ 0 @ ] keep ] ] bi + [ dup c-setter '[ _ heap-size [ 0 @ ] keep ] ] bi (( value -- c-ptr )) define-inline ; -: >c-bool ( ? -- int ) 1 0 ? ; inline - -: c-bool> ( int -- ? ) 0 = not ; inline - : define-primitive-type ( type name -- ) [ typedef ] [ name>> define-deref ] [ name>> define-out ] tri ; -: malloc-file-contents ( path -- alien len ) - binary file-contents [ malloc-byte-array ] [ length ] bi ; - : if-void ( type true false -- ) pick void? [ drop nip call ] [ nip call ] if ; inline @@ -510,8 +433,8 @@ SYMBOLS: \ uchar define-primitive-type - [ alien-unsigned-1 c-bool> ] >>getter - [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter + [ alien-unsigned-1 0 = not ] >>getter + [ [ 1 0 ? ] 2dip set-alien-unsigned-1 ] >>setter 1 >>size 1 >>align "box_boolean" >>boxer diff --git a/basis/alien/data/authors.txt b/basis/alien/data/authors.txt new file mode 100644 index 0000000000..1901f27a24 --- /dev/null +++ b/basis/alien/data/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/basis/alien/data/data-docs.factor b/basis/alien/data/data-docs.factor new file mode 100644 index 0000000000..19bfaaa8ce --- /dev/null +++ b/basis/alien/data/data-docs.factor @@ -0,0 +1,148 @@ +USING: alien alien.c-types help.syntax help.markup libc kernel.private +byte-arrays math strings hashtables alien.syntax alien.strings sequences +io.encodings.string debugger destructors vocabs.loader ; +IN: alien.data + +HELP: +{ $values { "len" "a non-negative integer" } { "c-type" "a C type" } { "array" byte-array } } +{ $description "Creates a byte array large enough to hold " { $snippet "n" } " values of a C type." } +{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." } +{ $errors "Throws an error if the type does not exist, the necessary specialized array vocabulary is not loaded, or the requested size is negative." } ; + +HELP: +{ $values { "type" "a C type" } { "array" byte-array } } +{ $description "Creates a byte array suitable for holding a value with the given C type." } +{ $errors "Throws an " { $link no-c-type } " error if the type does not exist." } ; + +{ malloc-object } related-words + +HELP: memory>byte-array +{ $values { "alien" c-ptr } { "len" "a non-negative integer" } { "byte-array" byte-array } } +{ $description "Reads " { $snippet "len" } " bytes starting from " { $snippet "base" } " and stores them in a new byte array." } ; + +HELP: byte-array>memory +{ $values { "byte-array" byte-array } { "base" c-ptr } } +{ $description "Writes a byte array to memory starting from the " { $snippet "base" } " address." } +{ $warning "This word is unsafe. Improper use can corrupt memory." } ; + +HELP: malloc-array +{ $values { "n" "a non-negative integer" } { "type" "a C type" } { "alien" alien } } +{ $description "Allocates an unmanaged memory block large enough to hold " { $snippet "n" } " values of a C type, then wraps the memory in a sequence object using " { $link } "." } +{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." } +{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } +{ $errors "Throws an error if the type does not exist, if the requested size is negative, if a direct specialized array class appropriate to the type is not loaded, or if memory allocation fails." } ; + +HELP: malloc-object +{ $values { "type" "a C type" } { "alien" alien } } +{ $description "Allocates an unmanaged memory block large enough to hold a value of a C type." } +{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } +{ $errors "Throws an error if the type does not exist or if memory allocation fails." } ; + +HELP: malloc-byte-array +{ $values { "byte-array" byte-array } { "alien" alien } } +{ $description "Allocates an unmanaged memory block of the same size as the byte array, and copies the contents of the byte array there." } +{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } +{ $errors "Throws an error if memory allocation fails." } ; + +{ malloc-array } related-words + +{ string>alien alien>string malloc-string } related-words + +ARTICLE: "malloc" "Manual memory management" +"Sometimes data passed to C functions must be allocated at a fixed address. See " { $link "byte-arrays-gc" } " for an explanation of when this is the case." +$nl +"Allocating a C datum with a fixed address:" +{ $subsection malloc-object } +{ $subsection malloc-array } +{ $subsection malloc-byte-array } +"There is a set of words in the " { $vocab-link "libc" } " vocabulary which directly call C standard library memory management functions:" +{ $subsection malloc } +{ $subsection calloc } +{ $subsection realloc } +"You must always free pointers returned by any of the above words when the block of memory is no longer in use:" +{ $subsection free } +"Utilities for automatically freeing memory in conjunction with " { $link with-destructors } ":" +{ $subsection &free } +{ $subsection |free } +"The " { $link &free } " and " { $link |free } " words are generated using " { $link "alien.destructors" } "." +$nl +"You can unsafely copy a range of bytes from one memory location to another:" +{ $subsection memcpy } +"You can copy a range of bytes from memory into a byte array:" +{ $subsection memory>byte-array } +"You can copy a byte array to memory unsafely:" +{ $subsection byte-array>memory } ; + + +ARTICLE: "c-byte-arrays" "Passing data in byte arrays" +"Instances of the " { $link byte-array } " class can be passed to C functions; the C function receives a pointer to the first element of the array." +$nl +"Byte arrays can be allocated directly with a byte count using the " { $link } " word. However in most cases, instead of computing a size in bytes directly, it is easier to use a higher-level word which expects C type and outputs a byte array large enough to hold that type:" +{ $subsection } +{ $subsection } +{ $warning +"The Factor garbage collector can move byte arrays around, and code passing byte arrays to C must obey important guidelines. See " { $link "byte-arrays-gc" } "." } +{ $see-also "c-arrays" } ; + +ARTICLE: "c-data" "Passing data between Factor and C" +"Two defining characteristics of Factor are dynamic typing and automatic memory management, which are somewhat incompatible with the machine-level data model exposed by C. Factor's C library interface defines its own set of C data types, distinct from Factor language types, together with automatic conversion between Factor values and C types. For example, C integer types must be declared and are fixed-width, whereas Factor supports arbitrary-precision integers." +$nl +"Furthermore, Factor's garbage collector can move objects in memory; for a discussion of the consequences, see " { $link "byte-arrays-gc" } "." +{ $subsection "c-types-specs" } +{ $subsection "c-byte-arrays" } +{ $subsection "malloc" } +{ $subsection "c-strings" } +{ $subsection "c-arrays" } +{ $subsection "c-out-params" } +"Important guidelines for passing data in byte arrays:" +{ $subsection "byte-arrays-gc" } +"C-style enumerated types are supported:" +{ $subsection POSTPONE: C-ENUM: } +"C types can be aliased for convenience and consitency with native library documentation:" +{ $subsection POSTPONE: TYPEDEF: } +"New C types can be defined:" +{ $subsection "c-structs" } +{ $subsection "c-unions" } +"A utility for defining " { $link "destructors" } " for deallocating memory:" +{ $subsection "alien.destructors" } +{ $see-also "aliens" } ; +HELP: malloc-string +{ $values { "string" string } { "encoding" "an encoding descriptor" } { "alien" c-ptr } } +{ $description "Encodes a string together with a trailing null code point using the given encoding, and stores the resulting bytes in a freshly-allocated unmanaged memory block." } +{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } +{ $errors "Throws an error if one of the following conditions occurs:" + { $list + "the string contains null code points" + "the string contains characters not representable using the encoding specified" + "memory allocation fails" + } +} ; + +HELP: require-c-array +{ $values { "c-type" "a C type" } } +{ $description "Generates a specialized array of " { $snippet "c-type" } " using the " { $link } " or " { $link } " vocabularies." } +{ $notes "This word must be called inside a compilation unit. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence types loaded." } ; + +HELP: +{ $values { "alien" c-ptr } { "len" integer } { "c-type" "a C type" } { "array" "a specialized direct array" } } +{ $description "Constructs a new specialized array of length " { $snippet "len" } " and element type " { $snippet "c-type" } " over the range of memory referenced by " { $snippet "alien" } "." } +{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, an error will be thrown. The vocabulary can be loaded with the " { $link require-c-array } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary for details on the underlying sequence type constructed." } ; + +ARTICLE: "c-strings" "C strings" +"C string types are arrays with shape " { $snippet "{ \"char*\" encoding }" } ", where " { $snippet "encoding" } " is an encoding descriptor. The type " { $snippet "\"char*\"" } " is an alias for " { $snippet "{ \"char*\" utf8 }" } ". See " { $link "encodings-descriptors" } " for information about encoding descriptors." +$nl +"Passing a Factor string to a C function expecting a C string allocates a " { $link byte-array } " in the Factor heap; the string is then converted to the requested format and a raw pointer is passed to the function." +$nl +"If the conversion fails, for example if the string contains null bytes or characters with values higher than 255, a " { $link c-string-error. } " is thrown." +$nl +"Care must be taken if the C function expects a " { $snippet "char*" } " with a length in bytes, rather than a null-terminated " { $snippet "char*" } "; passing the result of calling " { $link length } " on the string object will not suffice. This is because a Factor string of " { $emphasis "n" } " characters will not necessarily encode to " { $emphasis "n" } " bytes. The correct idiom for C functions which take a string with a length is to first encode the string using " { $link encode } ", and then pass the resulting byte array together with the length of this byte array." +$nl +"Sometimes a C function has a parameter type of " { $snippet "void*" } ", and various data types, among them strings, can be passed in. In this case, strings are not automatically converted to aliens, and instead you must call one of these words:" +{ $subsection string>alien } +{ $subsection malloc-string } +"The first allocates " { $link byte-array } "s, and the latter allocates manually-managed memory which is not moved by the garbage collector and has to be explicitly freed by calling " { $link free } ". See " { $link "byte-arrays-gc" } " for a discussion of the two approaches." +$nl +"A word to read strings from arbitrary addresses:" +{ $subsection alien>string } +"For example, if a C function returns a " { $snippet "char*" } " but stipulates that the caller must deallocate the memory afterward, you must define the function as returning " { $snippet "void*" } ", and call one of the above words before passing the pointer to " { $link free } "." ; + diff --git a/basis/alien/data/data.factor b/basis/alien/data/data.factor new file mode 100644 index 0000000000..1f2c5160e1 --- /dev/null +++ b/basis/alien/data/data.factor @@ -0,0 +1,83 @@ +! (c)2009 Slava Pestov, Joe Groff bsd license +USING: accessors alien alien.c-types alien.strings arrays +byte-arrays cpu.architecture fry io io.encodings.binary +io.files io.streams.memory kernel libc math sequences ; +IN: alien.data + +GENERIC: require-c-array ( c-type -- ) + +M: array require-c-array first require-c-array ; + +GENERIC: c-array-constructor ( c-type -- word ) + +GENERIC: c-(array)-constructor ( c-type -- word ) + +GENERIC: c-direct-array-constructor ( c-type -- word ) + +GENERIC: ( len c-type -- array ) + +M: c-type-name + c-array-constructor execute( len -- array ) ; inline + +GENERIC: (c-array) ( len c-type -- array ) + +M: c-type-name (c-array) + c-(array)-constructor execute( len -- array ) ; inline + +GENERIC: ( alien len c-type -- array ) + +M: c-type-name + c-direct-array-constructor execute( alien len -- array ) ; inline + +: malloc-array ( n type -- alien ) + [ heap-size calloc ] [ ] 2bi ; inline + +: (malloc-array) ( n type -- alien ) + [ heap-size * malloc ] [ ] 2bi ; inline + +: ( type -- array ) + heap-size ; inline + +: (c-object) ( type -- array ) + heap-size (byte-array) ; inline + +: malloc-object ( type -- alien ) + 1 swap heap-size calloc ; inline + +: (malloc-object) ( type -- alien ) + heap-size malloc ; inline + +: malloc-byte-array ( byte-array -- alien ) + dup byte-length [ nip malloc dup ] 2keep memcpy ; + +: memory>byte-array ( alien len -- byte-array ) + [ nip (byte-array) dup ] 2keep memcpy ; + +: malloc-string ( string encoding -- alien ) + string>alien malloc-byte-array ; + +: malloc-file-contents ( path -- alien len ) + binary file-contents [ malloc-byte-array ] [ length ] bi ; + +M: memory-stream stream-read + [ + [ index>> ] [ alien>> ] bi + swap memory>byte-array + ] [ [ + ] change-index drop ] 2bi ; + +: byte-array>memory ( byte-array base -- ) + swap dup byte-length memcpy ; inline + +: >c-bool ( ? -- int ) 1 0 ? ; inline + +: c-bool> ( int -- ? ) 0 = not ; inline + +M: value-type c-type-rep drop int-rep ; + +M: value-type c-type-getter + drop [ swap ] ; + +M: value-type c-type-setter ( type -- quot ) + [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri + '[ @ swap @ _ memcpy ] ; + diff --git a/basis/alien/data/summary.txt b/basis/alien/data/summary.txt new file mode 100644 index 0000000000..addddb2da4 --- /dev/null +++ b/basis/alien/data/summary.txt @@ -0,0 +1 @@ +Words for allocating objects and arrays of C types diff --git a/basis/alien/fortran/fortran-tests.factor b/basis/alien/fortran/fortran-tests.factor index 9d893b95c4..238207f192 100644 --- a/basis/alien/fortran/fortran-tests.factor +++ b/basis/alien/fortran/fortran-tests.factor @@ -1,7 +1,7 @@ ! (c) 2009 Joe Groff, see BSD license USING: accessors alien alien.c-types alien.complex -alien.fortran alien.fortran.private alien.strings classes.struct -arrays assocs byte-arrays combinators fry +alien.data alien.fortran alien.fortran.private alien.strings +classes.struct arrays assocs byte-arrays combinators fry generalizations io.encodings.ascii kernel macros macros.expander namespaces sequences shuffle tools.test ; IN: alien.fortran.tests diff --git a/basis/alien/fortran/fortran.factor b/basis/alien/fortran/fortran.factor index 3670a376e1..bf8721b549 100644 --- a/basis/alien/fortran/fortran.factor +++ b/basis/alien/fortran/fortran.factor @@ -1,5 +1,5 @@ ! (c) 2009 Joe Groff, see BSD license -USING: accessors alien alien.c-types alien.complex grouping +USING: accessors alien alien.c-types alien.complex alien.data grouping alien.strings alien.syntax arrays ascii assocs byte-arrays combinators combinators.short-circuit fry generalizations kernel lexer macros math math.parser namespaces parser sequences diff --git a/basis/alien/remote-control/remote-control.factor b/basis/alien/remote-control/remote-control.factor index b72c79e478..4ccd0e7488 100644 --- a/basis/alien/remote-control/remote-control.factor +++ b/basis/alien/remote-control/remote-control.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien alien.c-types alien.strings parser +USING: accessors alien alien.data alien.strings parser threads words kernel.private kernel io.encodings.utf8 eval ; IN: alien.remote-control diff --git a/basis/alien/structs/structs-docs.factor b/basis/alien/structs/structs-docs.factor index 62a3817fec..d0485ae4ba 100644 --- a/basis/alien/structs/structs-docs.factor +++ b/basis/alien/structs/structs-docs.factor @@ -1,4 +1,4 @@ -USING: alien.c-types strings help.markup help.syntax alien.syntax +USING: alien.c-types alien.data strings help.markup help.syntax alien.syntax sequences io arrays kernel words assocs namespaces ; IN: alien.structs diff --git a/basis/alien/structs/structs-tests.factor b/basis/alien/structs/structs-tests.factor index 3f84377d5c..d22aa5ee45 100755 --- a/basis/alien/structs/structs-tests.factor +++ b/basis/alien/structs/structs-tests.factor @@ -1,4 +1,4 @@ -USING: alien alien.syntax alien.c-types kernel tools.test +USING: alien alien.syntax alien.c-types alien.data kernel tools.test sequences system libc words vocabs namespaces layouts ; IN: alien.structs.tests diff --git a/basis/bit-arrays/bit-arrays.factor b/basis/bit-arrays/bit-arrays.factor index 0f87cf4cb6..f5613da6b5 100644 --- a/basis/bit-arrays/bit-arrays.factor +++ b/basis/bit-arrays/bit-arrays.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2007, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types accessors math alien.accessors kernel +USING: alien.c-types alien.data accessors math alien.accessors kernel kernel.private sequences sequences.private byte-arrays parser prettyprint.custom fry ; IN: bit-arrays diff --git a/basis/checksums/openssl/openssl.factor b/basis/checksums/openssl/openssl.factor index 6f21d96e86..673500b62a 100644 --- a/basis/checksums/openssl/openssl.factor +++ b/basis/checksums/openssl/openssl.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2008 Slava Pestov ! See http://factorcode.org/license.txt for BSD license. -USING: accessors byte-arrays alien.c-types kernel continuations -destructors sequences io openssl openssl.libcrypto checksums -checksums.stream ; +USING: accessors byte-arrays alien.c-types alien.data kernel +continuations destructors sequences io openssl openssl.libcrypto +checksums checksums.stream ; IN: checksums.openssl ERROR: unknown-digest name ; diff --git a/basis/classes/struct/prettyprint/prettyprint.factor b/basis/classes/struct/prettyprint/prettyprint.factor index 7f57e8568a..43d24e5716 100644 --- a/basis/classes/struct/prettyprint/prettyprint.factor +++ b/basis/classes/struct/prettyprint/prettyprint.factor @@ -1,5 +1,5 @@ ! (c)Joe Groff bsd license -USING: accessors alien alien.c-types alien.prettyprint arrays +USING: accessors alien alien.c-types alien.data alien.prettyprint arrays assocs classes classes.struct combinators combinators.short-circuit continuations fry kernel libc make math math.parser mirrors prettyprint.backend prettyprint.custom prettyprint.sections diff --git a/basis/classes/struct/struct-tests.factor b/basis/classes/struct/struct-tests.factor index e9e45487f9..b60bfa375b 100755 --- a/basis/classes/struct/struct-tests.factor +++ b/basis/classes/struct/struct-tests.factor @@ -1,5 +1,5 @@ ! (c)Joe Groff bsd license -USING: accessors alien alien.c-types ascii +USING: accessors alien alien.c-types alien.data ascii assocs byte-arrays classes.struct classes.tuple.private combinators compiler.tree.debugger compiler.units destructors io.encodings.utf8 io.pathnames io.streams.string kernel libc diff --git a/basis/classes/struct/struct.factor b/basis/classes/struct/struct.factor index dabdead10c..7e99328652 100755 --- a/basis/classes/struct/struct.factor +++ b/basis/classes/struct/struct.factor @@ -1,5 +1,5 @@ ! (c)Joe Groff bsd license -USING: accessors alien alien.c-types alien.parser arrays +USING: accessors alien alien.c-types alien.data alien.parser arrays byte-arrays classes classes.parser classes.tuple classes.tuple.parser classes.tuple.private combinators combinators.short-circuit combinators.smart cpu.architecture definitions functors.backend diff --git a/basis/cocoa/enumeration/enumeration.factor b/basis/cocoa/enumeration/enumeration.factor index caa83331ab..c7bdf625d9 100755 --- a/basis/cocoa/enumeration/enumeration.factor +++ b/basis/cocoa/enumeration/enumeration.factor @@ -1,17 +1,16 @@ ! Copyright (C) 2008 Joe Groff. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors kernel classes.struct cocoa cocoa.types alien.c-types -locals math sequences vectors fry libc destructors ; +USING: accessors kernel classes.struct cocoa cocoa.runtime cocoa.types alien.data +locals math sequences vectors fry libc destructors specialized-arrays ; +SPECIALIZED-ARRAY: id IN: cocoa.enumeration -<< "id" require-c-array >> - CONSTANT: NS-EACH-BUFFER-SIZE 16 : with-enumeration-buffers ( quot -- ) '[ NSFastEnumerationState malloc-struct &free - NS-EACH-BUFFER-SIZE "id" malloc-array &free + NS-EACH-BUFFER-SIZE id malloc-array &free NS-EACH-BUFFER-SIZE @ ] with-destructors ; inline @@ -19,7 +18,7 @@ CONSTANT: NS-EACH-BUFFER-SIZE 16 :: (NSFastEnumeration-each) ( object quot: ( elt -- ) state stackbuf count -- ) object state stackbuf count -> countByEnumeratingWithState:objects:count: :> items-count items-count 0 = [ - state itemsPtr>> [ items-count "id" ] [ stackbuf ] if* :> items + state itemsPtr>> [ items-count id ] [ stackbuf ] if* :> items items-count iota [ items nth quot call ] each object quot state stackbuf count (NSFastEnumeration-each) ] unless ; inline recursive diff --git a/basis/cocoa/plists/plists.factor b/basis/cocoa/plists/plists.factor index ceb097bb3a..86b13b2ddc 100644 --- a/basis/cocoa/plists/plists.factor +++ b/basis/cocoa/plists/plists.factor @@ -4,8 +4,8 @@ USING: strings arrays hashtables assocs sequences fry macros cocoa.messages cocoa.classes cocoa.application cocoa kernel namespaces io.backend math cocoa.enumeration byte-arrays -combinators alien.c-types words core-foundation quotations -core-foundation.data core-foundation.utilities ; +combinators alien.c-types alien.data words core-foundation +quotations core-foundation.data core-foundation.utilities ; IN: cocoa.plists : >plist ( value -- plist ) >cf -> autorelease ; diff --git a/basis/compiler/tests/intrinsics.factor b/basis/compiler/tests/intrinsics.factor index dc2f5d9257..24114e0ccb 100644 --- a/basis/compiler/tests/intrinsics.factor +++ b/basis/compiler/tests/intrinsics.factor @@ -3,7 +3,7 @@ math math.constants math.private math.integers.private sequences strings tools.test words continuations sequences.private hashtables.private byte-arrays system random layouts vectors sbufs strings.private slots.private alien math.order -alien.accessors alien.c-types alien.syntax alien.strings +alien.accessors alien.c-types alien.data alien.syntax alien.strings namespaces libc io.encodings.ascii classes compiler ; FROM: math => float ; IN: compiler.tests.intrinsics diff --git a/basis/cpu/ppc/ppc.factor b/basis/cpu/ppc/ppc.factor index 063f3b37b1..72ad543307 100644 --- a/basis/cpu/ppc/ppc.factor +++ b/basis/cpu/ppc/ppc.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs sequences kernel combinators make math math.order math.ranges system namespaces locals layouts words -alien alien.accessors alien.c-types literals cpu.architecture +alien alien.accessors alien.c-types alien.data literals cpu.architecture cpu.ppc.assembler cpu.ppc.assembler.backend compiler.cfg.registers compiler.cfg.instructions compiler.cfg.comparisons compiler.codegen.fixup compiler.cfg.intrinsics diff --git a/basis/db/postgresql/lib/lib.factor b/basis/db/postgresql/lib/lib.factor index 2278afe4ed..5398e669ed 100644 --- a/basis/db/postgresql/lib/lib.factor +++ b/basis/db/postgresql/lib/lib.factor @@ -2,11 +2,11 @@ ! See http://factorcode.org/license.txt for BSD license. USING: arrays continuations db io kernel math namespaces quotations sequences db.postgresql.ffi alien alien.c-types -db.types tools.walker ascii splitting math.parser combinators -libc calendar.format byte-arrays destructors prettyprint -accessors strings serialize io.encodings.binary io.encodings.utf8 -alien.strings io.streams.byte-array summary present urls -specialized-arrays db.private ; +alien.data db.types tools.walker ascii splitting math.parser +combinators libc calendar.format byte-arrays destructors +prettyprint accessors strings serialize io.encodings.binary +io.encodings.utf8 alien.strings io.streams.byte-array summary +present urls specialized-arrays db.private ; SPECIALIZED-ARRAY: uint SPECIALIZED-ARRAY: void* IN: db.postgresql.lib diff --git a/basis/db/sqlite/lib/lib.factor b/basis/db/sqlite/lib/lib.factor index 3565b09856..163026f5ff 100644 --- a/basis/db/sqlite/lib/lib.factor +++ b/basis/db/sqlite/lib/lib.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Chris Double, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types arrays assocs kernel math math.parser +USING: alien.c-types alien.data arrays assocs kernel math math.parser namespaces sequences db.sqlite.ffi db combinators continuations db.types calendar.format serialize io.streams.byte-array byte-arrays io.encodings.binary diff --git a/basis/environment/unix/unix.factor b/basis/environment/unix/unix.factor index 84dfbbd43e..3fc8c2f79b 100644 --- a/basis/environment/unix/unix.factor +++ b/basis/environment/unix/unix.factor @@ -1,8 +1,9 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types alien.strings alien.syntax kernel -layouts sequences system unix environment io.encodings.utf8 -unix.utilities vocabs.loader combinators alien.accessors ; +USING: alien alien.c-types alien.data alien.strings +alien.syntax kernel layouts sequences system unix +environment io.encodings.utf8 unix.utilities vocabs.loader +combinators alien.accessors ; IN: environment.unix HOOK: environ os ( -- void* ) diff --git a/basis/environment/winnt/winnt.factor b/basis/environment/winnt/winnt.factor index 518a7d5d7a..cba92a0e3c 100755 --- a/basis/environment/winnt/winnt.factor +++ b/basis/environment/winnt/winnt.factor @@ -1,15 +1,14 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: alien.strings fry io.encodings.utf16n kernel -splitting windows windows.kernel32 system environment -alien.c-types sequences windows.errors io.streams.memory -io.encodings io ; +splitting windows windows.kernel32 windows.types system +environment alien.data sequences windows.errors +io.streams.memory io.encodings io ; +SPECIALIZED-ARRAY: TCHAR IN: environment.winnt -<< "TCHAR" require-c-array >> - M: winnt os-env ( key -- value ) - MAX_UNICODE_PATH "TCHAR" + MAX_UNICODE_PATH TCHAR [ dup length GetEnvironmentVariable ] keep over 0 = [ 2drop f ] [ diff --git a/basis/game-input/dinput/dinput.factor b/basis/game-input/dinput/dinput.factor index a7489f26a2..16bea60ea5 100755 --- a/basis/game-input/dinput/dinput.factor +++ b/basis/game-input/dinput/dinput.factor @@ -6,7 +6,7 @@ math.rectangles namespaces parser sequences shuffle specialized-arrays ui.backend.windows vectors windows.com windows.dinput windows.dinput.constants windows.errors windows.kernel32 windows.messages windows.ole32 -windows.user32 classes.struct ; +windows.user32 classes.struct alien.data ; SPECIALIZED-ARRAY: DIDEVICEOBJECTDATA IN: game-input.dinput diff --git a/basis/game-input/dinput/keys-array/keys-array.factor b/basis/game-input/dinput/keys-array/keys-array.factor index 9a84747dd8..a8813b0397 100755 --- a/basis/game-input/dinput/keys-array/keys-array.factor +++ b/basis/game-input/dinput/keys-array/keys-array.factor @@ -1,5 +1,5 @@ -USING: sequences sequences.private math alien.c-types -accessors ; +USING: sequences sequences.private math +accessors alien.data ; IN: game-input.dinput.keys-array TUPLE: keys-array diff --git a/basis/game-input/iokit/iokit.factor b/basis/game-input/iokit/iokit.factor index 71d547ad29..85f058f283 100755 --- a/basis/game-input/iokit/iokit.factor +++ b/basis/game-input/iokit/iokit.factor @@ -3,7 +3,8 @@ kernel cocoa.enumeration destructors math.parser cocoa.application sequences locals combinators.short-circuit threads namespaces assocs arrays combinators hints alien core-foundation.run-loop accessors sequences.private -alien.c-types math parser game-input vectors bit-arrays ; +alien.c-types alien.data math parser game-input vectors +bit-arrays ; IN: game-input.iokit SINGLETON: iokit-game-input-backend diff --git a/basis/images/memory/memory.factor b/basis/images/memory/memory.factor index 1a977b604e..ccf891d770 100644 --- a/basis/images/memory/memory.factor +++ b/basis/images/memory/memory.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien.c-types destructors fry images kernel -libc math sequences ; +USING: accessors alien.c-types alien.data destructors fry images +kernel libc math sequences ; IN: images.memory ! Some code shared by core-graphics and cairo for constructing @@ -27,4 +27,4 @@ PRIVATE> : make-memory-bitmap ( dim quot -- image ) '[ [ malloc-bitmap-data ] keep _ [ ] 2bi - ] with-destructors ; inline \ No newline at end of file + ] with-destructors ; inline diff --git a/basis/io/backend/windows/nt/privileges/privileges.factor b/basis/io/backend/windows/nt/privileges/privileges.factor index 57878ba75b..bb9e0edc33 100755 --- a/basis/io/backend/windows/nt/privileges/privileges.factor +++ b/basis/io/backend/windows/nt/privileges/privileges.factor @@ -1,4 +1,4 @@ -USING: alien alien.c-types alien.syntax arrays continuations +USING: alien alien.c-types alien.data alien.syntax arrays continuations destructors generic io.mmap io.ports io.backend.windows io.files.windows kernel libc math math.bitwise namespaces quotations sequences windows windows.advapi32 windows.kernel32 io.backend system accessors diff --git a/basis/io/buffers/buffers-tests.factor b/basis/io/buffers/buffers-tests.factor index 4425e08106..d366df7c54 100644 --- a/basis/io/buffers/buffers-tests.factor +++ b/basis/io/buffers/buffers-tests.factor @@ -1,7 +1,7 @@ IN: io.buffers.tests -USING: alien alien.c-types io.buffers kernel kernel.private libc -sequences tools.test namespaces byte-arrays strings accessors -destructors ; +USING: alien alien.c-types alien.data io.buffers kernel +kernel.private libc sequences tools.test namespaces byte-arrays +strings accessors destructors ; : buffer-set ( string buffer -- ) over >byte-array over ptr>> byte-array>memory diff --git a/basis/io/buffers/buffers.factor b/basis/io/buffers/buffers.factor index 82c5326b1d..aa9cedf340 100644 --- a/basis/io/buffers/buffers.factor +++ b/basis/io/buffers/buffers.factor @@ -2,8 +2,8 @@ ! Copyright (C) 2006, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors alien alien.accessors alien.c-types -alien.syntax kernel libc math sequences byte-arrays strings -hints math.order destructors combinators ; +alien.data alien.syntax kernel libc math sequences byte-arrays +strings hints math.order destructors combinators ; IN: io.buffers TUPLE: buffer diff --git a/basis/io/files/info/windows/windows.factor b/basis/io/files/info/windows/windows.factor index bb3a412669..5ae21fcfee 100755 --- a/basis/io/files/info/windows/windows.factor +++ b/basis/io/files/info/windows/windows.factor @@ -6,7 +6,7 @@ windows.time windows accessors alien.c-types combinators generalizations system alien.strings io.encodings.utf16n sequences splitting windows.errors fry continuations destructors calendar ascii combinators.short-circuit locals classes.struct -specialized-arrays ; +specialized-arrays alien.data ; SPECIALIZED-ARRAY: ushort IN: io.files.info.windows diff --git a/basis/io/files/windows/windows.factor b/basis/io/files/windows/windows.factor index 43463bd3f1..ca5c9b3c4a 100755 --- a/basis/io/files/windows/windows.factor +++ b/basis/io/files/windows/windows.factor @@ -6,7 +6,7 @@ io.backend.windows kernel math splitting fry alien.strings windows windows.kernel32 windows.time calendar combinators math.functions sequences namespaces make words system destructors accessors math.bitwise continuations windows.errors -arrays byte-arrays generalizations ; +arrays byte-arrays generalizations alien.data ; IN: io.files.windows : open-file ( path access-mode create-mode flags -- handle ) diff --git a/basis/io/mmap/mmap.factor b/basis/io/mmap/mmap.factor index 704a585dd4..a866232760 100644 --- a/basis/io/mmap/mmap.factor +++ b/basis/io/mmap/mmap.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: continuations destructors io.files io.files.info io.backend kernel quotations system alien alien.accessors -accessors vocabs.loader combinators alien.c-types +accessors vocabs.loader combinators alien.c-types alien.data math ; IN: io.mmap diff --git a/basis/io/monitors/windows/nt/nt.factor b/basis/io/monitors/windows/nt/nt.factor index 3d837d79d8..9cd8bc4df8 100755 --- a/basis/io/monitors/windows/nt/nt.factor +++ b/basis/io/monitors/windows/nt/nt.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2008 Doug Coleman, Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types alien.strings libc destructors locals -kernel math assocs namespaces make continuations sequences +USING: alien alien.c-types alien.data alien.strings libc destructors +locals kernel math assocs namespaces make continuations sequences hashtables sorting arrays combinators math.bitwise strings system accessors threads splitting io.backend io.backend.windows io.backend.windows.nt io.files.windows.nt io.monitors io.ports diff --git a/basis/io/sockets/secure/openssl/openssl.factor b/basis/io/sockets/secure/openssl/openssl.factor index 8f596da0bd..6d01a66cf0 100644 --- a/basis/io/sockets/secure/openssl/openssl.factor +++ b/basis/io/sockets/secure/openssl/openssl.factor @@ -1,10 +1,10 @@ ! Copyright (C) 2007, 2008, Slava Pestov, Elie CHAFTARI. ! See http://factorcode.org/license.txt for BSD license. USING: accessors byte-arrays kernel sequences namespaces math -math.order combinators init alien alien.c-types alien.strings -libc continuations destructors summary splitting assocs random -math.parser locals unicode.case openssl openssl.libcrypto -openssl.libssl io.backend io.ports io.pathnames +math.order combinators init alien alien.c-types alien.data +alien.strings libc continuations destructors summary splitting +assocs random math.parser locals unicode.case openssl +openssl.libcrypto openssl.libssl io.backend io.ports io.pathnames io.encodings.8-bit io.timeouts io.sockets.secure ; IN: io.sockets.secure.openssl diff --git a/basis/io/sockets/sockets.factor b/basis/io/sockets/sockets.factor index 601d269d5c..a542575446 100755 --- a/basis/io/sockets/sockets.factor +++ b/basis/io/sockets/sockets.factor @@ -6,7 +6,7 @@ arrays io.encodings io.ports io.streams.duplex io.encodings.ascii alien.strings io.binary accessors destructors classes byte-arrays parser alien.c-types math.parser splitting grouping math assocs summary system vocabs.loader combinators present fry vocabs.parser -classes.struct ; +classes.struct alien.data ; IN: io.sockets << { diff --git a/basis/io/sockets/unix/unix.factor b/basis/io/sockets/unix/unix.factor index e892c6a7ef..fa46a71ca0 100755 --- a/basis/io/sockets/unix/unix.factor +++ b/basis/io/sockets/unix/unix.factor @@ -5,7 +5,7 @@ threads sequences byte-arrays io.binary io.backend.unix io.streams.duplex io.backend io.pathnames io.sockets.private io.files.private io.encodings.utf8 math.parser continuations libc combinators system accessors destructors unix locals init -classes.struct ; +classes.struct alien.data ; EXCLUDE: namespaces => bind ; EXCLUDE: io => read write ; diff --git a/basis/io/sockets/windows/nt/nt.factor b/basis/io/sockets/windows/nt/nt.factor index f423a42b65..7cc21c9611 100755 --- a/basis/io/sockets/windows/nt/nt.factor +++ b/basis/io/sockets/windows/nt/nt.factor @@ -1,4 +1,4 @@ -USING: alien alien.accessors alien.c-types byte-arrays +USING: alien alien.accessors alien.c-types alien.data byte-arrays continuations destructors io.ports io.timeouts io.sockets io.sockets.private io namespaces io.streams.duplex io.backend.windows io.sockets.windows io.backend.windows.nt diff --git a/basis/libc/libc.factor b/basis/libc/libc.factor index 4142e40c68..fe56c83516 100644 --- a/basis/libc/libc.factor +++ b/basis/libc/libc.factor @@ -2,29 +2,29 @@ ! Copyright (C) 2007, 2009 Slava Pestov ! Copyright (C) 2007, 2008 Doug Coleman ! See http://factorcode.org/license.txt for BSD license. -USING: alien assocs continuations alien.destructors kernel +USING: alien alien.c-types assocs continuations alien.destructors kernel namespaces accessors sets summary destructors destructors.private ; IN: libc : errno ( -- int ) - "int" "factor" "err_no" { } alien-invoke ; + int "factor" "err_no" { } alien-invoke ; : clear-errno ( -- ) - "void" "factor" "clear_err_no" { } alien-invoke ; + void "factor" "clear_err_no" { } alien-invoke ; >c-ptr [ delete-malloc ] [ (free) ] bi ; : memcpy ( dst src size -- ) - "void" "libc" "memcpy" { "void*" "void*" "ulong" } alien-invoke ; + void "libc" "memcpy" { void* void* ulong } alien-invoke ; : memcmp ( a b size -- cmp ) - "int" "libc" "memcmp" { "void*" "void*" "ulong" } alien-invoke ; + int "libc" "memcmp" { void* void* ulong } alien-invoke ; : memory= ( a b size -- ? ) memcmp 0 = ; : strlen ( alien -- len ) - "size_t" "libc" "strlen" { "char*" } alien-invoke ; + size_t "libc" "strlen" { char* } alien-invoke ; DESTRUCTOR: free diff --git a/basis/math/blas/matrices/matrices.factor b/basis/math/blas/matrices/matrices.factor index 4212f32b2d..aa9681bb2e 100755 --- a/basis/math/blas/matrices/matrices.factor +++ b/basis/math/blas/matrices/matrices.factor @@ -1,10 +1,10 @@ -USING: accessors alien alien.c-types arrays byte-arrays combinators -combinators.short-circuit fry kernel locals macros -math math.blas.ffi math.blas.vectors math.blas.vectors.private -math.complex math.functions math.order functors words -sequences sequences.merged sequences.private shuffle -parser prettyprint.backend prettyprint.custom ascii -specialized-arrays ; +USING: accessors alien alien.c-types alien.data arrays +byte-arrays combinators combinators.short-circuit fry +kernel locals macros math math.blas.ffi math.blas.vectors +math.blas.vectors.private math.complex math.functions +math.order functors words sequences sequences.merged +sequences.private shuffle parser prettyprint.backend +prettyprint.custom ascii specialized-arrays ; FROM: alien.c-types => float ; SPECIALIZED-ARRAY: float SPECIALIZED-ARRAY: double diff --git a/basis/math/vectors/simd/intrinsics/intrinsics.factor b/basis/math/vectors/simd/intrinsics/intrinsics.factor index 28547f8cf9..914d1ef169 100644 --- a/basis/math/vectors/simd/intrinsics/intrinsics.factor +++ b/basis/math/vectors/simd/intrinsics/intrinsics.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel alien alien.c-types cpu.architecture libc ; +USING: kernel alien alien.data cpu.architecture libc ; IN: math.vectors.simd.intrinsics ERROR: bad-simd-call ; diff --git a/basis/opengl/shaders/shaders.factor b/basis/opengl/shaders/shaders.factor index 26ffd0cf88..562cbc91ce 100755 --- a/basis/opengl/shaders/shaders.factor +++ b/basis/opengl/shaders/shaders.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2008 Joe Groff. ! See http://factorcode.org/license.txt for BSD license. USING: kernel opengl.gl alien.c-types continuations namespaces -assocs alien alien.strings libc opengl math sequences combinators +assocs alien alien.data alien.strings libc opengl math sequences combinators macros arrays io.encodings.ascii fry specialized-arrays destructors accessors ; SPECIALIZED-ARRAY: uint diff --git a/basis/random/windows/windows.factor b/basis/random/windows/windows.factor index 83b1fab0d0..d959b191c9 100644 --- a/basis/random/windows/windows.factor +++ b/basis/random/windows/windows.factor @@ -1,4 +1,4 @@ -USING: accessors alien.c-types byte-arrays +USING: accessors alien.c-types alien.data byte-arrays combinators.short-circuit continuations destructors init kernel locals namespaces random windows.advapi32 windows.errors windows.kernel32 math.bitwise ; diff --git a/basis/specialized-arrays/specialized-arrays-tests.factor b/basis/specialized-arrays/specialized-arrays-tests.factor index e289efb077..5d88f42d50 100755 --- a/basis/specialized-arrays/specialized-arrays-tests.factor +++ b/basis/specialized-arrays/specialized-arrays-tests.factor @@ -4,7 +4,7 @@ specialized-arrays.private sequences alien.c-types accessors kernel arrays combinators compiler compiler.units classes.struct combinators.smart compiler.tree.debugger math libc destructors sequences.private multiline eval words vocabs namespaces -assocs prettyprint ; +assocs prettyprint alien.data ; FROM: alien.c-types => float ; SPECIALIZED-ARRAY: int diff --git a/basis/specialized-arrays/specialized-arrays.factor b/basis/specialized-arrays/specialized-arrays.factor index 0490ede304..6931c83677 100755 --- a/basis/specialized-arrays/specialized-arrays.factor +++ b/basis/specialized-arrays/specialized-arrays.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien alien.c-types alien.parser assocs +USING: accessors alien alien.c-types alien.data alien.parser assocs byte-arrays classes compiler.units functors kernel lexer libc math math.vectors.specialization namespaces parser prettyprint.custom sequences sequences.private strings summary vocabs vocabs.loader diff --git a/basis/tools/deploy/config/config-docs.factor b/basis/tools/deploy/config/config-docs.factor index bd612c644a..12016168fb 100644 --- a/basis/tools/deploy/config/config-docs.factor +++ b/basis/tools/deploy/config/config-docs.factor @@ -1,4 +1,4 @@ -USING: help.markup help.syntax words alien.c-types assocs +USING: help.markup help.syntax words alien.c-types alien.data assocs kernel math ; IN: tools.deploy.config diff --git a/basis/tools/disassembler/disassembler.factor b/basis/tools/disassembler/disassembler.factor index 0a8ab0b116..16408c0eb8 100755 --- a/basis/tools/disassembler/disassembler.factor +++ b/basis/tools/disassembler/disassembler.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: alien alien.c-types arrays byte-arrays combinators destructors generic io kernel libc math sequences system tr -vocabs.loader words ; +vocabs.loader words alien.data ; IN: tools.disassembler GENERIC: disassemble ( obj -- ) diff --git a/basis/tools/disassembler/udis/udis.factor b/basis/tools/disassembler/udis/udis.factor index 2f0456ab62..aaa54ae527 100755 --- a/basis/tools/disassembler/udis/udis.factor +++ b/basis/tools/disassembler/udis/udis.factor @@ -4,7 +4,7 @@ USING: tools.disassembler namespaces combinators alien alien.syntax alien.c-types lexer parser kernel sequences layouts math math.order alien.libraries math.parser system make fry arrays libc destructors -tools.disassembler.utils splitting ; +tools.disassembler.utils splitting alien.data ; IN: tools.disassembler.udis << diff --git a/basis/ui/backend/cocoa/views/views.factor b/basis/ui/backend/cocoa/views/views.factor index 6ae56af030..a49d22735d 100644 --- a/basis/ui/backend/cocoa/views/views.factor +++ b/basis/ui/backend/cocoa/views/views.factor @@ -1,10 +1,10 @@ ! Copyright (C) 2006, 2008 Slava Pestov ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien alien.c-types alien.strings arrays assocs -cocoa kernel math cocoa.messages cocoa.subclassing cocoa.classes -cocoa.views cocoa.application cocoa.pasteboard cocoa.types -cocoa.windows sequences io.encodings.utf8 ui ui.private ui.gadgets -ui.gadgets.private ui.gadgets.worlds ui.gestures +USING: accessors alien alien.c-types alien.data alien.strings +arrays assocs cocoa kernel math cocoa.messages cocoa.subclassing +cocoa.classes cocoa.views cocoa.application cocoa.pasteboard +cocoa.types cocoa.windows sequences io.encodings.utf8 ui ui.private +ui.gadgets ui.gadgets.private ui.gadgets.worlds ui.gestures core-foundation.strings core-graphics core-graphics.types threads combinators math.rectangles ; IN: ui.backend.cocoa.views diff --git a/basis/ui/backend/windows/windows.factor b/basis/ui/backend/windows/windows.factor index 2be6e70df8..5e2c25ea30 100755 --- a/basis/ui/backend/windows/windows.factor +++ b/basis/ui/backend/windows/windows.factor @@ -13,7 +13,7 @@ opengl ui.render math.bitwise locals accessors math.rectangles math.order calendar ascii sets io.encodings.utf16n windows.errors literals ui.pixel-formats ui.pixel-formats.private memoize classes -specialized-arrays classes.struct ; +specialized-arrays classes.struct alien.data ; SPECIALIZED-ARRAY: POINT IN: ui.backend.windows diff --git a/basis/unix/process/process.factor b/basis/unix/process/process.factor index 131d8dda5d..2912f8b744 100644 --- a/basis/unix/process/process.factor +++ b/basis/unix/process/process.factor @@ -1,6 +1,6 @@ -USING: kernel alien.c-types alien.strings sequences math alien.syntax -unix namespaces continuations threads assocs io.backend.unix -io.encodings.utf8 unix.utilities fry ; +USING: kernel alien.c-types alien.data alien.strings sequences +math alien.syntax unix namespaces continuations threads assocs +io.backend.unix io.encodings.utf8 unix.utilities fry ; IN: unix.process ! Low-level Unix process launching utilities. These are used diff --git a/basis/unix/utilities/utilities.factor b/basis/unix/utilities/utilities.factor index 8d141ccb24..919b2ae8a2 100644 --- a/basis/unix/utilities/utilities.factor +++ b/basis/unix/utilities/utilities.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types alien.strings +USING: alien alien.c-types alien.data alien.strings combinators.short-circuit fry kernel layouts sequences accessors specialized-arrays ; IN: unix.utilities diff --git a/basis/unix/utmpx/utmpx.factor b/basis/unix/utmpx/utmpx.factor index 6e72f7d114..f6ccf6858b 100644 --- a/basis/unix/utmpx/utmpx.factor +++ b/basis/unix/utmpx/utmpx.factor @@ -1,9 +1,9 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types alien.syntax combinators continuations -io.encodings.string io.encodings.utf8 kernel sequences strings -unix calendar system accessors unix.time calendar.unix -vocabs.loader ; +USING: alien.c-types alien.data alien.syntax combinators +continuations io.encodings.string io.encodings.utf8 kernel +sequences strings unix calendar system accessors unix.time +calendar.unix vocabs.loader ; IN: unix.utmpx CONSTANT: EMPTY 0 diff --git a/basis/windows/com/com.factor b/basis/windows/com/com.factor index d485692a91..e06f5b6071 100644 --- a/basis/windows/com/com.factor +++ b/basis/windows/com/com.factor @@ -1,6 +1,6 @@ USING: alien alien.c-types alien.destructors windows.com.syntax windows.ole32 windows.types continuations kernel alien.syntax -libc destructors accessors ; +libc destructors accessors alien.data ; IN: windows.com LIBRARY: ole32 diff --git a/basis/windows/com/wrapper/wrapper.factor b/basis/windows/com/wrapper/wrapper.factor index e69fc5b820..e4f0ef0654 100755 --- a/basis/windows/com/wrapper/wrapper.factor +++ b/basis/windows/com/wrapper/wrapper.factor @@ -1,9 +1,9 @@ -USING: alien alien.c-types alien.accessors windows.com.syntax -init windows.com.syntax.private windows.com continuations kernel -namespaces windows.ole32 libc vocabs assocs accessors arrays -sequences quotations combinators math words compiler.units -destructors fry math.parser generalizations sets -specialized-arrays windows.kernel32 classes.struct ; +USING: alien alien.c-types alien.data alien.accessors +windows.com.syntax init windows.com.syntax.private windows.com +continuations kernel namespaces windows.ole32 libc vocabs +assocs accessors arrays sequences quotations combinators math +words compiler.units destructors fry math.parser generalizations +sets specialized-arrays windows.kernel32 classes.struct ; SPECIALIZED-ARRAY: void* IN: windows.com.wrapper diff --git a/basis/windows/dinput/constants/constants.factor b/basis/windows/dinput/constants/constants.factor index 270c2fa3dd..3c0509c49d 100755 --- a/basis/windows/dinput/constants/constants.factor +++ b/basis/windows/dinput/constants/constants.factor @@ -1,8 +1,9 @@ USING: windows.dinput windows.kernel32 windows.ole32 windows.com -windows.com.syntax alien alien.c-types alien.syntax kernel system namespaces -combinators sequences fry math accessors macros words quotations -libc continuations generalizations splitting locals assocs init -specialized-arrays memoize classes.struct strings arrays ; +windows.com.syntax alien alien.c-types alien.data alien.syntax +kernel system namespaces combinators sequences fry math accessors +macros words quotations libc continuations generalizations +splitting locals assocs init specialized-arrays memoize +classes.struct strings arrays ; SPECIALIZED-ARRAY: DIOBJECTDATAFORMAT IN: windows.dinput.constants diff --git a/basis/windows/dragdrop-listener/dragdrop-listener.factor b/basis/windows/dragdrop-listener/dragdrop-listener.factor index bd6512341f..bb8e60cdf5 100755 --- a/basis/windows/dragdrop-listener/dragdrop-listener.factor +++ b/basis/windows/dragdrop-listener/dragdrop-listener.factor @@ -1,17 +1,16 @@ USING: alien.strings io.encodings.utf16n windows.com windows.com.wrapper combinators windows.kernel32 windows.ole32 -windows.shell32 kernel accessors +windows.shell32 kernel accessors windows.types prettyprint namespaces ui.tools.listener ui.tools.workspace -alien.c-types alien sequences math ; +alien.data alien sequences math ; +SPECIALIZED-ARRAY: WCHAR IN: windows.dragdrop-listener -<< "WCHAR" require-c-array >> - : filenames-from-hdrop ( hdrop -- filenames ) dup HEX: FFFFFFFF f 0 DragQueryFile ! get count of files [ 2dup f 0 DragQueryFile 1 + ! get size of filename buffer - dup "WCHAR" + dup WCHAR [ swap DragQueryFile drop ] keep utf16n alien>string ] with map ; diff --git a/basis/windows/errors/errors.factor b/basis/windows/errors/errors.factor index d2ee337726..483494ba0c 100755 --- a/basis/windows/errors/errors.factor +++ b/basis/windows/errors/errors.factor @@ -1,11 +1,10 @@ -USING: alien.c-types kernel locals math math.bitwise +USING: alien.data kernel locals math math.bitwise windows.kernel32 sequences byte-arrays unicode.categories io.encodings.string io.encodings.utf16n alien.strings -arrays literals ; +arrays literals windows.types ; +SPECIALIZED-ARRAY: TCHAR IN: windows.errors -<< "TCHAR" require-c-array >> - CONSTANT: ERROR_SUCCESS 0 CONSTANT: ERROR_INVALID_FUNCTION 1 CONSTANT: ERROR_FILE_NOT_FOUND 2 @@ -698,8 +697,6 @@ CONSTANT: FORMAT_MESSAGE_MAX_WIDTH_MASK HEX: 000000FF : make-lang-id ( lang1 lang2 -- n ) 10 shift bitor ; inline -<< "TCHAR" require-c-array >> - ERROR: error-message-failed id ; :: n>win32-error-string ( id -- string ) { @@ -709,7 +706,7 @@ ERROR: error-message-failed id ; f id LANG_NEUTRAL SUBLANG_DEFAULT make-lang-id - 32768 [ "TCHAR" ] [ ] bi + 32768 [ TCHAR ] [ ] bi f pick [ FormatMessage 0 = [ id error-message-failed ] when ] dip utf16n alien>string [ blank? ] trim ; diff --git a/basis/windows/offscreen/offscreen.factor b/basis/windows/offscreen/offscreen.factor index 63cfd92ba1..e38477c98c 100755 --- a/basis/windows/offscreen/offscreen.factor +++ b/basis/windows/offscreen/offscreen.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2009 Joe Groff, Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types kernel combinators sequences -math windows.gdi32 windows.types images destructors -accessors fry locals classes.struct ; +USING: alien.c-types alien.data kernel combinators +sequences math windows.gdi32 windows.types images +destructors accessors fry locals classes.struct ; IN: windows.offscreen : (bitmap-info) ( dim -- BITMAPINFO ) diff --git a/basis/windows/ole32/ole32.factor b/basis/windows/ole32/ole32.factor index 9e117c8522..fe47a7f923 100755 --- a/basis/windows/ole32/ole32.factor +++ b/basis/windows/ole32/ole32.factor @@ -1,5 +1,5 @@ -USING: alien alien.syntax alien.c-types alien.strings math -kernel sequences windows.errors windows.types io accessors +USING: alien alien.syntax alien.c-types alien.data alien.strings +math kernel sequences windows.errors windows.types io accessors math.order namespaces make math.parser windows.kernel32 combinators locals specialized-arrays literals splitting grouping classes.struct combinators.smart ; diff --git a/basis/x11/xlib/xlib.factor b/basis/x11/xlib/xlib.factor index 98305e8304..0cd7704cf8 100644 --- a/basis/x11/xlib/xlib.factor +++ b/basis/x11/xlib/xlib.factor @@ -10,9 +10,9 @@ ! add to this library and are wondering what part of the file to ! modify, just find the function or data structure in the manual ! and note the section. -USING: accessors kernel arrays alien alien.c-types alien.strings -alien.syntax classes.struct math math.bitwise words sequences -namespaces continuations io io.encodings.ascii x11.syntax ; +USING: accessors kernel arrays alien alien.c-types alien.data +alien.strings alien.syntax classes.struct math math.bitwise words +sequences namespaces continuations io io.encodings.ascii x11.syntax ; FROM: alien.c-types => short ; IN: x11.xlib diff --git a/core/alien/strings/strings-tests.factor b/core/alien/strings/strings-tests.factor index 6a0a42253b..c1b5a9e159 100644 --- a/core/alien/strings/strings-tests.factor +++ b/core/alien/strings/strings-tests.factor @@ -1,4 +1,4 @@ -USING: alien.strings alien.c-types tools.test kernel libc +USING: alien.strings alien.c-types alien.data tools.test kernel libc io.encodings.8-bit io.encodings.utf8 io.encodings.utf16 io.encodings.utf16n io.encodings.ascii alien io.encodings.string ; IN: alien.strings.tests diff --git a/extra/alien/inline/syntax/syntax-tests.factor b/extra/alien/inline/syntax/syntax-tests.factor index e6a0b8b7d8..c49b2b5aae 100644 --- a/extra/alien/inline/syntax/syntax-tests.factor +++ b/extra/alien/inline/syntax/syntax-tests.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2009 Jeremy Hughes. ! See http://factorcode.org/license.txt for BSD license. USING: alien.inline alien.inline.syntax io.directories io.files -kernel namespaces tools.test alien.c-types alien.structs ; +kernel namespaces tools.test alien.c-types alien.data alien.structs ; IN: alien.inline.syntax.tests DELETE-C-LIBRARY: test diff --git a/extra/alien/marshall/marshall-docs.factor b/extra/alien/marshall/marshall-docs.factor index 361753a0d3..5d6ec29912 100644 --- a/extra/alien/marshall/marshall-docs.factor +++ b/extra/alien/marshall/marshall-docs.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2009 Jeremy Hughes. ! See http://factorcode.org/license.txt for BSD license. USING: help.markup help.syntax kernel quotations sequences -strings alien alien.c-types math byte-arrays ; +strings alien alien.c-types alien.data math byte-arrays ; IN: alien.marshall float short ; diff --git a/extra/alien/marshall/private/private.factor b/extra/alien/marshall/private/private.factor index c85b722d11..d138282ff3 100644 --- a/extra/alien/marshall/private/private.factor +++ b/extra/alien/marshall/private/private.factor @@ -3,7 +3,7 @@ USING: accessors alien alien.c-types alien.inline arrays combinators fry functors kernel lexer libc macros math sequences specialized-arrays libc.private -combinators.short-circuit ; +combinators.short-circuit alien.data ; SPECIALIZED-ARRAY: void* IN: alien.marshall.private diff --git a/extra/alien/marshall/structs/structs.factor b/extra/alien/marshall/structs/structs.factor index 54bcab45f2..3f9c8e3a7e 100644 --- a/extra/alien/marshall/structs/structs.factor +++ b/extra/alien/marshall/structs/structs.factor @@ -3,7 +3,7 @@ USING: accessors alien.c-types alien.marshall arrays assocs classes.tuple combinators destructors generalizations generic kernel libc locals parser quotations sequences slots words -alien.structs lexer vocabs.parser fry effects ; +alien.structs lexer vocabs.parser fry effects alien.data ; IN: alien.marshall.structs float ; SPECIALIZED-ARRAY: int diff --git a/extra/half-floats/half-floats-tests.factor b/extra/half-floats/half-floats-tests.factor index cf3d7d3690..ad3d156bc4 100644 --- a/extra/half-floats/half-floats-tests.factor +++ b/extra/half-floats/half-floats-tests.factor @@ -1,5 +1,5 @@ USING: alien.c-types alien.syntax half-floats kernel math tools.test -specialized-arrays ; +specialized-arrays alien.data ; SPECIALIZED-ARRAY: half IN: half-floats.tests diff --git a/extra/half-floats/half-floats.factor b/extra/half-floats/half-floats.factor index 2c089e4330..4d78068c03 100755 --- a/extra/half-floats/half-floats.factor +++ b/extra/half-floats/half-floats.factor @@ -1,5 +1,5 @@ ! (c)2009 Joe Groff bsd license -USING: accessors alien.c-types alien.syntax kernel math math.order ; +USING: accessors alien.c-types alien.data alien.syntax kernel math math.order ; IN: half-floats : half>bits ( float -- bits ) diff --git a/extra/io/serial/unix/unix.factor b/extra/io/serial/unix/unix.factor index 1ba8031dfc..57c30dde15 100644 --- a/extra/io/serial/unix/unix.factor +++ b/extra/io/serial/unix/unix.factor @@ -1,8 +1,9 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien.c-types alien.syntax combinators io.ports -io.streams.duplex system kernel math math.bitwise -vocabs.loader unix io.serial io.serial.unix.termios io.backend.unix ; +USING: accessors alien.c-types alien.syntax alien.data +combinators io.ports io.streams.duplex system kernel +math math.bitwise vocabs.loader unix io.serial +io.serial.unix.termios io.backend.unix ; IN: io.serial.unix << { diff --git a/extra/memory/piles/piles.factor b/extra/memory/piles/piles.factor index 46729c42be..a5602273d2 100644 --- a/extra/memory/piles/piles.factor +++ b/extra/memory/piles/piles.factor @@ -1,5 +1,5 @@ ! (c)2009 Joe Groff bsd license -USING: accessors alien alien.c-types destructors kernel libc math ; +USING: accessors alien alien.c-types alien.data destructors kernel libc math ; IN: memory.piles TUPLE: pile diff --git a/extra/system-info/windows/ce/ce.factor b/extra/system-info/windows/ce/ce.factor index 13c7cb9433..8c4f81a117 100755 --- a/extra/system-info/windows/ce/ce.factor +++ b/extra/system-info/windows/ce/ce.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types system-info kernel math namespaces +USING: alien.c-types alien.data system-info kernel math namespaces windows windows.kernel32 system-info.backend system ; IN: system-info.windows.ce From b48beb48f4d74f97e566363b54d67dce017968ac Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 18 Sep 2009 10:01:38 -0500 Subject: [PATCH 297/345] fix loading problems on windows --- basis/environment/winnt/winnt.factor | 2 +- basis/windows/errors/errors.factor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/basis/environment/winnt/winnt.factor b/basis/environment/winnt/winnt.factor index cba92a0e3c..894415ace8 100755 --- a/basis/environment/winnt/winnt.factor +++ b/basis/environment/winnt/winnt.factor @@ -3,7 +3,7 @@ USING: alien.strings fry io.encodings.utf16n kernel splitting windows windows.kernel32 windows.types system environment alien.data sequences windows.errors -io.streams.memory io.encodings io ; +io.streams.memory io.encodings io specialized-arrays ; SPECIALIZED-ARRAY: TCHAR IN: environment.winnt diff --git a/basis/windows/errors/errors.factor b/basis/windows/errors/errors.factor index 483494ba0c..a7a41433f7 100755 --- a/basis/windows/errors/errors.factor +++ b/basis/windows/errors/errors.factor @@ -1,7 +1,7 @@ USING: alien.data kernel locals math math.bitwise windows.kernel32 sequences byte-arrays unicode.categories io.encodings.string io.encodings.utf16n alien.strings -arrays literals windows.types ; +arrays literals windows.types specialized-arrays ; SPECIALIZED-ARRAY: TCHAR IN: windows.errors From ceff1b40bec2b63dfc93c3b25e433aeda72cd344 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 18 Sep 2009 13:41:55 -0500 Subject: [PATCH 298/345] helper words for qtkit --- extra/qtkit/qtkit.factor | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/extra/qtkit/qtkit.factor b/extra/qtkit/qtkit.factor index d0567bdd48..b573cd51ab 100644 --- a/extra/qtkit/qtkit.factor +++ b/extra/qtkit/qtkit.factor @@ -1,4 +1,5 @@ -USING: classes.struct cocoa core-foundation.strings ; +USING: classes.struct cocoa cocoa.application cocoa.classes +cocoa.enumeration cocoa.plists core-foundation.strings kernel ; IN: qtkit STRUCT: QTTime @@ -74,3 +75,19 @@ IMPORT: QTMovieView IMPORT: QTSampleBuffer IMPORT: QTTrack +: ( filename -- movie ) + QTMovie swap f -> movieWithFile:error: -> retain ; + +: movie-attributes ( movie -- attributes ) + -> movieAttributes plist> ; + +: play ( movie -- ) + -> play ; +: stop ( movie -- ) + -> stop ; + +: movie-tracks ( movie -- tracks ) + -> tracks NSFastEnumeration>vector ; + +: track-attributes ( track -- attributes ) + -> trackAttributes plist> ; From 238f600da28e01cd4f999149f646a079e3db70f8 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 18 Sep 2009 15:11:01 -0500 Subject: [PATCH 299/345] document number-base prettyprinter variable. add more docs about hex float syntax --- basis/prettyprint/config/config-docs.factor | 3 +++ basis/prettyprint/prettyprint-docs.factor | 5 +++-- core/math/parser/parser-docs.factor | 6 +++--- core/syntax/syntax-docs.factor | 11 +++++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/basis/prettyprint/config/config-docs.factor b/basis/prettyprint/config/config-docs.factor index 1dcb1b5617..ccc63c61cb 100644 --- a/basis/prettyprint/config/config-docs.factor +++ b/basis/prettyprint/config/config-docs.factor @@ -19,6 +19,9 @@ HELP: length-limit HELP: line-limit { $var-description "The maximum number of lines output by the prettyprinter before output is truncated with \"...\". The default is " { $link f } ", denoting unlimited line count." } ; +HELP: number-base +{ $var-description "The number base in which the prettyprinter will output numeric literals. A value of " { $snippet "2" } " will print integers and ratios in binary with " { $link POSTPONE: BIN: } ", and " { $snippet "8" } " will print them in octal with " { $link POSTPONE: OCT: } ". A value of " { $snippet "16" } " will print all integers, ratios, and floating-point values in hexadecimal with " { $link POSTPONE: HEX: } ". Other values of " { $snippet "number-base" } " will print numbers in decimal, which is the default." } ; + HELP: string-limit? { $var-description "Toggles whether printed strings are truncated to the margin." } ; diff --git a/basis/prettyprint/prettyprint-docs.factor b/basis/prettyprint/prettyprint-docs.factor index 7c114f2e22..1560b208ab 100644 --- a/basis/prettyprint/prettyprint-docs.factor +++ b/basis/prettyprint/prettyprint-docs.factor @@ -28,6 +28,7 @@ ARTICLE: "prettyprint-variables" "Prettyprint control variables" { $subsection nesting-limit } { $subsection length-limit } { $subsection line-limit } +{ $subsection number-base } { $subsection string-limit? } { $subsection boa-tuples? } { $subsection c-object-pointers? } @@ -202,8 +203,8 @@ HELP: .o { $description "Outputs an integer in octal." } ; HELP: .h -{ $values { "n" "an integer" } } -{ $description "Outputs an integer in hexadecimal." } ; +{ $values { "n" "an integer or floating-point value" } } +{ $description "Outputs an integer or floating-point value in hexadecimal." } ; HELP: stack. { $values { "seq" "a sequence" } } diff --git a/core/math/parser/parser-docs.factor b/core/math/parser/parser-docs.factor index ebb9c8aa5e..c3ee350099 100644 --- a/core/math/parser/parser-docs.factor +++ b/core/math/parser/parser-docs.factor @@ -61,7 +61,7 @@ HELP: bin> $nl "Outputs " { $link f } " if the string does not represent a number." } ; -{ bin> POSTPONE: BIN: bin> .b } related-words +{ >bin POSTPONE: BIN: bin> .b } related-words HELP: oct> { $values { "str" string } { "n/f" "a real number or " { $link f } } } @@ -69,7 +69,7 @@ HELP: oct> $nl "Outputs " { $link f } " if the string does not represent a number." } ; -{ oct> POSTPONE: OCT: oct> .o } related-words +{ >oct POSTPONE: OCT: oct> .o } related-words HELP: hex> { $values { "str" string } { "n/f" "a real number or " { $link f } } } @@ -77,7 +77,7 @@ HELP: hex> $nl "Outputs " { $link f } " if the string does not represent a number." } ; -{ hex> POSTPONE: HEX: hex> .h } related-words +{ >hex POSTPONE: HEX: hex> .h } related-words HELP: >base { $values { "n" real } { "radix" "an integer between 2 and 36" } { "str" string } } diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index e34fb0957f..394ae3f67c 100644 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -593,10 +593,13 @@ HELP: #! { $description "Discards all input until the end of the line." } ; HELP: HEX: -{ $syntax "HEX: integer" } -{ $values { "integer" "hexadecimal digits (0-9, a-f, A-F)" } } -{ $description "Adds an integer read from a hexadecimal literal to the parse tree." } -{ $examples { $example "USE: prettyprint" "HEX: ff ." "255" } } ; +{ $syntax "HEX: NNN" "HEX: NNN.NNNpEEE" } +{ $values { "N" "hexadecimal digit (0-9, a-f, A-F)" } { "pEEE" "decimal exponent value" } } +{ $description "Adds an integer or floating-point value read from a hexadecimal literal to the parse tree." } +{ $examples + { $example "USE: prettyprint" "HEX: ff ." "255" } + { $example "USE: prettyprint" "HEX: 1.8p5 ." "48.0" } +} ; HELP: OCT: { $syntax "OCT: integer" } From 53752b4cfd1eba6fe1722c06ab4b191c3db5089d Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 18 Sep 2009 16:14:02 -0500 Subject: [PATCH 300/345] update C-STRUCT:s in audio.wav --- extra/audio/wav/wav.factor | 64 ++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/extra/audio/wav/wav.factor b/extra/audio/wav/wav.factor index 4df8b52424..89cd04ad60 100644 --- a/extra/audio/wav/wav.factor +++ b/extra/audio/wav/wav.factor @@ -1,7 +1,7 @@ USING: alien.c-types alien.syntax audio combinators combinators.short-circuit io io.binary io.encodings.binary io.files io.streams.byte-array kernel locals math -sequences alien.data ; +sequences alien alien.data classes.struct accessors ; IN: audio.wav CONSTANT: RIFF-MAGIC "RIFF" @@ -9,30 +9,26 @@ CONSTANT: WAVE-MAGIC "WAVE" CONSTANT: FMT-MAGIC "fmt " CONSTANT: DATA-MAGIC "data" -C-STRUCT: riff-chunk-header - { "char[4]" "id" } - { "uchar[4]" "size" } - ; +STRUCT: riff-chunk-header + { id char[4] } + { size char[4] } ; -C-STRUCT: riff-chunk - { "riff-chunk-header" "header" } - { "char[4]" "format" } - ; +STRUCT: riff-chunk + { header riff-chunk-header } + { format char[4] } ; -C-STRUCT: wav-fmt-chunk - { "riff-chunk-header" "header" } - { "uchar[2]" "audio-format" } - { "uchar[2]" "num-channels" } - { "uchar[4]" "sample-rate" } - { "uchar[4]" "byte-rate" } - { "uchar[2]" "block-align" } - { "uchar[2]" "bits-per-sample" } - ; +STRUCT: wav-fmt-chunk + { header riff-chunk-header } + { audio-format uchar[2] } + { num-channels uchar[2] } + { sample-rate uchar[4] } + { byte-rate uchar[4] } + { block-align uchar[2] } + { bits-per-sample uchar[2] } ; -C-STRUCT: wav-data-chunk - { "riff-chunk-header" "header" } - { "uchar[0]" "body" } - ; +STRUCT: wav-data-chunk + { header riff-chunk-header } + { body uchar[0] } ; ERROR: invalid-wav-file ; @@ -44,39 +40,39 @@ ERROR: invalid-wav-file ; : read-chunk ( -- byte-array/f ) 4 ensured-read [ 4 ensured-read* dup le> ensured-read* 3append ] [ f ] if* ; : read-riff-chunk ( -- byte-array/f ) - "riff-chunk" heap-size ensured-read* ; + riff-chunk heap-size ensured-read* ; : id= ( chunk id -- ? ) - [ 4 head ] dip sequence= ; + [ 4 head ] dip sequence= ; inline -: check-chunk ( chunk id min-size -- ? ) - [ id= ] [ [ length ] dip >= ] bi-curry* bi and ; +: check-chunk ( chunk id class -- ? ) + heap-size [ id= ] [ [ length ] dip >= ] bi-curry* bi and ; :: read-wav-chunks ( -- fmt data ) f :> fmt! f :> data! [ { [ fmt data and not ] [ read-chunk ] } 0&& dup ] [ { - { [ dup FMT-MAGIC "wav-fmt-chunk" heap-size check-chunk ] [ fmt! ] } - { [ dup DATA-MAGIC "wav-data-chunk" heap-size check-chunk ] [ data! ] } + { [ dup FMT-MAGIC wav-fmt-chunk check-chunk ] [ wav-fmt-chunk memory>struct fmt! ] } + { [ dup DATA-MAGIC wav-data-chunk check-chunk ] [ wav-data-chunk memory>struct data! ] } } cond ] while drop fmt data 2dup and [ invalid-wav-file ] unless ; : verify-wav ( chunk -- ) { [ RIFF-MAGIC id= ] - [ riff-chunk-format 4 memory>byte-array WAVE-MAGIC id= ] + [ riff-chunk memory>struct format>> 4 memory>byte-array WAVE-MAGIC id= ] } 1&& [ invalid-wav-file ] unless ; : (read-wav) ( -- audio ) read-wav-chunks [ - [ wav-fmt-chunk-num-channels 2 memory>byte-array le> ] - [ wav-fmt-chunk-bits-per-sample 2 memory>byte-array le> ] - [ wav-fmt-chunk-sample-rate 4 memory>byte-array le> ] tri + [ num-channels>> 2 memory>byte-array le> ] + [ bits-per-sample>> 2 memory>byte-array le> ] + [ sample-rate>> 4 memory>byte-array le> ] tri ] [ - [ riff-chunk-header-size 4 memory>byte-array le> dup ] - [ wav-data-chunk-body ] bi swap memory>byte-array + [ header>> size>> 4 memory>byte-array le> dup ] + [ body>> >c-ptr ] bi swap memory>byte-array ] bi*