Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
8a70545b
Commit
8a70545b
authored
Feb 07, 2009
by
Ken Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unsafe.Sizeof and unsafe.Offsetof
R=r OCL=24639 CL=24639
parent
bcf48076
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
0 deletions
+56
-0
dcl.c
src/cmd/gc/dcl.c
+51
-0
go.h
src/cmd/gc/go.h
+1
-0
go.y
src/cmd/gc/go.y
+2
-0
unsafe.go
src/cmd/gc/unsafe.go
+2
-0
No files found.
src/cmd/gc/dcl.c
View file @
8a70545b
...
@@ -1483,3 +1483,54 @@ loop:
...
@@ -1483,3 +1483,54 @@ loop:
c
=
listnext
(
&
citer
);
c
=
listnext
(
&
citer
);
goto
loop
;
goto
loop
;
}
}
/*
* look for
* unsafe.Sizeof
* unsafe.Offsetof
* rewrite with a constant
*/
Node
*
unsafenmagic
(
Node
*
l
,
Node
*
r
)
{
Node
*
n
;
Sym
*
s
;
long
v
;
Val
val
;
if
(
l
==
N
||
r
==
N
)
goto
no
;
if
(
l
->
op
!=
ONAME
)
goto
no
;
s
=
l
->
sym
;
if
(
s
==
S
)
goto
no
;
if
(
strcmp
(
s
->
opackage
,
"unsafe"
)
!=
0
)
goto
no
;
if
(
strcmp
(
s
->
name
,
"Sizeof"
)
==
0
)
{
walktype
(
r
,
Erv
);
if
(
r
->
type
==
T
)
goto
no
;
v
=
r
->
type
->
width
;
goto
yes
;
}
if
(
strcmp
(
s
->
name
,
"Offsetof"
)
==
0
)
{
if
(
r
->
op
!=
ODOT
&&
r
->
op
!=
ODOTPTR
)
goto
no
;
walktype
(
r
,
Erv
);
v
=
n
->
xoffset
;
goto
yes
;
}
no:
return
N
;
yes:
val
.
ctype
=
CTINT
;
val
.
u
.
xval
=
mal
(
sizeof
(
*
n
->
val
.
u
.
xval
));
mpmovecfix
(
val
.
u
.
xval
,
v
);
n
=
nod
(
OLITERAL
,
N
,
N
);
n
->
val
=
val
;
return
n
;
}
src/cmd/gc/go.h
View file @
8a70545b
...
@@ -763,6 +763,7 @@ void constiter(Node*, Type*, Node*);
...
@@ -763,6 +763,7 @@ void constiter(Node*, Type*, Node*);
void
funclit0
(
Type
*
);
void
funclit0
(
Type
*
);
Node
*
funclit1
(
Type
*
,
Node
*
);
Node
*
funclit1
(
Type
*
,
Node
*
);
Node
*
unsafenmagic
(
Node
*
,
Node
*
);
/*
/*
* export.c
* export.c
...
...
src/cmd/gc/go.y
View file @
8a70545b
...
@@ -804,6 +804,8 @@ pexpr:
...
@@ -804,6 +804,8 @@ pexpr:
}
}
|
pexpr
'('
oexpr_list
')'
|
pexpr
'('
oexpr_list
')'
{
{
$$
=
unsafenmagic
($
1
,
$
3
);
if
($$
==
N
)
$$
=
nod
(
OCALL
,
$
1
,
$
3
);
$$
=
nod
(
OCALL
,
$
1
,
$
3
);
}
}
|
LLEN
'('
expr
')'
|
LLEN
'('
expr
')'
...
...
src/cmd/gc/unsafe.go
View file @
8a70545b
...
@@ -6,3 +6,5 @@
...
@@ -6,3 +6,5 @@
package
PACKAGE
package
PACKAGE
type
Pointer
*
any
;
type
Pointer
*
any
;
func
Offsetof
(
any
)
int
;
func
Sizeof
(
any
)
int
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment