Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Side by Side Diff: dart/frog/leg/ssa/codegen.dart

Issue 9368033: Support is checks on primitive types. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class SsaCodeGeneratorTask extends CompilerTask { 5 class SsaCodeGeneratorTask extends CompilerTask {
6 SsaCodeGeneratorTask(Compiler compiler) : super(compiler); 6 SsaCodeGeneratorTask(Compiler compiler) : super(compiler);
7 String get name() => 'SSA code generator'; 7 String get name() => 'SSA code generator';
8 8
9 String generate(WorkItem work, HGraph graph) { 9 String generate(WorkItem work, HGraph graph) {
10 return measure(() { 10 return measure(() {
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 for (int i = 2; i < node.inputs.length; i++) { 778 for (int i = 2; i < node.inputs.length; i++) {
779 if (i != 2) buffer.add(', '); 779 if (i != 2) buffer.add(', ');
780 use(node.inputs[i]); 780 use(node.inputs[i]);
781 } 781 }
782 buffer.add(")"); 782 buffer.add(")");
783 } else { 783 } else {
784 return visitInvokeStatic(node); 784 return visitInvokeStatic(node);
785 } 785 }
786 } 786 }
787 787
788 void checkInt(HInstruction input, String cmp) {
789 use(input);
790 buffer.add(' $cmp (');
791 use(input);
792 buffer.add(' | 0)');
793 }
794
795 void checkNum(HInstruction input, String cmp) {
796 buffer.add('typeof ');
797 use(input);
798 buffer.add(" $cmp 'number'");
799 }
800
801 void checkDouble(HInstruction input, String cmp) {
802 checkNum(input, cmp);
803 }
804
805 void checkString(HInstruction input, String cmp) {
806 buffer.add('typeof ');
807 use(input);
808 buffer.add(" $cmp 'string'");
809 }
810
811 void checkBool(HInstruction input, String cmp) {
812 buffer.add('typeof ');
813 use(input);
814 buffer.add(" $cmp 'boolean'");
815 }
816
817 void checkArray(HInstruction input, String cmp) {
818 use(input);
819 buffer.add('.constructor $cmp Array');
820 }
821
788 void visitIs(HIs node) { 822 void visitIs(HIs node) {
789 // TODO(ahe): Get Object class from somewhere instead. 823 ClassElement element = node.typeExpression;
790 if (node.typeExpression.name == const SourceString('Object')) { 824 LibraryElement coreLibrary = compiler.coreLibrary;
825 HInstruction input = node.expression;
826 if (element == coreLibrary.find(const SourceString('Object'))) {
791 // TODO(ahe): This probably belongs in the constant folder. 827 // TODO(ahe): This probably belongs in the constant folder.
792 if (node.expression.generateAtUseSite()) { 828 buffer.add('true');
793 buffer.add('(('); 829 } else if (element == coreLibrary.find(const SourceString('String'))) {
794 visit(node.expression); 830 checkString(input, '===');
795 buffer.add('), true)'); 831 } else if (element == coreLibrary.find(const SourceString('double'))) {
796 } else { 832 checkDouble(input, '===');
797 buffer.add('true'); 833 } else if (element == coreLibrary.find(const SourceString('num'))) {
834 checkNum(input, '===');
835 } else if (element == coreLibrary.find(const SourceString('bool'))) {
836 checkBool(input, '===');
837 } else if (element == coreLibrary.find(const SourceString('int'))) {
838 checkInt(input, '===');
839 } else {
840 if (element == coreLibrary.find(const SourceString('List'))) {
841 checkArray(input, '===');
842 buffer.add(' || ');
798 } 843 }
799 } else { 844 buffer.add('!!(');
800 buffer.add('(!!('); 845 use(input);
801 use(node.expression); 846 buffer.add('.');
802 buffer.add(').');
803 buffer.add(compiler.namer.operatorIs(node.typeExpression)); 847 buffer.add(compiler.namer.operatorIs(node.typeExpression));
804 buffer.add(')'); 848 buffer.add(')');
805 } 849 }
806 } 850 }
807 } 851 }
808 852
809 class SsaOptimizedCodeGenerator extends SsaCodeGenerator { 853 class SsaOptimizedCodeGenerator extends SsaCodeGenerator {
810 final List<HTypeGuard> guards; 854 final List<HTypeGuard> guards;
811 int state = 0; 855 int state = 0;
812 856
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 buffer.add(' 0'); 895 buffer.add(' 0');
852 } 896 }
853 buffer.add(')'); 897 buffer.add(')');
854 } 898 }
855 899
856 void visitTypeGuard(HTypeGuard node) { 900 void visitTypeGuard(HTypeGuard node) {
857 HInstruction input = node.guarded; 901 HInstruction input = node.guarded;
858 assert(!input.generateAtUseSite() || input is HParameterValue); 902 assert(!input.generateAtUseSite() || input is HParameterValue);
859 if (node.isInteger()) { 903 if (node.isInteger()) {
860 buffer.add('if ('); 904 buffer.add('if (');
861 use(input); 905 checkInt(input, '!==');
862 buffer.add(' !== ('); 906 buffer.add(') ');
863 use(input);
864 buffer.add(' | 0)) ');
865 bailout(node, 'Not an integer'); 907 bailout(node, 'Not an integer');
866 } else if (node.isNumber()) { 908 } else if (node.isNumber()) {
867 buffer.add('if (typeof '); 909 buffer.add('if (');
868 use(input); 910 checkNum(input, '!==');
869 buffer.add(" !== 'number') "); 911 buffer.add(') ');
870 bailout(node, 'Not a number'); 912 bailout(node, 'Not a number');
871 } else if (node.isBoolean()) { 913 } else if (node.isBoolean()) {
872 buffer.add('if (typeof '); 914 buffer.add('if (');
873 use(input); 915 checkBool(input, '!==');
874 buffer.add(" !== 'boolean') "); 916 buffer.add(') ');
875 bailout(node, 'Not a boolean'); 917 bailout(node, 'Not a boolean');
876 } else if (node.isString()) { 918 } else if (node.isString()) {
877 buffer.add('if (typeof '); 919 buffer.add('if (');
878 use(input); 920 checkString(input, '!==');
879 buffer.add(" !== 'string') "); 921 buffer.add(') ');
880 bailout(node, 'Not a string'); 922 bailout(node, 'Not a string');
881 } else if (node.isArray()) { 923 } else if (node.isArray()) {
882 buffer.add('if ('); 924 buffer.add('if (');
883 use(input); 925 checkArray(input, '!==');
884 buffer.add(".constructor !== Array) "); 926 buffer.add(') ');
885 bailout(node, 'Not an array'); 927 bailout(node, 'Not an array');
886 } else if (node.isStringOrArray()) { 928 } else if (node.isStringOrArray()) {
887 buffer.add('if (typeof '); 929 buffer.add('if (');
888 use(input); 930 checkString(input, '!==');
889 buffer.add(" !== 'string' && "); 931 buffer.add(' && ');
890 use(input); 932 checkArray(input, '!==');
891 buffer.add(".constructor !== Array) "); 933 buffer.add(') ');
892 bailout(node, 'Not a string or array'); 934 bailout(node, 'Not a string or array');
893 } else { 935 } else {
894 unreachable(); 936 unreachable();
895 } 937 }
896 } 938 }
897 939
898 void beginLoop(HBasicBlock block) { 940 void beginLoop(HBasicBlock block) {
899 addIndentation(); 941 addIndentation();
900 buffer.add('while (true) {\n'); 942 buffer.add('while (true) {\n');
901 indent++; 943 indent++;
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
1146 startBailoutSwitch(); 1188 startBailoutSwitch();
1147 } 1189 }
1148 } 1190 }
1149 1191
1150 void endElse(HIf node) { 1192 void endElse(HIf node) {
1151 if (node.elseBlock.hasBailouts()) { 1193 if (node.elseBlock.hasBailouts()) {
1152 endBailoutSwitch(); 1194 endBailoutSwitch();
1153 } 1195 }
1154 } 1196 }
1155 } 1197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698