| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// CompilerCode.cpp |
| 3 |
// Compile intermediate code into native C++ code |
| 4 |
// $Date: 2003/02/20 00:24:16 $ |
| 5 |
// $Revision: 1.4 $ |
| 6 |
//---------------------------------------------------------------------------- |
| 7 |
#ifdef __BORLANDC__ |
| 8 |
#include <vcl.h> |
| 9 |
#pragma hdrstop |
| 10 |
#endif //__BORLANDC__ |
| 11 |
|
| 12 |
#include <iostream> |
| 13 |
#include <fstream> |
| 14 |
#include "QCompilerCode.h" |
| 15 |
|
| 16 |
#ifndef M_PI |
| 17 |
#define M_PI 3.1415926535897932384626433832795028841971693993751 |
| 18 |
#endif//M_PI |
| 19 |
|
| 20 |
//---------------------------------------------------------------------------- |
| 21 |
#ifdef __BORLANDC__ |
| 22 |
#pragma package(smart_init) |
| 23 |
#endif //__BORLANDC__ |
| 24 |
|
| 25 |
//---------------------------------------------------------------------------- |
| 26 |
/** |
| 27 |
* Constructor |
| 28 |
*/ |
| 29 |
QCompilerCode::QCompilerCode(const char * const filename) : QCompiler() { |
| 30 |
mQParseInfo.clear(); |
| 31 |
mTargetName = filename; |
| 32 |
} |
| 33 |
|
| 34 |
//---------------------------------------------------------------------------- |
| 35 |
/** |
| 36 |
* Constructor with input stream |
| 37 |
*/ |
| 38 |
QCompilerCode::QCompilerCode(std::istream &is, const char * const filename) |
| 39 |
: QCompiler(is) { |
| 40 |
mQParseInfo.clear(); |
| 41 |
mTargetName = filename; |
| 42 |
} |
| 43 |
|
| 44 |
//---------------------------------------------------------------------------- |
| 45 |
/** |
| 46 |
* Destructor |
| 47 |
*/ |
| 48 |
QCompilerCode::~QCompilerCode() { |
| 49 |
mQParseInfo.clear(); |
| 50 |
} |
| 51 |
|
| 52 |
//---------------------------------------------------------------------------- |
| 53 |
/** |
| 54 |
* Save the compile result to output stream |
| 55 |
*/ |
| 56 |
void |
| 57 |
QCompilerCode::SaveToStream(std::ostream &os) { |
| 58 |
int vec_size = mQParseInfo.size(); |
| 59 |
|
| 60 |
if (false == mState) { |
| 61 |
std::cerr << "QCompilerCode: compile has not been completed.\n"; |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
WriteHeader(os); |
| 66 |
for (int i = 0; i < vec_size; i++) { |
| 67 |
const std::vector<int> &indices = mQParseInfo[i].getTargetIndices(); |
| 68 |
switch (mQParseInfo[i].getOperator()) { |
| 69 |
case cn_cnot: |
| 70 |
WriteCNot(os, indices[0], indices[1]); |
| 71 |
break; |
| 72 |
case cn_crot: |
| 73 |
WriteCRot(os, indices[0], indices[1], |
| 74 |
(double)180 / (double)mQParseInfo[i].getRotation() * M_PI); |
| 75 |
break; |
| 76 |
case cn_h: |
| 77 |
WriteHadam(os, indices[0]); |
| 78 |
break; |
| 79 |
case cn_m: |
| 80 |
// do nothing |
| 81 |
break; |
| 82 |
case cn_swap: |
| 83 |
//TODO: |
| 84 |
break; |
| 85 |
case cn_x: |
| 86 |
//TODO: |
| 87 |
break; |
| 88 |
case cn_y: |
| 89 |
//TODO: |
| 90 |
break; |
| 91 |
case cn_z: |
| 92 |
//TODO: |
| 93 |
break; |
| 94 |
case cn_not: |
| 95 |
//TODO: |
| 96 |
break; |
| 97 |
case cn_ccnot: |
| 98 |
WriteCCNot(os, indices[0], indices[1], indices[2]); |
| 99 |
break; |
| 100 |
case cn_init: |
| 101 |
WriteQBits(os, indices[0]); |
| 102 |
break; |
| 103 |
default: |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
WriteFooter(os); |
| 108 |
} |
| 109 |
|
| 110 |
//---------------------------------------------------------------------------- |
| 111 |
/** |
| 112 |
* Save the compile result to a file whose name is specified with 'filename' |
| 113 |
*/ |
| 114 |
void |
| 115 |
QCompilerCode::SaveToFile(const char * const filename) { |
| 116 |
std::ofstream ofs(filename); |
| 117 |
|
| 118 |
if (!ofs) { |
| 119 |
std::cerr << "QCompilerCode: failed in file open.\n"; |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
SaveToStream(ofs); |
| 124 |
|
| 125 |
ofs.close(); |
| 126 |
} |
| 127 |
|
| 128 |
//---------------------------------------------------------------------------- |
| 129 |
/** |
| 130 |
* Implementation of a virtual function |
| 131 |
*/ |
| 132 |
bool |
| 133 |
QCompilerCode::CompileOneLine(const QParseInfo &pinfo) { |
| 134 |
if (false == pinfo.getParseResult()) return false; |
| 135 |
mQParseInfo.push_back(pinfo); |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
//---------------------------------------------------------------------------- |
| 140 |
/** |
| 141 |
* Implementation of a virtual function |
| 142 |
*/ |
| 143 |
void |
| 144 |
QCompilerCode::CatchError(const QParseInfo &pinfo, const int at) { |
| 145 |
std::cerr << "There is an error at line " << at << "\n"; |
| 146 |
switch (pinfo.getErrorNo()) { |
| 147 |
case QParseInfo::er_syntax_error: |
| 148 |
std::cerr << "Syntax Error.\n"; |
| 149 |
break; |
| 150 |
case QParseInfo::er_unknown_operation: |
| 151 |
std::cerr << "Unknown Operation.\n"; |
| 152 |
break; |
| 153 |
case QParseInfo::er_lack_of_arguments: |
| 154 |
std::cerr << "Lack of Arguments.\n"; |
| 155 |
break; |
| 156 |
case QParseInfo::er_too_many_arguments: |
| 157 |
std::cerr << "Too Many Arguments.\n"; |
| 158 |
break; |
| 159 |
case QParseInfo::er_invalid_arguments: |
| 160 |
std::cerr << "Invalid Arguments.\n"; |
| 161 |
break; |
| 162 |
default: |
| 163 |
std::cerr << "Unknown Error.\n"; |
| 164 |
break; |
| 165 |
} |
| 166 |
mQParseInfo.clear(); |
| 167 |
} |
| 168 |
|
| 169 |
//---------------------------------------------------------------------------- |
| 170 |
/** |
| 171 |
* Subcontract function of SaveToStream() |
| 172 |
*/ |
| 173 |
void |
| 174 |
QCompilerCode::WriteHeader(std::ostream &os) { |
| 175 |
os << "//----------------------------------------------------------------------------\n" |
| 176 |
<< "// QCAD compiled code\n" |
| 177 |
<< "//----------------------------------------------------------------------------\n" |
| 178 |
<< "#include <iostream>\n" |
| 179 |
<< "#include \"QC_all.h\"\n" |
| 180 |
<< "#include \"qclib.h\"\n\n" |
| 181 |
<< "int main(void)\n{\n" |
| 182 |
<< " std::cerr << \"\\nCopyright (c) 2003 QCAD project.\\n\"\n" |
| 183 |
<< " << \"This application is automatically generated by qcc.\\n\"" |
| 184 |
<< " << \"The result will be stored into \\\"" |
| 185 |
<< mTargetName << "\\\".\\n\\n\";" |
| 186 |
<< "\n"; |
| 187 |
} |
| 188 |
|
| 189 |
//---------------------------------------------------------------------------- |
| 190 |
/** |
| 191 |
* Subcontract function of SaveToStream() |
| 192 |
*/ |
| 193 |
void |
| 194 |
QCompilerCode::WriteQBits(std::ostream &os, const int &t1) { |
| 195 |
os << " QBits qbits(" << t1 << ");\n" |
| 196 |
<< " std::cerr << \"Calcuate with " << t1 << " qubits.\\n\";\n" |
| 197 |
<< " qc::allocWith(qbits);\n"; |
| 198 |
} |
| 199 |
|
| 200 |
//---------------------------------------------------------------------------- |
| 201 |
/** |
| 202 |
* Subcontract function of SaveToStream() |
| 203 |
*/ |
| 204 |
void |
| 205 |
QCompilerCode::WriteFooter(std::ostream &os) { |
| 206 |
os << " qc::getQBits()->SaveToFile(\"" << mTargetName << "\");\n" |
| 207 |
<< " qc::release();\n\n" |
| 208 |
<< " std::cerr << \"done.\\n\";\n" |
| 209 |
<< " return 0;\n}\n"; |
| 210 |
} |
| 211 |
|
| 212 |
//---------------------------------------------------------------------------- |
| 213 |
/** |
| 214 |
* Subcontract function of SaveToStream() |
| 215 |
*/ |
| 216 |
void |
| 217 |
QCompilerCode::WriteCNot(std::ostream &os, const int &t1, const int &c1) { |
| 218 |
os << " QC_cnot::calc(" << t1 << ", " << c1 << ",qbits.GetBitsR(),qbits.GetBitsI(),qbits.GetNumberOfQBits());\n" |
| 219 |
<< " std::cerr << \"Controlled-Not(" << t1 << ", " << c1 << ")\\n\";\n"; |
| 220 |
} |
| 221 |
|
| 222 |
//---------------------------------------------------------------------------- |
| 223 |
/** |
| 224 |
* Subcontract function of SaveToStream() |
| 225 |
*/ |
| 226 |
void |
| 227 |
QCompilerCode::WriteCCNot (std::ostream &os, const int &t1, |
| 228 |
const int &c1, const int &c2) { |
| 229 |
os << " QC_ccnot::calc(" << t1 << ", " << c1 << ", " << c2 << ",qbits.GetBitsR(),qbits.GetBitsI(),qbits.GetNumberOfQBits());\n" |
| 230 |
<< " std::cerr << \"Toffoli(" |
| 231 |
<< t1 << ", " << c1 << ", " << c2 << ")\\n\";\n"; |
| 232 |
} |
| 233 |
|
| 234 |
//---------------------------------------------------------------------------- |
| 235 |
/** |
| 236 |
* Subcontract function of SaveToStream() |
| 237 |
*/ |
| 238 |
void |
| 239 |
QCompilerCode::WriteCRot (std::ostream &os, const int &t1, |
| 240 |
const int &c1, const double rad) { |
| 241 |
os << " QC_crot::calc(" << t1 << ", " << c1 << ", " << rad << ",qbits.GetBitsR(),qbits.GetBitsI(),qbits.GetNumberOfQBits());\n" |
| 242 |
<< " std::cerr << \"Controlled-Rot(" |
| 243 |
<< t1 << ", " << c1 << ")\\n\";\n"; |
| 244 |
} |
| 245 |
|
| 246 |
//---------------------------------------------------------------------------- |
| 247 |
/** |
| 248 |
* Subcontract function of SaveToStream() |
| 249 |
*/ |
| 250 |
void |
| 251 |
QCompilerCode::WriteHadam (std::ostream &os, const int &t1) { |
| 252 |
os << " QC_hadamard::calc(" << t1 << ",qbits.GetBitsR(),qbits.GetBitsI(),qbits.GetNumberOfQBits());\n" |
| 253 |
<< " std::cerr << \"Hadamard(" << t1 << ")\\n\";\n"; |
| 254 |
} |
| 255 |
|