@designliquido/delegua 1.18.4 → 1.18.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/analisador-semantico/analisador-semantico.d.ts.map +1 -1
- package/analisador-semantico/analisador-semantico.js +3 -0
- package/analisador-semantico/analisador-semantico.js.map +1 -1
- package/analisador-semantico/dialetos/analisador-semantico-pitugues.d.ts.map +1 -1
- package/analisador-semantico/dialetos/analisador-semantico-pitugues.js +3 -0
- package/analisador-semantico/dialetos/analisador-semantico-pitugues.js.map +1 -1
- package/avaliador-sintatico/avaliador-sintatico.d.ts +1 -0
- package/avaliador-sintatico/avaliador-sintatico.d.ts.map +1 -1
- package/avaliador-sintatico/avaliador-sintatico.js +19 -0
- package/avaliador-sintatico/avaliador-sintatico.js.map +1 -1
- package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.d.ts.map +1 -1
- package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.js +22 -9
- package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.js.map +1 -1
- package/avaliador-sintatico/micro-avaliador-sintatico.d.ts +1 -0
- package/avaliador-sintatico/micro-avaliador-sintatico.d.ts.map +1 -1
- package/avaliador-sintatico/micro-avaliador-sintatico.js +18 -0
- package/avaliador-sintatico/micro-avaliador-sintatico.js.map +1 -1
- package/bin/package.json +1 -1
- package/declaracoes/propriedade-classe.d.ts +3 -2
- package/declaracoes/propriedade-classe.d.ts.map +1 -1
- package/declaracoes/propriedade-classe.js +2 -1
- package/declaracoes/propriedade-classe.js.map +1 -1
- package/interpretador/dialetos/pitugues/interpretador-pitugues.d.ts +5 -2
- package/interpretador/dialetos/pitugues/interpretador-pitugues.d.ts.map +1 -1
- package/interpretador/dialetos/pitugues/interpretador-pitugues.js +40 -3
- package/interpretador/dialetos/pitugues/interpretador-pitugues.js.map +1 -1
- package/interpretador/estruturas/descritor-tipo-classe.d.ts +1 -0
- package/interpretador/estruturas/descritor-tipo-classe.d.ts.map +1 -1
- package/interpretador/estruturas/descritor-tipo-classe.js +1 -0
- package/interpretador/estruturas/descritor-tipo-classe.js.map +1 -1
- package/interpretador/estruturas/objeto-delegua-classe.d.ts.map +1 -1
- package/interpretador/estruturas/objeto-delegua-classe.js +7 -1
- package/interpretador/estruturas/objeto-delegua-classe.js.map +1 -1
- package/interpretador/interpretador-base.d.ts.map +1 -1
- package/interpretador/interpretador-base.js +3 -0
- package/interpretador/interpretador-base.js.map +1 -1
- package/package.json +1 -1
- package/tradutores/tradutor-assembly-arm.js +151 -151
- package/tradutores/tradutor-assembly-risc-v.js +85 -85
- package/tradutores/tradutor-assembly-x64.js +176 -176
- package/tradutores/tradutor-webassembly.js +29 -29
- package/umd/delegua.js +516 -452
package/umd/delegua.js
CHANGED
|
@@ -1086,6 +1086,9 @@ class AnalisadorSemantico extends analisador_semantico_base_1.AnalisadorSemantic
|
|
|
1086
1086
|
verificarVariavel(variavel) {
|
|
1087
1087
|
const variavelEscopo = this.gerenciadorEscopos.buscar(variavel.simbolo.lexema);
|
|
1088
1088
|
if (!variavelEscopo) {
|
|
1089
|
+
if (this.funcoes[variavel.simbolo.lexema]) {
|
|
1090
|
+
return Promise.resolve();
|
|
1091
|
+
}
|
|
1089
1092
|
this.erro(variavel.simbolo, `Variável '${variavel.simbolo.lexema}' ainda não foi declarada até este ponto.`);
|
|
1090
1093
|
return Promise.resolve();
|
|
1091
1094
|
}
|
|
@@ -3173,10 +3176,29 @@ class AvaliadorSintatico extends avaliador_sintatico_base_1.AvaliadorSintaticoBa
|
|
|
3173
3176
|
if (this.verificarSeSimboloAtualEIgualA(delegua_2.default.NAO, delegua_2.default.NEGACAO, delegua_2.default.ADICAO, delegua_2.default.SUBTRACAO, delegua_2.default.BIT_NOT, delegua_2.default.INCREMENTAR, delegua_2.default.DECREMENTAR)) {
|
|
3174
3177
|
const operador = this.simbolos[this.atual - 1];
|
|
3175
3178
|
const direito = await this.unario();
|
|
3179
|
+
if (operador.tipo === delegua_2.default.NEGACAO ||
|
|
3180
|
+
operador.tipo === delegua_2.default.NAO) {
|
|
3181
|
+
this.verificarOperandoNegacao(operador, direito);
|
|
3182
|
+
}
|
|
3176
3183
|
return new construtos_1.Unario(this.hashArquivo, operador, direito, 'ANTES');
|
|
3177
3184
|
}
|
|
3178
3185
|
return await this.chamar();
|
|
3179
3186
|
}
|
|
3187
|
+
verificarOperandoNegacao(operador, operando) {
|
|
3188
|
+
if (operando instanceof construtos_1.Literal) {
|
|
3189
|
+
if (operando.tipo !== 'lógico') {
|
|
3190
|
+
this.erros.push(this.erro(operador, `Operador '!' só pode ser usado com valores lógicos. Tipo recebido: ${operando.tipo}.`));
|
|
3191
|
+
}
|
|
3192
|
+
return;
|
|
3193
|
+
}
|
|
3194
|
+
if (operando instanceof construtos_1.Unario) {
|
|
3195
|
+
const tipoOp = operando.operador.tipo;
|
|
3196
|
+
if (tipoOp === delegua_2.default.NEGACAO || tipoOp === delegua_2.default.NAO) {
|
|
3197
|
+
return;
|
|
3198
|
+
}
|
|
3199
|
+
this.verificarOperandoNegacao(operador, operando.operando);
|
|
3200
|
+
}
|
|
3201
|
+
}
|
|
3180
3202
|
/**
|
|
3181
3203
|
* A exponenciacão é uma exceção na ordem de avaliação (resolve primeiro à direita).
|
|
3182
3204
|
* Por isso `direito` chama `exponenciacao()`, e não `unario()`.
|
|
@@ -6879,7 +6901,7 @@ class AvaliadorSintaticoPitugues {
|
|
|
6879
6901
|
while (this.verificarSeSimboloAtualEIgualA(pitugues_2.default.SE)) {
|
|
6880
6902
|
const operador = this.simbolos[this.atual - 1];
|
|
6881
6903
|
const expressaoOuCondicao = await this.seTernario();
|
|
6882
|
-
this.consumir(pitugues_2.default.SENAO, `Esperado 'senão' ou 'senao' após caminho positivo em se ternário. Atual:
|
|
6904
|
+
this.consumir(pitugues_2.default.SENAO, `Esperado 'senão' ou 'senao' após caminho positivo em se ternário. Atual:
|
|
6883
6905
|
${this.simbolos[this.atual].lexema}.`);
|
|
6884
6906
|
const expressaoSenao = await this.seTernario();
|
|
6885
6907
|
expressaoEntao = new construtos_1.SeTernario(this.hashArquivo, expressaoOuCondicao, expressaoEntao, operador, expressaoSenao);
|
|
@@ -7468,24 +7490,37 @@ class AvaliadorSintaticoPitugues {
|
|
|
7468
7490
|
this.consumir(pitugues_2.default.DOIS_PONTOS, "Esperado ':' antes do escopo da classe.");
|
|
7469
7491
|
const possivelDocumentacao = this.declaracaoTextoDeDocumentacao();
|
|
7470
7492
|
const metodos = [];
|
|
7471
|
-
const
|
|
7472
|
-
const indentacaoLinha = this
|
|
7493
|
+
const propriedadesDeClasse = [];
|
|
7494
|
+
const indentacaoLinha = this
|
|
7495
|
+
.localizacoes[this.simboloAtual().linha]
|
|
7496
|
+
.espacosIndentacao;
|
|
7473
7497
|
while (!this.estaNoFinal() &&
|
|
7474
7498
|
this.localizacoes[this.simboloAtual().linha].espacosIndentacao === indentacaoLinha &&
|
|
7475
7499
|
this.verificarSeSimboloAtualEIgualA(pitugues_2.default.CONSTRUTOR, pitugues_2.default.FUNCAO, pitugues_2.default.FUNÇÃO, pitugues_2.default.IDENTIFICADOR)) {
|
|
7476
7500
|
const simboloAnterior = this.simbolos[this.atual - 1];
|
|
7477
7501
|
if (simboloAnterior.tipo === pitugues_2.default.IDENTIFICADOR) {
|
|
7478
|
-
this.
|
|
7479
|
-
|
|
7480
|
-
|
|
7481
|
-
|
|
7502
|
+
if (this.verificarSeSimboloAtualEIgualA(pitugues_2.default.DOIS_PONTOS)) {
|
|
7503
|
+
const simboloTipo = this.consumir(pitugues_2.default.IDENTIFICADOR, 'Esperado tipo da propriedade após os dois pontos.');
|
|
7504
|
+
const propriedade = new declaracoes_1.PropriedadeClasse(simboloAnterior, simboloTipo.lexema, [], 'publico', false, undefined);
|
|
7505
|
+
propriedadesDeClasse.push(propriedade);
|
|
7506
|
+
}
|
|
7507
|
+
else if (this.verificarSeSimboloAtualEIgualA(pitugues_2.default.IGUAL)) {
|
|
7508
|
+
const valorPropriedade = await this.expressao();
|
|
7509
|
+
const propriedade = new declaracoes_1.PropriedadeClasse(simboloAnterior, undefined, [], 'publico', true, valorPropriedade);
|
|
7510
|
+
propriedadesDeClasse.push(propriedade);
|
|
7511
|
+
}
|
|
7512
|
+
else {
|
|
7513
|
+
throw this.erro(this.simboloAtual(), "Esperado ':' (para tipo) ou '=' (para valor) após o nome da propriedade.");
|
|
7514
|
+
}
|
|
7482
7515
|
}
|
|
7483
7516
|
else {
|
|
7484
|
-
|
|
7517
|
+
const ehConstrutor = simboloAnterior.tipo === pitugues_2.default.CONSTRUTOR;
|
|
7518
|
+
const metodoResolvido = await this.funcao('método', ehConstrutor);
|
|
7519
|
+
metodos.push(metodoResolvido);
|
|
7485
7520
|
}
|
|
7486
7521
|
}
|
|
7487
7522
|
this.superclasseAtual = undefined;
|
|
7488
|
-
const definicaoClasse = new declaracoes_1.Classe(simbolo, superClasse ? [superClasse] : [], metodos,
|
|
7523
|
+
const definicaoClasse = new declaracoes_1.Classe(simbolo, superClasse ? [superClasse] : [], metodos, propriedadesDeClasse);
|
|
7489
7524
|
if (possivelDocumentacao) {
|
|
7490
7525
|
definicaoClasse.documentacao = possivelDocumentacao;
|
|
7491
7526
|
}
|
|
@@ -10907,10 +10942,28 @@ class MicroAvaliadorSintatico extends micro_avaliador_sintatico_base_1.MicroAval
|
|
|
10907
10942
|
}
|
|
10908
10943
|
return expressao;
|
|
10909
10944
|
}
|
|
10945
|
+
verificarOperandoNegacao(operador, operando) {
|
|
10946
|
+
if (operando instanceof construtos_1.Literal) {
|
|
10947
|
+
if (operando.tipo !== 'lógico') {
|
|
10948
|
+
this.erro(operador, `Operador '!' só pode ser usado com valores lógicos. Tipo recebido: ${operando.tipo}.`);
|
|
10949
|
+
}
|
|
10950
|
+
return;
|
|
10951
|
+
}
|
|
10952
|
+
if (operando instanceof construtos_1.Unario) {
|
|
10953
|
+
const tipoOp = operando.operador.tipo;
|
|
10954
|
+
if (tipoOp === delegua_1.default.NEGACAO) {
|
|
10955
|
+
return;
|
|
10956
|
+
}
|
|
10957
|
+
this.verificarOperandoNegacao(operador, operando.operando);
|
|
10958
|
+
}
|
|
10959
|
+
}
|
|
10910
10960
|
unario() {
|
|
10911
10961
|
if (this.verificarSeSimboloAtualEIgualA(delegua_1.default.NEGACAO, delegua_1.default.SUBTRACAO, delegua_1.default.BIT_NOT, delegua_1.default.INCREMENTAR, delegua_1.default.DECREMENTAR)) {
|
|
10912
10962
|
const operador = this.simbolos[this.atual - 1];
|
|
10913
10963
|
const direito = this.unario();
|
|
10964
|
+
if (operador.tipo === delegua_1.default.NEGACAO) {
|
|
10965
|
+
this.verificarOperandoNegacao(operador, direito);
|
|
10966
|
+
}
|
|
10914
10967
|
return new construtos_1.Unario(-1, operador, direito, 'ANTES');
|
|
10915
10968
|
}
|
|
10916
10969
|
return this.chamar();
|
|
@@ -15754,7 +15807,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15754
15807
|
exports.PropriedadeClasse = void 0;
|
|
15755
15808
|
const declaracao_1 = require("./declaracao");
|
|
15756
15809
|
class PropriedadeClasse extends declaracao_1.Declaracao {
|
|
15757
|
-
constructor(nome, tipo, decoradores = [], acesso = 'publico', estatico = false) {
|
|
15810
|
+
constructor(nome, tipo, decoradores = [], acesso = 'publico', estatico = false, valorInicial) {
|
|
15758
15811
|
super(Number(nome.linha), nome.hashArquivo);
|
|
15759
15812
|
this.nome = nome;
|
|
15760
15813
|
this.tipo = tipo;
|
|
@@ -15763,6 +15816,7 @@ class PropriedadeClasse extends declaracao_1.Declaracao {
|
|
|
15763
15816
|
this.estatico = estatico;
|
|
15764
15817
|
this.autoObter = false;
|
|
15765
15818
|
this.autoDefinir = false;
|
|
15819
|
+
this.valorInicial = valorInicial;
|
|
15766
15820
|
}
|
|
15767
15821
|
async aceitar(visitante) {
|
|
15768
15822
|
return Promise.reject(new Error('Não utilizado por enquanto.'));
|
|
@@ -19761,6 +19815,7 @@ class DescritorTipoClasse extends chamavel_1.Chamavel {
|
|
|
19761
19815
|
constructor(simboloOriginal, superClasses, metodos, propriedades) {
|
|
19762
19816
|
super();
|
|
19763
19817
|
this.dialetoRequerExpansaoPropriedadesEspacoMemoria = false;
|
|
19818
|
+
this.sombrearPropriedadesDeClasse = false;
|
|
19764
19819
|
this.simboloOriginal = simboloOriginal;
|
|
19765
19820
|
if (Array.isArray(superClasses)) {
|
|
19766
19821
|
this.superClasses = superClasses;
|
|
@@ -20566,7 +20621,13 @@ class ObjetoDeleguaClasse {
|
|
|
20566
20621
|
throw new excecoes_1.ErroEmTempoDeExecucao(simbolo, `Propriedade '${simbolo.lexema}' é somente-leitura.`);
|
|
20567
20622
|
}
|
|
20568
20623
|
if (Object.prototype.hasOwnProperty.call(this.classe.membrosEstaticos, simbolo.lexema)) {
|
|
20569
|
-
|
|
20624
|
+
if (this.classe.sombrearPropriedadesDeClasse) {
|
|
20625
|
+
this.verificarAcessoLeitura(simbolo.lexema, simbolo, visitante);
|
|
20626
|
+
this.propriedades[simbolo.lexema] = valor;
|
|
20627
|
+
}
|
|
20628
|
+
else {
|
|
20629
|
+
await this.classe.definirEstatico(simbolo.lexema, valor, visitante);
|
|
20630
|
+
}
|
|
20570
20631
|
return;
|
|
20571
20632
|
}
|
|
20572
20633
|
// Verificar acesso antes da atribuição.
|
|
@@ -22550,6 +22611,9 @@ class InterpretadorBase {
|
|
|
22550
22611
|
if (tipoResolvido.startsWith('função<')) {
|
|
22551
22612
|
tipoResolvido = tipoResolvido.replace('função<', '').replace('>', '');
|
|
22552
22613
|
}
|
|
22614
|
+
if (!declaracao.tipoExplicito && tipoResolvido === delegua_2.default.QUALQUER && valorFinal instanceof Array) {
|
|
22615
|
+
tipoResolvido = (0, inferenciador_1.inferirTipoVariavel)(valorFinal);
|
|
22616
|
+
}
|
|
22553
22617
|
this.pilhaEscoposExecucao.definirVariavel(declaracao.simbolo.lexema, valorFinal, tipoResolvido, declaracao.tipoExplicito && declaracao.tipoOriginal !== 'qualquer');
|
|
22554
22618
|
// TODO: É relevante registrar uma declaração de variável no
|
|
22555
22619
|
// resultado do interpretador?
|
|
@@ -42747,10 +42811,10 @@ class TradutorAssemblyARM {
|
|
|
42747
42811
|
// Para android: ainda é um binário standalone, mas com rótulo Delegua_main
|
|
42748
42812
|
// para deixar claro que não é o _start do CRT padrão.
|
|
42749
42813
|
const entryLabel = this.alvo === 'android' ? 'Delegua_main' : '_start';
|
|
42750
|
-
this.text = `
|
|
42751
|
-
.text
|
|
42752
|
-
.global ${entryLabel}
|
|
42753
|
-
|
|
42814
|
+
this.text = `
|
|
42815
|
+
.text
|
|
42816
|
+
.global ${entryLabel}
|
|
42817
|
+
|
|
42754
42818
|
${entryLabel}:`;
|
|
42755
42819
|
}
|
|
42756
42820
|
gerarDigitoAleatorio() {
|
|
@@ -42791,11 +42855,11 @@ ${entryLabel}:`;
|
|
|
42791
42855
|
}
|
|
42792
42856
|
const indice = this.dicionarioConstrutos[construto.indice.constructor.name](construto.indice);
|
|
42793
42857
|
const reg = this.obterRegistrador();
|
|
42794
|
-
this.text += `
|
|
42795
|
-
ldr ${reg}, =${nomeVar}
|
|
42796
|
-
ldr r0, =${indice}
|
|
42797
|
-
lsl r0, r0, #2 @ multiply index by 4 (word size)
|
|
42798
|
-
add ${reg}, ${reg}, r0
|
|
42858
|
+
this.text += `
|
|
42859
|
+
ldr ${reg}, =${nomeVar}
|
|
42860
|
+
ldr r0, =${indice}
|
|
42861
|
+
lsl r0, r0, #2 @ multiply index by 4 (word size)
|
|
42862
|
+
add ${reg}, ${reg}, r0
|
|
42799
42863
|
ldr r0, [${reg}]`;
|
|
42800
42864
|
this.liberarRegistrador(reg);
|
|
42801
42865
|
return 'r0';
|
|
@@ -42818,12 +42882,12 @@ ${entryLabel}:`;
|
|
|
42818
42882
|
const indice = this.dicionarioConstrutos[construto.indice.constructor.name](construto.indice);
|
|
42819
42883
|
const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
|
|
42820
42884
|
const reg = this.obterRegistrador();
|
|
42821
|
-
this.text += `
|
|
42822
|
-
ldr ${reg}, =${nomeVar}
|
|
42823
|
-
ldr r1, =${indice}
|
|
42824
|
-
lsl r1, r1, #2 @ multiply by 4
|
|
42825
|
-
add ${reg}, ${reg}, r1
|
|
42826
|
-
ldr r1, =${valor}
|
|
42885
|
+
this.text += `
|
|
42886
|
+
ldr ${reg}, =${nomeVar}
|
|
42887
|
+
ldr r1, =${indice}
|
|
42888
|
+
lsl r1, r1, #2 @ multiply by 4
|
|
42889
|
+
add ${reg}, ${reg}, r1
|
|
42890
|
+
ldr r1, =${valor}
|
|
42827
42891
|
str r1, [${reg}]`;
|
|
42828
42892
|
this.liberarRegistrador(reg);
|
|
42829
42893
|
}
|
|
@@ -42841,9 +42905,9 @@ ${entryLabel}:`;
|
|
|
42841
42905
|
this.bss += ` ${varLabel}: .space 4\n`;
|
|
42842
42906
|
this.variaveis.set(nomeVar, varLabel);
|
|
42843
42907
|
}
|
|
42844
|
-
this.text += `
|
|
42845
|
-
ldr r0, =${valor}
|
|
42846
|
-
ldr r1, =${this.variaveis.get(nomeVar)}
|
|
42908
|
+
this.text += `
|
|
42909
|
+
ldr r0, =${valor}
|
|
42910
|
+
ldr r1, =${this.variaveis.get(nomeVar)}
|
|
42847
42911
|
str r0, [r1]`;
|
|
42848
42912
|
}
|
|
42849
42913
|
traduzirConstrutoBinario(construto) {
|
|
@@ -42853,75 +42917,75 @@ ${entryLabel}:`;
|
|
|
42853
42917
|
const reg = this.obterRegistrador();
|
|
42854
42918
|
// Load left operand into r0
|
|
42855
42919
|
if (esquerda !== 'r0') {
|
|
42856
|
-
this.text += `
|
|
42920
|
+
this.text += `
|
|
42857
42921
|
ldr r0, =${esquerda}`;
|
|
42858
42922
|
}
|
|
42859
42923
|
// Load right operand into reg
|
|
42860
|
-
this.text += `
|
|
42924
|
+
this.text += `
|
|
42861
42925
|
ldr ${reg}, =${direita}`;
|
|
42862
42926
|
switch (operador) {
|
|
42863
42927
|
case '+':
|
|
42864
|
-
this.text += `
|
|
42928
|
+
this.text += `
|
|
42865
42929
|
add r0, r0, ${reg}`;
|
|
42866
42930
|
break;
|
|
42867
42931
|
case '-':
|
|
42868
|
-
this.text += `
|
|
42932
|
+
this.text += `
|
|
42869
42933
|
sub r0, r0, ${reg}`;
|
|
42870
42934
|
break;
|
|
42871
42935
|
case '*':
|
|
42872
|
-
this.text += `
|
|
42936
|
+
this.text += `
|
|
42873
42937
|
mul r0, r0, ${reg}`;
|
|
42874
42938
|
break;
|
|
42875
42939
|
case '/':
|
|
42876
|
-
this.text += `
|
|
42940
|
+
this.text += `
|
|
42877
42941
|
sdiv r0, r0, ${reg}`;
|
|
42878
42942
|
break;
|
|
42879
42943
|
case '%':
|
|
42880
|
-
this.text += `
|
|
42881
|
-
sdiv r1, r0, ${reg}
|
|
42882
|
-
mul r1, r1, ${reg}
|
|
42944
|
+
this.text += `
|
|
42945
|
+
sdiv r1, r0, ${reg}
|
|
42946
|
+
mul r1, r1, ${reg}
|
|
42883
42947
|
sub r0, r0, r1 @ r0 = r0 - (r0/reg)*reg`;
|
|
42884
42948
|
break;
|
|
42885
42949
|
case '<':
|
|
42886
|
-
this.text += `
|
|
42887
|
-
cmp r0, ${reg}
|
|
42888
|
-
movlt r0, #1
|
|
42950
|
+
this.text += `
|
|
42951
|
+
cmp r0, ${reg}
|
|
42952
|
+
movlt r0, #1
|
|
42889
42953
|
movge r0, #0`;
|
|
42890
42954
|
break;
|
|
42891
42955
|
case '>':
|
|
42892
|
-
this.text += `
|
|
42893
|
-
cmp r0, ${reg}
|
|
42894
|
-
movgt r0, #1
|
|
42956
|
+
this.text += `
|
|
42957
|
+
cmp r0, ${reg}
|
|
42958
|
+
movgt r0, #1
|
|
42895
42959
|
movle r0, #0`;
|
|
42896
42960
|
break;
|
|
42897
42961
|
case '<=':
|
|
42898
|
-
this.text += `
|
|
42899
|
-
cmp r0, ${reg}
|
|
42900
|
-
movle r0, #1
|
|
42962
|
+
this.text += `
|
|
42963
|
+
cmp r0, ${reg}
|
|
42964
|
+
movle r0, #1
|
|
42901
42965
|
movgt r0, #0`;
|
|
42902
42966
|
break;
|
|
42903
42967
|
case '>=':
|
|
42904
|
-
this.text += `
|
|
42905
|
-
cmp r0, ${reg}
|
|
42906
|
-
movge r0, #1
|
|
42968
|
+
this.text += `
|
|
42969
|
+
cmp r0, ${reg}
|
|
42970
|
+
movge r0, #1
|
|
42907
42971
|
movlt r0, #0`;
|
|
42908
42972
|
break;
|
|
42909
42973
|
case '==':
|
|
42910
42974
|
case '===':
|
|
42911
|
-
this.text += `
|
|
42912
|
-
cmp r0, ${reg}
|
|
42913
|
-
moveq r0, #1
|
|
42975
|
+
this.text += `
|
|
42976
|
+
cmp r0, ${reg}
|
|
42977
|
+
moveq r0, #1
|
|
42914
42978
|
movne r0, #0`;
|
|
42915
42979
|
break;
|
|
42916
42980
|
case '!=':
|
|
42917
42981
|
case '!==':
|
|
42918
|
-
this.text += `
|
|
42919
|
-
cmp r0, ${reg}
|
|
42920
|
-
movne r0, #1
|
|
42982
|
+
this.text += `
|
|
42983
|
+
cmp r0, ${reg}
|
|
42984
|
+
movne r0, #1
|
|
42921
42985
|
moveq r0, #0`;
|
|
42922
42986
|
break;
|
|
42923
42987
|
default:
|
|
42924
|
-
this.text += `
|
|
42988
|
+
this.text += `
|
|
42925
42989
|
@ Operador ${operador} não implementado`;
|
|
42926
42990
|
}
|
|
42927
42991
|
this.liberarRegistrador(reg);
|
|
@@ -42938,35 +43002,35 @@ ${entryLabel}:`;
|
|
|
42938
43002
|
if (index < registrosArgs.length) {
|
|
42939
43003
|
const valorArg = this.dicionarioConstrutos[arg.constructor.name](arg);
|
|
42940
43004
|
if (valorArg !== registrosArgs[index]) {
|
|
42941
|
-
this.text += `
|
|
43005
|
+
this.text += `
|
|
42942
43006
|
ldr ${registrosArgs[index]}, =${valorArg}`;
|
|
42943
43007
|
}
|
|
42944
43008
|
}
|
|
42945
43009
|
else {
|
|
42946
43010
|
// Push extra args on stack
|
|
42947
43011
|
const valorArg = this.dicionarioConstrutos[arg.constructor.name](arg);
|
|
42948
|
-
this.text += `
|
|
42949
|
-
ldr r0, =${valorArg}
|
|
43012
|
+
this.text += `
|
|
43013
|
+
ldr r0, =${valorArg}
|
|
42950
43014
|
push {r0}`;
|
|
42951
43015
|
}
|
|
42952
43016
|
});
|
|
42953
|
-
this.text += `
|
|
43017
|
+
this.text += `
|
|
42954
43018
|
bl ${nomeFuncao}`;
|
|
42955
43019
|
}
|
|
42956
43020
|
traduzirConstrutoDefinirValor(construto) {
|
|
42957
43021
|
const objeto = this.dicionarioConstrutos[construto.objeto.constructor.name](construto.objeto);
|
|
42958
43022
|
const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
|
|
42959
|
-
this.text += `
|
|
42960
|
-
ldr r0, =${valor}
|
|
42961
|
-
ldr r1, =${objeto}
|
|
43023
|
+
this.text += `
|
|
43024
|
+
ldr r0, =${valor}
|
|
43025
|
+
ldr r1, =${objeto}
|
|
42962
43026
|
str r0, [r1]`;
|
|
42963
43027
|
}
|
|
42964
43028
|
traduzirFuncaoConstruto(construto) {
|
|
42965
43029
|
const labelFuncao = `func_${this.gerarDigitoAleatorio()}`;
|
|
42966
|
-
this.text += `
|
|
42967
|
-
|
|
42968
|
-
${labelFuncao}:
|
|
42969
|
-
push {fp, lr}
|
|
43030
|
+
this.text += `
|
|
43031
|
+
|
|
43032
|
+
${labelFuncao}:
|
|
43033
|
+
push {fp, lr}
|
|
42970
43034
|
mov fp, sp`;
|
|
42971
43035
|
// Traduzir corpo da função
|
|
42972
43036
|
if (construto.corpo && Array.isArray(construto.corpo)) {
|
|
@@ -42976,8 +43040,8 @@ ${labelFuncao}:
|
|
|
42976
43040
|
}
|
|
42977
43041
|
});
|
|
42978
43042
|
}
|
|
42979
|
-
this.text += `
|
|
42980
|
-
mov sp, fp
|
|
43043
|
+
this.text += `
|
|
43044
|
+
mov sp, fp
|
|
42981
43045
|
pop {fp, pc}`;
|
|
42982
43046
|
}
|
|
42983
43047
|
traduzirConstrutoLiteral(construto) {
|
|
@@ -42992,29 +43056,29 @@ ${labelFuncao}:
|
|
|
42992
43056
|
const operador = construto.operador.lexema;
|
|
42993
43057
|
const labelVerdadeiro = this.gerarLabel();
|
|
42994
43058
|
const labelFim = this.gerarLabel();
|
|
42995
|
-
this.text += `
|
|
42996
|
-
ldr r0, =${esquerda}
|
|
43059
|
+
this.text += `
|
|
43060
|
+
ldr r0, =${esquerda}
|
|
42997
43061
|
cmp r0, #0`;
|
|
42998
43062
|
if (operador === 'e' || operador === '&&') {
|
|
42999
|
-
this.text += `
|
|
43000
|
-
beq ${labelFim}
|
|
43001
|
-
ldr r0, =${direita}
|
|
43002
|
-
cmp r0, #0
|
|
43003
|
-
beq ${labelFim}
|
|
43004
|
-
${labelVerdadeiro}:
|
|
43005
|
-
mov r0, #1
|
|
43063
|
+
this.text += `
|
|
43064
|
+
beq ${labelFim}
|
|
43065
|
+
ldr r0, =${direita}
|
|
43066
|
+
cmp r0, #0
|
|
43067
|
+
beq ${labelFim}
|
|
43068
|
+
${labelVerdadeiro}:
|
|
43069
|
+
mov r0, #1
|
|
43006
43070
|
${labelFim}:`;
|
|
43007
43071
|
}
|
|
43008
43072
|
else if (operador === 'ou' || operador === '||') {
|
|
43009
|
-
this.text += `
|
|
43010
|
-
bne ${labelVerdadeiro}
|
|
43011
|
-
ldr r0, =${direita}
|
|
43012
|
-
cmp r0, #0
|
|
43013
|
-
bne ${labelVerdadeiro}
|
|
43014
|
-
mov r0, #0
|
|
43015
|
-
b ${labelFim}
|
|
43016
|
-
${labelVerdadeiro}:
|
|
43017
|
-
mov r0, #1
|
|
43073
|
+
this.text += `
|
|
43074
|
+
bne ${labelVerdadeiro}
|
|
43075
|
+
ldr r0, =${direita}
|
|
43076
|
+
cmp r0, #0
|
|
43077
|
+
bne ${labelVerdadeiro}
|
|
43078
|
+
mov r0, #0
|
|
43079
|
+
b ${labelFim}
|
|
43080
|
+
${labelVerdadeiro}:
|
|
43081
|
+
mov r0, #1
|
|
43018
43082
|
${labelFim}:`;
|
|
43019
43083
|
}
|
|
43020
43084
|
return 'r0';
|
|
@@ -43026,16 +43090,16 @@ ${labelFim}:`;
|
|
|
43026
43090
|
traduzirConstrutoUnario(construto) {
|
|
43027
43091
|
const operando = this.dicionarioConstrutos[construto.operando.constructor.name](construto.operando);
|
|
43028
43092
|
const operador = construto.operador.lexema;
|
|
43029
|
-
this.text += `
|
|
43093
|
+
this.text += `
|
|
43030
43094
|
ldr r0, =${operando}`;
|
|
43031
43095
|
if (operador === '-') {
|
|
43032
|
-
this.text += `
|
|
43096
|
+
this.text += `
|
|
43033
43097
|
neg r0, r0`;
|
|
43034
43098
|
}
|
|
43035
43099
|
else if (operador === '!' || operador === 'nao') {
|
|
43036
|
-
this.text += `
|
|
43037
|
-
cmp r0, #0
|
|
43038
|
-
moveq r0, #1
|
|
43100
|
+
this.text += `
|
|
43101
|
+
cmp r0, #0
|
|
43102
|
+
moveq r0, #1
|
|
43039
43103
|
movne r0, #0`;
|
|
43040
43104
|
}
|
|
43041
43105
|
return 'r0';
|
|
@@ -43044,8 +43108,8 @@ ${labelFim}:`;
|
|
|
43044
43108
|
const nomeVar = construto.simbolo?.lexema;
|
|
43045
43109
|
if (nomeVar && this.variaveis.has(nomeVar)) {
|
|
43046
43110
|
const varLabel = this.variaveis.get(nomeVar);
|
|
43047
|
-
this.text += `
|
|
43048
|
-
ldr r0, =${varLabel}
|
|
43111
|
+
this.text += `
|
|
43112
|
+
ldr r0, =${varLabel}
|
|
43049
43113
|
ldr r0, [r0]`;
|
|
43050
43114
|
return 'r0';
|
|
43051
43115
|
}
|
|
@@ -43059,9 +43123,9 @@ ${labelFim}:`;
|
|
|
43059
43123
|
construto.valores.forEach((valor, index) => {
|
|
43060
43124
|
if (this.dicionarioConstrutos[valor.constructor.name]) {
|
|
43061
43125
|
const valorTraduzido = this.dicionarioConstrutos[valor.constructor.name](valor);
|
|
43062
|
-
this.text += `
|
|
43063
|
-
ldr r0, =${valorTraduzido}
|
|
43064
|
-
ldr r1, =${labelVetor}
|
|
43126
|
+
this.text += `
|
|
43127
|
+
ldr r0, =${valorTraduzido}
|
|
43128
|
+
ldr r1, =${labelVetor}
|
|
43065
43129
|
str r0, [r1, #${index * 4}]`;
|
|
43066
43130
|
}
|
|
43067
43131
|
});
|
|
@@ -43081,17 +43145,17 @@ ${labelFim}:`;
|
|
|
43081
43145
|
traduzirDeclaracaoEnquanto(declaracao) {
|
|
43082
43146
|
const labelInicio = this.gerarLabel();
|
|
43083
43147
|
const labelFim = this.gerarLabel();
|
|
43084
|
-
this.text += `
|
|
43148
|
+
this.text += `
|
|
43085
43149
|
${labelInicio}:`;
|
|
43086
43150
|
const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
|
|
43087
|
-
this.text += `
|
|
43088
|
-
cmp ${condicao}, #0
|
|
43151
|
+
this.text += `
|
|
43152
|
+
cmp ${condicao}, #0
|
|
43089
43153
|
beq ${labelFim}`;
|
|
43090
43154
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
43091
43155
|
this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
|
|
43092
43156
|
}
|
|
43093
|
-
this.text += `
|
|
43094
|
-
b ${labelInicio}
|
|
43157
|
+
this.text += `
|
|
43158
|
+
b ${labelInicio}
|
|
43095
43159
|
${labelFim}:`;
|
|
43096
43160
|
}
|
|
43097
43161
|
traduzirDeclaracaoEscolha(declaracao) {
|
|
@@ -43102,10 +43166,10 @@ ${labelFim}:`;
|
|
|
43102
43166
|
const labelProximo = this.gerarLabel();
|
|
43103
43167
|
if (caminho.condicoes && caminho.condicoes[0]) {
|
|
43104
43168
|
const valorCaso = this.dicionarioConstrutos[caminho.condicoes[0].constructor.name](caminho.condicoes[0]);
|
|
43105
|
-
this.text += `
|
|
43106
|
-
ldr r0, =${valorEscolha}
|
|
43107
|
-
ldr r1, =${valorCaso}
|
|
43108
|
-
cmp r0, r1
|
|
43169
|
+
this.text += `
|
|
43170
|
+
ldr r0, =${valorEscolha}
|
|
43171
|
+
ldr r1, =${valorCaso}
|
|
43172
|
+
cmp r0, r1
|
|
43109
43173
|
bne ${labelProximo}`;
|
|
43110
43174
|
if (caminho.declaracoes && Array.isArray(caminho.declaracoes)) {
|
|
43111
43175
|
caminho.declaracoes.forEach((decl) => {
|
|
@@ -43114,13 +43178,13 @@ ${labelFim}:`;
|
|
|
43114
43178
|
}
|
|
43115
43179
|
});
|
|
43116
43180
|
}
|
|
43117
|
-
this.text += `
|
|
43118
|
-
b ${labelFim}
|
|
43181
|
+
this.text += `
|
|
43182
|
+
b ${labelFim}
|
|
43119
43183
|
${labelProximo}:`;
|
|
43120
43184
|
}
|
|
43121
43185
|
});
|
|
43122
43186
|
}
|
|
43123
|
-
this.text += `
|
|
43187
|
+
this.text += `
|
|
43124
43188
|
${labelFim}:`;
|
|
43125
43189
|
}
|
|
43126
43190
|
traduzirDeclaracaoExpressao(declaracao) {
|
|
@@ -43131,7 +43195,7 @@ ${labelFim}:`;
|
|
|
43131
43195
|
}
|
|
43132
43196
|
traduzirDeclaracaoFazer(declaracao) {
|
|
43133
43197
|
const labelInicio = this.gerarLabel();
|
|
43134
|
-
this.text += `
|
|
43198
|
+
this.text += `
|
|
43135
43199
|
${labelInicio}:`;
|
|
43136
43200
|
if (declaracao.caminhoFazer && declaracao.caminhoFazer.declaracoes) {
|
|
43137
43201
|
declaracao.caminhoFazer.declaracoes.forEach((decl) => {
|
|
@@ -43142,8 +43206,8 @@ ${labelInicio}:`;
|
|
|
43142
43206
|
}
|
|
43143
43207
|
if (declaracao.condicaoEnquanto) {
|
|
43144
43208
|
const condicao = this.dicionarioConstrutos[declaracao.condicaoEnquanto.constructor.name](declaracao.condicaoEnquanto);
|
|
43145
|
-
this.text += `
|
|
43146
|
-
cmp ${condicao}, #0
|
|
43209
|
+
this.text += `
|
|
43210
|
+
cmp ${condicao}, #0
|
|
43147
43211
|
bne ${labelInicio}`;
|
|
43148
43212
|
}
|
|
43149
43213
|
}
|
|
@@ -43157,18 +43221,18 @@ ${labelInicio}:`;
|
|
|
43157
43221
|
mensagem = this.dicionarioConstrutos[explicacao.constructor.name](explicacao);
|
|
43158
43222
|
}
|
|
43159
43223
|
}
|
|
43160
|
-
this.text += `
|
|
43161
|
-
@ Falhar com mensagem: ${mensagem}
|
|
43162
|
-
mov r0, #1
|
|
43163
|
-
mov r7, #1 @ sys_exit
|
|
43224
|
+
this.text += `
|
|
43225
|
+
@ Falhar com mensagem: ${mensagem}
|
|
43226
|
+
mov r0, #1
|
|
43227
|
+
mov r7, #1 @ sys_exit
|
|
43164
43228
|
swi 0`;
|
|
43165
43229
|
}
|
|
43166
43230
|
traduzirDeclaracaoFuncao(declaracao) {
|
|
43167
43231
|
const nomeFuncao = declaracao.simbolo?.lexema || 'funcao';
|
|
43168
|
-
this.text += `
|
|
43169
|
-
|
|
43170
|
-
${nomeFuncao}:
|
|
43171
|
-
push {fp, lr}
|
|
43232
|
+
this.text += `
|
|
43233
|
+
|
|
43234
|
+
${nomeFuncao}:
|
|
43235
|
+
push {fp, lr}
|
|
43172
43236
|
mov fp, sp`;
|
|
43173
43237
|
if (declaracao.funcao &&
|
|
43174
43238
|
declaracao.funcao.corpo &&
|
|
@@ -43179,12 +43243,12 @@ ${nomeFuncao}:
|
|
|
43179
43243
|
}
|
|
43180
43244
|
});
|
|
43181
43245
|
}
|
|
43182
|
-
this.text += `
|
|
43183
|
-
mov sp, fp
|
|
43246
|
+
this.text += `
|
|
43247
|
+
mov sp, fp
|
|
43184
43248
|
pop {fp, pc}`;
|
|
43185
43249
|
}
|
|
43186
43250
|
traduzirDeclaracaoImportar(declaracao) {
|
|
43187
|
-
this.text += `
|
|
43251
|
+
this.text += `
|
|
43188
43252
|
@ Importar: ${declaracao.caminho || 'unknown'}`;
|
|
43189
43253
|
}
|
|
43190
43254
|
traduzirDeclaracaoLeia(declaracao) {
|
|
@@ -43201,11 +43265,11 @@ ${nomeFuncao}:
|
|
|
43201
43265
|
this.bss += ` ${varLabel}: .space 256\n`;
|
|
43202
43266
|
this.variaveis.set(nomeVar, varLabel);
|
|
43203
43267
|
}
|
|
43204
|
-
this.text += `
|
|
43205
|
-
ldr r1, =${this.variaveis.get(nomeVar)}
|
|
43206
|
-
mov r2, #256
|
|
43207
|
-
mov r0, #0 @ stdin
|
|
43208
|
-
mov r7, #3 @ sys_read
|
|
43268
|
+
this.text += `
|
|
43269
|
+
ldr r1, =${this.variaveis.get(nomeVar)}
|
|
43270
|
+
mov r2, #256
|
|
43271
|
+
mov r0, #0 @ stdin
|
|
43272
|
+
mov r7, #3 @ sys_read
|
|
43209
43273
|
swi 0`;
|
|
43210
43274
|
}
|
|
43211
43275
|
traduzirDeclaracaoPara(declaracao) {
|
|
@@ -43220,12 +43284,12 @@ ${nomeFuncao}:
|
|
|
43220
43284
|
this.dicionarioConstrutos[tipoInicializador](declaracao.inicializador);
|
|
43221
43285
|
}
|
|
43222
43286
|
}
|
|
43223
|
-
this.text += `
|
|
43287
|
+
this.text += `
|
|
43224
43288
|
${labelInicio}:`;
|
|
43225
43289
|
if (declaracao.condicao) {
|
|
43226
43290
|
const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
|
|
43227
|
-
this.text += `
|
|
43228
|
-
cmp ${condicao}, #0
|
|
43291
|
+
this.text += `
|
|
43292
|
+
cmp ${condicao}, #0
|
|
43229
43293
|
beq ${labelFim}`;
|
|
43230
43294
|
}
|
|
43231
43295
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
@@ -43236,8 +43300,8 @@ ${labelInicio}:`;
|
|
|
43236
43300
|
this.dicionarioConstrutos[declaracao.incrementar.constructor.name](declaracao.incrementar);
|
|
43237
43301
|
}
|
|
43238
43302
|
}
|
|
43239
|
-
this.text += `
|
|
43240
|
-
b ${labelInicio}
|
|
43303
|
+
this.text += `
|
|
43304
|
+
b ${labelInicio}
|
|
43241
43305
|
${labelFim}:`;
|
|
43242
43306
|
}
|
|
43243
43307
|
traduzirDeclaracaoParaCada(declaracao) {
|
|
@@ -43252,55 +43316,55 @@ ${labelFim}:`;
|
|
|
43252
43316
|
if (vetor instanceof construtos_1.Vetor) {
|
|
43253
43317
|
tamanhoVetor = vetor.tamanho || 0;
|
|
43254
43318
|
}
|
|
43255
|
-
this.text += `
|
|
43256
|
-
mov r4, #0 @ counter
|
|
43257
|
-
${labelInicio}:
|
|
43258
|
-
cmp r4, #${tamanhoVetor}
|
|
43319
|
+
this.text += `
|
|
43320
|
+
mov r4, #0 @ counter
|
|
43321
|
+
${labelInicio}:
|
|
43322
|
+
cmp r4, #${tamanhoVetor}
|
|
43259
43323
|
bge ${labelFim}`;
|
|
43260
43324
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
43261
43325
|
this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
|
|
43262
43326
|
}
|
|
43263
|
-
this.text += `
|
|
43264
|
-
add r4, r4, #1
|
|
43265
|
-
b ${labelInicio}
|
|
43327
|
+
this.text += `
|
|
43328
|
+
add r4, r4, #1
|
|
43329
|
+
b ${labelInicio}
|
|
43266
43330
|
${labelFim}:`;
|
|
43267
43331
|
}
|
|
43268
43332
|
traduzirDeclaracaoRetorna(declaracao) {
|
|
43269
43333
|
if (declaracao.valor) {
|
|
43270
43334
|
const valor = this.dicionarioConstrutos[declaracao.valor.constructor.name](declaracao.valor);
|
|
43271
|
-
this.text += `
|
|
43335
|
+
this.text += `
|
|
43272
43336
|
ldr r0, =${valor}`;
|
|
43273
43337
|
}
|
|
43274
|
-
this.text += `
|
|
43275
|
-
mov sp, fp
|
|
43338
|
+
this.text += `
|
|
43339
|
+
mov sp, fp
|
|
43276
43340
|
pop {fp, pc}`;
|
|
43277
43341
|
}
|
|
43278
43342
|
traduzirDeclaracaoSe(declaracao) {
|
|
43279
43343
|
const labelSenao = this.gerarLabel();
|
|
43280
43344
|
const labelFim = this.gerarLabel();
|
|
43281
43345
|
const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
|
|
43282
|
-
this.text += `
|
|
43283
|
-
cmp ${condicao}, #0
|
|
43346
|
+
this.text += `
|
|
43347
|
+
cmp ${condicao}, #0
|
|
43284
43348
|
beq ${labelSenao}`;
|
|
43285
43349
|
if (this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name]) {
|
|
43286
43350
|
this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name](declaracao.caminhoEntao);
|
|
43287
43351
|
}
|
|
43288
|
-
this.text += `
|
|
43289
|
-
b ${labelFim}
|
|
43352
|
+
this.text += `
|
|
43353
|
+
b ${labelFim}
|
|
43290
43354
|
${labelSenao}:`;
|
|
43291
43355
|
if (declaracao.caminhoSenao &&
|
|
43292
43356
|
this.dicionarioDeclaracoes[declaracao.caminhoSenao.constructor.name]) {
|
|
43293
43357
|
this.dicionarioDeclaracoes[declaracao.caminhoSenao.constructor.name](declaracao.caminhoSenao);
|
|
43294
43358
|
}
|
|
43295
|
-
this.text += `
|
|
43359
|
+
this.text += `
|
|
43296
43360
|
${labelFim}:`;
|
|
43297
43361
|
}
|
|
43298
43362
|
traduzirDeclaracaoClasse(declaracao) {
|
|
43299
|
-
this.text += `
|
|
43363
|
+
this.text += `
|
|
43300
43364
|
@ Classe: ${declaracao.simbolo?.lexema || 'unknown'}`;
|
|
43301
43365
|
}
|
|
43302
43366
|
traduzirDeclaracaoTente(declaracao) {
|
|
43303
|
-
this.text += `
|
|
43367
|
+
this.text += `
|
|
43304
43368
|
@ Tente-pegue`;
|
|
43305
43369
|
if (declaracao.caminhoTente && Array.isArray(declaracao.caminhoTente)) {
|
|
43306
43370
|
declaracao.caminhoTente.forEach((decl) => {
|
|
@@ -43337,9 +43401,9 @@ ${labelFim}:`;
|
|
|
43337
43401
|
}
|
|
43338
43402
|
else if (this.dicionarioConstrutos[tipoInicializador]) {
|
|
43339
43403
|
const valor = this.dicionarioConstrutos[tipoInicializador](declaracao.inicializador);
|
|
43340
|
-
this.text += `
|
|
43341
|
-
ldr r0, =${valor}
|
|
43342
|
-
ldr r1, =${varLabel}
|
|
43404
|
+
this.text += `
|
|
43405
|
+
ldr r0, =${valor}
|
|
43406
|
+
ldr r1, =${varLabel}
|
|
43343
43407
|
str r0, [r1]`;
|
|
43344
43408
|
}
|
|
43345
43409
|
}
|
|
@@ -43366,20 +43430,20 @@ ${labelFim}:`;
|
|
|
43366
43430
|
// Para ambos linux-arm e android, continuamos usando a convenção
|
|
43367
43431
|
// de syscall Linux ARM (write). Em Android típico, esse binário
|
|
43368
43432
|
// seria executado via adb/Termux, onde essa convenção ainda é válida.
|
|
43369
|
-
this.text += `
|
|
43370
|
-
ldr r1, =${nome_string_literal}
|
|
43371
|
-
mov r2, #${tam_string_literal}
|
|
43372
|
-
mov r0, #1 @ fd stdout
|
|
43373
|
-
mov r7, #4 @ sys_write
|
|
43433
|
+
this.text += `
|
|
43434
|
+
ldr r1, =${nome_string_literal}
|
|
43435
|
+
mov r2, #${tam_string_literal}
|
|
43436
|
+
mov r0, #1 @ fd stdout
|
|
43437
|
+
mov r7, #4 @ sys_write
|
|
43374
43438
|
swi 0`;
|
|
43375
43439
|
}
|
|
43376
43440
|
saidaSistema() {
|
|
43377
43441
|
// Mesmo comentário da função Escreva: usamos sys_exit Linux.
|
|
43378
43442
|
// Em um futuro modo Android "NDK/JNI", esta função deveria
|
|
43379
43443
|
// apenas retornar ao chamador em vez de fazer syscall direta.
|
|
43380
|
-
this.text += `
|
|
43381
|
-
mov r0, #1 @ exit status
|
|
43382
|
-
mov r7, #1 @ sys_exit
|
|
43444
|
+
this.text += `
|
|
43445
|
+
mov r0, #1 @ exit status
|
|
43446
|
+
mov r7, #1 @ sys_exit
|
|
43383
43447
|
swi 0`;
|
|
43384
43448
|
}
|
|
43385
43449
|
traduzir(declaracoes) {
|
|
@@ -43455,10 +43519,10 @@ class TradutorAssemblyRISCV {
|
|
|
43455
43519
|
Var: this.traduzirDeclaracaoVar.bind(this),
|
|
43456
43520
|
};
|
|
43457
43521
|
this.indentacao = 0;
|
|
43458
|
-
this.text = `
|
|
43459
|
-
.text
|
|
43460
|
-
.global _start
|
|
43461
|
-
|
|
43522
|
+
this.text = `
|
|
43523
|
+
.text
|
|
43524
|
+
.global _start
|
|
43525
|
+
|
|
43462
43526
|
_start:`;
|
|
43463
43527
|
}
|
|
43464
43528
|
gerarDigitoAleatorio() {
|
|
@@ -43524,9 +43588,9 @@ _start:`;
|
|
|
43524
43588
|
const reg = this.obterRegistrador();
|
|
43525
43589
|
this.text += `\n la ${reg}, ${nomeVar}`;
|
|
43526
43590
|
this.emitirCarga('a0', indice);
|
|
43527
|
-
this.text += `
|
|
43528
|
-
slli a0, a0, 3 # multiplicar índice por 8 (palavra de 64 bits)
|
|
43529
|
-
add ${reg}, ${reg}, a0
|
|
43591
|
+
this.text += `
|
|
43592
|
+
slli a0, a0, 3 # multiplicar índice por 8 (palavra de 64 bits)
|
|
43593
|
+
add ${reg}, ${reg}, a0
|
|
43530
43594
|
ld a0, 0(${reg})`;
|
|
43531
43595
|
this.liberarRegistrador(reg);
|
|
43532
43596
|
return 'a0';
|
|
@@ -43550,8 +43614,8 @@ _start:`;
|
|
|
43550
43614
|
const reg = this.obterRegistrador();
|
|
43551
43615
|
this.text += `\n la ${reg}, ${nomeVar}`;
|
|
43552
43616
|
this.emitirCarga('a1', indice);
|
|
43553
|
-
this.text += `
|
|
43554
|
-
slli a1, a1, 3 # multiplicar índice por 8
|
|
43617
|
+
this.text += `
|
|
43618
|
+
slli a1, a1, 3 # multiplicar índice por 8
|
|
43555
43619
|
add ${reg}, ${reg}, a1`;
|
|
43556
43620
|
this.emitirCarga('a1', valor);
|
|
43557
43621
|
this.text += `\n sd a1, 0(${reg})`;
|
|
@@ -43571,8 +43635,8 @@ _start:`;
|
|
|
43571
43635
|
this.variaveis.set(nomeVar, varLabel);
|
|
43572
43636
|
}
|
|
43573
43637
|
this.emitirCarga('a0', valor);
|
|
43574
|
-
this.text += `
|
|
43575
|
-
la a1, ${this.variaveis.get(nomeVar)}
|
|
43638
|
+
this.text += `
|
|
43639
|
+
la a1, ${this.variaveis.get(nomeVar)}
|
|
43576
43640
|
sd a0, 0(a1)`;
|
|
43577
43641
|
}
|
|
43578
43642
|
traduzirConstrutoBinario(construto) {
|
|
@@ -43656,18 +43720,18 @@ _start:`;
|
|
|
43656
43720
|
const objeto = this.dicionarioConstrutos[construto.objeto.constructor.name](construto.objeto);
|
|
43657
43721
|
const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
|
|
43658
43722
|
this.emitirCarga('a0', valor);
|
|
43659
|
-
this.text += `
|
|
43660
|
-
la a1, ${objeto}
|
|
43723
|
+
this.text += `
|
|
43724
|
+
la a1, ${objeto}
|
|
43661
43725
|
sd a0, 0(a1)`;
|
|
43662
43726
|
}
|
|
43663
43727
|
traduzirFuncaoConstruto(construto) {
|
|
43664
43728
|
const labelFuncao = `func_${this.gerarDigitoAleatorio()}`;
|
|
43665
|
-
this.text += `
|
|
43666
|
-
|
|
43667
|
-
${labelFuncao}:
|
|
43668
|
-
addi sp, sp, -16
|
|
43669
|
-
sd ra, 8(sp)
|
|
43670
|
-
sd s0, 0(sp)
|
|
43729
|
+
this.text += `
|
|
43730
|
+
|
|
43731
|
+
${labelFuncao}:
|
|
43732
|
+
addi sp, sp, -16
|
|
43733
|
+
sd ra, 8(sp)
|
|
43734
|
+
sd s0, 0(sp)
|
|
43671
43735
|
addi s0, sp, 16`;
|
|
43672
43736
|
if (construto.corpo && Array.isArray(construto.corpo)) {
|
|
43673
43737
|
construto.corpo.forEach((declaracao) => {
|
|
@@ -43676,10 +43740,10 @@ ${labelFuncao}:
|
|
|
43676
43740
|
}
|
|
43677
43741
|
});
|
|
43678
43742
|
}
|
|
43679
|
-
this.text += `
|
|
43680
|
-
ld ra, 8(sp)
|
|
43681
|
-
ld s0, 0(sp)
|
|
43682
|
-
addi sp, sp, 16
|
|
43743
|
+
this.text += `
|
|
43744
|
+
ld ra, 8(sp)
|
|
43745
|
+
ld s0, 0(sp)
|
|
43746
|
+
addi sp, sp, 16
|
|
43683
43747
|
ret`;
|
|
43684
43748
|
}
|
|
43685
43749
|
traduzirConstrutoLiteral(construto) {
|
|
@@ -43703,21 +43767,21 @@ ${labelFuncao}:
|
|
|
43703
43767
|
if (operador === 'e' || operador === '&&') {
|
|
43704
43768
|
this.text += `\n beqz a0, ${labelFim}`;
|
|
43705
43769
|
this.emitirCarga('a0', direita);
|
|
43706
|
-
this.text += `
|
|
43707
|
-
beqz a0, ${labelFim}
|
|
43708
|
-
${labelVerdadeiro}:
|
|
43709
|
-
li a0, 1
|
|
43770
|
+
this.text += `
|
|
43771
|
+
beqz a0, ${labelFim}
|
|
43772
|
+
${labelVerdadeiro}:
|
|
43773
|
+
li a0, 1
|
|
43710
43774
|
${labelFim}:`;
|
|
43711
43775
|
}
|
|
43712
43776
|
else if (operador === 'ou' || operador === '||') {
|
|
43713
43777
|
this.text += `\n bnez a0, ${labelVerdadeiro}`;
|
|
43714
43778
|
this.emitirCarga('a0', direita);
|
|
43715
|
-
this.text += `
|
|
43716
|
-
bnez a0, ${labelVerdadeiro}
|
|
43717
|
-
li a0, 0
|
|
43718
|
-
j ${labelFim}
|
|
43719
|
-
${labelVerdadeiro}:
|
|
43720
|
-
li a0, 1
|
|
43779
|
+
this.text += `
|
|
43780
|
+
bnez a0, ${labelVerdadeiro}
|
|
43781
|
+
li a0, 0
|
|
43782
|
+
j ${labelFim}
|
|
43783
|
+
${labelVerdadeiro}:
|
|
43784
|
+
li a0, 1
|
|
43721
43785
|
${labelFim}:`;
|
|
43722
43786
|
}
|
|
43723
43787
|
return 'a0';
|
|
@@ -43742,8 +43806,8 @@ ${labelFim}:`;
|
|
|
43742
43806
|
const nomeVar = construto.simbolo?.lexema;
|
|
43743
43807
|
if (nomeVar && this.variaveis.has(nomeVar)) {
|
|
43744
43808
|
const varLabel = this.variaveis.get(nomeVar);
|
|
43745
|
-
this.text += `
|
|
43746
|
-
la a0, ${varLabel}
|
|
43809
|
+
this.text += `
|
|
43810
|
+
la a0, ${varLabel}
|
|
43747
43811
|
ld a0, 0(a0)`;
|
|
43748
43812
|
return 'a0';
|
|
43749
43813
|
}
|
|
@@ -43759,8 +43823,8 @@ ${labelFim}:`;
|
|
|
43759
43823
|
if (this.dicionarioConstrutos[valor.constructor.name]) {
|
|
43760
43824
|
const valorTraduzido = this.dicionarioConstrutos[valor.constructor.name](valor);
|
|
43761
43825
|
this.emitirCarga('a0', valorTraduzido);
|
|
43762
|
-
this.text += `
|
|
43763
|
-
la a1, ${labelVetor}
|
|
43826
|
+
this.text += `
|
|
43827
|
+
la a1, ${labelVetor}
|
|
43764
43828
|
sd a0, ${index * 8}(a1)`;
|
|
43765
43829
|
}
|
|
43766
43830
|
});
|
|
@@ -43788,8 +43852,8 @@ ${labelFim}:`;
|
|
|
43788
43852
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
43789
43853
|
this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
|
|
43790
43854
|
}
|
|
43791
|
-
this.text += `
|
|
43792
|
-
j ${labelInicio}
|
|
43855
|
+
this.text += `
|
|
43856
|
+
j ${labelInicio}
|
|
43793
43857
|
${labelFim}:`;
|
|
43794
43858
|
}
|
|
43795
43859
|
traduzirDeclaracaoEscolha(declaracao) {
|
|
@@ -43810,8 +43874,8 @@ ${labelFim}:`;
|
|
|
43810
43874
|
}
|
|
43811
43875
|
});
|
|
43812
43876
|
}
|
|
43813
|
-
this.text += `
|
|
43814
|
-
j ${labelFim}
|
|
43877
|
+
this.text += `
|
|
43878
|
+
j ${labelFim}
|
|
43815
43879
|
${labelProximo}:`;
|
|
43816
43880
|
}
|
|
43817
43881
|
});
|
|
@@ -43855,20 +43919,20 @@ ${labelProximo}:`;
|
|
|
43855
43919
|
mensagem = this.dicionarioConstrutos[explicacao.constructor.name](explicacao);
|
|
43856
43920
|
}
|
|
43857
43921
|
}
|
|
43858
|
-
this.text += `
|
|
43859
|
-
# Falhar com mensagem: ${mensagem}
|
|
43860
|
-
li a0, 1
|
|
43861
|
-
li a7, 93 # sys_exit
|
|
43922
|
+
this.text += `
|
|
43923
|
+
# Falhar com mensagem: ${mensagem}
|
|
43924
|
+
li a0, 1
|
|
43925
|
+
li a7, 93 # sys_exit
|
|
43862
43926
|
ecall`;
|
|
43863
43927
|
}
|
|
43864
43928
|
traduzirDeclaracaoFuncao(declaracao) {
|
|
43865
43929
|
const nomeFuncao = declaracao.simbolo?.lexema || 'funcao';
|
|
43866
|
-
this.text += `
|
|
43867
|
-
|
|
43868
|
-
${nomeFuncao}:
|
|
43869
|
-
addi sp, sp, -16
|
|
43870
|
-
sd ra, 8(sp)
|
|
43871
|
-
sd s0, 0(sp)
|
|
43930
|
+
this.text += `
|
|
43931
|
+
|
|
43932
|
+
${nomeFuncao}:
|
|
43933
|
+
addi sp, sp, -16
|
|
43934
|
+
sd ra, 8(sp)
|
|
43935
|
+
sd s0, 0(sp)
|
|
43872
43936
|
addi s0, sp, 16`;
|
|
43873
43937
|
if (declaracao.funcao &&
|
|
43874
43938
|
declaracao.funcao.corpo &&
|
|
@@ -43879,10 +43943,10 @@ ${nomeFuncao}:
|
|
|
43879
43943
|
}
|
|
43880
43944
|
});
|
|
43881
43945
|
}
|
|
43882
|
-
this.text += `
|
|
43883
|
-
ld ra, 8(sp)
|
|
43884
|
-
ld s0, 0(sp)
|
|
43885
|
-
addi sp, sp, 16
|
|
43946
|
+
this.text += `
|
|
43947
|
+
ld ra, 8(sp)
|
|
43948
|
+
ld s0, 0(sp)
|
|
43949
|
+
addi sp, sp, 16
|
|
43886
43950
|
ret`;
|
|
43887
43951
|
}
|
|
43888
43952
|
traduzirDeclaracaoImportar(declaracao) {
|
|
@@ -43902,11 +43966,11 @@ ${nomeFuncao}:
|
|
|
43902
43966
|
this.bss += ` ${varLabel}: .space 256\n`;
|
|
43903
43967
|
this.variaveis.set(nomeVar, varLabel);
|
|
43904
43968
|
}
|
|
43905
|
-
this.text += `
|
|
43906
|
-
li a0, 0 # fd stdin
|
|
43907
|
-
la a1, ${this.variaveis.get(nomeVar)}
|
|
43908
|
-
li a2, 256
|
|
43909
|
-
li a7, 63 # sys_read
|
|
43969
|
+
this.text += `
|
|
43970
|
+
li a0, 0 # fd stdin
|
|
43971
|
+
la a1, ${this.variaveis.get(nomeVar)}
|
|
43972
|
+
li a2, 256
|
|
43973
|
+
li a7, 63 # sys_read
|
|
43910
43974
|
ecall`;
|
|
43911
43975
|
}
|
|
43912
43976
|
traduzirDeclaracaoPara(declaracao) {
|
|
@@ -43937,8 +44001,8 @@ ${nomeFuncao}:
|
|
|
43937
44001
|
this.dicionarioConstrutos[declaracao.incrementar.constructor.name](declaracao.incrementar);
|
|
43938
44002
|
}
|
|
43939
44003
|
}
|
|
43940
|
-
this.text += `
|
|
43941
|
-
j ${labelInicio}
|
|
44004
|
+
this.text += `
|
|
44005
|
+
j ${labelInicio}
|
|
43942
44006
|
${labelFim}:`;
|
|
43943
44007
|
}
|
|
43944
44008
|
traduzirDeclaracaoParaCada(declaracao) {
|
|
@@ -43950,17 +44014,17 @@ ${labelFim}:`;
|
|
|
43950
44014
|
tamanhoVetor = vetor.tamanho || 0;
|
|
43951
44015
|
}
|
|
43952
44016
|
// t0: contador; t1: limite (caller-saved, seguros aqui)
|
|
43953
|
-
this.text += `
|
|
43954
|
-
li t0, 0 # contador
|
|
43955
|
-
li t1, ${tamanhoVetor}
|
|
43956
|
-
${labelInicio}:
|
|
44017
|
+
this.text += `
|
|
44018
|
+
li t0, 0 # contador
|
|
44019
|
+
li t1, ${tamanhoVetor}
|
|
44020
|
+
${labelInicio}:
|
|
43957
44021
|
bge t0, t1, ${labelFim}`;
|
|
43958
44022
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
43959
44023
|
this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
|
|
43960
44024
|
}
|
|
43961
|
-
this.text += `
|
|
43962
|
-
addi t0, t0, 1
|
|
43963
|
-
j ${labelInicio}
|
|
44025
|
+
this.text += `
|
|
44026
|
+
addi t0, t0, 1
|
|
44027
|
+
j ${labelInicio}
|
|
43964
44028
|
${labelFim}:`;
|
|
43965
44029
|
}
|
|
43966
44030
|
traduzirDeclaracaoRetorna(declaracao) {
|
|
@@ -43968,10 +44032,10 @@ ${labelFim}:`;
|
|
|
43968
44032
|
const valor = this.dicionarioConstrutos[declaracao.valor.constructor.name](declaracao.valor);
|
|
43969
44033
|
this.emitirCarga('a0', valor);
|
|
43970
44034
|
}
|
|
43971
|
-
this.text += `
|
|
43972
|
-
ld ra, 8(sp)
|
|
43973
|
-
ld s0, 0(sp)
|
|
43974
|
-
addi sp, sp, 16
|
|
44035
|
+
this.text += `
|
|
44036
|
+
ld ra, 8(sp)
|
|
44037
|
+
ld s0, 0(sp)
|
|
44038
|
+
addi sp, sp, 16
|
|
43975
44039
|
ret`;
|
|
43976
44040
|
}
|
|
43977
44041
|
traduzirDeclaracaoSe(declaracao) {
|
|
@@ -43985,8 +44049,8 @@ ${labelFim}:`;
|
|
|
43985
44049
|
if (this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name]) {
|
|
43986
44050
|
this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name](declaracao.caminhoEntao);
|
|
43987
44051
|
}
|
|
43988
|
-
this.text += `
|
|
43989
|
-
j ${labelFim}
|
|
44052
|
+
this.text += `
|
|
44053
|
+
j ${labelFim}
|
|
43990
44054
|
${labelSenao}:`;
|
|
43991
44055
|
if (declaracao.caminhoSenao &&
|
|
43992
44056
|
this.dicionarioDeclaracoes[declaracao.caminhoSenao.constructor.name]) {
|
|
@@ -44032,8 +44096,8 @@ ${labelSenao}:`;
|
|
|
44032
44096
|
else if (this.dicionarioConstrutos[tipoInicializador]) {
|
|
44033
44097
|
const valor = this.dicionarioConstrutos[tipoInicializador](declaracao.inicializador);
|
|
44034
44098
|
this.emitirCarga('a0', valor);
|
|
44035
|
-
this.text += `
|
|
44036
|
-
la a1, ${varLabel}
|
|
44099
|
+
this.text += `
|
|
44100
|
+
la a1, ${varLabel}
|
|
44037
44101
|
sd a0, 0(a1)`;
|
|
44038
44102
|
}
|
|
44039
44103
|
}
|
|
@@ -44060,17 +44124,17 @@ ${labelSenao}:`;
|
|
|
44060
44124
|
tamanhoString = String(stringValue.length);
|
|
44061
44125
|
}
|
|
44062
44126
|
// sys_write(fd=1, buf, count): a7=64, a0=fd, a1=buf, a2=count
|
|
44063
|
-
this.text += `
|
|
44064
|
-
la a1, ${nomeStringLiteral}
|
|
44065
|
-
li a2, ${tamanhoString}
|
|
44066
|
-
li a0, 1 # fd stdout
|
|
44067
|
-
li a7, 64 # sys_write
|
|
44127
|
+
this.text += `
|
|
44128
|
+
la a1, ${nomeStringLiteral}
|
|
44129
|
+
li a2, ${tamanhoString}
|
|
44130
|
+
li a0, 1 # fd stdout
|
|
44131
|
+
li a7, 64 # sys_write
|
|
44068
44132
|
ecall`;
|
|
44069
44133
|
}
|
|
44070
44134
|
saidaSistema() {
|
|
44071
|
-
this.text += `
|
|
44072
|
-
li a0, 0 # código de saída
|
|
44073
|
-
li a7, 93 # sys_exit
|
|
44135
|
+
this.text += `
|
|
44136
|
+
li a0, 0 # código de saída
|
|
44137
|
+
li a7, 93 # sys_exit
|
|
44074
44138
|
ecall`;
|
|
44075
44139
|
}
|
|
44076
44140
|
traduzir(declaracoes) {
|
|
@@ -44146,14 +44210,14 @@ class TradutorAssemblyX64 {
|
|
|
44146
44210
|
Escreva: this.traduzirDeclaracaoEscreva.bind(this),
|
|
44147
44211
|
};
|
|
44148
44212
|
this.indentacao = 0;
|
|
44149
|
-
this.textoSaida = `
|
|
44150
|
-
section .text
|
|
44151
|
-
${this.alvo === 'linux' ? 'global _start' : 'global main'}
|
|
44213
|
+
this.textoSaida = `
|
|
44214
|
+
section .text
|
|
44215
|
+
${this.alvo === 'linux' ? 'global _start' : 'global main'}
|
|
44152
44216
|
${this.alvo === 'linux' ? '_start:' : 'main:'}`;
|
|
44153
44217
|
if (this.alvo === 'windows') {
|
|
44154
44218
|
this.textoSaida =
|
|
44155
|
-
`
|
|
44156
|
-
extern printf
|
|
44219
|
+
`
|
|
44220
|
+
extern printf
|
|
44157
44221
|
` + this.textoSaida;
|
|
44158
44222
|
}
|
|
44159
44223
|
}
|
|
@@ -44213,8 +44277,8 @@ extern printf
|
|
|
44213
44277
|
}
|
|
44214
44278
|
const indice = this.dicionarioConstrutos[construto.indice.constructor.name](construto.indice);
|
|
44215
44279
|
const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
|
|
44216
|
-
this.textoSaida += `
|
|
44217
|
-
mov rax, ${valor}
|
|
44280
|
+
this.textoSaida += `
|
|
44281
|
+
mov rax, ${valor}
|
|
44218
44282
|
mov [${nomeVar} + ${indice} * 8], rax`;
|
|
44219
44283
|
}
|
|
44220
44284
|
traduzirConstrutoAtribuir(construto) {
|
|
@@ -44232,8 +44296,8 @@ extern printf
|
|
|
44232
44296
|
this.bss += ` ${varLabel} resq 1\n`;
|
|
44233
44297
|
this.variaveis.set(nomeVar, varLabel);
|
|
44234
44298
|
}
|
|
44235
|
-
this.textoSaida += `
|
|
44236
|
-
mov rax, ${valor}
|
|
44299
|
+
this.textoSaida += `
|
|
44300
|
+
mov rax, ${valor}
|
|
44237
44301
|
mov [${this.variaveis.get(nomeVar)}], rax`;
|
|
44238
44302
|
}
|
|
44239
44303
|
traduzirConstrutoBinario(construto) {
|
|
@@ -44241,71 +44305,71 @@ extern printf
|
|
|
44241
44305
|
const direita = this.dicionarioConstrutos[construto.direita.constructor.name](construto.direita);
|
|
44242
44306
|
const operador = construto.operador.lexema;
|
|
44243
44307
|
const registrador = this.obterRegistrador();
|
|
44244
|
-
this.textoSaida += `
|
|
44245
|
-
mov rax, ${esquerda}
|
|
44308
|
+
this.textoSaida += `
|
|
44309
|
+
mov rax, ${esquerda}
|
|
44246
44310
|
mov ${registrador}, ${direita}`;
|
|
44247
44311
|
switch (operador) {
|
|
44248
44312
|
case '+':
|
|
44249
|
-
this.textoSaida += `
|
|
44313
|
+
this.textoSaida += `
|
|
44250
44314
|
add rax, ${registrador}`;
|
|
44251
44315
|
break;
|
|
44252
44316
|
case '-':
|
|
44253
|
-
this.textoSaida += `
|
|
44317
|
+
this.textoSaida += `
|
|
44254
44318
|
sub rax, ${registrador}`;
|
|
44255
44319
|
break;
|
|
44256
44320
|
case '*':
|
|
44257
|
-
this.textoSaida += `
|
|
44321
|
+
this.textoSaida += `
|
|
44258
44322
|
imul rax, ${registrador}`;
|
|
44259
44323
|
break;
|
|
44260
44324
|
case '/':
|
|
44261
|
-
this.textoSaida += `
|
|
44262
|
-
xor rdx, rdx
|
|
44325
|
+
this.textoSaida += `
|
|
44326
|
+
xor rdx, rdx
|
|
44263
44327
|
idiv ${registrador}`;
|
|
44264
44328
|
break;
|
|
44265
44329
|
case '%':
|
|
44266
|
-
this.textoSaida += `
|
|
44267
|
-
xor rdx, rdx
|
|
44268
|
-
idiv ${registrador}
|
|
44330
|
+
this.textoSaida += `
|
|
44331
|
+
xor rdx, rdx
|
|
44332
|
+
idiv ${registrador}
|
|
44269
44333
|
mov rax, rdx`;
|
|
44270
44334
|
break;
|
|
44271
44335
|
case '<':
|
|
44272
|
-
this.textoSaida += `
|
|
44273
|
-
cmp rax, ${registrador}
|
|
44274
|
-
setl al
|
|
44336
|
+
this.textoSaida += `
|
|
44337
|
+
cmp rax, ${registrador}
|
|
44338
|
+
setl al
|
|
44275
44339
|
movzx rax, al`;
|
|
44276
44340
|
break;
|
|
44277
44341
|
case '>':
|
|
44278
|
-
this.textoSaida += `
|
|
44279
|
-
cmp rax, ${registrador}
|
|
44280
|
-
setg al
|
|
44342
|
+
this.textoSaida += `
|
|
44343
|
+
cmp rax, ${registrador}
|
|
44344
|
+
setg al
|
|
44281
44345
|
movzx rax, al`;
|
|
44282
44346
|
break;
|
|
44283
44347
|
case '<=':
|
|
44284
|
-
this.textoSaida += `
|
|
44285
|
-
cmp rax, ${registrador}
|
|
44286
|
-
setle al
|
|
44348
|
+
this.textoSaida += `
|
|
44349
|
+
cmp rax, ${registrador}
|
|
44350
|
+
setle al
|
|
44287
44351
|
movzx rax, al`;
|
|
44288
44352
|
break;
|
|
44289
44353
|
case '>=':
|
|
44290
|
-
this.textoSaida += `
|
|
44291
|
-
cmp rax, ${registrador}
|
|
44292
|
-
setge al
|
|
44354
|
+
this.textoSaida += `
|
|
44355
|
+
cmp rax, ${registrador}
|
|
44356
|
+
setge al
|
|
44293
44357
|
movzx rax, al`;
|
|
44294
44358
|
break;
|
|
44295
44359
|
case '==':
|
|
44296
|
-
this.textoSaida += `
|
|
44297
|
-
cmp rax, ${registrador}
|
|
44298
|
-
sete al
|
|
44360
|
+
this.textoSaida += `
|
|
44361
|
+
cmp rax, ${registrador}
|
|
44362
|
+
sete al
|
|
44299
44363
|
movzx rax, al`;
|
|
44300
44364
|
break;
|
|
44301
44365
|
case '!=':
|
|
44302
|
-
this.textoSaida += `
|
|
44303
|
-
cmp rax, ${registrador}
|
|
44304
|
-
setne al
|
|
44366
|
+
this.textoSaida += `
|
|
44367
|
+
cmp rax, ${registrador}
|
|
44368
|
+
setne al
|
|
44305
44369
|
movzx rax, al`;
|
|
44306
44370
|
break;
|
|
44307
44371
|
default:
|
|
44308
|
-
this.textoSaida += `
|
|
44372
|
+
this.textoSaida += `
|
|
44309
44373
|
; Operador ${operador} não implementado`;
|
|
44310
44374
|
}
|
|
44311
44375
|
this.liberarRegistrador(registrador);
|
|
@@ -44323,28 +44387,28 @@ extern printf
|
|
|
44323
44387
|
construto.argumentos.forEach((arg, index) => {
|
|
44324
44388
|
if (index < registrosArgs.length) {
|
|
44325
44389
|
const valorArg = this.dicionarioConstrutos[arg.constructor.name](arg);
|
|
44326
|
-
this.textoSaida += `
|
|
44390
|
+
this.textoSaida += `
|
|
44327
44391
|
mov ${registrosArgs[index]}, ${valorArg}`;
|
|
44328
44392
|
}
|
|
44329
44393
|
else {
|
|
44330
44394
|
// TODO: push extra args on stack according to target ABI
|
|
44331
44395
|
}
|
|
44332
44396
|
});
|
|
44333
|
-
this.textoSaida += `
|
|
44397
|
+
this.textoSaida += `
|
|
44334
44398
|
call ${nomeFuncao}`;
|
|
44335
44399
|
}
|
|
44336
44400
|
traduzirConstrutoDefinirValor(construto) {
|
|
44337
44401
|
const objeto = this.dicionarioConstrutos[construto.objeto.constructor.name](construto.objeto);
|
|
44338
44402
|
const valor = this.dicionarioConstrutos[construto.valor.constructor.name](construto.valor);
|
|
44339
|
-
this.textoSaida += `
|
|
44340
|
-
mov rax, ${valor}
|
|
44403
|
+
this.textoSaida += `
|
|
44404
|
+
mov rax, ${valor}
|
|
44341
44405
|
mov [${objeto}], rax`;
|
|
44342
44406
|
}
|
|
44343
44407
|
traduzirFuncaoConstruto(construto) {
|
|
44344
44408
|
const labelFuncao = `func_${this.gerarDigitoAleatorio()}`;
|
|
44345
|
-
this.textoSaida += `
|
|
44346
|
-
${labelFuncao}:
|
|
44347
|
-
push rbp
|
|
44409
|
+
this.textoSaida += `
|
|
44410
|
+
${labelFuncao}:
|
|
44411
|
+
push rbp
|
|
44348
44412
|
mov rbp, rsp`;
|
|
44349
44413
|
// Traduzir corpo da função
|
|
44350
44414
|
if (construto.corpo && Array.isArray(construto.corpo)) {
|
|
@@ -44354,8 +44418,8 @@ ${labelFuncao}:
|
|
|
44354
44418
|
}
|
|
44355
44419
|
});
|
|
44356
44420
|
}
|
|
44357
|
-
this.textoSaida += `
|
|
44358
|
-
pop rbp
|
|
44421
|
+
this.textoSaida += `
|
|
44422
|
+
pop rbp
|
|
44359
44423
|
ret`;
|
|
44360
44424
|
}
|
|
44361
44425
|
traduzirConstrutoLiteral(construto) {
|
|
@@ -44370,29 +44434,29 @@ ${labelFuncao}:
|
|
|
44370
44434
|
const operador = construto.operador.lexema;
|
|
44371
44435
|
const labelVerdadeiro = this.gerarLabel();
|
|
44372
44436
|
const labelFim = this.gerarLabel();
|
|
44373
|
-
this.textoSaida += `
|
|
44374
|
-
mov rax, ${esquerda}
|
|
44437
|
+
this.textoSaida += `
|
|
44438
|
+
mov rax, ${esquerda}
|
|
44375
44439
|
cmp rax, 0`;
|
|
44376
44440
|
if (operador === 'e' || operador === '&&') {
|
|
44377
|
-
this.textoSaida += `
|
|
44378
|
-
je ${labelFim}
|
|
44379
|
-
mov rax, ${direita}
|
|
44380
|
-
cmp rax, 0
|
|
44381
|
-
je ${labelFim}
|
|
44382
|
-
${labelVerdadeiro}:
|
|
44383
|
-
mov rax, 1
|
|
44441
|
+
this.textoSaida += `
|
|
44442
|
+
je ${labelFim}
|
|
44443
|
+
mov rax, ${direita}
|
|
44444
|
+
cmp rax, 0
|
|
44445
|
+
je ${labelFim}
|
|
44446
|
+
${labelVerdadeiro}:
|
|
44447
|
+
mov rax, 1
|
|
44384
44448
|
${labelFim}:`;
|
|
44385
44449
|
}
|
|
44386
44450
|
else if (operador === 'ou' || operador === '||') {
|
|
44387
|
-
this.textoSaida += `
|
|
44388
|
-
jne ${labelVerdadeiro}
|
|
44389
|
-
mov rax, ${direita}
|
|
44390
|
-
cmp rax, 0
|
|
44391
|
-
jne ${labelVerdadeiro}
|
|
44392
|
-
mov rax, 0
|
|
44393
|
-
jmp ${labelFim}
|
|
44394
|
-
${labelVerdadeiro}:
|
|
44395
|
-
mov rax, 1
|
|
44451
|
+
this.textoSaida += `
|
|
44452
|
+
jne ${labelVerdadeiro}
|
|
44453
|
+
mov rax, ${direita}
|
|
44454
|
+
cmp rax, 0
|
|
44455
|
+
jne ${labelVerdadeiro}
|
|
44456
|
+
mov rax, 0
|
|
44457
|
+
jmp ${labelFim}
|
|
44458
|
+
${labelVerdadeiro}:
|
|
44459
|
+
mov rax, 1
|
|
44396
44460
|
${labelFim}:`;
|
|
44397
44461
|
}
|
|
44398
44462
|
return 'rax';
|
|
@@ -44405,26 +44469,26 @@ ${labelFim}:`;
|
|
|
44405
44469
|
traduzirConstrutoUnario(construto) {
|
|
44406
44470
|
const operando = this.dicionarioConstrutos[construto.operando.constructor.name](construto.operando);
|
|
44407
44471
|
const operador = construto.operador.lexema;
|
|
44408
|
-
this.textoSaida += `
|
|
44472
|
+
this.textoSaida += `
|
|
44409
44473
|
mov rax, ${operando}`;
|
|
44410
44474
|
if (operador === '-') {
|
|
44411
|
-
this.textoSaida += `
|
|
44475
|
+
this.textoSaida += `
|
|
44412
44476
|
neg rax`;
|
|
44413
44477
|
}
|
|
44414
44478
|
else if (operador === '!' || operador === 'nao') {
|
|
44415
|
-
this.textoSaida += `
|
|
44416
|
-
cmp rax, 0
|
|
44417
|
-
sete al
|
|
44479
|
+
this.textoSaida += `
|
|
44480
|
+
cmp rax, 0
|
|
44481
|
+
sete al
|
|
44418
44482
|
movzx rax, al`;
|
|
44419
44483
|
}
|
|
44420
44484
|
else if (operador === '++') {
|
|
44421
|
-
this.textoSaida += `
|
|
44422
|
-
inc rax
|
|
44485
|
+
this.textoSaida += `
|
|
44486
|
+
inc rax
|
|
44423
44487
|
mov ${operando}, rax`;
|
|
44424
44488
|
}
|
|
44425
44489
|
else if (operador === '--') {
|
|
44426
|
-
this.textoSaida += `
|
|
44427
|
-
dec rax
|
|
44490
|
+
this.textoSaida += `
|
|
44491
|
+
dec rax
|
|
44428
44492
|
mov ${operando}, rax`;
|
|
44429
44493
|
}
|
|
44430
44494
|
return 'rax';
|
|
@@ -44444,8 +44508,8 @@ ${labelFim}:`;
|
|
|
44444
44508
|
construto.valores.forEach((valor, index) => {
|
|
44445
44509
|
if (this.dicionarioConstrutos[valor.constructor.name]) {
|
|
44446
44510
|
const valorTraduzido = this.dicionarioConstrutos[valor.constructor.name](valor);
|
|
44447
|
-
this.textoSaida += `
|
|
44448
|
-
mov rax, ${valorTraduzido}
|
|
44511
|
+
this.textoSaida += `
|
|
44512
|
+
mov rax, ${valorTraduzido}
|
|
44449
44513
|
mov [${rotuloVetor} + ${index * 8}], rax`;
|
|
44450
44514
|
}
|
|
44451
44515
|
});
|
|
@@ -44465,17 +44529,17 @@ ${labelFim}:`;
|
|
|
44465
44529
|
traduzirDeclaracaoEnquanto(declaracao) {
|
|
44466
44530
|
const labelInicio = this.gerarLabel();
|
|
44467
44531
|
const labelFim = this.gerarLabel();
|
|
44468
|
-
this.textoSaida += `
|
|
44532
|
+
this.textoSaida += `
|
|
44469
44533
|
${labelInicio}:`;
|
|
44470
44534
|
const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
|
|
44471
|
-
this.textoSaida += `
|
|
44472
|
-
cmp ${condicao}, 0
|
|
44535
|
+
this.textoSaida += `
|
|
44536
|
+
cmp ${condicao}, 0
|
|
44473
44537
|
je ${labelFim}`;
|
|
44474
44538
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
44475
44539
|
this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
|
|
44476
44540
|
}
|
|
44477
|
-
this.textoSaida += `
|
|
44478
|
-
jmp ${labelInicio}
|
|
44541
|
+
this.textoSaida += `
|
|
44542
|
+
jmp ${labelInicio}
|
|
44479
44543
|
${labelFim}:`;
|
|
44480
44544
|
}
|
|
44481
44545
|
traduzirDeclaracaoEscolha(declaracao) {
|
|
@@ -44486,9 +44550,9 @@ ${labelFim}:`;
|
|
|
44486
44550
|
const labelProximo = this.gerarLabel();
|
|
44487
44551
|
if (caminho.condicoes && caminho.condicoes[0]) {
|
|
44488
44552
|
const valorCaso = this.dicionarioConstrutos[caminho.condicoes[0].constructor.name](caminho.condicoes[0]);
|
|
44489
|
-
this.textoSaida += `
|
|
44490
|
-
mov rax, ${valorEscolha}
|
|
44491
|
-
cmp rax, ${valorCaso}
|
|
44553
|
+
this.textoSaida += `
|
|
44554
|
+
mov rax, ${valorEscolha}
|
|
44555
|
+
cmp rax, ${valorCaso}
|
|
44492
44556
|
jne ${labelProximo}`;
|
|
44493
44557
|
if (caminho.declaracoes && Array.isArray(caminho.declaracoes)) {
|
|
44494
44558
|
caminho.declaracoes.forEach((declaracao) => {
|
|
@@ -44497,13 +44561,13 @@ ${labelFim}:`;
|
|
|
44497
44561
|
}
|
|
44498
44562
|
});
|
|
44499
44563
|
}
|
|
44500
|
-
this.textoSaida += `
|
|
44501
|
-
jmp ${labelFim}
|
|
44564
|
+
this.textoSaida += `
|
|
44565
|
+
jmp ${labelFim}
|
|
44502
44566
|
${labelProximo}:`;
|
|
44503
44567
|
}
|
|
44504
44568
|
});
|
|
44505
44569
|
}
|
|
44506
|
-
this.textoSaida += `
|
|
44570
|
+
this.textoSaida += `
|
|
44507
44571
|
${labelFim}:`;
|
|
44508
44572
|
}
|
|
44509
44573
|
traduzirDeclaracaoExpressao(declaracao) {
|
|
@@ -44514,7 +44578,7 @@ ${labelFim}:`;
|
|
|
44514
44578
|
}
|
|
44515
44579
|
traduzirDeclaracaoFazer(declaracao) {
|
|
44516
44580
|
const labelInicio = this.gerarLabel();
|
|
44517
|
-
this.textoSaida += `
|
|
44581
|
+
this.textoSaida += `
|
|
44518
44582
|
${labelInicio}:`;
|
|
44519
44583
|
// Em Delégua, fazer-enquanto tem caminhoFazer que é um Bloco
|
|
44520
44584
|
if (declaracao.caminhoFazer && declaracao.caminhoFazer.declaracoes) {
|
|
@@ -44526,8 +44590,8 @@ ${labelInicio}:`;
|
|
|
44526
44590
|
}
|
|
44527
44591
|
if (declaracao.condicaoEnquanto) {
|
|
44528
44592
|
const condicao = this.dicionarioConstrutos[declaracao.condicaoEnquanto.constructor.name](declaracao.condicaoEnquanto);
|
|
44529
|
-
this.textoSaida += `
|
|
44530
|
-
cmp ${condicao}, 0
|
|
44593
|
+
this.textoSaida += `
|
|
44594
|
+
cmp ${condicao}, 0
|
|
44531
44595
|
jne ${labelInicio}`;
|
|
44532
44596
|
}
|
|
44533
44597
|
}
|
|
@@ -44542,24 +44606,24 @@ ${labelInicio}:`;
|
|
|
44542
44606
|
}
|
|
44543
44607
|
}
|
|
44544
44608
|
if (this.alvo === 'linux') {
|
|
44545
|
-
this.textoSaida += `
|
|
44546
|
-
; Falhar com mensagem: ${mensagem}
|
|
44547
|
-
mov eax, 1
|
|
44548
|
-
mov ebx, 1
|
|
44609
|
+
this.textoSaida += `
|
|
44610
|
+
; Falhar com mensagem: ${mensagem}
|
|
44611
|
+
mov eax, 1
|
|
44612
|
+
mov ebx, 1
|
|
44549
44613
|
int 0x80`;
|
|
44550
44614
|
}
|
|
44551
44615
|
else {
|
|
44552
|
-
this.textoSaida += `
|
|
44553
|
-
; Falhar com mensagem: ${mensagem}
|
|
44554
|
-
mov eax, 1
|
|
44616
|
+
this.textoSaida += `
|
|
44617
|
+
; Falhar com mensagem: ${mensagem}
|
|
44618
|
+
mov eax, 1
|
|
44555
44619
|
ret`;
|
|
44556
44620
|
}
|
|
44557
44621
|
}
|
|
44558
44622
|
traduzirDeclaracaoFuncao(declaracao) {
|
|
44559
44623
|
const nomeFuncao = declaracao.simbolo?.lexema || 'funcao';
|
|
44560
|
-
this.textoSaida += `
|
|
44561
|
-
${nomeFuncao}:
|
|
44562
|
-
push rbp
|
|
44624
|
+
this.textoSaida += `
|
|
44625
|
+
${nomeFuncao}:
|
|
44626
|
+
push rbp
|
|
44563
44627
|
mov rbp, rsp`;
|
|
44564
44628
|
if (declaracao.funcao &&
|
|
44565
44629
|
declaracao.funcao.corpo &&
|
|
@@ -44570,13 +44634,13 @@ ${nomeFuncao}:
|
|
|
44570
44634
|
}
|
|
44571
44635
|
});
|
|
44572
44636
|
}
|
|
44573
|
-
this.textoSaida += `
|
|
44574
|
-
pop rbp
|
|
44637
|
+
this.textoSaida += `
|
|
44638
|
+
pop rbp
|
|
44575
44639
|
ret`;
|
|
44576
44640
|
}
|
|
44577
44641
|
traduzirDeclaracaoImportar(declaracao) {
|
|
44578
44642
|
// Importação é tratada em tempo de linkagem
|
|
44579
|
-
this.textoSaida += `
|
|
44643
|
+
this.textoSaida += `
|
|
44580
44644
|
; Importar: ${declaracao.caminho || 'unknown'}`;
|
|
44581
44645
|
}
|
|
44582
44646
|
traduzirDeclaracaoLeia(declaracao) {
|
|
@@ -44593,11 +44657,11 @@ ${nomeFuncao}:
|
|
|
44593
44657
|
this.bss += ` ${varLabel} resb 256\n`;
|
|
44594
44658
|
this.variaveis.set(nomeVar, varLabel);
|
|
44595
44659
|
}
|
|
44596
|
-
this.textoSaida += `
|
|
44597
|
-
mov eax, 3
|
|
44598
|
-
mov ebx, 0
|
|
44599
|
-
mov ecx, ${this.variaveis.get(nomeVar)}
|
|
44600
|
-
mov edx, 256
|
|
44660
|
+
this.textoSaida += `
|
|
44661
|
+
mov eax, 3
|
|
44662
|
+
mov ebx, 0
|
|
44663
|
+
mov ecx, ${this.variaveis.get(nomeVar)}
|
|
44664
|
+
mov edx, 256
|
|
44601
44665
|
int 0x80`;
|
|
44602
44666
|
}
|
|
44603
44667
|
traduzirDeclaracaoPara(declaracao) {
|
|
@@ -44623,12 +44687,12 @@ ${nomeFuncao}:
|
|
|
44623
44687
|
}
|
|
44624
44688
|
}
|
|
44625
44689
|
}
|
|
44626
|
-
this.textoSaida += `
|
|
44690
|
+
this.textoSaida += `
|
|
44627
44691
|
${labelInicio}:`;
|
|
44628
44692
|
if (declaracao.condicao) {
|
|
44629
44693
|
const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
|
|
44630
|
-
this.textoSaida += `
|
|
44631
|
-
cmp ${condicao}, 0
|
|
44694
|
+
this.textoSaida += `
|
|
44695
|
+
cmp ${condicao}, 0
|
|
44632
44696
|
je ${labelFim}`;
|
|
44633
44697
|
}
|
|
44634
44698
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
@@ -44639,8 +44703,8 @@ ${labelInicio}:`;
|
|
|
44639
44703
|
this.dicionarioConstrutos[declaracao.incrementar.constructor.name](declaracao.incrementar);
|
|
44640
44704
|
}
|
|
44641
44705
|
}
|
|
44642
|
-
this.textoSaida += `
|
|
44643
|
-
jmp ${labelInicio}
|
|
44706
|
+
this.textoSaida += `
|
|
44707
|
+
jmp ${labelInicio}
|
|
44644
44708
|
${labelFim}:`;
|
|
44645
44709
|
}
|
|
44646
44710
|
traduzirDeclaracaoParaCada(declaracao) {
|
|
@@ -44655,57 +44719,57 @@ ${labelFim}:`;
|
|
|
44655
44719
|
if (vetor instanceof construtos_1.Vetor) {
|
|
44656
44720
|
tamanhoVetor = vetor.tamanho || 0;
|
|
44657
44721
|
}
|
|
44658
|
-
this.textoSaida += `
|
|
44659
|
-
xor rcx, rcx
|
|
44660
|
-
${rotuloInicio}:
|
|
44661
|
-
cmp rcx, ${tamanhoVetor}
|
|
44722
|
+
this.textoSaida += `
|
|
44723
|
+
xor rcx, rcx
|
|
44724
|
+
${rotuloInicio}:
|
|
44725
|
+
cmp rcx, ${tamanhoVetor}
|
|
44662
44726
|
jge ${rotuloFim}`;
|
|
44663
44727
|
if (this.dicionarioDeclaracoes[declaracao.corpo.constructor.name]) {
|
|
44664
44728
|
this.dicionarioDeclaracoes[declaracao.corpo.constructor.name](declaracao.corpo);
|
|
44665
44729
|
}
|
|
44666
|
-
this.textoSaida += `
|
|
44667
|
-
inc rcx
|
|
44668
|
-
jmp ${rotuloInicio}
|
|
44730
|
+
this.textoSaida += `
|
|
44731
|
+
inc rcx
|
|
44732
|
+
jmp ${rotuloInicio}
|
|
44669
44733
|
${rotuloFim}:`;
|
|
44670
44734
|
}
|
|
44671
44735
|
traduzirDeclaracaoRetorna(declaracao) {
|
|
44672
44736
|
if (declaracao.valor) {
|
|
44673
44737
|
const valor = this.dicionarioConstrutos[declaracao.valor.constructor.name](declaracao.valor);
|
|
44674
|
-
this.textoSaida += `
|
|
44738
|
+
this.textoSaida += `
|
|
44675
44739
|
mov rax, ${valor}`;
|
|
44676
44740
|
}
|
|
44677
|
-
this.textoSaida += `
|
|
44678
|
-
pop rbp
|
|
44741
|
+
this.textoSaida += `
|
|
44742
|
+
pop rbp
|
|
44679
44743
|
ret`;
|
|
44680
44744
|
}
|
|
44681
44745
|
traduzirDeclaracaoSe(declaracao) {
|
|
44682
44746
|
const rotuloSenao = this.gerarLabel();
|
|
44683
44747
|
const rotuloFim = this.gerarLabel();
|
|
44684
44748
|
const condicao = this.dicionarioConstrutos[declaracao.condicao.constructor.name](declaracao.condicao);
|
|
44685
|
-
this.textoSaida += `
|
|
44686
|
-
cmp ${condicao}, 0
|
|
44749
|
+
this.textoSaida += `
|
|
44750
|
+
cmp ${condicao}, 0
|
|
44687
44751
|
je ${rotuloSenao}`;
|
|
44688
44752
|
if (this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name]) {
|
|
44689
44753
|
this.dicionarioDeclaracoes[declaracao.caminhoEntao.constructor.name](declaracao.caminhoEntao);
|
|
44690
44754
|
}
|
|
44691
|
-
this.textoSaida += `
|
|
44692
|
-
jmp ${rotuloFim}
|
|
44755
|
+
this.textoSaida += `
|
|
44756
|
+
jmp ${rotuloFim}
|
|
44693
44757
|
${rotuloSenao}:`;
|
|
44694
44758
|
if (declaracao.caminhoSenao &&
|
|
44695
44759
|
this.dicionarioDeclaracoes[declaracao.caminhoSenao.constructor.name]) {
|
|
44696
44760
|
this.dicionarioDeclaracoes[declaracao.caminhoSenao.constructor.name](declaracao.caminhoSenao);
|
|
44697
44761
|
}
|
|
44698
|
-
this.textoSaida += `
|
|
44762
|
+
this.textoSaida += `
|
|
44699
44763
|
${rotuloFim}:`;
|
|
44700
44764
|
}
|
|
44701
44765
|
traduzirDeclaracaoClasse(declaracao) {
|
|
44702
44766
|
// Classes em assembly são complexas - implementação básica
|
|
44703
|
-
this.textoSaida += `
|
|
44767
|
+
this.textoSaida += `
|
|
44704
44768
|
; Classe: ${declaracao.simbolo?.lexema || 'unknown'}`;
|
|
44705
44769
|
}
|
|
44706
44770
|
traduzirDeclaracaoTente(declaracao) {
|
|
44707
44771
|
// Try-catch em assembly requer handler complexo
|
|
44708
|
-
this.textoSaida += `
|
|
44772
|
+
this.textoSaida += `
|
|
44709
44773
|
; Tente-pegue`;
|
|
44710
44774
|
if (declaracao.caminhoTente && Array.isArray(declaracao.caminhoTente)) {
|
|
44711
44775
|
declaracao.caminhoTente.forEach((declaracao) => {
|
|
@@ -44742,8 +44806,8 @@ ${rotuloFim}:`;
|
|
|
44742
44806
|
}
|
|
44743
44807
|
else if (this.dicionarioConstrutos[tipoInicializador]) {
|
|
44744
44808
|
const valor = this.dicionarioConstrutos[tipoInicializador](declaracao.inicializador);
|
|
44745
|
-
this.textoSaida += `
|
|
44746
|
-
mov rax, ${valor}
|
|
44809
|
+
this.textoSaida += `
|
|
44810
|
+
mov rax, ${valor}
|
|
44747
44811
|
mov [${varLabel}], rax`;
|
|
44748
44812
|
}
|
|
44749
44813
|
}
|
|
@@ -44763,33 +44827,33 @@ ${rotuloFim}:`;
|
|
|
44763
44827
|
return;
|
|
44764
44828
|
this.helperPrintIntEmitido = true;
|
|
44765
44829
|
this.bss += ` __print_buf resb 24\n`;
|
|
44766
|
-
this.helpers += `
|
|
44767
|
-
__delegua_print_int:
|
|
44768
|
-
push rbx
|
|
44769
|
-
push rcx
|
|
44770
|
-
push rdx
|
|
44771
|
-
push rsi
|
|
44772
|
-
mov rcx, 10
|
|
44773
|
-
lea rsi, [__print_buf + 22]
|
|
44774
|
-
mov byte [rsi], 10
|
|
44775
|
-
.print_digitloop:
|
|
44776
|
-
xor rdx, rdx
|
|
44777
|
-
div rcx
|
|
44778
|
-
add dl, '0'
|
|
44779
|
-
dec rsi
|
|
44780
|
-
mov [rsi], dl
|
|
44781
|
-
test rax, rax
|
|
44782
|
-
jnz .print_digitloop
|
|
44783
|
-
lea rdx, [__print_buf + 23]
|
|
44784
|
-
sub rdx, rsi
|
|
44785
|
-
mov ecx, esi
|
|
44786
|
-
mov ebx, 1
|
|
44787
|
-
mov eax, 4
|
|
44788
|
-
int 0x80
|
|
44789
|
-
pop rsi
|
|
44790
|
-
pop rdx
|
|
44791
|
-
pop rcx
|
|
44792
|
-
pop rbx
|
|
44830
|
+
this.helpers += `
|
|
44831
|
+
__delegua_print_int:
|
|
44832
|
+
push rbx
|
|
44833
|
+
push rcx
|
|
44834
|
+
push rdx
|
|
44835
|
+
push rsi
|
|
44836
|
+
mov rcx, 10
|
|
44837
|
+
lea rsi, [__print_buf + 22]
|
|
44838
|
+
mov byte [rsi], 10
|
|
44839
|
+
.print_digitloop:
|
|
44840
|
+
xor rdx, rdx
|
|
44841
|
+
div rcx
|
|
44842
|
+
add dl, '0'
|
|
44843
|
+
dec rsi
|
|
44844
|
+
mov [rsi], dl
|
|
44845
|
+
test rax, rax
|
|
44846
|
+
jnz .print_digitloop
|
|
44847
|
+
lea rdx, [__print_buf + 23]
|
|
44848
|
+
sub rdx, rsi
|
|
44849
|
+
mov ecx, esi
|
|
44850
|
+
mov ebx, 1
|
|
44851
|
+
mov eax, 4
|
|
44852
|
+
int 0x80
|
|
44853
|
+
pop rsi
|
|
44854
|
+
pop rdx
|
|
44855
|
+
pop rcx
|
|
44856
|
+
pop rbx
|
|
44793
44857
|
ret`;
|
|
44794
44858
|
}
|
|
44795
44859
|
traduzirDeclaracaoEscreva(declaracaoEscreva) {
|
|
@@ -44798,16 +44862,16 @@ __delegua_print_int:
|
|
|
44798
44862
|
const nome_string_literal = this.criaStringLiteral(arg);
|
|
44799
44863
|
const tam_string_literal = this.criaTamanhoNaMemoriaReferenteAVar(nome_string_literal);
|
|
44800
44864
|
if (this.alvo === 'linux') {
|
|
44801
|
-
this.textoSaida += `
|
|
44802
|
-
mov edx, ${tam_string_literal}
|
|
44803
|
-
mov ecx, ${nome_string_literal}
|
|
44804
|
-
mov ebx, 1 ; fd stdout
|
|
44805
|
-
mov eax, 4 ; sys_write
|
|
44865
|
+
this.textoSaida += `
|
|
44866
|
+
mov edx, ${tam_string_literal}
|
|
44867
|
+
mov ecx, ${nome_string_literal}
|
|
44868
|
+
mov ebx, 1 ; fd stdout
|
|
44869
|
+
mov eax, 4 ; sys_write
|
|
44806
44870
|
int 0x80`;
|
|
44807
44871
|
}
|
|
44808
44872
|
else {
|
|
44809
|
-
this.textoSaida += `
|
|
44810
|
-
lea rcx, [rel ${nome_string_literal}]
|
|
44873
|
+
this.textoSaida += `
|
|
44874
|
+
lea rcx, [rel ${nome_string_literal}]
|
|
44811
44875
|
call printf`;
|
|
44812
44876
|
}
|
|
44813
44877
|
}
|
|
@@ -44815,8 +44879,8 @@ __delegua_print_int:
|
|
|
44815
44879
|
const valor = this.dicionarioConstrutos[arg.constructor.name](arg);
|
|
44816
44880
|
if (this.alvo === 'linux') {
|
|
44817
44881
|
this.emitirHelperPrintInt();
|
|
44818
|
-
this.textoSaida += `
|
|
44819
|
-
mov rax, ${valor}
|
|
44882
|
+
this.textoSaida += `
|
|
44883
|
+
mov rax, ${valor}
|
|
44820
44884
|
call __delegua_print_int`;
|
|
44821
44885
|
}
|
|
44822
44886
|
else {
|
|
@@ -44824,24 +44888,24 @@ __delegua_print_int:
|
|
|
44824
44888
|
this.data += ` __fmt_int db '%d', 10, 0\n`;
|
|
44825
44889
|
this.fmtIntWindowsEmitido = true;
|
|
44826
44890
|
}
|
|
44827
|
-
this.textoSaida += `
|
|
44828
|
-
mov rdx, ${valor}
|
|
44829
|
-
lea rcx, [rel __fmt_int]
|
|
44891
|
+
this.textoSaida += `
|
|
44892
|
+
mov rdx, ${valor}
|
|
44893
|
+
lea rcx, [rel __fmt_int]
|
|
44830
44894
|
call printf`;
|
|
44831
44895
|
}
|
|
44832
44896
|
}
|
|
44833
44897
|
}
|
|
44834
44898
|
saidaSistema() {
|
|
44835
44899
|
if (this.alvo === 'linux') {
|
|
44836
|
-
this.textoSaida += `
|
|
44837
|
-
mov eax, 1 ; sys_exit
|
|
44838
|
-
xor ebx, ebx ; status 0
|
|
44900
|
+
this.textoSaida += `
|
|
44901
|
+
mov eax, 1 ; sys_exit
|
|
44902
|
+
xor ebx, ebx ; status 0
|
|
44839
44903
|
int 0x80`;
|
|
44840
44904
|
}
|
|
44841
44905
|
else {
|
|
44842
44906
|
// Windows: return from main with 0 in EAX
|
|
44843
|
-
this.textoSaida += `
|
|
44844
|
-
xor eax, eax
|
|
44907
|
+
this.textoSaida += `
|
|
44908
|
+
xor eax, eax
|
|
44845
44909
|
ret`;
|
|
44846
44910
|
}
|
|
44847
44911
|
}
|
|
@@ -53253,35 +53317,35 @@ class TradutorWebAssembly {
|
|
|
53253
53317
|
* Fornece os imports necessários (`delegua.escreva_texto`, `delegua.escreva_inteiro`).
|
|
53254
53318
|
*/
|
|
53255
53319
|
gerarArquivoHost() {
|
|
53256
|
-
return `// Gerado por Delégua -> WebAssembly
|
|
53257
|
-
// Uso: node delegua-host.mjs <arquivo.wasm>
|
|
53258
|
-
import { readFileSync } from 'fs';
|
|
53259
|
-
|
|
53260
|
-
const args = process.argv.slice(2);
|
|
53261
|
-
const wasmPath = args[0] ?? 'saida.wasm';
|
|
53262
|
-
|
|
53263
|
-
let memoryExport;
|
|
53264
|
-
|
|
53265
|
-
const importObject = {
|
|
53266
|
-
delegua: {
|
|
53267
|
-
/** Imprime texto a partir de um ponteiro e comprimento na memória linear. */
|
|
53268
|
-
escreva_texto(ptr, len) {
|
|
53269
|
-
const bytes = new Uint8Array(memoryExport.buffer, ptr, len);
|
|
53270
|
-
process.stdout.write(new TextDecoder('utf-8').decode(bytes));
|
|
53271
|
-
},
|
|
53272
|
-
/** Imprime um inteiro de 64 bits (recebido como BigInt no JS). */
|
|
53273
|
-
escreva_inteiro(valor) {
|
|
53274
|
-
process.stdout.write(String(valor));
|
|
53275
|
-
},
|
|
53276
|
-
}
|
|
53277
|
-
};
|
|
53278
|
-
|
|
53279
|
-
const wasmBuffer = readFileSync(wasmPath);
|
|
53280
|
-
const { instance } = await WebAssembly.instantiate(wasmBuffer, importObject);
|
|
53281
|
-
memoryExport = instance.exports.memory;
|
|
53282
|
-
|
|
53283
|
-
const codigoSaida = Number(instance.exports.principal());
|
|
53284
|
-
process.exit(codigoSaida);
|
|
53320
|
+
return `// Gerado por Delégua -> WebAssembly
|
|
53321
|
+
// Uso: node delegua-host.mjs <arquivo.wasm>
|
|
53322
|
+
import { readFileSync } from 'fs';
|
|
53323
|
+
|
|
53324
|
+
const args = process.argv.slice(2);
|
|
53325
|
+
const wasmPath = args[0] ?? 'saida.wasm';
|
|
53326
|
+
|
|
53327
|
+
let memoryExport;
|
|
53328
|
+
|
|
53329
|
+
const importObject = {
|
|
53330
|
+
delegua: {
|
|
53331
|
+
/** Imprime texto a partir de um ponteiro e comprimento na memória linear. */
|
|
53332
|
+
escreva_texto(ptr, len) {
|
|
53333
|
+
const bytes = new Uint8Array(memoryExport.buffer, ptr, len);
|
|
53334
|
+
process.stdout.write(new TextDecoder('utf-8').decode(bytes));
|
|
53335
|
+
},
|
|
53336
|
+
/** Imprime um inteiro de 64 bits (recebido como BigInt no JS). */
|
|
53337
|
+
escreva_inteiro(valor) {
|
|
53338
|
+
process.stdout.write(String(valor));
|
|
53339
|
+
},
|
|
53340
|
+
}
|
|
53341
|
+
};
|
|
53342
|
+
|
|
53343
|
+
const wasmBuffer = readFileSync(wasmPath);
|
|
53344
|
+
const { instance } = await WebAssembly.instantiate(wasmBuffer, importObject);
|
|
53345
|
+
memoryExport = instance.exports.memory;
|
|
53346
|
+
|
|
53347
|
+
const codigoSaida = Number(instance.exports.principal());
|
|
53348
|
+
process.exit(codigoSaida);
|
|
53285
53349
|
`;
|
|
53286
53350
|
}
|
|
53287
53351
|
}
|