Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
test-primops
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Glasgow Haskell Compiler
test-primops
Commits
e699643f
Commit
e699643f
authored
3 years ago
by
Ben Gamari
Browse files
Options
Downloads
Patches
Plain Diff
CCall: Extend to test signed arguments and results
Closes
#3
.
parent
00a693b8
No related branches found
No related tags found
No related merge requests found
Pipeline
#53680
failed
3 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/CCall.hs
+36
-24
36 additions, 24 deletions
src/CCall.hs
with
36 additions
and
24 deletions
src/CCall.hs
+
36
−
24
View file @
e699643f
...
...
@@ -4,7 +4,6 @@ module CCall
,
testCCall
)
where
import
Numeric.Natural
import
System.FilePath
import
System.IO.Temp
import
Test.QuickCheck
...
...
@@ -17,7 +16,8 @@ import Number
data
CCallDesc
=
CCallDesc
{
callRet
::
SomeNumber
,
callArgs
::
[
SomeNumber
]
,
callRetSignedness
::
Signedness
,
callArgs
::
[(
Signedness
,
SomeNumber
)]
}
deriving
(
Show
)
...
...
@@ -30,9 +30,10 @@ mAX_ARGS = 32
instance
Arbitrary
CCallDesc
where
arbitrary
=
do
ret
<-
arbitrary
ret_signedness
<-
arbitrary
n
<-
chooseInt
(
0
,
mAX_ARGS
)
args
<-
vectorOf
n
arbitrary
return
$
CCallDesc
ret
args
return
$
CCallDesc
ret
ret_signedness
args
testCCall
::
Compiler
...
...
@@ -44,11 +45,11 @@ testCCall comp c =
writeFile
(
tmpDir
</>
"test.cmm"
)
(
cCallCmm
c
)
compile
comp
tmpDir
[
"test_c.c"
,
"test.cmm"
]
soName
[
"-shared"
,
"-dynamic"
]
out
<-
runIt
comp
(
tmpDir
</>
soName
)
let
saw
::
[
Natural
]
let
saw
::
[
Integer
]
saw
=
map
read
(
lines
out
)
expected
::
[
Natural
]
expected
=
map
(
\
(
SomeNumber
e
)
->
toUnsigned
e
)
(
callArgs
c
)
++
[
ret
]
ret
=
case
callRet
c
of
SomeNumber
n
->
toUnsigned
n
expected
::
[
Integer
]
expected
=
map
(
\
(
s
,
SomeNumber
e
)
->
asInteger
s
e
)
(
callArgs
c
)
++
[
ret
]
ret
=
case
callRet
c
of
SomeNumber
n
->
asInteger
(
callRetSignedness
c
)
n
return
$
saw
===
expected
where
...
...
@@ -65,11 +66,14 @@ cStub c
]
where
argBndrs
=
[
"arg"
++
show
i
|
(
i
,
_
)
<-
zip
[
0
::
Int
..
]
(
callArgs
c
)
]
argWidths
=
[
knownWidth
@
w
|
SomeNumber
(
_
::
Number
w
)
<-
callArgs
c
]
argTypes
=
[
(
signedness
,
knownWidth
@
w
)
|
(
signedness
,
SomeNumber
(
_
::
Number
w
))
<-
callArgs
c
]
funcDef
=
unlines
$
[
cType
(
retWidth
c
)
<>
" test_c("
<>
argList
<>
") {"
]
++
zipWith
printArg
arg
Width
s
argBndrs
++
[
cType
Unsigned
(
retWidth
c
)
<>
" test_c("
<>
argList
<>
") {"
]
++
zipWith
printArg
arg
Type
s
argBndrs
++
[
" fflush(stdout);"
,
" return "
++
show
(
someNumberToUnsigned
$
callRet
c
)
++
"ULL;"
,
"}"
...
...
@@ -77,39 +81,47 @@ cStub c
argList
=
commaList
[
unwords
[
cType
w
,
bndr
]
|
(
w
,
bndr
)
<-
zip
argWidths
argBndrs
[
unwords
[
ty
,
bndr
]
|
(
ty
,
bndr
)
<-
zip
(
map
(
uncurry
cType
)
argTypes
)
argBndrs
]
printArg
w
bndr
=
" printf("
++
quoted
(
formatStr
w
++
"
\\
n"
)
++
", "
++
bndr
++
");"
printArg
ty
bndr
=
" printf("
++
quoted
(
formatStr
ty
++
"
\\
n"
)
++
", "
++
bndr
++
");"
quoted
::
String
->
String
quoted
s
=
"
\"
"
++
s
++
"
\"
"
formatStr
::
Width
->
String
formatStr
w
=
formatStr
::
(
Signedness
,
Width
)
->
String
formatStr
(
_signedness
,
w
)
=
"0x%"
++
quoted
(
"PRIx"
++
show
n
)
where
n
=
widthBits
w
cType
::
Width
->
String
cType
W8
=
"uint8_t"
cType
W16
=
"uint16_t"
cType
W32
=
"uint32_t"
cType
W64
=
"uint64_t"
cType
::
Signedness
->
Width
->
String
cType
signedness
width
=
prefix
++
"int"
++
show
n
++
"_t"
where
n
=
widthBits
width
prefix
=
case
signedness
of
Signed
->
""
Unsigned
->
"u"
cCallCmm
::
CCallDesc
->
String
cCallCmm
c
=
unlines
[
"test("
++
cmmWordType
++
" buffer) {"
,
" "
++
cmmType
(
retWidth
c
)
++
" ret;"
,
" (ret) = foreign
\"
C
\"
test_c("
++
argList
++
");"
,
" (
"
++
retHint
++
"
ret) = foreign
\"
C
\"
test_c("
++
argList
++
");"
,
" return ("
++
widenOp
++
"(ret));"
,
"}"
]
where
retHint
=
case
callRetSignedness
c
of
Signed
->
"
\"
signed
\"
"
Unsigned
->
""
widenOp
=
"%zx"
++
show
(
widthBits
wordSize
)
argList
=
commaList
[
exprToCmm
$
ELit
e
|
SomeNumber
e
<-
callArgs
c
[
exprToCmm
(
ELit
e
)
++
hint
|
(
signedness
,
SomeNumber
e
)
<-
callArgs
c
,
let
hint
=
case
signedness
of
Signed
->
"
\"
signed
\"
"
Unsigned
->
""
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment