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

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

Issue 9360039: Create values for literal Strings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add test. 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 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 buffer.add('new $jsClassReference('); 544 buffer.add('new $jsClassReference(');
545 // We can't use 'visitArguments', since our arguments start at input[0]. 545 // We can't use 'visitArguments', since our arguments start at input[0].
546 List<HInstruction> inputs = node.inputs; 546 List<HInstruction> inputs = node.inputs;
547 for (int i = 0; i < inputs.length; i++) { 547 for (int i = 0; i < inputs.length; i++) {
548 if (i != 0) buffer.add(', '); 548 if (i != 0) buffer.add(', ');
549 use(inputs[i]); 549 use(inputs[i]);
550 } 550 }
551 buffer.add(')'); 551 buffer.add(')');
552 } 552 }
553 553
554 /**
555 * Write the contents of the quoted string to a [StringBuffer] in
556 * a form that is valid as JavaScript string literal content.
557 * The string is assumed quoted by single quote characters.
558 */
559 static void writeEscapedString(DartString string,
560 StringBuffer buffer,
561 void cancel(String reason)) {
562 Iterator<int> iterator = string.iterator();
563 while (iterator.hasNext()) {
564 int code = iterator.next();
565 if (code === $SQ) {
566 buffer.add(@"\'");
567 } else if (code === $LF) {
568 buffer.add(@'\n');
569 } else if (code === $CR) {
570 buffer.add(@'\r');
571 } else if (code === $LS) {
572 // This Unicode line terminator and $PS are invalid in JS string
573 // literals.
574 buffer.add(@'\u2028');
575 } else if (code === $PS) {
576 buffer.add(@'\u2029');
577 } else if (code === $BACKSLASH) {
578 buffer.add(@'\\');
579 } else {
580 if (code > 0xffff) {
581 cancel("Unhandled non-BMP character: U+" + code.toRadixString(16));
582 }
583 // TODO(lrn): Consider whether all codes above 0x7f really need to
584 // be escaped. We build a Dart string here, so it should be a literal
585 // stage that converts it to, e.g., UTF-8 for a JS interpreter.
586 if (code < 0x20) {
587 buffer.add(@'\x');
588 if (code < 0x10) buffer.add('0');
589 buffer.add(code.toRadixString(16));
590 } else if (code >= 0x80) {
591 if (code < 0x100) {
592 buffer.add(@'\x');
593 buffer.add(code.toRadixString(16));
594 } else {
595 buffer.add(@'\u');
596 if (code < 0x1000) {
597 buffer.add('0');
598 }
599 buffer.add(code.toRadixString(16));
600 }
601 } else {
602 buffer.add(new String.fromCharCodes(<int>[code]));
603 }
604 }
605 }
606 }
607
608
609 visitLiteral(HLiteral node) { 554 visitLiteral(HLiteral node) {
610 if (node.isLiteralNull()) { 555 if (node.isLiteralNull()) {
611 buffer.add("(void 0)"); 556 buffer.add("(void 0)");
612 } else if (node.value is num && node.value < 0) { 557 } else if (node.value is num && node.value < 0) {
613 buffer.add('(${node.value})'); 558 buffer.add('(${node.value})');
614 } else if (node.isLiteralString()) { 559 } else if (node.isLiteralString()) {
615 DartString string = node.value; 560 DartString string = node.value;
616 buffer.add("'"); 561 buffer.add("'");
617 writeEscapedString(string, buffer, 562 CompileTimeConstantHandler.writeEscapedString(string, buffer,
618 (String reason) { 563 (String reason) {
619 compiler.cancel(reason, instruction:node); 564 compiler.cancel(reason, instruction:node);
floitsch 2012/02/14 16:08:02 space after instruction:
karlklose 2012/02/15 10:37:30 Done.
620 }); 565 });
621 buffer.add("'"); 566 buffer.add("'");
622 } else { 567 } else {
623 buffer.add(node.value); 568 buffer.add(node.value);
624 } 569 }
625 } 570 }
626 571
627 visitLoopBranch(HLoopBranch node) { 572 visitLoopBranch(HLoopBranch node) {
628 HBasicBlock branchBlock = currentBlock; 573 HBasicBlock branchBlock = currentBlock;
629 handleLoopCondition(node); 574 handleLoopCondition(node);
630 List<HBasicBlock> dominated = currentBlock.dominatedBlocks; 575 List<HBasicBlock> dominated = currentBlock.dominatedBlocks;
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after
1188 startBailoutSwitch(); 1133 startBailoutSwitch();
1189 } 1134 }
1190 } 1135 }
1191 1136
1192 void endElse(HIf node) { 1137 void endElse(HIf node) {
1193 if (node.elseBlock.hasBailouts()) { 1138 if (node.elseBlock.hasBailouts()) {
1194 endBailoutSwitch(); 1139 endBailoutSwitch();
1195 } 1140 }
1196 } 1141 }
1197 } 1142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698