]> gitweb.factorcode.org Git - factor.git/commitdiff
some progress on alien
authorSlava Pestov <slava@factorcode.org>
Tue, 14 Dec 2004 00:14:03 +0000 (00:14 +0000)
committerSlava Pestov <slava@factorcode.org>
Tue, 14 Dec 2004 00:14:03 +0000 (00:14 +0000)
library/bootstrap/boot-stage2.factor
library/compiler/alien-types.factor
library/compiler/alien.factor
library/sdl/sdl-event.factor
library/sdl/sdl-gfx.factor
library/sdl/sdl-video.factor
library/sdl/sdl.factor

index 5dead6a080c353022d8a681c6f5b6e80e42f136d..2ba4b556960d087ed6cefbd928134ac04b8a5b17 100644 (file)
@@ -128,6 +128,7 @@ USE: stdio
     "/library/sdl/sdl-gfx.factor"\r
     "/library/sdl/sdl-keysym.factor"\r
     "/library/sdl/sdl-utils.factor"\r
+    "/library/sdl/hsv.factor"\r
 \r
     "/library/bootstrap/image.factor"\r
     "/library/bootstrap/cross-compiler.factor"\r
@@ -148,7 +149,6 @@ USE: stdio
     "/library/tools/jedit.factor"\r
 \r
     "/library/cli.factor"\r
