Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
c5d8aae5
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c5d8aae5
编写于
2月 25, 2011
作者:
M
Maxim Shafirov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
codegen, initial
上级
80167437
变更
10
显示空白变更内容
内联
并排
Showing
10 changed file
with
507 addition
and
0 deletion
+507
-0
.idea/dictionaries/max.xml
.idea/dictionaries/max.xml
+7
-0
idea/src/org/jetbrains/jet/codegen/ClassVisitorFactory.java
idea/src/org/jetbrains/jet/codegen/ClassVisitorFactory.java
+17
-0
idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java
idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java
+256
-0
idea/src/org/jetbrains/jet/codegen/FunctionCodegen.java
idea/src/org/jetbrains/jet/codegen/FunctionCodegen.java
+25
-0
idea/src/org/jetbrains/jet/codegen/MethodCodeGen.java
idea/src/org/jetbrains/jet/codegen/MethodCodeGen.java
+8
-0
idea/src/org/jetbrains/jet/codegen/NamespaceCodegen.java
idea/src/org/jetbrains/jet/codegen/NamespaceCodegen.java
+45
-0
idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java
idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java
+25
-0
idea/src/org/jetbrains/jet/lang/psi/JetConstantExpression.java
...src/org/jetbrains/jet/lang/psi/JetConstantExpression.java
+43
-0
idea/src/org/jetbrains/jet/lang/psi/JetNamespace.java
idea/src/org/jetbrains/jet/lang/psi/JetNamespace.java
+4
-0
idea/tests/org/jetbrains/jet/codegen/ExpressionGenTest.java
idea/tests/org/jetbrains/jet/codegen/ExpressionGenTest.java
+77
-0
未找到文件。
.idea/dictionaries/max.xml
0 → 100644
浏览文件 @
c5d8aae5
<component
name=
"ProjectDictionaryState"
>
<dictionary
name=
"max"
>
<words>
<w>
codegen
</w>
</words>
</dictionary>
</component>
\ No newline at end of file
idea/src/org/jetbrains/jet/codegen/ClassVisitorFactory.java
0 → 100644
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.codegen
;
import
org.jetbrains.jet.lang.psi.JetNamespace
;
import
org.objectweb.asm.ClassVisitor
;
import
org.objectweb.asm.util.TraceClassVisitor
;
import
java.io.PrintWriter
;
import
java.io.StringWriter
;
/**
* @author max
*/
public
class
ClassVisitorFactory
{
public
ClassVisitor
visitorForClassIn
(
JetNamespace
namespace
)
{
return
new
TraceClassVisitor
(
new
PrintWriter
(
new
StringWriter
()));
}
}
idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java
0 → 100644
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.codegen
;
import
gnu.trove.TObjectIntHashMap
;
import
org.jetbrains.jet.lang.psi.*
;
import
org.objectweb.asm.Label
;
import
org.objectweb.asm.MethodVisitor
;
import
org.objectweb.asm.Type
;
import
org.objectweb.asm.commons.InstructionAdapter
;
import
java.util.List
;
import
java.util.Stack
;
/**
* @author max
*/
public
class
ExpressionCodegen
extends
JetVisitor
{
private
final
Stack
<
Label
>
myLoopStarts
=
new
Stack
<
Label
>();
private
final
Stack
<
Label
>
myLoopEnds
=
new
Stack
<
Label
>();
private
final
InstructionAdapter
v
;
private
final
TObjectIntHashMap
<
JetProperty
>
myVarIndex
=
new
TObjectIntHashMap
<
JetProperty
>();
private
int
myMaxVarIndex
=
0
;
public
ExpressionCodegen
(
MethodVisitor
v
)
{
this
.
v
=
new
InstructionAdapter
(
v
);
}
private
void
gen
(
JetElement
expr
)
{
if
(
expr
==
null
)
throw
new
CompilationException
();
expr
.
accept
(
this
);
}
@Override
public
void
visitExpression
(
JetExpression
expression
)
{
throw
new
UnsupportedOperationException
(
"Codegen for "
+
expression
+
" is not yet implemented"
);
}
@Override
public
void
visitParenthesizedExpression
(
JetParenthesizedExpression
expression
)
{
gen
(
expression
.
getExpression
());
}
@Override
public
void
visitAnnotatedExpression
(
JetAnnotatedExpression
expression
)
{
gen
(
expression
.
getBaseExpression
());
}
@Override
public
void
visitIfExpression
(
JetIfExpression
expression
)
{
gen
(
expression
.
getCondition
());
unboxBoolean
();
JetExpression
thenExpression
=
expression
.
getThen
();
JetExpression
elseExpression
=
expression
.
getElse
();
if
(
thenExpression
==
null
&&
elseExpression
==
null
)
{
throw
new
CompilationException
();
}
if
(
thenExpression
==
null
)
{
generateSingleBranchIf
(
elseExpression
,
false
);
return
;
}
if
(
elseExpression
==
null
)
{
generateSingleBranchIf
(
thenExpression
,
true
);
return
;
}
Label
elseLabel
=
new
Label
();
v
.
ifeq
(
elseLabel
);
// == 0, i.e. false
gen
(
thenExpression
);
Label
endLabel
=
new
Label
();
v
.
goTo
(
endLabel
);
v
.
mark
(
elseLabel
);
gen
(
elseExpression
);
v
.
mark
(
endLabel
);
}
@Override
public
void
visitWhileExpression
(
JetWhileExpression
expression
)
{
Label
condition
=
new
Label
();
myLoopStarts
.
push
(
condition
);
v
.
mark
(
condition
);
Label
end
=
new
Label
();
myLoopEnds
.
push
(
end
);
gen
(
expression
.
getCondition
());
unboxBoolean
();
v
.
ifeq
(
end
);
genToUnit
(
expression
.
getBody
());
v
.
goTo
(
condition
);
v
.
mark
(
end
);
myLoopEnds
.
pop
();
myLoopStarts
.
pop
();
}
@Override
public
void
visitDoWhileExpression
(
JetDoWhileExpression
expression
)
{
Label
condition
=
new
Label
();
v
.
mark
(
condition
);
myLoopStarts
.
push
(
condition
);
Label
end
=
new
Label
();
myLoopEnds
.
push
(
end
);
genToUnit
(
expression
.
getBody
());
gen
(
expression
.
getCondition
());
unboxBoolean
();
v
.
ifne
(
condition
);
v
.
mark
(
end
);
myLoopEnds
.
pop
();
myLoopStarts
.
pop
();
}
@Override
public
void
visitBreakExpression
(
JetBreakExpression
expression
)
{
String
labelName
=
expression
.
getLabelName
();
Label
label
=
labelName
==
null
?
myLoopEnds
.
peek
()
:
null
;
// TODO:
v
.
goTo
(
label
);
}
@Override
public
void
visitContinueExpression
(
JetContinueExpression
expression
)
{
String
labelName
=
expression
.
getLabelName
();
Label
label
=
labelName
==
null
?
myLoopStarts
.
peek
()
:
null
;
// TODO:
v
.
goTo
(
label
);
}
private
void
unboxBoolean
()
{
v
.
invokevirtual
(
"java/lang/Boolean"
,
"booleanValue"
,
"()Z"
);
}
private
void
generateSingleBranchIf
(
JetExpression
expression
,
boolean
inverse
)
{
Label
endLabel
=
new
Label
();
if
(
inverse
)
{
v
.
ifeq
(
endLabel
);
}
else
{
v
.
ifne
(
endLabel
);
}
genToUnit
(
expression
);
v
.
mark
(
endLabel
);
}
private
void
genToUnit
(
JetExpression
expression
)
{
gen
(
expression
);
if
(!
isUnitType
(
expression
))
{
v
.
pop
();
}
}
@Override
public
void
visitConstantExpression
(
JetConstantExpression
expression
)
{
Object
value
=
expression
.
getValue
();
v
.
aconst
(
value
);
if
(
value
instanceof
Integer
)
{
v
.
invokestatic
(
"java/lang/Integer"
,
"valueOf"
,
"(I)Ljava/lang/Integer;"
);
}
else
if
(
value
instanceof
Boolean
)
{
v
.
invokestatic
(
"java/lang/Boolean"
,
"valueOf"
,
"(Z)Ljava/lang/Boolean;"
);
}
else
if
(
value
instanceof
Character
)
{
v
.
invokestatic
(
"java/lang/Character"
,
"valueOf"
,
"(C)Ljava/lang/Character;"
);
}
else
if
(
value
instanceof
Short
)
{
v
.
invokestatic
(
"java/lang/Short"
,
"valueOf"
,
"(S)Ljava/lang/Short;"
);
}
else
if
(
value
instanceof
Long
)
{
v
.
invokestatic
(
"java/lang/Long"
,
"valueOf"
,
"(J)Ljava/lang/Long;"
);
}
else
if
(
value
instanceof
Byte
)
{
v
.
invokestatic
(
"java/lang/Byte"
,
"valueOf"
,
"(B)Ljava/lang/Byte;"
);
}
else
if
(
value
instanceof
Float
)
{
v
.
invokestatic
(
"java/lang/Float"
,
"valueOf"
,
"(F)Ljava/lang/Float;"
);
}
else
if
(
value
instanceof
Double
)
{
v
.
invokestatic
(
"java/lang/Double"
,
"valueOf"
,
"(D)Ljava/lang/Double;"
);
}
}
@Override
public
void
visitBlockExpression
(
JetBlockExpression
expression
)
{
Label
blockStart
=
new
Label
();
v
.
mark
(
blockStart
);
List
<
JetElement
>
statements
=
expression
.
getStatements
();
for
(
JetElement
statement
:
statements
)
{
if
(
statement
instanceof
JetProperty
)
{
myVarIndex
.
put
((
JetProperty
)
statement
,
myMaxVarIndex
++);
}
}
for
(
JetElement
statement
:
statements
)
{
gen
(
statement
);
}
Label
blockEnd
=
new
Label
();
v
.
mark
(
blockEnd
);
for
(
JetElement
statement
:
statements
)
{
if
(
statement
instanceof
JetProperty
)
{
JetProperty
var
=
(
JetProperty
)
statement
;
int
index
=
myVarIndex
.
remove
(
var
);
v
.
visitLocalVariable
(
var
.
getName
(),
getType
(
var
).
getDescriptor
(),
null
,
blockStart
,
blockEnd
,
index
);
myMaxVarIndex
--;
}
}
}
private
Type
getType
(
JetProperty
var
)
{
return
InstructionAdapter
.
OBJECT_TYPE
;
// TODO:
}
private
boolean
isUnitType
(
JetExpression
e
)
{
return
false
;
// TODO:!!!
}
@Override
public
void
visitProperty
(
JetProperty
property
)
{
int
index
=
myVarIndex
.
get
(
property
);
assert
index
>=
0
;
JetExpression
initializer
=
property
.
getInitializer
();
if
(
initializer
!=
null
)
{
gen
(
initializer
);
v
.
store
(
index
,
getType
(
property
));
}
}
private
static
class
CompilationException
extends
RuntimeException
{
}
}
idea/src/org/jetbrains/jet/codegen/FunctionCodegen.java
0 → 100644
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.codegen
;
import
org.jetbrains.jet.lang.psi.JetClass
;
import
org.jetbrains.jet.lang.psi.JetFunction
;
import
org.jetbrains.jet.lang.psi.JetNamespace
;
import
org.objectweb.asm.ClassVisitor
;
/**
* @author max
*/
public
class
FunctionCodegen
{
private
final
ClassVisitor
v
;
public
FunctionCodegen
(
ClassVisitor
v
)
{
this
.
v
=
v
;
}
public
void
gen
(
JetFunction
f
,
JetNamespace
owner
)
{
}
public
void
gen
(
JetFunction
f
,
JetClass
owner
)
{
}
}
idea/src/org/jetbrains/jet/codegen/MethodCodeGen.java
0 → 100644
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.codegen
;
/**
* @author max
*/
public
class
MethodCodeGen
{
}
idea/src/org/jetbrains/jet/codegen/NamespaceCodegen.java
0 → 100644
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.codegen
;
import
org.jetbrains.jet.lang.psi.JetDeclaration
;
import
org.jetbrains.jet.lang.psi.JetFunction
;
import
org.jetbrains.jet.lang.psi.JetNamespace
;
import
org.jetbrains.jet.lang.psi.JetProperty
;
import
org.objectweb.asm.ClassVisitor
;
import
org.objectweb.asm.Opcodes
;
/**
* @author max
*/
public
class
NamespaceCodegen
{
private
final
ClassVisitorFactory
factory
;
public
NamespaceCodegen
(
ClassVisitorFactory
factory
)
{
this
.
factory
=
factory
;
}
public
void
generate
(
JetNamespace
namespace
)
{
final
ClassVisitor
v
=
factory
.
visitorForClassIn
(
namespace
);
final
PropertyCodegen
propertyCodegen
=
new
PropertyCodegen
(
v
);
final
FunctionCodegen
functionCodegen
=
new
FunctionCodegen
(
v
);
v
.
visit
(
Opcodes
.
V1_6
,
Opcodes
.
ACC_PUBLIC
,
namespace
.
getFQName
().
replace
(
'.'
,
'/'
)
+
"/namespace"
,
null
,
"jet/lang/Namespace"
,
new
String
[
0
]
);
for
(
JetDeclaration
declaration
:
namespace
.
getDeclarations
())
{
if
(
declaration
instanceof
JetProperty
)
{
propertyCodegen
.
gen
((
JetProperty
)
declaration
,
namespace
);
}
else
if
(
declaration
instanceof
JetFunction
)
{
functionCodegen
.
gen
((
JetFunction
)
declaration
,
namespace
);
}
}
v
.
visitEnd
();
}
}
idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java
0 → 100644
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.codegen
;
import
org.jetbrains.jet.lang.psi.JetClass
;
import
org.jetbrains.jet.lang.psi.JetNamespace
;
import
org.jetbrains.jet.lang.psi.JetProperty
;
import
org.objectweb.asm.ClassVisitor
;
/**
* @author max
*/
public
class
PropertyCodegen
{
private
final
ClassVisitor
v
;
public
PropertyCodegen
(
ClassVisitor
v
)
{
this
.
v
=
v
;
}
public
void
gen
(
JetProperty
p
,
JetNamespace
owner
)
{
}
public
void
gen
(
JetProperty
p
,
JetClass
owner
)
{
}
}
idea/src/org/jetbrains/jet/lang/psi/JetConstantExpression.java
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.lang.psi
;
import
com.intellij.lang.ASTNode
;
import
com.intellij.psi.tree.IElementType
;
import
org.jetbrains.annotations.NotNull
;
import
org.jetbrains.jet.JetNodeTypes
;
/**
* @author max
...
...
@@ -15,4 +17,45 @@ public class JetConstantExpression extends JetExpression {
public
void
accept
(
JetVisitor
visitor
)
{
visitor
.
visitConstantExpression
(
this
);
}
public
Object
getValue
()
{
IElementType
elementType
=
getNode
().
getElementType
();
String
nodeText
=
getNode
().
getText
();
if
(
elementType
==
JetNodeTypes
.
INTEGER_CONSTANT
)
{
return
Integer
.
parseInt
(
nodeText
);
}
else
if
(
elementType
==
JetNodeTypes
.
LONG_CONSTANT
)
{
return
Long
.
parseLong
(
nodeText
);
}
else
if
(
elementType
==
JetNodeTypes
.
FLOAT_CONSTANT
)
{
assert
nodeText
.
length
()
>
0
;
char
lastChar
=
nodeText
.
charAt
(
nodeText
.
length
()
-
1
);
if
(
lastChar
==
'f'
||
lastChar
==
'F'
)
{
return
Float
.
parseFloat
(
nodeText
.
substring
(
0
,
nodeText
.
length
()
-
1
));
}
else
if
(
lastChar
==
'd'
||
lastChar
==
'D'
)
{
return
Double
.
parseDouble
(
nodeText
.
substring
(
0
,
nodeText
.
length
()
-
1
));
}
else
{
return
Double
.
parseDouble
(
nodeText
);
}
}
else
if
(
elementType
==
JetNodeTypes
.
BOOLEAN_CONSTANT
)
{
return
Boolean
.
parseBoolean
(
nodeText
);
}
else
if
(
elementType
==
JetNodeTypes
.
CHARACTER_CONSTANT
)
{
return
nodeText
.
charAt
(
1
);
}
else
if
(
elementType
==
JetNodeTypes
.
STRING_CONSTANT
)
{
int
tail
=
nodeText
.
length
();
if
(
nodeText
.
endsWith
(
"\""
))
tail
--;
return
nodeText
.
substring
(
1
,
tail
);
}
else
if
(
elementType
==
JetNodeTypes
.
NULL
)
{
return
null
;
}
else
{
throw
new
IllegalArgumentException
(
"Unsupported constant: "
+
this
);
}
}
}
idea/src/org/jetbrains/jet/lang/psi/JetNamespace.java
浏览文件 @
c5d8aae5
...
...
@@ -35,4 +35,8 @@ public class JetNamespace extends JetDeclaration {
PsiElement
nameNode
=
findChildByType
(
JetNodeTypes
.
NAMESPACE_NAME
);
return
nameNode
!=
null
?
nameNode
.
getText
()
:
""
;
}
public
String
getFQName
()
{
return
getName
();
// TODO: Must include container namespace names, module root namespace
}
}
idea/tests/org/jetbrains/jet/codegen/ExpressionGenTest.java
0 → 100644
浏览文件 @
c5d8aae5
package
org.jetbrains.jet.codegen
;
import
com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase
;
import
com.intellij.psi.util.PsiTreeUtil
;
import
org.jetbrains.jet.lang.psi.JetProperty
;
import
org.objectweb.asm.util.TraceMethodVisitor
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.io.StringWriter
;
/**
* @author max
*/
public
class
ExpressionGenTest
extends
LightDaemonAnalyzerTestCase
{
public
void
testIntegerZeroExpression
()
throws
Exception
{
checkCode
(
"0"
,
"LDC 0\n"
+
"INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;"
);
}
public
void
testIf
()
throws
Exception
{
checkCode
(
"if (false) 15 else 20"
,
"LDC false\n"
+
"INVOKESTATIC java/lang/Boolean.valueOf (Z)Ljava/lang/Boolean;\n"
+
"INVOKEVIRTUAL java/lang/Boolean.booleanValue ()Z\n"
+
"IFEQ L0\n"
+
"LDC 15\n"
+
"INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;\n"
+
"GOTO L1\n"
+
"L0\n"
+
"LDC 20\n"
+
"INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;\n"
+
"L1"
);
checkCode
(
"if (false) 15"
,
"LDC false\n"
+
"INVOKESTATIC java/lang/Boolean.valueOf (Z)Ljava/lang/Boolean;\n"
+
"INVOKEVIRTUAL java/lang/Boolean.booleanValue ()Z\n"
+
"IFEQ L0\n"
+
"LDC 15\n"
+
"INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;\n"
+
"POP\n"
+
"L0"
);
checkCode
(
"if (false) else 20"
,
"LDC false\n"
+
"INVOKESTATIC java/lang/Boolean.valueOf (Z)Ljava/lang/Boolean;\n"
+
"INVOKEVIRTUAL java/lang/Boolean.booleanValue ()Z\n"
+
"IFNE L0\n"
+
"LDC 20\n"
+
"INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;\n"
+
"POP\n"
+
"L0"
);
}
private
void
checkCode
(
String
expr
,
String
expectation
)
throws
IOException
{
configureFromFileText
(
"test.jet"
,
"val x = "
+
expr
);
JetProperty
p
=
PsiTreeUtil
.
getParentOfType
(
getFile
().
findElementAt
(
0
),
JetProperty
.
class
);
TraceMethodVisitor
trace
=
new
TraceMethodVisitor
();
p
.
getInitializer
().
accept
(
new
ExpressionCodegen
(
trace
));
StringWriter
out
=
new
StringWriter
();
trace
.
print
(
new
PrintWriter
(
out
));
assertEquals
(
trimLines
(
expectation
),
trimLines
(
out
.
getBuffer
().
toString
()));
}
private
static
String
trimLines
(
String
s
)
{
StringBuilder
b
=
new
StringBuilder
();
for
(
String
line
:
s
.
split
(
"\\n"
))
{
b
.
append
(
line
.
trim
()).
append
(
'\n'
);
}
return
b
.
toString
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录