Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
WAST::ModulePrintContext Struct Reference
Collaboration diagram for WAST::ModulePrintContext:

Public Member Functions

 ModulePrintContext (const Module &inModule, std::string &inString)
 
void printModule ()
 
void printInitializerExpression (const InitializerExpression &expression)
 

Public Attributes

const Modulemodule
 
std::string & string
 
DisassemblyNames names
 

Detailed Description

Definition at line 160 of file Print.cpp.

Constructor & Destructor Documentation

◆ ModulePrintContext()

WAST::ModulePrintContext::ModulePrintContext ( const Module & inModule,
std::string & inString )
inline

Definition at line 167 of file Print.cpp.

168 : module(inModule), string(inString)
169 {
170 // Start with the names from the module's user name section, but make sure they are unique, and add the "$" sigil.
172 NameScope globalNameScope('$');
173 for(auto& name : names.types) { globalNameScope.map(name); }
174 for(auto& name : names.tables) { globalNameScope.map(name); }
175 for(auto& name : names.memories) { globalNameScope.map(name); }
176 for(auto& name : names.globals) { globalNameScope.map(name); }
177 for(auto& function : names.functions)
178 {
179 globalNameScope.map(function.name);
180
181 NameScope localNameScope('$');
182 for(auto& name : function.locals) { localNameScope.map(name); }
183 }
184 }
std::string name
IR_API void getDisassemblyNames(const Module &module, DisassemblyNames &outNames)
std::vector< std::string > memories
Definition Module.h:174
std::vector< std::string > globals
Definition Module.h:175
std::vector< std::string > tables
Definition Module.h:173
std::vector< std::string > types
Definition Module.h:171
std::vector< Function > functions
Definition Module.h:172
const Module & module
Definition Print.cpp:162
std::string & string
Definition Print.cpp:163
DisassemblyNames names
Definition Print.cpp:165
Here is the call graph for this function:

Member Function Documentation

◆ printInitializerExpression()

void WAST::ModulePrintContext::printInitializerExpression ( const InitializerExpression & expression)
inline

Definition at line 188 of file Print.cpp.

189 {
190 switch(expression.type)
191 {
192 case InitializerExpression::Type::i32_const: string += "(i32.const " + std::to_string(expression.i32) + ')'; break;
193 case InitializerExpression::Type::i64_const: string += "(i64.const " + std::to_string(expression.i64) + ')'; break;
194 case InitializerExpression::Type::f32_const: string += "(f32.const " + Floats::asString(expression.f32) + ')'; break;
195 case InitializerExpression::Type::f64_const: string += "(f64.const " + Floats::asString(expression.f64) + ')'; break;
196 case InitializerExpression::Type::get_global: string += "(get_global " + names.globals[expression.globalIndex] + ')'; break;
197 default: Errors::unreachable();
198 };
199 }
void unreachable()
Definition Errors.h:22
std::string asString(Float f)
Definition Floats.h:84
Here is the call graph for this function:
Here is the caller graph for this function:

◆ printModule()

void WAST::ModulePrintContext::printModule ( )

Definition at line 477 of file Print.cpp.