-    "/library/sdl/hsv.factor"\r
 ] [\r
     dup print\r
     run-resource\r
index 8de5359d6b7e7e68a58dcfad4eb31845d0f9ef28..5c9c1b2326f17d7246034ae13b0e34d409aa9073 100644 (file)
@@ -39,6 +39,25 @@ USE: words
 
 ! Some code for interfacing with C structures.
 
+: BEGIN-ENUM:
+    #! C-style enumerations. Their use is not encouraged unless
+    #! it is for C library interfaces. Used like this:
+    #!
+    #! BEGIN-ENUM 0
+    #!     ENUM: x
+    #!     ENUM: y
+    #!     ENUM: z
+    #! END-ENUM
+    #!
+    #! This is the same as : x 0 ; : y 1 ; : z 2 ;.
+    scan str>number ; parsing
+
+: ENUM:
+    dup CREATE swap unit define-compound succ ; parsing
+
+: END-ENUM
+    drop ; parsing
+
 : <c-type> ( -- type )
     <namespace> [
         [ "No setter" throw ] "setter" set
index 5e5e4817643caae7b60d8d13a2ec37d0b4f6a2cb..cec3613168d7279da31e9db6346d11fb21a26646 100644 (file)
@@ -28,6 +28,8 @@
 IN: alien
 USE: compiler
 USE: errors
+USE: inference
+USE: interpreter
 USE: kernel
 USE: lists
 USE: math
@@ -35,32 +37,6 @@ USE: namespaces
 USE: parser
 USE: words
 
-: BEGIN-ENUM:
-    #! C-style enumerations. Their use is not encouraged unless
-    #! it is for C library interfaces. Used like this:
-    #!
-    #! BEGIN-ENUM 0
-    #!     ENUM: x
-    #!     ENUM: y
-    #!     ENUM: z
-    #! END-ENUM
-    #!
-    #! This is the same as : x 0 ; : y 1 ; : z 2 ;.
-    scan str>number ; parsing
-
-: ENUM:
-    dup CREATE swap unit define-compound succ ; parsing
-
-: END-ENUM
-    drop ; parsing
-
-: alien-call ( ... returns library function parameters -- ... )
-    #! Call a C library function.
-    #! 'returns' is a type spec, and 'parameters' is a list of
-    #! type specs. 'library' is an entry in the "libraries"
-    #! namespace.
-    "alien-call cannot be interpreted." throw ;
-
 : library ( name -- handle )
     "libraries" get [
         dup get dup dll? [
@@ -73,12 +49,88 @@ USE: words
 : alien-function ( function library -- )
     [ library dlsym ] [ dlsym-self ] ifte* ;
 
-! : compile-alien-call
-!     pop-literal reverse PARAMETERS >r
-!     pop-literal pop-literal alien-function CALL JUMP-FIXUP
-!     r> CLEANUP
-!     pop-literal RETURNS ;
-! 
-! global [ <namespace> "libraries" set ] bind
-! 
-! \ alien-call [ compile-alien-call ] "compiling" set-word-property
+SYMBOL: #c-invoke ( C ABI -- Unix and most Windows libs )
+SYMBOL: #cleanup ( unwind stack by parameter )
+
+SYMBOL: #c-call ( jump to raw address )
+
+SYMBOL: #unbox ( move top of datastack to C stack )
+SYMBOL: #box ( move EAX to datastack )
+
+SYMBOL: #std-invoke ( stdcall ABI -- Win32 )
+
+! These are set in the #c-invoke and #std-invoke dataflow IR
+! nodes.
+SYMBOL: alien-returns
+SYMBOL: alien-parameters
+
+: infer-alien ( op -- )
+    >r 4 ensure-d
+    dataflow-drop, pop-d car
+    dataflow-drop, pop-d car
+    dataflow-drop, pop-d car alien-function
+    dataflow-drop, pop-d car swap
+    r> dataflow, [
+        alien-returns set
+        alien-parameters set
+    ] bind ;
+
+: unbox-parameter ( function -- )
+    dlsym-self #unbox swons , ;
+
+: linearize-parameters ( params -- count )
+    #! Generate code for boxing a list of C types.
+    #! Return amount stack must be unwound by.
+    [ alien-parameters get ] bind 0 swap [
+        c-type [
+            "width" get cell align +
+            "unboxer" get
+        ] bind unbox-parameter
+    ] each ;
+
+: box-parameter ( function -- )
+    dlsym-self #box swons , ;
+
+: linearize-returns ( returns -- )
+    [ alien-returns get ] bind dup "void" = [
+        drop
+    ] [
+        c-type [ "boxer" get ] bind box-parameter
+    ] ifte ;
+
+: linearize-alien ( node -- )
+    dup linearize-parameters >r
+    dup [ node-param get ] bind #c-call swons ,
+    dup [ node-op get #c-invoke = ] bind
+    r> swap [ #cleanup swons , ] [ drop ] ifte
+    linearize-returns ;
+
+: c-invoke ( ... returns library function parameters -- ... )
+    #! Call a C library function.
+    #! 'returns' is a type spec, and 'parameters' is a list of
+    #! type specs. 'library' is an entry in the "libraries"
+    #! namespace.
+    "c-invoke cannot be interpreted." throw ;
+
+\ c-invoke [ 4 | 0 ] "infer-effect" set-word-property
+
+\ c-invoke [ #c-invoke infer-alien ] "infer" set-word-property
+
+#c-invoke [ linearize-alien ] "linearizer" set-word-property
+
+: std-invoke ( ... returns library function parameters -- ... )
+    #! Call a C library function with the stdcall ABI (Win32).
+    #! 'returns' is a type spec, and 'parameters' is a list of
+    #! type specs. 'library' is an entry in the "libraries"
+    #! namespace.
+    "std-invoke cannot be interpreted." throw ;
+
+\ std-invoke [ 4 | 0 ] "infer-effect" set-word-property
+
+\ std-invoke [ #std-invoke infer-alien ] "infer" set-word-property
+
+#std-invoke [ linearize-alien ] "linearizer" set-word-property
+
+global [
+    "libraries" get [ <namespace> "libraries" set ] unless
+] bind
index d6308db95a7146a894432f5722a831da4d88b1d2..288cea334aaa5416f346adfed3a4d55fb3c23666 100644 (file)
@@ -194,7 +194,7 @@ BEGIN-UNION: event
 END-UNION
 
 : SDL_WaitEvent ( event -- )
-    "int" "sdl" "SDL_WaitEvent" [ "event*" ] alien-call ;
+    "int" "sdl" "SDL_WaitEvent" [ "event*" ] c-invoke ;
 
 : SDL_PollEvent ( event -- ? )
-    "bool" "sdl" "SDL_PollEvent" [ "event*" ] alien-call ;
+    "bool" "sdl" "SDL_PollEvent" [ "event*" ] c-invoke ;
index 02178434b4fc00ded43e3952bea74aaa69cf41dc..b96f4097272efdf27c3dd9cc4e4591c64e5dfc19 100644 (file)
@@ -31,99 +31,99 @@ USE: alien
 : pixelColor ( surface x y color -- )
     "void" "sdl-gfx" "pixelColor"
     [ "surface*" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : hlineColor ( surface x1 x2 y color -- )
     "void" "sdl-gfx" "hlineColor"
     [ "surface*" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : vlineColor ( surface x y1 y2 color -- )
     "void" "sdl-gfx" "vlineColor"
     [ "surface*" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : rectangleColor ( surface x1 y1 x2 y2 color -- )
     "void" "sdl-gfx" "rectangleColor"
     [ "surface*" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : boxColor ( surface x1 y1 x2 y2 color -- )
     "void" "sdl-gfx" "boxColor"
     [ "surface*" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : lineColor ( surface x1 y1 x2 y2 color -- )
     "void" "sdl-gfx" "lineColor"
     [ "surface*" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : aalineColor ( surface x1 y1 x2 y2 color -- )
     "void" "sdl-gfx" "aalineColor"
     [ "surface*" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : circleColor ( surface x y r color -- )
     "void" "sdl-gfx" "circleColor"
     [ "surface*" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : aacircleColor ( surface x y r color -- )
     "void" "sdl-gfx" "aacircleColor"
     [ "surface*" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : filledCircleColor ( surface x y r color -- )
     "void" "sdl-gfx" "filledCircleColor"
     [ "surface*" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : ellipseColor ( surface x y rx ry color -- )
     "void" "sdl-gfx" "ellipseColor"
     [ "surface*" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : aaellipseColor ( surface x y rx ry color -- )
     "void" "sdl-gfx" "aaellipseColor"
     [ "surface*" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : filledEllipseColor ( surface x y rx ry color -- )
     "void" "sdl-gfx" "filledEllipseColor"
     [ "surface*" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : pieColor ( surface x y rad start end color -- )
     "void" "sdl-gfx" "pieColor"
     [ "surface*" "short" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : filledPieColor ( surface x y rad start end color -- )
     "void" "sdl-gfx" "filledPieColor"
     [ "surface*" "short" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : trigonColor ( surface x1 y1 x2 y2 x3 y3 color -- )
     "void" "sdl-gfx" "trigonColor"
     [ "surface*" "short" "short" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : aatrigonColor ( surface x1 y1 x2 y2 x3 y3 color -- )
     "void" "sdl-gfx" "aatrigonColor"
     [ "surface*" "short" "short" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : filledTrigonColor ( surface x1 y1 x2 y2 x3 y3 color -- )
     "void" "sdl-gfx" "filledTrigonColor"
     [ "surface*" "short" "short" "short" "short" "short" "short" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : characterColor ( surface x y c color -- )
     "void" "sdl-gfx" "characterColor"
     [ "surface*" "short" "short" "char" "uint" ]
-    alien-call ;
+    c-invoke ;
 
 : stringColor ( surface x y str color -- )
     "void" "sdl-gfx" "stringColor"
     [ "surface*" "short" "short" "char*" "uint" ]
-    alien-call ;
+    c-invoke ;
index 5f0cca88b1491b52ae9f5d3454429139d3663c61..f50b0602b63448a8ff54a1eb29502589e249d623 100644 (file)
@@ -119,50 +119,50 @@ END-STRUCT
 
 : SDL_VideoInit ( driver-name flags -- )
     "int" "sdl" "SDL_VideoInit"
-    [ "char*" "int" ] alien-call ;
+    [ "char*" "int" ] c-invoke ;
 
 : SDL_VideoQuit ( -- )
-    "void" "sdl" "SDL_VideoQuit" [ ] alien-call ;
+    "void" "sdl" "SDL_VideoQuit" [ ] c-invoke ;
 
 ! SDL_VideoDriverName -- needs strings as out params.
 
 : SDL_GetVideoSurface ( -- surface )
-    "surface*" "sdl" "SDL_GetVideoSurface" [ ] alien-call ;
+    "surface*" "sdl" "SDL_GetVideoSurface" [ ] c-invoke ;
 
 ! SDL_GetVideoInfo needs C struct bitfield support
 
 : SDL_VideoModeOK ( width height bpp flags -- )
     "int" "sdl" "SDL_VideoModeOK"
-    [ "int" "int" "int" "int" ] alien-call ;
+    [ "int" "int" "int" "int" ] c-invoke ;
 
 ! SDL_ListModes needs array of structs support
 
 : SDL_SetVideoMode ( width height bpp flags -- )
     "surface*" "sdl" "SDL_SetVideoMode"
-    [ "int" "int" "int" "int" ] alien-call ;
+    [ "int" "int" "int" "int" ] c-invoke ;
 
 ! UpdateRects, UpdateRect
 
 : SDL_Flip ( surface -- )
-    "bool" "sdl" "SDL_Flip" [ "surface*" ] alien-call ;
+    "bool" "sdl" "SDL_Flip" [ "surface*" ] c-invoke ;
 
 ! SDL_SetGamma: float types
 
 : SDL_FillRect ( surface rect color -- n )
     #! If rect is null, fills entire surface.
     "bool" "sdl" "SDL_FillRect"
-    [ "surface*" "rect*" "uint" ] alien-call ;
+    [ "surface*" "rect*" "uint" ] c-invoke ;
 
 : SDL_LockSurface ( surface -- )
-    "bool" "sdl" "SDL_LockSurface" [ "surface*" ] alien-call ;
+    "bool" "sdl" "SDL_LockSurface" [ "surface*" ] c-invoke ;
 
 : SDL_UnlockSurface ( surface -- )
-    "void" "sdl" "SDL_UnlockSurface" [ "surface*" ] alien-call ;
+    "void" "sdl" "SDL_UnlockSurface" [ "surface*" ] c-invoke ;
 
 : SDL_MapRGB ( surface r g b -- )
     "uint" "sdl" "SDL_MapRGB"
-    [ "surface*" "uchar" "uchar" "uchar" ] alien-call ;
+    [ "surface*" "uchar" "uchar" "uchar" ] c-invoke ;
 
 : SDL_WM_SetCaption ( title icon -- )
     "void" "sdl" "SDL_WM_SetCaption"
-    [ "char*" "char*" ] alien-call ;
+    [ "char*" "char*" ] c-invoke ;
index 04e1839677321a2e5cce1f6e4987a6e6cfff8f9f..71e5840fbd7c54fab976855799e8d31bf1982012 100644 (file)
@@ -39,10 +39,10 @@ USE: compiler
 : SDL_INIT_EVERYTHING   HEX: 0000FFFF ;
 
 : SDL_Init ( mode -- )
-    "int" "sdl" "SDL_Init" [ "int" ] alien-call ;
+    "int" "sdl" "SDL_Init" [ "int" ] c-invoke ;
 
 : SDL_GetError ( -- error )
-    "char*" "sdl" "SDL_GetError" [ ] alien-call ;
+    "char*" "sdl" "SDL_GetError" [ ] c-invoke ;
 
 : SDL_Quit ( -- )
-    "void" "sdl" "SDL_Quit" [ ] alien-call ;
+    "void" "sdl" "SDL_Quit" [ ] c-invoke ;