Beispiel #1
0
static void pushLocation(bytes& o_code, uint32_t _locationValue)
{
	o_code.push_back((byte)Instruction::PUSH4);
	o_code.resize(o_code.size() + 4);
	bytesRef r(&o_code[o_code.size() - 4], 4);
	toBigEndian(_locationValue, r);
}
Beispiel #2
0
void Packet::push(bytes &arr, int &index, byte data) {
    if (arr.size() > (unsigned) index) {
        arr[index++] = data;
    } else {
        arr.push_back(data);
        index++;
    }
}
Beispiel #3
0
static void appendCode(bytes& o_code, vector<unsigned>& o_locs, bytes _code, vector<unsigned>& _locs)
{
	o_locs.reserve(o_locs.size() + _locs.size());
	for (auto i: _locs)
	{
		increaseLocation(_code, i, (unsigned)o_code.size());
		o_locs.push_back(i + (unsigned)o_code.size());
	}
	o_code.reserve(o_code.size() + _code.size());
	for (auto i: _code)
		o_code.push_back(i);
}
Beispiel #4
0
static unsigned pushLiteral(bytes& o_code, u256 _literalValue)
{
	unsigned br = max<unsigned>(1, bytesRequired(_literalValue));
	o_code.push_back((byte)Instruction::PUSH1 + br - 1);
	o_code.resize(o_code.size() + br);
	for (unsigned i = 0; i < br; ++i)
	{
		o_code[o_code.size() - 1 - i] = (byte)(_literalValue & 0xff);
		_literalValue >>= 8;
	}
	return br + 1;
}
Beispiel #5
0
static int compileLispFragment(char const*& d, char const* e, bool _quiet, bytes& o_code, vector<unsigned>& o_locs, map<string, unsigned>& _vars)
{
	std::map<std::string, Instruction> const c_arith = { { "+", Instruction::ADD }, { "-", Instruction::SUB }, { "*", Instruction::MUL }, { "/", Instruction::DIV }, { "%", Instruction::MOD } };
	std::map<std::string, pair<Instruction, bool>> const c_binary = { { "<", { Instruction::LT, false } }, { "<=", { Instruction::GT, true } }, { ">", { Instruction::GT, false } }, { ">=", { Instruction::LT, true } }, { "=", { Instruction::EQ, false } }, { "!=", { Instruction::EQ, true } } };
	std::map<std::string, Instruction> const c_unary = { { "!", Instruction::NOT } };
	std::set<char> const c_allowed = { '+', '-', '*', '/', '%', '<', '>', '=', '!' };

	bool exec = false;
	int outs = 0;
	bool seq = false;

	while (d != e)
	{
		// skip to next token
		for (; d != e && !isalnum(*d) && *d != '(' && *d != ')' && *d != '{' && *d != '}' && *d != '_' && *d != '"' && *d != '@' && *d != '[' && !c_allowed.count(*d) && *d != ';'; ++d) {}
		if (d == e)
			break;

		switch (*d)
		{
		case ';':
			for (; d != e && *d != '\n'; ++d) {}
			break;
		case '(':
			exec = true;
			++d;
			break;
		case '{':
			++d;
			while (d != e)
			{
				bytes codes;
				vector<unsigned> locs;
				outs = 0;
				int o;
				if ((o = compileLispFragment(d, e, _quiet, codes, locs, _vars)) > -1)
				{
					for (int i = 0; i < outs; ++i)
						o_code.push_back((byte)Instruction::POP);	// pop additional items off stack for the previous item (final item's returns get left on).
					outs = o;
					appendCode(o_code, o_locs, codes, locs);
				}
				else
					break;
			}
			seq = true;
			break;
		case '}':
			if (seq)
			{
				++d;
				return outs;
			}
			return -1;
		case ')':
			if (exec)
			{
				++d;
				return outs;
			}
			else
				// unexpected - return false as we don't know what to do with it.
				return -1;

		case '@':
		{
			if (exec)
				return -1;
			bool store = false;
			++d;
			if (*d == '@')
			{
				++d;
				store = true;
			}
			bytes codes;
			vector<unsigned> locs;
			if (compileLispFragment(d, e, _quiet, codes, locs, _vars) != 1)
				return -1;
			while (d != e && isspace(*d))
				++d;
			appendCode(o_code, o_locs, codes, locs);
			o_code.push_back((byte)(store ? Instruction::SLOAD : Instruction::MLOAD));
			return 1;
		}
		case '[':
		{
			if (exec)
				return -1;
			bool store = false;
			++d;
			if (*d == '[')
			{
				++d;
				store = true;
			}
			bytes codes;
			vector<unsigned> locs;
			if (compileLispFragment(d, e, _quiet, codes, locs, _vars) != 1)
				return -1;
			while (d != e && isspace(*d))
				++d;

			if (*d != ']')
				return -1;
			++d;
			if (store)
			{
				if (*d != ']')
					return -1;
				++d;
			}

			if (compileLispFragment(d, e, _quiet, o_code, o_locs, _vars) != 1)
				return -1;

			appendCode(o_code, o_locs, codes, locs);
			o_code.push_back((byte)(store ? Instruction::SSTORE: Instruction::MSTORE));
			return 0;
		}
		default:
		{
			bool haveLiteral = false;
			u256 literalValue = 0;
			string t;

			if (*d == '"')
			{
				string s = readQuoted(d, e);
				if (s.size() > 32)
				{
					if (!_quiet)
						cwarn << "String literal > 32 characters. Cropping.";
					s.resize(32);
				}
				h256 valHash;
				memcpy(valHash.data(), s.data(), s.size());
				memset(valHash.data() + s.size(), 0, 32 - s.size());
				literalValue = (u256)valHash;
				haveLiteral = true;
			}
			else
			{
				char const* s = d;
				for (; d != e && (isalnum(*d) || *d == '_' || c_allowed.count(*d)); ++d) {}
				t = string(s, d - s);
				if (isdigit(t[0]))
				{
					literalValue = readNumeric(t, _quiet);
					haveLiteral = true;
				}
			}

			if (haveLiteral)
			{
				bool bareLoad = true;
				if (exec)
				{
					bytes codes;
					vector<unsigned> locs;
					if (compileLispFragment(d, e, _quiet, codes, locs, _vars) != -1)
					{
						appendCode(o_code, o_locs, codes, locs);
						while (compileLispFragment(d, e, _quiet, codes, locs, _vars) != -1)
							if (!_quiet)
								cwarn << "Additional items in bare store. Ignoring.";
						bareLoad = false;
					}
				}
				pushLiteral(o_code, literalValue);
				if (exec)
					o_code.push_back(bareLoad ? (byte)Instruction::SLOAD : (byte)Instruction::SSTORE);
				outs = bareLoad ? 1 : 0;
			}
			else
			{
				boost::algorithm::to_upper(t);
				if (t == "IF")
				{
					// Compile all the code...
					bytes codes[4];
					vector<unsigned> locs[4];
					for (int i = 0; i < 3; ++i)
					{
						int o = compileLispFragment(d, e, _quiet, codes[i], locs[i], _vars);
						if (i == 1)
							outs = o;
						if ((i == 0 && o != 1) || o == -1 || (i == 2 && o != outs))
							return -1;
					}
					if (compileLispFragment(d, e, _quiet, codes[3], locs[3], _vars) != -1)
						return false;

					// First fragment - predicate
					appendCode(o_code, o_locs, codes[0], locs[0]);

					// Push the positive location.
					unsigned posLocation = (unsigned)o_code.size();
					o_locs.push_back(posLocation);
					pushLocation(o_code, 0);

					// Jump to negative if false.
					o_code.push_back((byte)Instruction::JUMPI);

					// Second fragment - negative.
					appendCode(o_code, o_locs, codes[2], locs[2]);

					// Jump to end after negative.
					unsigned endLocation = (unsigned)o_code.size();
					o_locs.push_back(endLocation);
					pushLocation(o_code, 0);
					o_code.push_back((byte)Instruction::JUMP);

					// Third fragment - positive.
					increaseLocation(o_code, posLocation, o_code.size());
					appendCode(o_code, o_locs, codes[1], locs[1]);

					// At end now.
					increaseLocation(o_code, endLocation, o_code.size());
				}
				else if (t == "WHEN" || t == "UNLESS")
				{
					outs = 0;
					// Compile all the code...
					bytes codes[3];
					vector<unsigned> locs[3];
					for (int i = 0; i < 2; ++i)
					{
						int o = compileLispFragment(d, e, _quiet, codes[i], locs[i], _vars);
						if (o == -1 || (i == 0 && o != 1))
							return false;
						if (i == 1)
							for (int j = 0; j < o; ++j)
								codes[i].push_back((byte)Instruction::POP);	// pop additional items off stack for the previous item (final item's returns get left on).
					}
					if (compileLispFragment(d, e, _quiet, codes[2], locs[2], _vars) != -1)
						return false;

					// First fragment - predicate
					appendCode(o_code, o_locs, codes[0], locs[0]);
					if (t == "WHEN")
						o_code.push_back((byte)Instruction::NOT);

					// Push the positive location.
					unsigned endLocation = (unsigned)o_code.size();
					o_locs.push_back(endLocation);
					pushLocation(o_code, 0);

					// Jump to end...
					o_code.push_back((byte)Instruction::JUMPI);

					// Second fragment - negative.
					appendCode(o_code, o_locs, codes[1], locs[1]);

					// At end now.
					increaseLocation(o_code, endLocation, o_code.size());
				}
				else if (t == "WHILE")
				{
					outs = 0;
					// Compile all the code...
					bytes codes[3];
					vector<unsigned> locs[3];
					for (int i = 0; i < 2; ++i)
					{
						int o = compileLispFragment(d, e, _quiet, codes[i], locs[i], _vars);
						if (o == -1 || (i == 0 && o != 1))
							return false;
						if (i == 1)
							for (int j = 0; j < o; ++j)
								codes[i].push_back((byte)Instruction::POP);	// pop additional items off stack for the previous item (final item's returns get left on).
					}
					if (compileLispFragment(d, e, _quiet, codes[2], locs[2], _vars) != -1)
						return false;

					unsigned startLocation = (unsigned)o_code.size();

					// First fragment - predicate
					appendCode(o_code, o_locs, codes[0], locs[0]);
					o_code.push_back((byte)Instruction::NOT);

					// Push the positive location.
					unsigned endInsertion = (unsigned)o_code.size();
					o_locs.push_back(endInsertion);
					pushLocation(o_code, 0);

					// Jump to positive if true.
					o_code.push_back((byte)Instruction::JUMPI);

					// Second fragment - negative.
					appendCode(o_code, o_locs, codes[1], locs[1]);

					// Jump to end after negative.
					o_locs.push_back((unsigned)o_code.size());
					pushLocation(o_code, startLocation);
					o_code.push_back((byte)Instruction::JUMP);

					// At end now.
					increaseLocation(o_code, endInsertion, o_code.size());
				}
				else if (t == "FOR")
				{
					compileLispFragment(d, e, _quiet, o_code, o_locs, _vars);
					outs = 0;
					// Compile all the code...
					bytes codes[4];
					vector<unsigned> locs[4];
					for (int i = 0; i < 3; ++i)
					{
						int o = compileLispFragment(d, e, _quiet, codes[i], locs[i], _vars);
						if (o == -1 || (i == 0 && o != 1))
							return false;
						cnote << "FOR " << i << o;
						if (i > 0)
							for (int j = 0; j < o; ++j)
								codes[i].push_back((byte)Instruction::POP);	// pop additional items off stack for the previous item (final item's returns get left on).
					}
					if (compileLispFragment(d, e, _quiet, codes[3], locs[3], _vars) != -1)
						return false;

					unsigned startLocation = (unsigned)o_code.size();

					// First fragment - predicate
					appendCode(o_code, o_locs, codes[0], locs[0]);
					o_code.push_back((byte)Instruction::NOT);

					// Push the positive location.
					unsigned endInsertion = (unsigned)o_code.size();
					o_locs.push_back(endInsertion);
					pushLocation(o_code, 0);

					// Jump to positive if true.
					o_code.push_back((byte)Instruction::JUMPI);

					// Second fragment - negative.
					appendCode(o_code, o_locs, codes[1], locs[1]);

					// Third fragment - incrementor.
					appendCode(o_code, o_locs, codes[2], locs[2]);

					// Jump to beginning afterwards.
					o_locs.push_back((unsigned)o_code.size());
					pushLocation(o_code, startLocation);
					o_code.push_back((byte)Instruction::JUMP);

					// At end now.
					increaseLocation(o_code, endInsertion, o_code.size());
				}
				else if (t == "SEQ")
				{
					while (d != e)
					{
						bytes codes;
						vector<unsigned> locs;
						outs = 0;
						int o;
						if ((o = compileLispFragment(d, e, _quiet, codes, locs, _vars)) > -1)
						{
							for (int i = 0; i < outs; ++i)
								o_code.push_back((byte)Instruction::POP);	// pop additional items off stack for the previous item (final item's returns get left on).
							outs = o;
							appendCode(o_code, o_locs, codes, locs);
						}
						else
							break;
					}
				}
				/*else if (t == "CALL")
				{
					if (exec)
					{
						vector<pair<bytes, vector<unsigned>>> codes(1);
						int totalArgs = 0;
						while (d != e)
						{
							int o = compileLispFragment(d, e, _quiet, codes.back().first, codes.back().second, _vars);
							if (o < 1)
								break;
							codes.push_back(pair<bytes, vector<unsigned>>());
							totalArgs += o;
						}
						if (totalArgs < 7)
						{
							cwarn << "Expected at least 7 arguments to CALL; got" << totalArgs << ".";
							break;
						}

						for (auto it = codes.rbegin(); it != codes.rend(); ++it)
							appendCode(o_code, o_locs, it->first, it->second);
						o_code.push_back((byte)Instruction::CALL);
						outs = 1;
					}
				}*/
				else if (t == "MULTI")
				{
					while (d != e)
					{
						bytes codes;
						vector<unsigned> locs;
						outs = 0;
						int o;
						if ((o = compileLispFragment(d, e, _quiet, codes, locs, _vars)) > -1)
						{
							outs += o;
							appendCode(o_code, o_locs, codes, locs);
						}
						else
							break;
					}
				}
				else if (t == "AND")
				{
					vector<bytes> codes;
					vector<vector<unsigned>> locs;
					while (d != e)
					{
						codes.resize(codes.size() + 1);
						locs.resize(locs.size() + 1);
						{
							int o = compileLispFragment(d, e, _quiet, codes.back(), locs.back(), _vars);
							if (o != 1)
								return false;
						}
						if (compileLispFragment(d, e, _quiet, codes.back(), locs.back(), _vars) != -1)
							break;
					}

					// last one is empty.
					if (codes.size() < 2)
						return false;

					codes.pop_back();
					locs.pop_back();

					vector<unsigned> ends;

					if (codes.size() > 1)
					{
						pushLiteral(o_code, 0);

						for (unsigned i = 1; i < codes.size(); ++i)
						{
							// Check if true - predicate
							appendCode(o_code, o_locs, codes[i - 1], locs[i - 1]);

							// Push the false location.
							ends.push_back((unsigned)o_code.size());
							o_locs.push_back(ends.back());
							pushLocation(o_code, 0);

							// Jump to end...
							o_code.push_back((byte)Instruction::NOT);
							o_code.push_back((byte)Instruction::JUMPI);
						}
						o_code.push_back((byte)Instruction::POP);
					}

					// Check if true - predicate
					appendCode(o_code, o_locs, codes.back(), locs.back());

					// At end now.
					for (auto i: ends)
						increaseLocation(o_code, i, o_code.size());
					outs = 1;
				}
				else if (t == "OR")
				{
					vector<bytes> codes;
					vector<vector<unsigned>> locs;
					while (d != e)
					{
						codes.resize(codes.size() + 1);
						locs.resize(locs.size() + 1);
						{
							int o = compileLispFragment(d, e, _quiet, codes.back(), locs.back(), _vars);
							if (o != 1)
								return false;
						}
					}

					// last one is empty.
					if (codes.size() < 2)
						return false;

					codes.pop_back();
					locs.pop_back();

					vector<unsigned> ends;

					if (codes.size() > 1)
					{
						pushLiteral(o_code, 1);

						for (unsigned i = 1; i < codes.size(); ++i)
						{
							// Check if true - predicate
							appendCode(o_code, o_locs, codes[i - 1], locs[i - 1]);

							// Push the false location.
							ends.push_back((unsigned)o_code.size());
							o_locs.push_back(ends.back());
							pushLocation(o_code, 0);

							// Jump to end...
							o_code.push_back((byte)Instruction::JUMPI);
						}
						o_code.push_back((byte)Instruction::POP);
					}

					// Check if true - predicate
					appendCode(o_code, o_locs, codes.back(), locs.back());

					// At end now.
					for (auto i: ends)
						increaseLocation(o_code, i, o_code.size());
					outs = 1;
				}
				else
				{
					auto it = c_instructions.find(t);
					if (it != c_instructions.end())
					{
						if (exec)
						{
							vector<pair<bytes, vector<unsigned>>> codes(1);
							int totalArgs = 0;
							while (d != e)
							{
								int o = compileLispFragment(d, e, _quiet, codes.back().first, codes.back().second, _vars);
								if (o < 1)
									break;
								codes.push_back(pair<bytes, vector<unsigned>>());
								totalArgs += o;
							}
							int ea = c_instructionInfo.at(it->second).args;
							if ((ea >= 0 && totalArgs != ea) || (ea < 0 && totalArgs < -ea))
							{
								cwarn << "Expected " << (ea < 0 ? "at least" : "exactly") << abs(ea) << "arguments to operation" << t << "; got" << totalArgs << ".";
								break;
							}

							for (auto it = codes.rbegin(); it != codes.rend(); ++it)
								appendCode(o_code, o_locs, it->first, it->second);
							o_code.push_back((byte)it->second);
							outs = c_instructionInfo.at(it->second).ret;
						}
						else
						{
							o_code.push_back((byte)Instruction::PUSH1);
							o_code.push_back((byte)it->second);
							outs = 1;
						}
					}
					else
					{
						auto it = c_arith.find(t);
						if (it != c_arith.end())
						{
							vector<pair<bytes, vector<unsigned>>> codes(1);
							int totalArgs = 0;
							while (d != e)
							{
								int o = compileLispFragment(d, e, _quiet, codes.back().first, codes.back().second, _vars);
								if (o < 1)
									break;
								codes.push_back(pair<bytes, vector<unsigned>>());
								totalArgs += o;
							}
							codes.pop_back();
							if (!totalArgs)
							{
								cwarn << "Expected at least one argument to operation" << t;
								break;
							}
							for (auto jt = codes.rbegin(); jt != codes.rend(); ++jt)
								appendCode(o_code, o_locs, jt->first, jt->second);
							o_code.push_back((byte)it->second);
							outs = 1;
						}
						else
						{
							auto it = c_binary.find(t);
							if (it != c_binary.end())
							{
								vector<pair<bytes, vector<unsigned>>> codes(1);
								int totalArgs = 0;
								while (d != e)
								{
									int o = compileLispFragment(d, e, _quiet, codes.back().first, codes.back().second, _vars);
									if (o < 1)
										break;
									codes.push_back(pair<bytes, vector<unsigned>>());
									totalArgs += o;
								}
								codes.pop_back();
//								int i = (int)codes.size();
								if (totalArgs != 2)
								{
									cwarn << "Expected two arguments to binary operator" << t << "; got" << totalArgs << ".";
									break;
								}
								for (auto jt = codes.rbegin(); jt != codes.rend(); ++jt)
									appendCode(o_code, o_locs, jt->first, jt->second);
								if (it->second.second)
									o_code.push_back((byte)Instruction::NOT);
								o_code.push_back((byte)it->second.first);
								outs = 1;
							}
							else
							{
								auto it = c_unary.find(t);
								if (it != c_unary.end())
								{
									vector<pair<bytes, vector<unsigned>>> codes(1);
									int totalArgs = 0;
									while (d != e)
									{
										int o = compileLispFragment(d, e, _quiet, codes.back().first, codes.back().second, _vars);
										if (o == -1)
											break;
										totalArgs += o;
										codes.push_back(pair<bytes, vector<unsigned>>());
									}
									codes.pop_back();
//									int i = (int)codes.size();
									if (totalArgs != 1)
									{
										cwarn << "Expected one argument to unary operator" << t << "; got" << totalArgs << ".";
										break;
									}
									for (auto it = codes.rbegin(); it != codes.rend(); ++it)
										appendCode(o_code, o_locs, it->first, it->second);
									o_code.push_back((byte)it->second);
									outs = 1;
								}
								else
								{
									auto it = _vars.find(t);
									if (it == _vars.end())
									{
										bool ok;
										tie(it, ok) = _vars.insert(make_pair(t, _vars.size() * 32));
									}
									pushLiteral(o_code, it->second);
									outs = 1;
									// happens when it's an actual literal, escapes with -1 :-(
								}
							}
						}
					}
				}
			}

			if (!exec)
				return outs;
		}
		}
	}
	return -1;
}