478 {
479 ScopedTagPrinter moduleTag(string,"module");
480
481 // Print the types.
482 for(Uptr typeIndex = 0;typeIndex < module.types.size();++typeIndex)
483 {
484 string += '\n';
485 ScopedTagPrinter typeTag(string,"type");
486 string += ' ';
487 string += names.types[typeIndex];
488 string += " (func ";
489 print(string,module.types[typeIndex]);
490 string += ')';
491 }
492
493 // Print the module imports.
494 for(Uptr importIndex = 0;importIndex < module.functions.imports.size();++importIndex)
495 {
496 printImport(string,module,module.functions.imports[importIndex],importIndex,names.functions[importIndex].name.c_str(),"func");
497 }
498 for(Uptr importIndex = 0;importIndex < module.tables.imports.size();++importIndex)
499 {
500 printImport(string,module,module.tables.imports[importIndex],importIndex,names.tables[importIndex].c_str(),"table");
501 }
502 for(Uptr importIndex = 0;importIndex < module.memories.imports.size();++importIndex)
503 {
504 printImport(string,module,module.memories.imports[importIndex],importIndex,names.memories[importIndex].c_str(),"memory");
505 }
506 for(Uptr importIndex = 0;importIndex < module.globals.imports.size();++importIndex)
507 {
508 printImport(string,module,module.globals.imports[importIndex],importIndex,names.globals[importIndex].c_str(),"global");
509 }
510 // Print the module exports.
511 for(auto export_ : module.exports)
512 {
513 string += '\n';
514 ScopedTagPrinter exportTag(string,"export");
515 string += " \"";
516 string += escapeString(export_.name.c_str(),export_.name.length());
517 string += "\" (";
518 switch(export_.kind)
519 {
520 case ObjectKind::function: string += "func " + names.functions[export_.index].name; break;
521 case ObjectKind::table: string += "table " + names.tables[export_.index]; break;
522 case ObjectKind::memory: string += "memory " + names.memories[export_.index]; break;
523 case ObjectKind::global: string += "global " + names.globals[export_.index]; break;
524 default: Errors::unreachable();
525 };
526 string += ')';
527 }
528
529 // Print the module memory definitions.
530 for(Uptr memoryDefIndex = 0;memoryDefIndex < module.memories.defs.size();++memoryDefIndex)
531 {
532 const MemoryDef& memoryDef = module.memories.defs[memoryDefIndex];
533 string += '\n';
534 ScopedTagPrinter memoryTag(string,"memory");
535 string += ' ';
536 string += names.memories[module.memories.imports.size() + memoryDefIndex];
537 string += ' ';
538 print(string,memoryDef.type);
539 }
540
541 // Print the module table definitions and elem segments.
542 for(Uptr tableDefIndex = 0;tableDefIndex < module.tables.defs.size();++tableDefIndex)
543 {
544 const TableDef& tableDef = module.tables.defs[tableDefIndex];
545 string += '\n';
546 ScopedTagPrinter memoryTag(string,"table");
547 string += ' ';
548 string += names.tables[module.tables.imports.size() + tableDefIndex];
549 string += ' ';
550 print(string,tableDef.type);
551 }
552
553 // Print the module global definitions.
554 for(Uptr globalDefIndex = 0;globalDefIndex < module.globals.defs.size();++globalDefIndex)
555 {
556 const GlobalDef& globalDef = module.globals.defs[globalDefIndex];
557 string += '\n';
558 ScopedTagPrinter memoryTag(string,"global");
559 string += ' ';
560 string += names.globals[module.globals.imports.size() + globalDefIndex];
561 string += ' ';
562 print(string,globalDef.type);
563 string += ' ';
565 }
566
567 // Print the data and table segment definitions.
568 for(auto tableSegment : module.tableSegments)
569 {
570 string += '\n';
571 ScopedTagPrinter dataTag(string,"elem");
572 string += ' ';
573 string += names.tables[tableSegment.tableIndex];
574 string += ' ';
575 printInitializerExpression(tableSegment.baseOffset);
576 enum { numElemsPerLine = 8 };
577 for(Uptr elementIndex = 0;elementIndex < tableSegment.indices.size();++elementIndex)
578 {
579 if(elementIndex % numElemsPerLine == 0) { string += '\n'; }
580 else { string += ' '; }
581 string += names.functions[tableSegment.indices[elementIndex]].name;
582 }
583 }
584 for(auto dataSegment : module.dataSegments)
585 {
586 string += '\n';
587 ScopedTagPrinter dataTag(string,"data");
588 string += ' ';
589 string += names.memories[dataSegment.memoryIndex];
590 string += ' ';
591 printInitializerExpression(dataSegment.baseOffset);
592 enum { numBytesPerLine = 64 };
593 for(Uptr offset = 0;offset < dataSegment.data.size();offset += numBytesPerLine)
594 {
595 string += "\n\"";
596 string += escapeString((const char*)dataSegment.data.data() + offset,std::min(dataSegment.data.size() - offset,(Uptr)numBytesPerLine));
597 string += "\"";
598 }
599 }
600
601 for(Uptr functionDefIndex = 0;functionDefIndex < module.functions.defs.size();++functionDefIndex)
602 {
603 const Uptr functionIndex = module.functions.imports.size() + functionDefIndex;
604 const FunctionDef& functionDef = module.functions.defs[functionDefIndex];
605 const FunctionType* functionType = module.types[functionDef.type.index];
606 FunctionPrintContext functionContext(*this,functionDefIndex);
607
608 string += "\n\n";
609 ScopedTagPrinter funcTag(string,"func");
610
611 string += ' ';
612 string += names.functions[functionIndex].name;
613
614 // Print the function parameters.
615 if(functionType->parameters.size())
616 {
617 for(Uptr parameterIndex = 0;parameterIndex < functionType->parameters.size();++parameterIndex)
618 {
619 string += '\n';
620 ScopedTagPrinter paramTag(string,"param");
621 string += ' ';
622 string += functionContext.localNames[parameterIndex];
623 string += ' ';
624 print(string,functionType->parameters[parameterIndex]);
625 }
626 }
627
628 // Print the function return type.
629 if(functionType->ret != ResultType::none)
630 {
631 string += '\n';
632 ScopedTagPrinter resultTag(string,"result");
633 string += ' ';
634 print(string,functionType->ret);
635 }
636
637 // Print the function's locals.
638 for(Uptr localIndex = 0;localIndex < functionDef.nonParameterLocalTypes.size();++localIndex)
639 {
640 string += '\n';
641 ScopedTagPrinter localTag(string,"local");
642 string += ' ';
643 string += functionContext.localNames[functionType->parameters.size() + localIndex];
644 string += ' ';
645 print(string,functionDef.nonParameterLocalTypes[localIndex]);
646 }
647
648 functionContext.printFunctionBody();
649 }
650
651 // Print user sections (other than the name section).
652 for(const auto& userSection : module.userSections)
653 {
654 if(userSection.name != "name")
655 {
656 string += '\n';
657 ScopedTagPrinter dataTag(string,"user_section");
658 string += " \"";
659 string += escapeString(userSection.name.c_str(),userSection.name.length());
660 string += "\" ";
661 enum { numBytesPerLine = 64 };
662 for(Uptr offset = 0;offset < userSection.data.size();offset += numBytesPerLine)
663 {
664 string += "\n\"";
665 string += escapeString((const char*)userSection.data.data() + offset,std::min(userSection.data.size() - offset,(Uptr)numBytesPerLine));
666 string += "\"";
667 }
668 }
669 }
670 }
PointerIntHelper< sizeof(size_t)>::UnsignedIntType Uptr
Definition BasicTypes.h:22
std::string escapeString(const char *string, Uptr numChars)
Definition Print.cpp:18
WAST_API std::string print(const IR::Module &module)
Definition Print.cpp:687
void printImport(std::string &string, const Module &module, const Import< Type > &import, Uptr importIndex, const char *name, const char *typeTag)
Definition Print.cpp:460
std::vector< ValueType > nonParameterLocalTypes
Definition Module.h:44
ResultType ret
Definition Types.h:195
std::vector< ValueType > parameters
Definition Types.h:196
GlobalType type
Definition Module.h:64
InitializerExpression initializer
Definition Module.h:65
MemoryType type
Definition Module.h:58
std::vector< UserSection > userSections
Definition Module.h:141
IndexSpace< MemoryDef, MemoryType > memories
Definition Module.h:135
IndexSpace< TableDef, TableType > tables
Definition Module.h:134
IndexSpace< FunctionDef, IndexedFunctionType > functions
Definition Module.h:133
std::vector< const FunctionType * > types
Definition Module.h:131
std::vector< TableSegment > tableSegments
Definition Module.h:140
IndexSpace< GlobalDef, GlobalType > globals
Definition Module.h:136
std::vector< DataSegment > dataSegments
Definition Module.h:139
std::vector< Export > exports
Definition Module.h:138
TableType type
Definition Module.h:52
void printInitializerExpression(const InitializerExpression &expression)
Definition Print.cpp:188
Here is the call graph for this function:

Member Data Documentation

◆ module

const Module& WAST::ModulePrintContext::module

Definition at line 162 of file Print.cpp.

◆ names

DisassemblyNames WAST::ModulePrintContext::names

Definition at line 165 of file Print.cpp.

◆ string

std::string& WAST::ModulePrintContext::string

Definition at line 163 of file Print.cpp.


The documentation for this struct was generated from the following file: